From cc71003cfcd8af8e037dd127ea627113fbc17103 Mon Sep 17 00:00:00 2001 From: Loris Cro Date: Sun, 3 Apr 2022 18:46:03 +0200 Subject: [PATCH] autodoc: improved rendering container fields and fixed rendering of func args --- lib/docs/main.js | 67 ++++++++++++++++++++++++++++++------------------ 1 file changed, 42 insertions(+), 25 deletions(-) diff --git a/lib/docs/main.js b/lib/docs/main.js index f7a434ae06..449b8a8081 100644 --- a/lib/docs/main.js +++ b/lib/docs/main.js @@ -391,8 +391,35 @@ var zigAnalysis; return isType(x) && typeKindIsContainer(/** @type {Type} */(x).kind) ; } - /** @param {Type} type */ - function typeShorthandName(type) { + /** @param {WalkResult} wr */ + function typeShorthandName(wr) { + let resolvedWr = resolveValue(wr); + if (!("type" in resolvedWr)) { + return null; + } + let type = /** @type {Type} */(zigAnalysis.types[resolvedWr.type]); + + outer: for (let i = 0; i < 10000; i += 1) { + switch (type.kind) { + case typeKinds.Optional: + case typeKinds.Pointer: + let child = /** @type {PointerType | OptionalType} */(type).child; + let resolvedChild = resolveValue(child); + if ("type" in resolvedChild) { + type = zigAnalysis.types[resolvedChild.type]; + continue; + } else { + return null; + } + default: + break outer; + } + + if (i == 9999) throw "Exhausted typeShorthandName quota"; + } + + + let name = undefined; if (type.kind === typeKinds.Struct) { name = "struct"; @@ -401,8 +428,10 @@ var zigAnalysis; } else if (type.kind === typeKinds.Union) { name = "union"; } else { - name = /** @type {any} */(type).name; + console.log("TODO: unhalndled case in typeShortName"); + return null; } + return escapeHtml(name); } @@ -954,8 +983,7 @@ var zigAnalysis; */ function resizeDomListDl(dlDom, desiredLen) { // add the missing dom entries - let i, ev; - for (i = dlDom.childElementCount / 2; i < desiredLen; i += 1) { + for (let i = dlDom.childElementCount / 2; i < desiredLen; i += 1) { dlDom.insertAdjacentHTML('beforeend', '
'); } // remove extra dom entries @@ -972,8 +1000,7 @@ var zigAnalysis; */ function resizeDomList(listDom, desiredLen, templateHtml) { // add the missing dom entries - let i, ev; - for (i = listDom.childElementCount; i < desiredLen; i += 1) { + for (let i = listDom.childElementCount; i < desiredLen; i += 1) { listDom.insertAdjacentHTML('beforeend', templateHtml); } // remove extra dom entries @@ -1050,7 +1077,6 @@ var zigAnalysis; console.assert("type" in typeValue) let typeIndex = typeValue.type; let typeObj = zigAnalysis.types[typeIndex]; - let declNameOk = declCanRepresentTypeKind(typeObj.kind); if (wantLink) { let declIndex = getCanonTypeDecl(typeIndex); let declPath = getCanonDeclPath(declIndex); @@ -1359,7 +1385,6 @@ var zigAnalysis; let value = fnObj.params[i]; let paramValue = resolveValue(value); - let isCte = "comptimeExpr" in paramValue; if (fields != null) { let paramNode = zigAnalysis.astNodes[fields[i]]; @@ -1402,7 +1427,7 @@ var zigAnalysis; payloadHtml += ''; } else if ("type" in value) { - let name = typeValueName(value, wantHtml, wantSubLink, fnDecl, linkFnNameDecl); + let name = typeValueName(value, false, false, fnDecl, linkFnNameDecl); payloadHtml += '' + escapeHtml(name) + ''; } else if ("comptimeExpr" in value) { payloadHtml += '[ComptimeExpr]'; @@ -1815,10 +1840,15 @@ var zigAnalysis; if (container.kind === typeKinds.Enum) { html += ' = ' + fieldName + ''; } else { - let field = container.fields[i]; + let fieldTypeWr = container.fields[i]; html += ": "; - let name = typeValueName(field, false, false); + let name = typeValueName(fieldTypeWr, false, false); html += ''+ name +''; + let tsn = typeShorthandName(fieldTypeWr); + if (tsn) { + html += ' ('+ tsn +')'; + + } } html += ','; @@ -2751,18 +2781,5 @@ function byNameProperty(a, b) { } -/** - * @template T - * @param {T} obj - * @returns {T} - */ -function clone(obj) { - let res = /** @type T */({}); - for (let key in obj) { - res[key] = obj[key]; - } - return res; -} - })();