authorgravatar for kappaloris@gmail.comLoris Cro <kappaloris@gmail.com> 2022-03-24 18:52:07+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-19 19:10:11-07:00
log36c4b1aac995a5b0ef273c02c0b2c0271cab6b94
tree45ea2bfae183be0ca36c825d29fde4a1481b4550
parentdf3074aa985da82a01f94ba6b9a43535c7d438a9

autodoc: improve rendering and add "show priv decls" checkbox


3 files changed, 154 insertions(+), 165 deletions(-)

lib/docs/index.html+40-37
......@@ -26,17 +26,17 @@
2626 --search-sh-color: rgba(0, 0, 0, 0.18);
2727 --help-sh-color: rgba(0, 0, 0, 0.75);
2828 }
29
29
3030 html, body { margin: 0; padding:0; height: 100%; }
3131
3232 a {
3333 text-decoration: none;
3434 }
35
35
3636 a:hover {
3737 text-decoration: underline;
3838 }
39
39
4040 .hidden {
4141 display: none;
4242 }
......@@ -58,7 +58,7 @@
5858 width: 100%;
5959 height: 100%;
6060 justify-content: center;
61
61
6262 z-index: 100;
6363 }
6464
......@@ -76,7 +76,7 @@
7676 overflow-wrap: break-word;
7777 flex-shrink: 0;
7878 flex-grow: 0;
79
79
8080 z-index: 300;
8181 }
8282
......@@ -86,7 +86,7 @@
8686 -webkit-overflow-scrolling: touch;
8787 flex-grow: 1;
8888 flex-shrink: 1;
89
89
9090 z-index: 200;
9191 }
9292
......@@ -95,44 +95,44 @@
9595 max-width: 85vw;
9696 flex-shrink: 1;
9797 }
98
98
9999 .help-modal {
100100 z-index: 400;
101101 }
102
102
103103 /* sidebar */
104104 .sidebar {
105105 font-size: 1rem;
106106 background-color: var(--bg-color);
107107 box-shadow: 0 0 1rem var(--sidebar-sh-color);
108108 }
109
109
110110 .sidebar .logo {
111111 padding: 1rem 0.35rem 0.35rem 0.35rem;
112112 }
113
113
114114 .sidebar .logo > svg {
115115 display: block;
116116 overflow: visible;
117117 }
118
118
119119 .sidebar h2 {
120120 margin: 0.5rem;
121121 padding: 0;
122122 font-size: 1.2rem;
123123 }
124
124
125125 .sidebar h2 > span {
126126 border-bottom: 0.125rem dotted var(--tx-color);
127127 }
128
128
129129 .sidebar .packages {
130130 list-style-type: none;
131131 margin: 0;
132132 padding: 0;
133133 background-color: var(--sidebar-pkg-bg-color);
134134 }
135
135
136136 .sidebar .packages > li > a {
137137 display: block;
138138 padding: 0.5rem 1rem;
......@@ -140,17 +140,17 @@
140140 background-color: var(--sidebar-pkglnk-bg-color);
141141 text-decoration: none;
142142 }
143
143
144144 .sidebar .packages > li > a:hover {
145145 color: var(--sidebar-pkglnk-tx-color-hover);
146146 background-color: var(--sidebar-pkglnk-bg-color-hover);
147147 }
148
148
149149 .sidebar .packages > li > a.active {
150150 color: var(--sidebar-pkglnk-tx-color-active);
151151 background-color: var(--sidebar-pkglnk-bg-color-active);
152152 }
153
153
154154 .sidebar p.str {
155155 margin: 0.5rem;
156156 font-family: var(--mono);
......@@ -183,28 +183,28 @@
183183 border-radius: 0;
184184 -webkit-appearance: none;
185185 }
186
186
187187 .docs .search:focus {
188188 background-color: var(--search-bg-color-focus);
189189 border-bottom-color: #ffbb4d;
190190 box-shadow: 0 0.3em 1em 0.125em var(--search-sh-color);
191191 }
192
192
193193 .docs .search::placeholder {
194194 font-size: 1rem;
195195 font-family: var(--ui);
196196 color: var(--tx-color);
197197 opacity: 0.5;
198198 }
199
199
200200 .docs a {
201201 color: var(--link-color);
202202 }
203
203
204204 .docs p {
205205 margin: 0.8rem 0;
206206 }
207
207
208208 .docs pre {
209209 font-family: var(--mono);
210210 font-size:1em;
......@@ -212,19 +212,19 @@
212212 padding:1em;
213213 overflow-x: auto;
214214 }
215
215
216216 .docs code {
217217 font-family: var(--mono);
218218 font-size: 1em;
219219 }
220
220
221221 .docs h1 {
222222 font-size: 1.4em;
223223 margin: 0.8em 0;
224224 padding: 0;
225225 border-bottom: 0.0625rem dashed;
226226 }
227
227
228228 .docs h2 {
229229 font-size: 1.3em;
230230 margin: 0.5em 0;
......@@ -264,7 +264,7 @@
264264 #tableFnErrors dt {
265265 font-weight: bold;
266266 }
267
267
268268 .examples {
269269 list-style-type: none;
270270 margin: 0;
......@@ -275,7 +275,7 @@
275275 white-space: nowrap;
276276 overflow-x: auto;
277277 }
278
278
279279 .docs td {
280280 margin: 0;
281281 padding: 0.5em;
......@@ -283,7 +283,7 @@
283283 text-overflow: ellipsis;
284284 overflow-x: hidden;
285285 }
286
286
287287 /* help dialog */
288288 .help-modal {
289289 display: flex;
......@@ -308,23 +308,23 @@
308308 border: 0.125rem solid #000;
309309 box-shadow: 0 0.5rem 2.5rem 0.3rem var(--help-sh-color);
310310 }
311
311
312312 .help-modal h1 {
313313 margin: 0.75em 2.5em 1em 2.5em;
314314 font-size: 1.5em;
315315 text-align: center;
316316 }
317
317
318318 .help-modal dt, .help-modal dd {
319319 display: inline;
320320 margin: 0 0.2em;
321321 }
322
322
323323 .help-modal dl {
324324 margin-left: 0.5em;
325325 margin-right: 0.5em;
326326 }
327
327
328328 .help-modal kbd {
329329 display: inline-block;
330330 padding: 0.3em 0.2em;
......@@ -341,7 +341,7 @@
341341 box-shadow: inset 0 -0.0625em 0 #c6cbd1;
342342 cursor: default;
343343 }
344
344
345345 /* tokens */
346346 .tok-kw {
347347 color: #333;
......@@ -371,10 +371,10 @@
371371 color: #458;
372372 font-weight: bold;
373373 }
374
374
375375 /* dark mode */
376376 @media (prefers-color-scheme: dark) {
377
377
378378 :root {
379379 --tx-color: #bbb;
380380 --bg-color: #111;
......@@ -392,7 +392,7 @@
392392 --search-sh-color: rgba(255, 255, 255, 0.28);
393393 --help-sh-color: rgba(142, 142, 142, 0.5);
394394 }
395
395
396396 .docs pre {
397397 background-color:#2A2A2A;
398398 }
......@@ -440,7 +440,7 @@
440440 .tok-type {
441441 color: #68f;
442442 }
443
443
444444 }
445445
446446 @media only screen and (max-width: 750px) {
......@@ -545,6 +545,9 @@
545545 <h2><span>Target</span></h2>
546546 <p class="str" id="tdTarget"></p>
547547 </div>
548 <div>
549 <input id="privDeclsBox" type="checkbox"/> Show Private Decls
550 </div>
548551 </nav>
549552 </div>
550553 <div class="flex-right">
lib/docs/main.js+108-127
......@@ -32,18 +32,6 @@
3232 } TypeKind
3333*/
3434
35/**
36 * @typedef {
37 | WalkResult
38 | { unspecified: {} }
39 | { anytype: {} }
40 | { type: number }
41 | { comptimeExpr: number }
42 | { call: number }
43 | { hasCte: boolean; declPath: number[] }
44 } TypeRef
45*/
46
4735/**
4836 * @typedef {
4937 | { void: {} }
......@@ -52,16 +40,16 @@
5240 | { type: number }
5341 | { comptimeExpr: number }
5442 | { call: number }
55 | { int: { typeRef: TypeRef; value: number } }
56 | { float: { typeRef: TypeRef; value: number } }
43 | { int: { typeRef: WalkResult; value: number } }
44 | { float: { typeRef: WalkResult; value: number } }
5745 | { bool: boolean }
58 | { undefined: TypeRef }
59 | { null: TypeRef }
46 | { undefined: WalkResult }
47 | { null: WalkResult }
6048 | { typeOf: WalkResult }
6149 | { compileError: string }
6250 | { string: string }
6351 | { struct: Struct }
64 | { hasCte: boolean; declPath: number[] }
52 | { refPath: WalkResult[] }
6553 | { array: ZigArray }
6654 | { enumLiteral: string }
6755 } WalkResult
......@@ -75,8 +63,8 @@
7563 | { len: WalkResult; child: TypeRef } // Array
7664 | { name: string; fields: { name: string; docs: string }[] } // ErrorSet
7765 | { size: "One" | "Many" | "Slice" | "C"; child: TypeRef } // Pointer
78 | { name: string; src?: number; privDecls: number[]; pubDecls: number[]; fields?: TypeRef[] } // Struct, Enum, Union
79 | { name: string; src?: number; ret: TypeRef; params?: TypeRef[] } // Fn
66 | { name: string; src: number; privDecls: number[]; pubDecls: number[]; fields: WalkResult[] } // Struct, Enum, Union
67 | { name: string; src: number; ret: WalkResult; params: WalkResult[] } // Fn
8068 )
8169 } Type
8270*/
......@@ -85,14 +73,14 @@
8573 * @typedef {{
8674 name: string,
8775 src: number | null,
88 ret: TypeRef,
89 params: TypeRef[] | null,
76 ret: WalkResult,
77 params: WalkResult[] | null,
9078 }} Fn
9179*/
9280
9381/**
9482 * @typedef {{
95 func: TypeRef,
83 func: WalkResult,
9684 args: WalkResult[],
9785 ret: WalkResult,
9886 }} Call
......@@ -135,21 +123,21 @@
135123 src?: number,
136124 privDecls: number[],
137125 pubDecls: number[],
138 fields?: TypeRef[],
126 fields?: WalkResult[],
139127 }} Struct
140128*/
141129
142130/**
143131 * @typedef {{
144132 len: WalkResult,
145 child: TypeRef,
133 child: WalkResult,
146134 }} ZigArray
147135*/
148136
149137/**
150138 * @typedef {{
151139 code: string,
152 typeRef: TypeRef,
140 typeRef: WalkResult,
153141 }} ComptimeExpr
154142*/
155143
......@@ -219,6 +207,7 @@ var zigAnalysis;
219207 var domSectSearchNoResults = document.getElementById("sectSearchNoResults");
220208 var domSectInfo = document.getElementById("sectInfo");
221209 var domTdTarget = document.getElementById("tdTarget");
210 var domPrivDeclsBox = document.getElementById("privDeclsBox");
222211 var domTdZigVer = document.getElementById("tdZigVer");
223212 var domHdrName = document.getElementById("hdrName");
224213 var domHelpModal = document.getElementById("helpDialog");
......@@ -238,6 +227,7 @@ var zigAnalysis;
238227 var canonTypeDecls = null; // lazy; use getCanonTypeDecl
239228
240229 var curNav = {
230 showPrivDecls: false,
241231 // each element is a package name, e.g. @import("a") then within there @import("b")
242232 // starting implicitly from root package
243233 pkgNames: [],
......@@ -264,6 +254,18 @@ var zigAnalysis;
264254 // var nodesToCallsMap = indexNodesToCalls();
265255
266256 domSearch.addEventListener('keydown', onSearchKeyDown, false);
257 domPrivDeclsBox.addEventListener('change', function() {
258 if (this.checked != curNav.showPrivDecls) {
259 if (this.checked && location.hash.length > 1 && location.hash[1] != '*'){
260 location.hash = "#*" + location.hash.substring(1);
261 return;
262 }
263 if (!this.checked && location.hash.length > 1 && location.hash[1] == '*') {
264 location.hash = "#" + location.hash.substring(2);
265 return;
266 }
267 }
268 }, false);
267269 window.addEventListener('hashchange', onHashChange, false);
268270 window.addEventListener('keydown', onWindowKeyDown, false);
269271 onHashChange();
......@@ -323,19 +325,13 @@ var zigAnalysis;
323325 return typeKind === typeKinds.ErrorSet || typeKindIsContainer(typeKind);
324326 }
325327
326 function findCteInDeclPath(path) {
328 function findCteInRefPath(path) {
327329 for (var i = path.length - 1; i >= 0; i -= 1) {
328 const decl = zigAnalysis.decls[path[i]];
329 if ("comptimeExpr" in decl.value) {
330 return decl;
331 }
332
333 if ("declPath" in decl.value) {
334 const res = findCteInDeclPath(decl.value.declPath);
335 if (res !== null) {
336 return res;
337 }
338 }
330 const ref = path[i];
331 if ("string" in ref) continue;
332 if ("comptimeExpr" in ref) return ref;
333 if ("refPath" in ref) return findCteinRefPath(ref.refPath);
334 return null;
339335 }
340336
341337 return null;
......@@ -346,14 +342,15 @@ var zigAnalysis;
346342 while(i < 1000) {
347343 i += 1;
348344
349 if ("declPath" in value) {
350 if (value.hasCte) {
351 return findCteInDeclPath(value.declPath).value;
352 }
353 value = zigAnalysis.decls[value.declPath[0]].value;
345 if ("refPath" in value) {
346 value = value.refPath[value.refPath.length -1];
354347 continue;
355348 }
356349
350 if ("declRef" in value) {
351 value = zigAnalysis.decls[value.declRef].value;
352 }
353
357354 return value;
358355
359356 }
......@@ -369,13 +366,15 @@ var zigAnalysis;
369366 return { type: typeTypeId };
370367 }
371368
372 if ("declPath" in decl.value) {
373 if (decl.value.hasCte) {
374 decl = findCteInDeclPath(decl.value.declPath);
375 } else {
376 decl = zigAnalysis.decls[decl.value.declPath[0]];
377 }
369 if ("refPath" in decl.value) {
370 decl = {
371 value: decl.value.refPath[decl.value.refPath.length -1]
372 };
373 continue;
374 }
378375
376 if ("declRef" in decl.value) {
377 decl = zigAnalysis.decls[decl.value.declRef];
379378 continue;
380379 }
381380
......@@ -402,8 +401,14 @@ var zigAnalysis;
402401
403402 if ("call" in decl.value) {
404403 const fn_call = zigAnalysis.calls[decl.value.call];
405 console.assert("declPath" in fn_call.func);
406 const fn_decl = zigAnalysis.decls[fn_call.func.declPath[0]];
404 var fn_decl = undefined;
405 if ("declRef" in fn_call.func) {
406 fn_decl = zigAnalysis.decls[fn_call.func.declRef];
407 } else if ("refPath" in fn_call.func) {
408 console.assert("declRef" in fn_call.func.refPath[fn_call.func.refPath.length -1]);
409 fn_decl = zigAnalysis.decls[fn_call.func.refPath[fn_call.func.refPath.length -1].declRef.value];
410 } else throw {};
411
407412 const fn_decl_value = resolveValue(fn_decl.value);
408413 console.assert("type" in fn_decl_value); //TODO handle comptimeExpr
409414 const fn_type = zigAnalysis.types[fn_decl_value.type];
......@@ -456,6 +461,8 @@ var zigAnalysis;
456461 renderInfo();
457462 renderPkgList();
458463
464 domPrivDeclsBox.checked = curNav.showPrivDecls;
465
459466 if (curNavSearch !== "") {
460467 return renderSearch();
461468 }
......@@ -547,7 +554,7 @@ var zigAnalysis;
547554 var typeObj = zigAnalysis.types[typeIndex];
548555 if (typeObj.kind !== typeKinds.Struct)
549556 return false;
550 return !typeObj.fields;
557 return typeObj.fields.length == 0;
551558 }
552559
553560 function typeIsGenericFn(typeIndex) {
......@@ -850,22 +857,14 @@ var zigAnalysis;
850857 if ("comptimeExpr" in typeValue) {
851858 return "[ComptimeExpr]";
852859 }
853 if ("declPath" in typeValue) {
860 if ("refPath" in typeValue) {
854861 var result = "";
855 for (var j = typeValue.declPath.length - 1; j >= 0; j--) {
856 var decl = zigAnalysis.decls[typeValue.declPath[j]];
857
858 // TODO: handle nested decl paths properly!
859 if (typeValue.hasCte) {
860 if (wantHtml)
861 result += "<a href=\"\">[ComptimeExpr]</a>";
862 else
863 result += "[ComptimeExpr]";
864 break;
865 }
866 var name = escapeHtml(decl.name);
862 for (var j = 0; j < typeValue.refPath.length; j++) {
863
864 var name = "[RefPath]";
867865 if (wantHtml) {
868 result += '<a href="'+navLinkDecl(decl.name)+'">';
866 //result += '<a href="'+navLinkDecl(decl.name)+'">';
867 result += '<a href="">';
869868 result += '<span class="tok-kw" style="color:lightblue;">' +
870869 name + '</span>';
871870 result += '</a>';
......@@ -1182,24 +1181,11 @@ var zigAnalysis;
11821181
11831182 if (isVarArgs && i === typeObj.params.length - 1) {
11841183 payloadHtml += '...';
1185 } else if ("declPath" in value) {
1186 if (value.hasCte) {
1187 var cte = findCteInDeclPath(value.declPath);
1188 payloadHtml += "[ComptimeExpr]";
1189 } else {
1190 var decl = zigAnalysis.decls[value.declPath[0]];
1191 var val = resolveValue(decl.value);
1192 if ("comptimeExpr" in val) {
1193 payloadHtml += "[ComptimeExpr]";
1194 } else {
1195 console.assert("type" in val);
1196 var valType = zigAnalysis.types[val.type];
1197 var valTypeName = typeShorthandName(valType);
1198 payloadHtml += '<a href="'+navLinkDecl(decl.name)+'">';
1199 payloadHtml += '<span class="tok-kw" style="color:lightblue;">' + escapeHtml(decl.name) + '</span>';
1200 payloadHtml += '</a>';
1201 }
1202 }
1184 } else if ("refPath" in value) {
1185 payloadHtml += '<a href="">';
1186 payloadHtml += '<span class="tok-kw" style="color:lightblue;">[Ref Path]</span>';
1187 payloadHtml += '</a>';
1188
12031189 } else if ("type" in value) {
12041190 var name = typeValueName(value, false);
12051191 payloadHtml += '<span class="tok-kw">' + escapeHtml(name) + '</span>';
......@@ -1422,17 +1408,13 @@ var zigAnalysis;
14221408 domFnProto.classList.remove("hidden");
14231409 }
14241410
1425 function renderContainer(container) {
1426 var typesList = [];
1427 var namespacesList = [];
1428 var errSetsList = [];
1429 var fnsList = [];
1430 var varsList = [];
1431 var valsList = [];
14321411
1433 var declLen = container.pubDecls ? container.pubDecls.length : 0;
1434 for (var i = 0; i < declLen; i += 1) {
1435 var decl = zigAnalysis.decls[container.pubDecls[i]];
1412 function categorizeDecls(decls,
1413 typesList, namespacesList, errSetsList,
1414 fnsList, varsList, valsList) {
1415
1416 for (var i = 0; i < decls.length; i += 1) {
1417 var decl = zigAnalysis.decls[decls[i]];
14361418 var declValue = resolveValue(decl.value);
14371419
14381420 if (decl.kind === 'var') {
......@@ -1441,7 +1423,19 @@ var zigAnalysis;
14411423 }
14421424
14431425 if (decl.kind === 'const') {
1444 if (!("type" in declValue)){
1426 if ("call" in declValue) {
1427 let c = zigAnalysis.calls[declValue.call];
1428 console.assert("comptimeExpr" in c.ret);
1429 let fDecl = resolveValue(c.func);
1430 console.assert("type" in fDecl);
1431 let fType = zigAnalysis.types[fDecl.type];
1432 console.assert("type" in fType.ret);
1433 if (fType.ret.type === typeTypeId) {
1434 typesList.push(decl);
1435 } else {
1436 valsList.push(decl);
1437 }
1438 } else if (!("type" in declValue)){
14451439 valsList.push(decl);
14461440 } else {
14471441 var value = zigAnalysis.types[declValue.type];
......@@ -1465,42 +1459,23 @@ var zigAnalysis;
14651459 }
14661460 }
14671461 }
1462 }
14681463
1469 declLen = container.privDecls ? container.privDecls.length : 0;
1470 for (var i = 0; i < declLen; i += 1) {
1471 var decl = zigAnalysis.decls[container.privDecls[i]];
1472 var declValue = resolveValue(decl.value);
1473
1474 if (decl.kind === 'var') {
1475 varsList.push(decl);
1476 continue;
1477 }
1464 function renderContainer(container) {
1465 var typesList = [];
1466 var namespacesList = [];
1467 var errSetsList = [];
1468 var fnsList = [];
1469 var varsList = [];
1470 var valsList = [];
14781471
1479 if (decl.kind === 'const') {
1480 if (!("type" in declValue)){
1481 valsList.push(decl);
1482 } else {
1483 var value = zigAnalysis.types[declValue.type];
1484 var kind = value.kind;
1485 if (kind === typeKinds.Fn) {
1486 // TODO: handle CTE return types when we know their type.
1487 const resVal = resolveValue(value.ret);
1488 if ("type" in resVal && resVal.type == typeTypeId) {
1489 typesList.push(decl);
1490 } else {
1491 fnsList.push(decl);
1492 }
1472 categorizeDecls(container.pubDecls,
1473 typesList, namespacesList, errSetsList,
1474 fnsList, varsList, valsList);
1475 if (curNav.showPrivDecls) categorizeDecls(container.privDecls,
1476 typesList, namespacesList, errSetsList,
1477 fnsList, varsList, valsList);
14931478
1494 } else if (typeIsErrSet(declValue.type)) {
1495 errSetsList.push(decl);
1496 } else if (typeIsStructWithNoFields(declValue.type)) {
1497 namespacesList.push(decl);
1498 } else {
1499 typesList.push(decl);
1500 }
1501 }
1502 }
1503 }
15041479
15051480 typesList.sort(byNameProperty);
15061481 namespacesList.sort(byNameProperty);
......@@ -1712,6 +1687,7 @@ var zigAnalysis;
17121687
17131688 function updateCurNav() {
17141689 curNav = {
1690 showPrivDecls: false,
17151691 pkgNames: [],
17161692 pkgObjs: [],
17171693 declNames: [],
......@@ -1721,6 +1697,11 @@ var zigAnalysis;
17211697
17221698 if (location.hash[0] === '#' && location.hash.length > 1) {
17231699 var query = location.hash.substring(1);
1700 if (query[0] === '*') {
1701 curNav.showPrivDecls = true;
1702 query = query.substring(1);
1703 }
1704
17241705 var qpos = query.indexOf("?");
17251706 var nonSearchPart;
17261707 if (qpos === -1) {
src/Autodoc.zig+6-1
......@@ -608,6 +608,7 @@ const DocData = struct {
608608 \\, "value": {s}1 }} }}
609609 , .{neg});
610610 // TODO: uncomment once float panic is fixed in stdlib
611 // See: https://github.com/ziglang/zig/issues/11283
611612 // try w.print(
612613 // \\, "value": {s}{e} }} }}
613614 // , .{ neg, v.value });
......@@ -621,7 +622,6 @@ const DocData = struct {
621622 .@"null" => |v| try std.json.stringify(v, options, w),
622623 .typeOf, .sizeOf => |v| try std.json.stringify(v, options, w),
623624 .compileError => |v| try std.json.stringify(v, options, w),
624 .string => |v| try std.json.stringify(v, options, w),
625625 .fieldRef => |v| try std.json.stringify(
626626 struct { fieldRef: FieldRef }{ .fieldRef = v },
627627 options,
......@@ -645,6 +645,11 @@ const DocData = struct {
645645 options,
646646 w,
647647 ),
648 .string => |v| try std.json.stringify(
649 struct { string: []const u8 }{ .string = v },
650 options,
651 w,
652 ),
648653 .enumLiteral => |v| try std.json.stringify(
649654 struct { @"enumLiteral": []const u8 }{ .@"enumLiteral" = v },
650655 options,