Skip to content

Commit 112b11c

Browse files
authored
group procs of the same name in TOC (#15487)
* group procs of the same name in TOC * correctly show `sink` parameters in TOC * no need to reinvent the wheel - `mgetorPut` exists * better setting of text color [ci skip] * fix CSS for better alignment
1 parent 5967b6f commit 112b11c

File tree

8 files changed

+220
-73
lines changed

8 files changed

+220
-73
lines changed

compiler/docgen.nim

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ type
3737
modDesc: Rope # module description
3838
module: PSym
3939
modDeprecationMsg: Rope
40-
toc, section: TSections
40+
toc, toc2, section: TSections
41+
tocTable: array[TSymKind, Table[string, Rope]]
4142
indexValFilename: string
4243
analytics: string # Google Analytics javascript, "" if doesn't exist
4344
seenSymbols: StringTableRef # avoids duplicate symbol generation for HTML.
@@ -871,6 +872,14 @@ proc genItem(d: PDoc, n, nameNode: PNode, k: TSymKind, docFlags: DocFlags) =
871872
itemIDRope, plainNameRope, plainSymbolRope, symbolOrIdRope,
872873
plainSymbolEncRope, symbolOrIdEncRope, attype]))
873874

875+
d.tocTable[k].mgetOrPut(cleanPlainSymbol, nil).add(ropeFormatNamedVars(
876+
d.conf, getConfigVar(d.conf, "doc.item.tocTable"),
877+
["name", "header", "desc", "itemID", "header_plain", "itemSym",
878+
"itemSymOrID", "itemSymEnc", "itemSymOrIDEnc", "attype"],
879+
[rope(getName(d, nameNode, d.splitAfter)), result, comm,
880+
itemIDRope, plainNameRope, plainSymbolRope,
881+
symbolOrIdRope, plainSymbolEncRope, symbolOrIdEncRope, attype]))
882+
874883
# Ironically for types the complexSymbol is *cleaner* than the plainName
875884
# because it doesn't include object fields or documentation comments. So we
876885
# use the plain one for callable elements, and the complex for the rest.
@@ -1167,7 +1176,7 @@ proc generateTags*(d: PDoc, n: PNode, r: var Rope) =
11671176
generateTags(d, lastSon(n[0]), r)
11681177
else: discard
11691178

1170-
proc genSection(d: PDoc, kind: TSymKind) =
1179+
proc genSection(d: PDoc, kind: TSymKind, groupedToc = false) =
11711180
const sectionNames: array[skModule..skField, string] = [
11721181
"Imports", "Types", "Vars", "Lets", "Consts", "Vars", "Procs", "Funcs",
11731182
"Methods", "Iterators", "Converters", "Macros", "Templates", "Exports"
@@ -1177,14 +1186,23 @@ proc genSection(d: PDoc, kind: TSymKind) =
11771186
d.section[kind] = ropeFormatNamedVars(d.conf, getConfigVar(d.conf, "doc.section"), [
11781187
"sectionid", "sectionTitle", "sectionTitleID", "content"], [
11791188
ord(kind).rope, title, rope(ord(kind) + 50), d.section[kind]])
1189+
1190+
var tocSource = d.toc
1191+
if groupedToc:
1192+
for p in d.tocTable[kind].keys:
1193+
d.toc2[kind].add ropeFormatNamedVars(d.conf, getConfigVar(d.conf, "doc.section.toc2"), [
1194+
"sectionid", "sectionTitle", "sectionTitleID", "content", "plainName"], [
1195+
ord(kind).rope, title, rope(ord(kind) + 50), d.tocTable[kind][p], p.rope])
1196+
tocSource = d.toc2
1197+
11801198
d.toc[kind] = ropeFormatNamedVars(d.conf, getConfigVar(d.conf, "doc.section.toc"), [
11811199
"sectionid", "sectionTitle", "sectionTitleID", "content"], [
1182-
ord(kind).rope, title, rope(ord(kind) + 50), d.toc[kind]])
1200+
ord(kind).rope, title, rope(ord(kind) + 50), tocSource[kind]])
11831201

11841202
proc relLink(outDir: AbsoluteDir, destFile: AbsoluteFile, linkto: RelativeFile): Rope =
11851203
rope($relativeTo(outDir / linkto, destFile.splitFile().dir, '/'))
11861204

1187-
proc genOutFile(d: PDoc): Rope =
1205+
proc genOutFile(d: PDoc, groupedToc = false): Rope =
11881206
var
11891207
code, content: Rope
11901208
title = ""
@@ -1193,7 +1211,8 @@ proc genOutFile(d: PDoc): Rope =
11931211
renderTocEntries(d[], j, 1, tmp)
11941212
var toc = tmp.rope
11951213
for i in TSymKind:
1196-
genSection(d, i)
1214+
var shouldSort = i in {skProc, skFunc} and groupedToc
1215+
genSection(d, i, shouldSort)
11971216
toc.add(d.toc[i])
11981217
if toc != nil:
11991218
toc = ropeFormatNamedVars(d.conf, getConfigVar(d.conf, "doc.toc"), ["content"], [toc])
@@ -1246,9 +1265,9 @@ proc updateOutfile(d: PDoc, outfile: AbsoluteFile) =
12461265
if isAbsolute(d.conf.outFile.string):
12471266
d.conf.outFile = splitPath(d.conf.outFile.string)[1].RelativeFile
12481267

1249-
proc writeOutput*(d: PDoc, useWarning = false) =
1268+
proc writeOutput*(d: PDoc, useWarning = false, groupedToc = false) =
12501269
runAllExamples(d)
1251-
var content = genOutFile(d)
1270+
var content = genOutFile(d, groupedToc)
12521271
if optStdout in d.conf.globalOptions:
12531272
writeRope(stdout, content)
12541273
else:

compiler/docgen2.nim

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ proc shouldProcess(g: PGen): bool =
2929
template closeImpl(body: untyped) {.dirty.} =
3030
var g = PGen(p)
3131
let useWarning = sfMainModule notin g.module.flags
32+
let groupedToc = true
3233
if shouldProcess(g):
3334
body
3435
try:
@@ -38,7 +39,7 @@ template closeImpl(body: untyped) {.dirty.} =
3839

3940
proc close(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
4041
closeImpl:
41-
writeOutput(g.doc, useWarning)
42+
writeOutput(g.doc, useWarning, groupedToc)
4243

4344
proc closeJson(graph: ModuleGraph; p: PPassContext, n: PNode): PNode =
4445
closeImpl:

compiler/typesrenderer.nim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ proc renderType(n: PNode): string =
7878
result = renderType(n[0]) & '['
7979
for i in 1..<n.len: result.add(renderType(n[i]) & ',')
8080
result[^1] = ']'
81+
of nkCommand:
82+
result = renderType(n[0])
83+
for i in 1..<n.len:
84+
if i > 1: result.add ", "
85+
result.add(renderType(n[i]))
8186
else: result = ""
8287

8388

config/nimdoc.cfg

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ doc.section.toc = """
2424
</li>
2525
"""
2626
27+
doc.section.toc2 = """
28+
<ul class="simple nested-toc-section">$plainName
29+
$content
30+
</ul>
31+
"""
32+
2733
# Chunk of HTML emitted for each entry in the HTML table of contents.
2834
# Available variables are:
2935
# * $desc: the actual docstring of the item.
@@ -54,6 +60,14 @@ doc.item.toc = """
5460
title="$header_plain">$name<span class="attachedType">$attype</span></a></li>
5561
"""
5662
63+
# This is used for TOC items which are grouped by the same name (e.g. procs).
64+
doc.item.tocTable = """
65+
<li><a class="reference" href="#$itemSymOrIDEnc"
66+
title="$header_plain">$itemSymOrID<span class="attachedType">$attype</span></a></li>
67+
"""
68+
69+
70+
5771
# HTML rendered for doc.item's seeSrc variable. Note that this will render to
5872
# the empty string if you don't pass anything through --git.url. Available
5973
# substitutaion variables here are:

doc/nimdoc.css

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,12 @@ body {
154154
margin-left: 0; }
155155

156156
.three.columns {
157-
width: 19%; }
157+
width: 22%;
158+
line-break: anywhere;
159+
}
158160

159161
.nine.columns {
160-
width: 80.0%; }
162+
width: 77.0%; }
161163

162164
.twelve.columns {
163165
width: 100%;
@@ -420,7 +422,7 @@ ul.simple-boot li {
420422
}
421423

422424
ol.simple > li, ul.simple > li {
423-
margin-bottom: 0.25em;
425+
margin-bottom: 0.2em;
424426
margin-left: 0.4em }
425427

426428
ul.simple.simple-toc > li {
@@ -439,9 +441,19 @@ ul.simple-toc > li {
439441

440442
ul.simple-toc-section {
441443
list-style-type: circle;
442-
margin-left: 1em;
444+
margin-left: 0.8em;
443445
color: #6c9aae; }
444446

447+
ul.nested-toc-section {
448+
list-style-type: circle;
449+
margin-left: -0.75em;
450+
color: var(--text);
451+
}
452+
453+
ul.nested-toc-section > li {
454+
margin-left: 1.25em;
455+
}
456+
445457

446458
ol.arabic {
447459
list-style: decimal; }

nimdoc/test_out_index_dot_html/expected/index.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ <h1 class="title">foo</h1>
9494
<li>
9595
<a class="reference reference-toplevel" href="#12" id="62">Procs</a>
9696
<ul class="simple simple-toc-section">
97+
<ul class="simple nested-toc-section">foo
9798
<li><a class="reference" href="#foo"
98-
title="foo()"><wbr />foo<span class="attachedType"></span></a></li>
99+
title="foo()">foo<span class="attachedType"></span></a></li>
100+
101+
</ul>
99102

100103
</ul>
101104
</li>

nimdoc/testproject/expected/subdir/subdir_b/utils.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ <h1 class="title">subdir/subdir_b/utils</h1>
108108
<li>
109109
<a class="reference reference-toplevel" href="#12" id="62">Procs</a>
110110
<ul class="simple simple-toc-section">
111+
<ul class="simple nested-toc-section">someType
111112
<li><a class="reference" href="#someType_2"
112-
title="someType(): SomeType"><wbr />some<wbr />Type<span class="attachedType">SomeType</span></a></li>
113+
title="someType(): SomeType">someType_2<span class="attachedType">SomeType</span></a></li>
114+
115+
</ul>
113116

114117
</ul>
115118
</li>

0 commit comments

Comments
 (0)