Skip to content

Commit

Permalink
Generate generics on search-index
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 12, 2017
1 parent 965ace5 commit 2db6ce6
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ impl ToJson for IndexItem {
/// A type used for the search index.
struct Type {
name: Option<String>,
generics: Option<Vec<String>>,
}

impl ToJson for Type {
Expand All @@ -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
Expand Down Expand Up @@ -420,7 +424,7 @@ fn init_ids() -> FxHashMap<String, usize> {
"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
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -3956,23 +3959,42 @@ fn get_index_search_type(item: &clean::Item) -> Option<IndexItemFunctionType> {
}

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<String> {
fn get_index_type_name(clean_type: &clean::Type, accept_generic: bool) -> Option<String> {
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<Vec<String>> {
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::<Vec<_>>();
if r.is_empty() {
None
} else {
Some(r)
}
})
}

pub fn cache() -> Arc<Cache> {
CACHE_KEY.with(|c| c.borrow().clone())
}
Expand Down

0 comments on commit 2db6ce6

Please sign in to comment.