Skip to content

Commit bc84eb4

Browse files
authored
Rollup merge of rust-lang#71724 - GuillaumeGomez:doc-alias-improvements, r=ollie27
Doc alias improvements After [this message](rust-lang#50146 (comment)), I realized that the **doc alias**. So this PR does the followings: * Align the alias discovery on items added into the search-index. It brings a few nice advantages: * Instead of cloning the data between the two (in rustdoc source code), we now have the search-index one and aliases which reference to the first one. So we go from one big map containing a lot of duplicated data to just integers... * In the front-end (main.js), I improved the code around aliases to allow them to go through the same transformation as other items when we show the search results. * Improve the search tester in order to perform multiple requests into one file (I think it's better in this case than having a file for each case considering how many there are...) * I also had to add the new function inside the tester (`handleAliases`) Once this PR is merged, I intend to finally stabilize this feature. r? @ollie27 cc @rust-lang/rustdoc
2 parents 9f0e5c4 + e17ac66 commit bc84eb4

File tree

9 files changed

+561
-143
lines changed

9 files changed

+561
-143
lines changed

src/librustdoc/clean/types.rs

+9
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,15 @@ impl Attributes {
643643
})
644644
.collect()
645645
}
646+
647+
pub fn get_doc_aliases(&self) -> FxHashSet<String> {
648+
self.other_attrs
649+
.lists(sym::doc)
650+
.filter(|a| a.check_name(sym::alias))
651+
.filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", "")))
652+
.filter(|v| !v.is_empty())
653+
.collect::<FxHashSet<_>>()
654+
}
646655
}
647656

648657
impl PartialEq for Attributes {

src/librustdoc/html/layout.rs

-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ pub fn render<T: Print, S: Print>(
114114
window.rootPath = \"{root_path}\";\
115115
window.currentCrate = \"{krate}\";\
116116
</script>\
117-
<script src=\"{root_path}aliases{suffix}.js\"></script>\
118117
<script src=\"{static_root_path}main{suffix}.js\"></script>\
119118
{static_extra_scripts}\
120119
{extra_scripts}\

src/librustdoc/html/render.rs

+6-37
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,12 @@ impl Serialize for IndexItem {
293293
where
294294
S: Serializer,
295295
{
296-
assert_eq!(self.parent.is_some(), self.parent_idx.is_some());
296+
assert_eq!(
297+
self.parent.is_some(),
298+
self.parent_idx.is_some(),
299+
"`{}` is missing idx",
300+
self.name
301+
);
297302

298303
(self.ty, &self.name, &self.path, &self.desc, self.parent_idx, &self.search_type)
299304
.serialize(serializer)
@@ -819,42 +824,6 @@ themePicker.onblur = handleThemeButtonsBlur;
819824
Ok((ret, krates))
820825
}
821826

822-
fn show_item(item: &IndexItem, krate: &str) -> String {
823-
format!(
824-
"{{'crate':'{}','ty':{},'name':'{}','desc':'{}','p':'{}'{}}}",
825-
krate,
826-
item.ty as usize,
827-
item.name,
828-
item.desc.replace("'", "\\'"),
829-
item.path,
830-
if let Some(p) = item.parent_idx { format!(",'parent':{}", p) } else { String::new() }
831-
)
832-
}
833-
834-
let dst = cx.dst.join(&format!("aliases{}.js", cx.shared.resource_suffix));
835-
{
836-
let (mut all_aliases, _) = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
837-
let mut output = String::with_capacity(100);
838-
for (alias, items) in &cx.cache.aliases {
839-
if items.is_empty() {
840-
continue;
841-
}
842-
output.push_str(&format!(
843-
"\"{}\":[{}],",
844-
alias,
845-
items.iter().map(|v| show_item(v, &krate.name)).collect::<Vec<_>>().join(",")
846-
));
847-
}
848-
all_aliases.push(format!("ALIASES[\"{}\"] = {{{}}};", krate.name, output));
849-
all_aliases.sort();
850-
let mut v = Buffer::html();
851-
writeln!(&mut v, "var ALIASES = {{}};");
852-
for aliases in &all_aliases {
853-
writeln!(&mut v, "{}", aliases);
854-
}
855-
cx.shared.fs.write(&dst, v.into_inner().into_bytes())?;
856-
}
857-
858827
use std::ffi::OsString;
859828

860829
#[derive(Debug)]

src/librustdoc/html/render/cache.rs

+24-40
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ crate struct Cache {
120120

121121
/// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
122122
/// we need the alias element to have an array of items.
123-
pub(super) aliases: FxHashMap<String, Vec<IndexItem>>,
123+
pub(super) aliases: BTreeMap<String, Vec<usize>>,
124124
}
125125

126126
impl Cache {
@@ -311,7 +311,7 @@ impl DocFolder for Cache {
311311
};
312312

313313
match parent {
314-
(parent, Some(path)) if is_inherent_impl_item || (!self.stripped_mod) => {
314+
(parent, Some(path)) if is_inherent_impl_item || !self.stripped_mod => {
315315
debug_assert!(!item.is_stripped());
316316

317317
// A crate has a module at its root, containing all items,
@@ -327,6 +327,13 @@ impl DocFolder for Cache {
327327
parent_idx: None,
328328
search_type: get_index_search_type(&item),
329329
});
330+
331+
for alias in item.attrs.get_doc_aliases() {
332+
self.aliases
333+
.entry(alias.to_lowercase())
334+
.or_insert(Vec::new())
335+
.push(self.search_index.len() - 1);
336+
}
330337
}
331338
}
332339
(Some(parent), None) if is_inherent_impl_item => {
@@ -376,11 +383,8 @@ impl DocFolder for Cache {
376383
{
377384
self.paths.insert(item.def_id, (self.stack.clone(), item.type_()));
378385
}
379-
self.add_aliases(&item);
380386
}
381-
382387
clean::PrimitiveItem(..) => {
383-
self.add_aliases(&item);
384388
self.paths.insert(item.def_id, (self.stack.clone(), item.type_()));
385389
}
386390

@@ -488,40 +492,6 @@ impl DocFolder for Cache {
488492
}
489493
}
490494

491-
impl Cache {
492-
fn add_aliases(&mut self, item: &clean::Item) {
493-
if item.def_id.index == CRATE_DEF_INDEX {
494-
return;
495-
}
496-
if let Some(ref item_name) = item.name {
497-
let path = self
498-
.paths
499-
.get(&item.def_id)
500-
.map(|p| p.0[..p.0.len() - 1].join("::"))
501-
.unwrap_or("std".to_owned());
502-
for alias in item
503-
.attrs
504-
.lists(sym::doc)
505-
.filter(|a| a.check_name(sym::alias))
506-
.filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", "")))
507-
.filter(|v| !v.is_empty())
508-
.collect::<FxHashSet<_>>()
509-
.into_iter()
510-
{
511-
self.aliases.entry(alias).or_insert(Vec::with_capacity(1)).push(IndexItem {
512-
ty: item.type_(),
513-
name: item_name.to_string(),
514-
path: path.clone(),
515-
desc: shorten(plain_summary_line(item.doc_value())),
516-
parent: None,
517-
parent_idx: None,
518-
search_type: get_index_search_type(&item),
519-
});
520-
}
521-
}
522-
}
523-
}
524-
525495
/// Attempts to find where an external crate is located, given that we're
526496
/// rendering in to the specified source destination.
527497
fn extern_location(
@@ -567,7 +537,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
567537
let mut crate_items = Vec::with_capacity(cache.search_index.len());
568538
let mut crate_paths = vec![];
569539

570-
let Cache { ref mut search_index, ref orphan_impl_items, ref paths, .. } = *cache;
540+
let Cache { ref mut search_index, ref orphan_impl_items, ref paths, ref mut aliases, .. } =
541+
*cache;
571542

572543
// Attach all orphan items to the type's definition if the type
573544
// has since been learned.
@@ -582,6 +553,12 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
582553
parent_idx: None,
583554
search_type: get_index_search_type(&item),
584555
});
556+
for alias in item.attrs.get_doc_aliases() {
557+
aliases
558+
.entry(alias.to_lowercase())
559+
.or_insert(Vec::new())
560+
.push(search_index.len() - 1);
561+
}
585562
}
586563
}
587564

@@ -630,6 +607,12 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
630607
items: Vec<&'a IndexItem>,
631608
#[serde(rename = "p")]
632609
paths: Vec<(ItemType, String)>,
610+
// The String is alias name and the vec is the list of the elements with this alias.
611+
//
612+
// To be noted: the `usize` elements are indexes to `items`.
613+
#[serde(rename = "a")]
614+
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
615+
aliases: &'a BTreeMap<String, Vec<usize>>,
633616
}
634617

635618
// Collect the index into a string
@@ -640,6 +623,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
640623
doc: crate_doc,
641624
items: crate_items,
642625
paths: crate_paths,
626+
aliases,
643627
})
644628
.expect("failed serde conversion")
645629
// All these `replace` calls are because we have to go through JS string for JSON content.

0 commit comments

Comments
 (0)