From 2db6ce6e96c12d0c2936ef09a4b28c7baaec31dc Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 31 Oct 2017 20:44:31 +0100 Subject: [PATCH] Generate generics on search-index --- src/librustdoc/html/render.rs | 36 ++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index eb59c57603a..69eaf24289b 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -361,6 +361,7 @@ impl ToJson for IndexItem { /// A type used for the search index. struct Type { name: Option, + generics: Option>, } impl ToJson for Type { @@ -369,6 +370,9 @@ impl ToJson for Type { Some(ref name) => { let mut data = BTreeMap::new(); data.insert("name".to_owned(), name.to_json()); + if let Some(ref generics) = self.generics { + data.insert("generics".to_owned(), generics.to_json()); + } Json::Object(data) }, None => Json::Null @@ -420,7 +424,7 @@ fn init_ids() -> FxHashMap { "methods", "deref-methods", "implementations", - ].into_iter().map(|id| (String::from(*id), 1)).collect() + ].into_iter().map(|id| (String::from(*id), 1)).collect() } /// This method resets the local table of used ID attributes. This is typically @@ -667,7 +671,6 @@ fn concise_compared_strs(s1: &str, s2: &str) -> (String, String) { (format!("...{}", concise_str(s1)), format!("...{}", concise_str(s2))) } - fn print_message(msg: &str, intro_msg: &mut bool, span: &Span, text: &str) { if !*intro_msg { println!("WARNING: documentation for this crate may be rendered \ @@ -3956,23 +3959,42 @@ fn get_index_search_type(item: &clean::Item) -> Option { } fn get_index_type(clean_type: &clean::Type) -> Type { - Type { name: get_index_type_name(clean_type).map(|s| s.to_ascii_lowercase()) } + let t = Type { + name: get_index_type_name(clean_type, true).map(|s| s.to_ascii_lowercase()), + generics: get_generics(clean_type), + }; + t } -fn get_index_type_name(clean_type: &clean::Type) -> Option { +fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option { match *clean_type { clean::ResolvedPath { ref path, .. } => { let segments = &path.segments; Some(segments[segments.len() - 1].name.clone()) - }, - clean::Generic(ref s) => Some(s.clone()), + } + clean::Generic(ref s) if accept_generic => Some(s.clone()), clean::Primitive(ref p) => Some(format!("{:?}", p)), - clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_), + clean::BorrowedRef { ref type_, .. } => get_index_type_name(type_, accept_generic), // FIXME: add all from clean::Type. _ => None } } +fn get_generics(clean_type: &clean::Type) -> Option> { + clean_type.generics() + .and_then(|types| { + let r = types.iter() + .filter_map(|t| get_index_type_name(t, false)) + .map(|s| s.to_ascii_lowercase()) + .collect::>(); + if r.is_empty() { + None + } else { + Some(r) + } + }) +} + pub fn cache() -> Arc { CACHE_KEY.with(|c| c.borrow().clone()) }