Commit 571123465b

Andrew Kelley <andrew@ziglang.org>
2019-10-06 00:01:01
generated docs: canonical package paths
1 parent 0e40fc4
Changed files (1)
lib
std
special
lib/std/special/doc/main.js
@@ -15,6 +15,9 @@
     var typeKindFnId;
     findTypeKinds();
 
+    // for each package, is an array with packages to get to this one
+    var canonPkgPaths = computeCanonicalPackagePaths();
+
     var curNav = {
         // each element is a package name, e.g. @import("a") then within there @import("b")
         // starting implicitly from root package
@@ -180,7 +183,7 @@
                 var liDom = domListPkgs.children[i];
                 var aDom = liDom.children[0];
                 aDom.textContent = list[i].name;
-                aDom.setAttribute('href', navLinkPkg(list[i].name));
+                aDom.setAttribute('href', navLinkPkg(list[i].pkg));
             }
 
             domSectPkgs.classList.remove("hidden");
@@ -197,8 +200,8 @@
         }
     }
 
-    function navLinkPkg(childName) {
-        return navLink(curNav.pkgNames.concat([childName]), []);
+    function navLinkPkg(pkgIndex) {
+        return navLink(canonPkgPaths[pkgIndex], []);
     }
 
     function navLinkDecl(childName) {
@@ -349,4 +352,31 @@
         }
         return null;
     }
+
+    function computeCanonicalPackagePaths() {
+        var list = new Array(zigAnalysis.packages.length);
+        // Now we try to find all the packages from root.
+        var rootPkg = zigAnalysis.packages[zigAnalysis.rootPkg];
+        // Breadth-first to keep the path shortest possible.
+        var stack = [{
+            path: [],
+            pkg: rootPkg,
+        }];
+        while (stack.length !== 0) {
+            var item = stack.pop();
+            for (var key in item.pkg.table) {
+                var childPkgIndex = item.pkg.table[key];
+                if (list[childPkgIndex] != null) continue;
+
+                var newPath = item.path.concat([key])
+                list[childPkgIndex] = newPath;
+                var childPkg = zigAnalysis.packages[childPkgIndex];
+                stack.push({
+                    path: newPath,
+                    pkg: childPkg,
+                });
+            }
+        }
+        return list;
+    }
 })();