From f96555bd102d534342eac14063bd8d93ec08a6dc Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 25 May 2020 06:44:23 -0700 Subject: [PATCH] fix #9227 procs can now have multiple interleaved doc comments + runnableExamples and be docgen'd correctly (#14441) * fix #9227 proc doc comments after 1st runnableExamples are not ignored anymore --- compiler/docgen.nim | 48 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/compiler/docgen.nim b/compiler/docgen.nim index bd967d4036..d989905b04 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -529,12 +529,38 @@ proc prepareExample(d: PDoc; n: PNode): string = for imp in imports: runnableExamples.add imp runnableExamples.add newTree(nkBlockStmt, newNode(nkEmpty), copyTree savedLastSon) -proc getAllRunnableExamplesRec(d: PDoc; n, orig: PNode; dest: var Rope) = - if n.info.fileIndex != orig.info.fileIndex: return +proc getAllRunnableExamplesRec(d: PDoc; n, orig: PNode; dest: var Rope, previousIsRunnable: var bool) = + ##[ + previousIsRunnable: keep track of whether previous sibling was a runnableExample (true if 1st sibling though). + This is to ensure this works: + proc fn* = + runnableExamples: discard + ## d1 + runnableExamples: discard + ## d2 + + ## d3 # <- this one should be out; it's part of rest of function body and would likey not make sense in doc comment + + It also works with: + proc fn* = + ## d0 + runnableExamples: discard + ## d1 + + etc + ]## + # xxx: checkme: owner check instead? this fails with the $nim/nimdoc/tester.nim test + # now that we're calling `genRecComment` only from here (to maintain correct order wrt runnableExample) + # if n.info.fileIndex != orig.info.fileIndex: return case n.kind + of nkCommentStmt: + if previousIsRunnable: + dest.add genRecComment(d, n) + previousIsRunnable = false of nkCallKinds: if isRunnableExamples(n[0]) and n.len >= 2 and n.lastSon.kind == nkStmtList: + previousIsRunnable = true let rdoccmd = prepareExample(d, n) var msg = "Example:" if rdoccmd.len > 0: msg.add " cmd: " & rdoccmd @@ -555,12 +581,15 @@ proc getAllRunnableExamplesRec(d: PDoc; n, orig: PNode; dest: var Rope) = inc i nodeToHighlightedHtml(d, b, dest, {renderRunnableExamples}, nil) dest.add(d.config.getOrDefault"doc.listing_end" % id) - else: discard + else: previousIsRunnable = false + + var previousIsRunnable2 = true for i in 0..