Skip to content

Commit

Permalink
fix #14691 docgen methods
Browse files Browse the repository at this point in the history
  • Loading branch information
timotheecour committed Jun 17, 2020
1 parent e7f280b commit a2f6083
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 13 deletions.
22 changes: 9 additions & 13 deletions compiler/docgen.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
58 changes: 58 additions & 0 deletions nimdoc/testproject/expected/testproject.html
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,27 @@ <h1 class="title">testproject</h1>

</ul>
</li>
<li>
<a class="reference reference-toplevel" href="#14" id="64">Methods</a>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#method1.e%2CMoo"
title="method1(self: Moo)"><wbr />method1<span class="attachedType"></span></a></li>
<li><a class="reference" href="#method2.e%2CMoo"
title="method2(self: Moo): int"><wbr />method2<span class="attachedType"></span></a></li>
<li><a class="reference" href="#method3.e%2CMoo"
title="method3(self: Moo): int"><wbr />method3<span class="attachedType"></span></a></li>

</ul>
</li>
<li>
<a class="reference reference-toplevel" href="#15" id="65">Iterators</a>
<ul class="simple simple-toc-section">
<li><a class="reference" href="#fromUtils1.i"
title="fromUtils1(): int"><wbr />from<wbr />Utils1<span class="attachedType"></span></a></li>
<li><a class="reference" href="#iter1.i%2Cint"
title="iter1(n: int): int"><wbr />iter1<span class="attachedType"></span></a></li>
<li><a class="reference" href="#iter2.i%2Cint"
title="iter2(n: int): int"><wbr />iter2<span class="attachedType"></span></a></li>

</ul>
</li>
Expand Down Expand Up @@ -653,6 +669,32 @@ <h1><a class="toc-backref" href="#13">Funcs</a></h1>

</dd>

</dl></div>
<div class="section" id="14">
<h1><a class="toc-backref" href="#14">Methods</a></h1>
<dl class="item">
<a id="method1.e,Moo"></a>
<dt><pre><span class="Keyword">method</span> <a href="#method1.e%2CMoo"><span class="Identifier">method1</span></a><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <span class="Identifier">Moo</span><span class="Other">)</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<dd>

foo1

</dd>
<a id="method2.e,Moo"></a>
<dt><pre><span class="Keyword">method</span> <a href="#method2.e%2CMoo"><span class="Identifier">method2</span></a><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <span class="Identifier">Moo</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<dd>

foo2

</dd>
<a id="method3.e,Moo"></a>
<dt><pre><span class="Keyword">method</span> <a href="#method3.e%2CMoo"><span class="Identifier">method3</span></a><span class="Other">(</span><span class="Identifier">self</span><span class="Other">:</span> <span class="Identifier">Moo</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<dd>

foo3

</dd>

</dl></div>
<div class="section" id="15">
<h1><a class="toc-backref" href="#15">Iterators</a></h1>
Expand All @@ -667,6 +709,22 @@ <h1><a class="toc-backref" href="#15">Iterators</a></h1>
</span><span class="Identifier">assert</span><span class="Whitespace"> </span><span class="DecNumber">1</span><span class="Whitespace"> </span><span class="Operator">==</span><span class="Whitespace"> </span><span class="DecNumber">1</span><span class="Whitespace">
</span><span class="Comment"># ok2</span></pre>

</dd>
<a id="iter1.i,int"></a>
<dt><pre><span class="Keyword">iterator</span> <a href="#iter1.i%2Cint"><span class="Identifier">iter1</span></a><span class="Other">(</span><span class="Identifier">n</span><span class="Other">:</span> <span class="Identifier">int</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<dd>

foo1

</dd>
<a id="iter2.i,int"></a>
<dt><pre><span class="Keyword">iterator</span> <a href="#iter2.i%2Cint"><span class="Identifier">iter2</span></a><span class="Other">(</span><span class="Identifier">n</span><span class="Other">:</span> <span class="Identifier">int</span><span class="Other">)</span><span class="Other">:</span> <span class="Identifier">int</span> <span><span class="Other">{</span><span class="Other pragmadots">...</span><span class="Other">}</span></span><span class="pragmawrap"><span class="Other">{.</span><span class="pragma"><span class="Identifier">raises</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span><span class="Other">,</span> <span class="Identifier">tags</span><span class="Other">:</span> <span class="Other">[</span><span class="Other">]</span></span><span class="Other">.}</span></span></pre></dt>
<dd>

foo2
<p><strong class="examples_text">Example:</strong></p>
<pre class="listing"><span class="Keyword">discard</span><span class="Whitespace"> </span><span class="Comment"># bar</span></pre>

</dd>

</dl></div>
Expand Down
5 changes: 5 additions & 0 deletions nimdoc/testproject/expected/testproject.idx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions nimdoc/testproject/expected/theindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,14 @@ <h1 class="title">Index</h1>
<li><a class="reference external"
data-doc-search-tag="testproject: isValid[T](x: T): bool" href="testproject.html#isValid%2CT">testproject: isValid[T](x: T): bool</a></li>
</ul></dd>
<dt><a name="iter1" href="#iter1"><span>iter1:</span></a></dt><dd><ul class="simple">
<li><a class="reference external"
data-doc-search-tag="testproject: iter1(n: int): int" href="testproject.html#iter1.i%2Cint">testproject: iter1(n: int): int</a></li>
</ul></dd>
<dt><a name="iter2" href="#iter2"><span>iter2:</span></a></dt><dd><ul class="simple">
<li><a class="reference external"
data-doc-search-tag="testproject: iter2(n: int): int" href="testproject.html#iter2.i%2Cint">testproject: iter2(n: int): int</a></li>
</ul></dd>
<dt><a name="low" href="#low"><span>low:</span></a></dt><dd><ul class="simple">
<li><a class="reference external"
data-doc-search-tag="testproject: low[T: Ordinal | enum | range](x: T): T" href="testproject.html#low%2CT">testproject: low[T: Ordinal | enum | range](x: T): T</a></li>
Expand All @@ -189,6 +197,18 @@ <h1 class="title">Index</h1>
<li><a class="reference external"
data-doc-search-tag="testproject: low2[T: Ordinal | enum | range](x: T): T" href="testproject.html#low2%2CT">testproject: low2[T: Ordinal | enum | range](x: T): T</a></li>
</ul></dd>
<dt><a name="method1" href="#method1"><span>method1:</span></a></dt><dd><ul class="simple">
<li><a class="reference external"
data-doc-search-tag="testproject: method1(self: Moo)" href="testproject.html#method1.e%2CMoo">testproject: method1(self: Moo)</a></li>
</ul></dd>
<dt><a name="method2" href="#method2"><span>method2:</span></a></dt><dd><ul class="simple">
<li><a class="reference external"
data-doc-search-tag="testproject: method2(self: Moo): int" href="testproject.html#method2.e%2CMoo">testproject: method2(self: Moo): int</a></li>
</ul></dd>
<dt><a name="method3" href="#method3"><span>method3:</span></a></dt><dd><ul class="simple">
<li><a class="reference external"
data-doc-search-tag="testproject: method3(self: Moo): int" href="testproject.html#method3.e%2CMoo">testproject: method3(self: Moo): int</a></li>
</ul></dd>
<dt><a name="myfn" href="#myfn"><span>myfn:</span></a></dt><dd><ul class="simple">
<li><a class="reference external"
data-doc-search-tag="testproject: myfn()" href="testproject.html#myfn.t">testproject: myfn()</a></li>
Expand Down
22 changes: 22 additions & 0 deletions nimdoc/testproject/testproject.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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..<n:
yield i
iterator iter2*(n: int): int =
## foo2
runnableExamples:
discard # bar
yield 0

when true: # (most) macros
macro bar*(): untyped =
result = newStmtList()
Expand Down

0 comments on commit a2f6083

Please sign in to comment.