Skip to content

Commit 93b86d6

Browse files
Rollup merge of #96748 - GuillaumeGomez:reexports-in-search, r=notriddle
Fixes reexports in search Fixes #96681. At some point we stopped reexporting items in search so this PR fixes it. It also adds a regression test. r? ```@notriddle```
2 parents c3ddd59 + fb2f97a commit 93b86d6

File tree

7 files changed

+71
-12
lines changed

7 files changed

+71
-12
lines changed

src/librustdoc/formats/cache.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,16 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
248248
}
249249

250250
// Index this method for searching later on.
251-
if let Some(ref s) = item.name {
251+
if let Some(ref s) = item.name.or_else(|| {
252+
if item.is_stripped() {
253+
None
254+
} else if let clean::ImportItem(ref i) = *item.kind &&
255+
let clean::ImportKind::Simple(s) = i.kind {
256+
Some(s)
257+
} else {
258+
None
259+
}
260+
}) {
252261
let (parent, is_inherent_impl_item) = match *item.kind {
253262
clean::StrippedItem(..) => ((None, None), false),
254263
clean::AssocConstItem(..) | clean::AssocTypeItem(..)

src/librustdoc/html/render/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -2542,7 +2542,16 @@ fn sidebar_module(buf: &mut Buffer, items: &[clean::Item]) {
25422542

25432543
let item_sections_in_use: FxHashSet<_> = items
25442544
.iter()
2545-
.filter(|it| !it.is_stripped() && it.name.is_some())
2545+
.filter(|it| {
2546+
!it.is_stripped()
2547+
&& it
2548+
.name
2549+
.or_else(|| {
2550+
if let clean::ImportItem(ref i) = *it.kind &&
2551+
let clean::ImportKind::Simple(s) = i.kind { Some(s) } else { None }
2552+
})
2553+
.is_some()
2554+
})
25462555
.map(|it| item_ty_to_section(it.type_()))
25472556
.collect();
25482557
for &sec in ItemSection::ALL.iter().filter(|sec| item_sections_in_use.contains(sec)) {

src/librustdoc/html/render/print_item.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
346346
w.write_str(ITEM_TABLE_ROW_OPEN);
347347
write!(
348348
w,
349-
"<div class=\"item-left {stab}{add}import-item\">\
349+
"<div class=\"item-left {stab}{add}import-item\"{id}>\
350350
<code>{vis}{imp}</code>\
351351
</div>\
352352
<div class=\"item-right docblock-short\">{stab_tags}</div>",
@@ -355,6 +355,11 @@ fn item_module(w: &mut Buffer, cx: &Context<'_>, item: &clean::Item, items: &[cl
355355
vis = myitem.visibility.print_with_space(myitem.item_id, cx),
356356
imp = import.print(cx),
357357
stab_tags = stab_tags.unwrap_or_default(),
358+
id = match import.kind {
359+
clean::ImportKind::Simple(s) =>
360+
format!(" id=\"{}\"", cx.derive_id(format!("reexport.{}", s))),
361+
clean::ImportKind::Glob => String::new(),
362+
},
358363
);
359364
w.write_str(ITEM_TABLE_ROW_CLOSE);
360365
}

src/librustdoc/html/static/js/search.js

+3
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,9 @@ window.initSearch = rawSearchIndex => {
15101510
displayPath = path + "::";
15111511
href = window.rootPath + path.replace(/::/g, "/") + "/" +
15121512
name + "/index.html";
1513+
} else if (type === "import") {
1514+
displayPath = item.path + "::";
1515+
href = window.rootPath + item.path.replace(/::/g, "/") + "/index.html#reexport." + name;
15131516
} else if (type === "primitive" || type === "keyword") {
15141517
displayPath = "";
15151518
href = window.rootPath + path.replace(/::/g, "/") +
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Checks that the reexports are present in the search index, can have
2+
// doc aliases and are highligted when their ID is the hash of the page.
3+
goto: file://|DOC_PATH|/test_docs/index.html
4+
local-storage: {"rustdoc-theme": "dark", "rustdoc-use-system-theme": "false"}
5+
reload:
6+
// First we check that the reexport has the correct ID and no background color.
7+
assert-text: ("//*[@id='reexport.TheStdReexport']", "pub use ::std as TheStdReexport;")
8+
assert-css: ("//*[@id='reexport.TheStdReexport']", {"background-color": "rgba(0, 0, 0, 0)"})
9+
write: (".search-input", "TheStdReexport")
10+
wait-for: "//a[@class='result-import']"
11+
assert-attribute: (
12+
"//a[@class='result-import']",
13+
{"href": "../test_docs/index.html#reexport.TheStdReexport"},
14+
)
15+
assert-text: ("//a[@class='result-import']", "test_docs::TheStdReexport")
16+
click: "//a[@class='result-import']"
17+
// We check that it has the background modified thanks to the focus.
18+
wait-for-css: ("//*[@id='reexport.TheStdReexport']", {"background-color": "rgb(73, 74, 61)"})
19+
20+
// We now check that the alias is working as well on the reexport.
21+
write: (".search-input", "AliasForTheStdReexport")
22+
wait-for: "//a[@class='result-import']"
23+
assert-text: (
24+
"//a[@class='result-import']",
25+
"AliasForTheStdReexport - see test_docs::TheStdReexport",
26+
)
27+
// Same thing again, we click on it to ensure the background is once again set as expected.
28+
click: "//a[@class='result-import']"
29+
wait-for-css: ("//*[@id='reexport.TheStdReexport']", {"background-color": "rgb(73, 74, 61)"})

src/test/rustdoc-gui/sidebar.goml

+10-9
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ assert-css: ("#all-types", {"color": "rgb(53, 109, 164)"})
1313
// We check that we have the crates list and that the "current" on is "test_docs".
1414
assert-text: (".sidebar-elems .crate > ul > li > a.current", "test_docs")
1515
// And we're also supposed to have the list of items in the current module.
16-
assert-text: (".sidebar-elems section ul > li:nth-child(1)", "Modules")
17-
assert-text: (".sidebar-elems section ul > li:nth-child(2)", "Macros")
18-
assert-text: (".sidebar-elems section ul > li:nth-child(3)", "Structs")
19-
assert-text: (".sidebar-elems section ul > li:nth-child(4)", "Enums")
20-
assert-text: (".sidebar-elems section ul > li:nth-child(5)", "Traits")
21-
assert-text: (".sidebar-elems section ul > li:nth-child(6)", "Functions")
22-
assert-text: (".sidebar-elems section ul > li:nth-child(7)", "Type Definitions")
23-
assert-text: (".sidebar-elems section ul > li:nth-child(8)", "Unions")
24-
assert-text: (".sidebar-elems section ul > li:nth-child(9)", "Keywords")
16+
assert-text: (".sidebar-elems section ul > li:nth-child(1)", "Re-exports")
17+
assert-text: (".sidebar-elems section ul > li:nth-child(2)", "Modules")
18+
assert-text: (".sidebar-elems section ul > li:nth-child(3)", "Macros")
19+
assert-text: (".sidebar-elems section ul > li:nth-child(4)", "Structs")
20+
assert-text: (".sidebar-elems section ul > li:nth-child(5)", "Enums")
21+
assert-text: (".sidebar-elems section ul > li:nth-child(6)", "Traits")
22+
assert-text: (".sidebar-elems section ul > li:nth-child(7)", "Functions")
23+
assert-text: (".sidebar-elems section ul > li:nth-child(8)", "Type Definitions")
24+
assert-text: (".sidebar-elems section ul > li:nth-child(9)", "Unions")
25+
assert-text: (".sidebar-elems section ul > li:nth-child(10)", "Keywords")
2526
assert-text: ("#structs + .item-table .item-left > a", "Foo")
2627
click: "#structs + .item-table .item-left > a"
2728

src/test/rustdoc-gui/src/test_docs/lib.rs

+3
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,6 @@ impl EmptyTrait3 for HasEmptyTraits {}
274274

275275
mod macros;
276276
pub use macros::*;
277+
278+
#[doc(alias = "AliasForTheStdReexport")]
279+
pub use ::std as TheStdReexport;

0 commit comments

Comments
 (0)