Skip to content

Commit fb74e62

Browse files
committed
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 4125b27 commit fb74e62

File tree

2 files changed

+25
-18
lines changed

2 files changed

+25
-18
lines changed

src/librustdoc/html/render.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -1487,8 +1487,8 @@ impl DocFolder for Cache {
14871487
clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) |
14881488
clean::ConstantItem(..) | clean::StaticItem(..) |
14891489
clean::UnionItem(..) | clean::ForeignTypeItem |
1490-
clean::MacroItem(..) | clean::ProcMacroItem(..)
1491-
if !self.stripped_mod => {
1490+
clean::MacroItem(..) | clean::ProcMacroItem(..) |
1491+
clean::VariantItem(..) if !self.stripped_mod => {
14921492
// Re-exported items mean that the same id can show up twice
14931493
// in the rustdoc ast that we're looking at. We know,
14941494
// however, that a re-exported item doesn't show up in the
@@ -1503,13 +1503,6 @@ impl DocFolder for Cache {
15031503
}
15041504
self.add_aliases(&item);
15051505
}
1506-
// Link variants to their parent enum because pages aren't emitted
1507-
// for each variant.
1508-
clean::VariantItem(..) if !self.stripped_mod => {
1509-
let mut stack = self.stack.clone();
1510-
stack.pop();
1511-
self.paths.insert(item.def_id, (stack, ItemType::Enum));
1512-
}
15131506

15141507
clean::PrimitiveItem(..) if item.visibility.is_some() => {
15151508
self.add_aliases(&item);
@@ -1524,7 +1517,7 @@ impl DocFolder for Cache {
15241517
let orig_parent_is_trait_impl = self.parent_is_trait_impl;
15251518
let parent_pushed = match item.inner {
15261519
clean::TraitItem(..) | clean::EnumItem(..) | clean::ForeignTypeItem |
1527-
clean::StructItem(..) | clean::UnionItem(..) => {
1520+
clean::StructItem(..) | clean::UnionItem(..) | clean::VariantItem(..) => {
15281521
self.parent_stack.push(item.def_id);
15291522
self.parent_is_trait_impl = false;
15301523
true

src/librustdoc/html/static/main.js

+22-8
Original file line numberDiff line numberDiff line change
@@ -1306,14 +1306,15 @@ function getSearchElement() {
13061306
var href;
13071307
var type = itemTypes[item.ty];
13081308
var name = item.name;
1309+
var path = item.path;
13091310

13101311
if (type === "mod") {
1311-
displayPath = item.path + "::";
1312-
href = rootPath + item.path.replace(/::/g, "/") + "/" +
1312+
displayPath = path + "::";
1313+
href = rootPath + path.replace(/::/g, "/") + "/" +
13131314
name + "/index.html";
13141315
} else if (type === "primitive" || type === "keyword") {
13151316
displayPath = "";
1316-
href = rootPath + item.path.replace(/::/g, "/") +
1317+
href = rootPath + path.replace(/::/g, "/") +
13171318
"/" + type + "." + name + ".html";
13181319
} else if (type === "externcrate") {
13191320
displayPath = "";
@@ -1322,14 +1323,27 @@ function getSearchElement() {
13221323
var myparent = item.parent;
13231324
var anchor = "#" + type + "." + name;
13241325
var parentType = itemTypes[myparent.ty];
1326+
var pageType = parentType;
1327+
var pageName = myparent.name;
1328+
13251329
if (parentType === "primitive") {
13261330
displayPath = myparent.name + "::";
1331+
} else if (type === "structfield" && parentType === "variant") {
1332+
// Structfields belonging to variants are special: the
1333+
// final path element is the enum name.
1334+
var splitPath = item.path.split("::");
1335+
var enumName = splitPath.pop();
1336+
path = splitPath.join("::");
1337+
displayPath = path + "::" + enumName + "::" + myparent.name + "::";
1338+
anchor = "#variant." + myparent.name + ".field." + name;
1339+
pageType = "enum";
1340+
pageName = enumName;
13271341
} else {
1328-
displayPath = item.path + "::" + myparent.name + "::";
1342+
displayPath = path + "::" + myparent.name + "::";
13291343
}
1330-
href = rootPath + item.path.replace(/::/g, "/") +
1331-
"/" + parentType +
1332-
"." + myparent.name +
1344+
href = rootPath + path.replace(/::/g, "/") +
1345+
"/" + pageType +
1346+
"." + pageName +
13331347
".html" + anchor;
13341348
} else {
13351349
displayPath = item.path + "::";
@@ -1611,7 +1625,7 @@ function getSearchElement() {
16111625
// (String) name]
16121626
var paths = rawSearchIndex[crate].p;
16131627

1614-
// convert `paths` into an object form
1628+
// convert `rawPaths` entries into object form
16151629
var len = paths.length;
16161630
for (i = 0; i < len; ++i) {
16171631
paths[i] = {ty: paths[i][0], name: paths[i][1]};

0 commit comments

Comments
 (0)