Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 05c6f32

Browse files
tomjakubowskiGuillaumeGomez
authored andcommittedJan 30, 2020
rustdoc: emit JS paths for struct-like variants
On the backend, rustdoc now emits `paths` entries to a crate's search index for struct-like enum variants, and index items of type structfield which belong to such variants point to their variant parents in the `paths` table, rather than their enum grandparents. The path entry for a variant is the fully qualified module path plus the enum name. On the frontend, the search code recognizes structfields belonging to structlike variants in the `paths` table and re-constructs the URL to the field's anchor on the enum documentation page. closes #16017
1 parent b60f08b commit 05c6f32

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed
 

‎src/librustdoc/html/static/main.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -1364,14 +1364,15 @@ function getSearchElement() {
13641364
var href;
13651365
var type = itemTypes[item.ty];
13661366
var name = item.name;
1367+
var path = item.path;
13671368

13681369
if (type === "mod") {
1369-
displayPath = item.path + "::";
1370-
href = rootPath + item.path.replace(/::/g, "/") + "/" +
1370+
displayPath = path + "::";
1371+
href = rootPath + path.replace(/::/g, "/") + "/" +
13711372
name + "/index.html";
13721373
} else if (type === "primitive" || type === "keyword") {
13731374
displayPath = "";
1374-
href = rootPath + item.path.replace(/::/g, "/") +
1375+
href = rootPath + path.replace(/::/g, "/") +
13751376
"/" + type + "." + name + ".html";
13761377
} else if (type === "externcrate") {
13771378
displayPath = "";
@@ -1380,14 +1381,27 @@ function getSearchElement() {
13801381
var myparent = item.parent;
13811382
var anchor = "#" + type + "." + name;
13821383
var parentType = itemTypes[myparent.ty];
1384+
var pageType = parentType;
1385+
var pageName = myparent.name;
1386+
13831387
if (parentType === "primitive") {
13841388
displayPath = myparent.name + "::";
1389+
} else if (type === "structfield" && parentType === "variant") {
1390+
// Structfields belonging to variants are special: the
1391+
// final path element is the enum name.
1392+
var splitPath = item.path.split("::");
1393+
var enumName = splitPath.pop();
1394+
path = splitPath.join("::");
1395+
displayPath = path + "::" + enumName + "::" + myparent.name + "::";
1396+
anchor = "#variant." + myparent.name + ".field." + name;
1397+
pageType = "enum";
1398+
pageName = enumName;
13851399
} else {
1386-
displayPath = item.path + "::" + myparent.name + "::";
1400+
displayPath = path + "::" + myparent.name + "::";
13871401
}
1388-
href = rootPath + item.path.replace(/::/g, "/") +
1389-
"/" + parentType +
1390-
"." + myparent.name +
1402+
href = rootPath + path.replace(/::/g, "/") +
1403+
"/" + pageType +
1404+
"." + pageName +
13911405
".html" + anchor;
13921406
} else {
13931407
displayPath = item.path + "::";
@@ -1668,7 +1682,7 @@ function getSearchElement() {
16681682
// (String) name]
16691683
var paths = rawSearchIndex[crate].p;
16701684

1671-
// convert `paths` into an object form
1685+
// convert `rawPaths` entries into object form
16721686
var len = paths.length;
16731687
for (i = 0; i < len; ++i) {
16741688
paths[i] = {ty: paths[i][0], name: paths[i][1]};

0 commit comments

Comments
 (0)
Failed to load comments.