Skip to content

Commit

Permalink
rustdoc: emit JS paths for struct-like variants
Browse files Browse the repository at this point in the history
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 rust-lang#16017
  • Loading branch information
tomjakubowski committed Sep 24, 2019
1 parent 4125b27 commit fb74e62
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
13 changes: 3 additions & 10 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1487,8 +1487,8 @@ impl DocFolder for Cache {
clean::ForeignFunctionItem(..) | clean::ForeignStaticItem(..) |
clean::ConstantItem(..) | clean::StaticItem(..) |
clean::UnionItem(..) | clean::ForeignTypeItem |
clean::MacroItem(..) | clean::ProcMacroItem(..)
if !self.stripped_mod => {
clean::MacroItem(..) | clean::ProcMacroItem(..) |
clean::VariantItem(..) if !self.stripped_mod => {
// Re-exported items mean that the same id can show up twice
// in the rustdoc ast that we're looking at. We know,
// however, that a re-exported item doesn't show up in the
Expand All @@ -1503,13 +1503,6 @@ impl DocFolder for Cache {
}
self.add_aliases(&item);
}
// Link variants to their parent enum because pages aren't emitted
// for each variant.
clean::VariantItem(..) if !self.stripped_mod => {
let mut stack = self.stack.clone();
stack.pop();
self.paths.insert(item.def_id, (stack, ItemType::Enum));
}

clean::PrimitiveItem(..) if item.visibility.is_some() => {
self.add_aliases(&item);
Expand All @@ -1524,7 +1517,7 @@ impl DocFolder for Cache {
let orig_parent_is_trait_impl = self.parent_is_trait_impl;
let parent_pushed = match item.inner {
clean::TraitItem(..) | clean::EnumItem(..) | clean::ForeignTypeItem |
clean::StructItem(..) | clean::UnionItem(..) => {
clean::StructItem(..) | clean::UnionItem(..) | clean::VariantItem(..) => {
self.parent_stack.push(item.def_id);
self.parent_is_trait_impl = false;
true
Expand Down
30 changes: 22 additions & 8 deletions src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1306,14 +1306,15 @@ function getSearchElement() {
var href;
var type = itemTypes[item.ty];
var name = item.name;
var path = item.path;

if (type === "mod") {
displayPath = item.path + "::";
href = rootPath + item.path.replace(/::/g, "/") + "/" +
displayPath = path + "::";
href = rootPath + path.replace(/::/g, "/") + "/" +
name + "/index.html";
} else if (type === "primitive" || type === "keyword") {
displayPath = "";
href = rootPath + item.path.replace(/::/g, "/") +
href = rootPath + path.replace(/::/g, "/") +
"/" + type + "." + name + ".html";
} else if (type === "externcrate") {
displayPath = "";
Expand All @@ -1322,14 +1323,27 @@ function getSearchElement() {
var myparent = item.parent;
var anchor = "#" + type + "." + name;
var parentType = itemTypes[myparent.ty];
var pageType = parentType;
var pageName = myparent.name;

if (parentType === "primitive") {
displayPath = myparent.name + "::";
} else if (type === "structfield" && parentType === "variant") {
// Structfields belonging to variants are special: the
// final path element is the enum name.
var splitPath = item.path.split("::");
var enumName = splitPath.pop();
path = splitPath.join("::");
displayPath = path + "::" + enumName + "::" + myparent.name + "::";
anchor = "#variant." + myparent.name + ".field." + name;
pageType = "enum";
pageName = enumName;
} else {
displayPath = item.path + "::" + myparent.name + "::";
displayPath = path + "::" + myparent.name + "::";
}
href = rootPath + item.path.replace(/::/g, "/") +
"/" + parentType +
"." + myparent.name +
href = rootPath + path.replace(/::/g, "/") +
"/" + pageType +
"." + pageName +
".html" + anchor;
} else {
displayPath = item.path + "::";
Expand Down Expand Up @@ -1611,7 +1625,7 @@ function getSearchElement() {
// (String) name]
var paths = rawSearchIndex[crate].p;

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

0 comments on commit fb74e62

Please sign in to comment.