From 883c177abb216fcef5b2d2369970394b0967f302 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Mon, 4 May 2020 15:26:17 +0200 Subject: [PATCH] Move doc alias discovery into the Attributes struct and some code improvements --- src/librustdoc/clean/types.rs | 9 +++++++ src/librustdoc/html/render.rs | 2 +- src/librustdoc/html/render/cache.rs | 41 +++-------------------------- src/librustdoc/html/static/main.js | 2 +- 4 files changed, 15 insertions(+), 39 deletions(-) diff --git a/src/librustdoc/clean/types.rs b/src/librustdoc/clean/types.rs index 0a682857b182..cd767802714b 100644 --- a/src/librustdoc/clean/types.rs +++ b/src/librustdoc/clean/types.rs @@ -643,6 +643,15 @@ impl Attributes { }) .collect() } + + pub fn get_doc_aliases(&self) -> FxHashSet { + self.other_attrs + .lists(sym::doc) + .filter(|a| a.check_name(sym::alias)) + .filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", ""))) + .filter(|v| !v.is_empty()) + .collect::>() + } } impl PartialEq for Attributes { diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index 9454baf76409..abca8ab778cc 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -278,7 +278,7 @@ pub struct RenderInfo { /// Struct representing one entry in the JS search index. These are all emitted /// by hand to a large JS file at the end of cache-creation. #[derive(Debug)] -pub struct IndexItem { +struct IndexItem { ty: ItemType, name: String, path: String, diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index 53cf1abb16d5..b8d97c2ac503 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -328,15 +328,7 @@ impl DocFolder for Cache { search_type: get_index_search_type(&item), }); - for alias in item - .attrs - .lists(sym::doc) - .filter(|a| a.check_name(sym::alias)) - .filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", ""))) - .filter(|v| !v.is_empty()) - .collect::>() - .into_iter() - { + for alias in item.attrs.get_doc_aliases() { self.aliases .entry(alias.to_lowercase()) .or_insert(Vec::with_capacity(1)) @@ -378,9 +370,6 @@ impl DocFolder for Cache { | clean::MacroItem(..) | clean::ProcMacroItem(..) | clean::VariantItem(..) - | clean::StructFieldItem(..) - | clean::TyMethodItem(..) - | clean::MethodItem(..) if !self.stripped_mod => { // Re-exported items mean that the same id can show up twice @@ -564,15 +553,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String { parent_idx: None, search_type: get_index_search_type(&item), }); - for alias in item - .attrs - .lists(sym::doc) - .filter(|a| a.check_name(sym::alias)) - .filter_map(|a| a.value_str().map(|s| s.to_string().replace("\"", ""))) - .filter(|v| !v.is_empty()) - .collect::>() - .into_iter() - { + for alias in item.attrs.get_doc_aliases().into_iter() { aliases .entry(alias.to_lowercase()) .or_insert(Vec::with_capacity(1)) @@ -619,22 +600,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String { .map(|module| shorten(plain_summary_line(module.doc_value()))) .unwrap_or(String::new()); - let crate_aliases = aliases - .iter() - .map(|(k, values)| { - ( - k.clone(), - values - .iter() - .filter_map(|v| { - let x = &crate_items[*v]; - if x.parent_idx.is_some() == x.parent.is_some() { Some(*v) } else { None } - }) - .collect::>(), - ) - }) - .filter(|(_, values)| !values.is_empty()) - .collect::>(); + let crate_aliases = + aliases.iter().map(|(k, values)| (k.clone(), values.clone())).collect::>(); #[derive(Serialize)] struct CrateData<'a> { diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 94ae69fde57f..22e312e13c01 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -972,7 +972,7 @@ function getSearchElement() { desc: item.desc, ty: item.ty, parent: item.parent, - type: item.parent, + type: item.type, is_alias: true, }; }