diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 0af830a42d06b..d863df62e38ec 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -588,6 +588,8 @@ proc getAllRunnableExamplesImpl(d: PDoc; n: PNode, dest: var Rope, state: Runnab # change this to `rsStart` if you want to keep generating doc comments # and runnableExamples that occur after some code in routine +import astalgo + proc getRoutineBody(n: PNode): PNode = ##[ nim transforms these quite differently: @@ -609,21 +611,15 @@ proc getRoutineBody(n: PNode): PNode = so we normalize the results to get to the statement list containing the (0 or more) doc comments and runnableExamples. - (even if using `result = n[bodyPos]`, you'd still to apply similar logic). ]## - result = n[^1] + result = n[bodyPos] case result.kind - of nkSym: - result = n[^2] - case result.kind - of nkAsgn: - doAssert result[0].kind == nkSym - doAssert result.len == 2 - result = result[1] - else: # eg: nkStmtList - discard - else: - discard + of nkAsgn: + doAssert result[0].kind == nkSym + doAssert result.len == 2 + result = result[1] + else: # eg: nkStmtList + discard proc getAllRunnableExamples(d: PDoc, n: PNode, dest: var Rope) = var n = n diff --git a/nimdoc/testproject/expected/testproject.html b/nimdoc/testproject/expected/testproject.html index 7bd2e64a338d4..a4f8876fd81d4 100644 --- a/nimdoc/testproject/expected/testproject.html +++ b/nimdoc/testproject/expected/testproject.html @@ -211,11 +211,27 @@

testproject

+
  • + Methods + +
  • Iterators
  • @@ -653,6 +669,32 @@

    Funcs

    + +
    +

    Methods

    +
    + +
    method method1(self: Moo) {...}{.raises: [], tags: [].}
    +
    + +foo1 + +
    + +
    method method2(self: Moo): int {...}{.raises: [], tags: [].}
    +
    + +foo2 + +
    + +
    method method3(self: Moo): int {...}{.raises: [], tags: [].}
    +
    + +foo3 + +
    +

    Iterators

    @@ -667,6 +709,22 @@

    Iterators

    assert 1 == 1 # ok2 + + +
    iterator iter1(n: int): int {...}{.raises: [], tags: [].}
    +
    + +foo1 + +
    + +
    iterator iter2(n: int): int {...}{.raises: [], tags: [].}
    +
    + +foo2 +

    Example:

    +
    discard # bar
    +
    diff --git a/nimdoc/testproject/expected/testproject.idx b/nimdoc/testproject/expected/testproject.idx index 46798f3140696..6d0bc6c5e7a69 100644 --- a/nimdoc/testproject/expected/testproject.idx +++ b/nimdoc/testproject/expected/testproject.idx @@ -38,6 +38,11 @@ c_nonexistant testproject.html#c_nonexistant,cstring testproject: c_nonexistant( low testproject.html#low,T testproject: low[T: Ordinal | enum | range](x: T): T low2 testproject.html#low2,T testproject: low2[T: Ordinal | enum | range](x: T): T tripleStrLitTest testproject.html#tripleStrLitTest testproject: tripleStrLitTest() +method1 testproject.html#method1.e,Moo testproject: method1(self: Moo) +method2 testproject.html#method2.e,Moo testproject: method2(self: Moo): int +method3 testproject.html#method3.e,Moo testproject: method3(self: Moo): int +iter1 testproject.html#iter1.i,int testproject: iter1(n: int): int +iter2 testproject.html#iter2.i,int testproject: iter2(n: int): int bar testproject.html#bar.m testproject: bar(): untyped z16 testproject.html#z16.m testproject: z16() z18 testproject.html#z18.m testproject: z18(): int diff --git a/nimdoc/testproject/expected/theindex.html b/nimdoc/testproject/expected/theindex.html index 14d07bfd99461..8d34506a6138c 100644 --- a/nimdoc/testproject/expected/theindex.html +++ b/nimdoc/testproject/expected/theindex.html @@ -181,6 +181,14 @@

    Index

  • testproject: isValid[T](x: T): bool
  • +
    iter1:
    +
    iter2:
    low:
    +
    method1:
    +
    method2:
    +
    method3:
    myfn: