mirror of
https://github.com/nim-lang/Nim.git
synced 2026-06-09 05:14:20 +00:00
* fix #14846; add macros.extractDocCommentsAndRunnables * fixup * update tests * address comment
This commit is contained in:
@@ -1692,3 +1692,39 @@ when defined(nimMacrosSizealignof):
|
||||
|
||||
proc isExported*(n: NimNode): bool {.noSideEffect.} =
|
||||
## Returns whether the symbol is exported or not.
|
||||
|
||||
proc extractDocCommentsAndRunnables*(n: NimNode): NimNode =
|
||||
## returns a `nnkStmtList` containing the top-level doc comments and
|
||||
## runnableExamples in `a`, stopping at the first child that is neither.
|
||||
## Example:
|
||||
##
|
||||
## .. code-block:: nim
|
||||
## import macros
|
||||
## macro transf(a): untyped =
|
||||
## result = quote do:
|
||||
## proc fun2*() = discard
|
||||
## let header = extractDocCommentsAndRunnables(a.body)
|
||||
## # correct usage: rest is appended
|
||||
## result.body = header
|
||||
## result.body.add quote do: discard # just an example
|
||||
## # incorrect usage: nesting inside a nnkStmtList:
|
||||
## # result.body = quote do: (`header`; discard)
|
||||
##
|
||||
## proc fun*() {.transf.} =
|
||||
## ## first comment
|
||||
## runnableExamples: discard
|
||||
## runnableExamples: discard
|
||||
## ## last comment
|
||||
## discard # first statement after doc comments + runnableExamples
|
||||
## ## not docgen'd
|
||||
|
||||
result = newStmtList()
|
||||
for ni in n:
|
||||
case ni.kind
|
||||
of nnkCommentStmt:
|
||||
result.add ni
|
||||
of nnkCall:
|
||||
if ni[0].kind == nnkIdent and ni[0].strVal == "runnableExamples":
|
||||
result.add ni
|
||||
else: break
|
||||
else: break
|
||||
|
||||
@@ -180,8 +180,7 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
|
||||
# Extract the documentation comment from the original procedure declaration.
|
||||
# Note that we're not removing it from the body in order not to make this
|
||||
# transformation even more complex.
|
||||
if prc.body.len > 1 and prc.body[0].kind == nnkCommentStmt:
|
||||
outerProcBody.add(prc.body[0])
|
||||
let body2 = extractDocCommentsAndRunnables(prc.body)
|
||||
|
||||
# -> var retFuture = newFuture[T]()
|
||||
var retFutureSym = genSym(nskVar, "retFuture")
|
||||
@@ -276,9 +275,10 @@ proc asyncSingleProc(prc: NimNode): NimNode {.compileTime.} =
|
||||
(cast[type(f)](internalTmpFuture)).read()
|
||||
|
||||
if procBody.kind != nnkEmpty:
|
||||
result.body = quote:
|
||||
body2.add quote do:
|
||||
`awaitDefinition`
|
||||
`outerProcBody`
|
||||
result.body = body2
|
||||
|
||||
#echo(treeRepr(result))
|
||||
#if prcName == "recvLineInto":
|
||||
|
||||
Reference in New Issue
Block a user