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 @@
+
+
+
+
+
+method method1(self: Moo) {...}{.raises: [], tags: [].}
+-
+
+foo1
+
+
+
+method method2(self: Moo): int {...}{.raises: [], tags: [].}
+-
+
+foo2
+
+
+
+method method3(self: Moo): int {...}{.raises: [], tags: [].}
+-
+
+foo3
+
+
+
@@ -667,6 +709,22 @@
assert 1 == 1
+
+
+
iterator iter1(n: int): int {...}{.raises: [], tags: [].}
+
+
+foo1
+
+
+
+
iterator iter2(n: int): int {...}{.raises: [], tags: [].}
+
+
+foo2
+Example:
+discard
+
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:
- testproject: myfn()
diff --git a/nimdoc/testproject/testproject.nim b/nimdoc/testproject/testproject.nim
index b05754d51d889..5282c6f7783cd 100644
--- a/nimdoc/testproject/testproject.nim
+++ b/nimdoc/testproject/testproject.nim
@@ -247,6 +247,28 @@ at indent 0
# should be in
# should be out
+when true: # methods; issue #14691
+ type Moo = object
+ method method1*(self: Moo) =
+ ## foo1
+ method method2*(self: Moo): int =
+ ## foo2
+ result = 1
+ method method3*(self: Moo): int =
+ ## foo3
+ 1
+
+when true: # iterators
+ iterator iter1*(n: int): int =
+ ## foo1
+ for i in 0..