fix #14846; add macros.extractDocCommentsAndRunnables (#14849)

* fix #14846; add macros.extractDocCommentsAndRunnables

* fixup

* update tests

* address comment
This commit is contained in:
Timothee Cour
2020-07-01 00:26:23 -07:00
committed by GitHub
parent 05384efec5
commit 2867a33ebc
7 changed files with 98 additions and 3 deletions

View File

@@ -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