mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-20 06:20:38 +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
|
||||
|
||||
Reference in New Issue
Block a user