Skip to content
Prev Previous commit
Next Next commit
Move doc alias discovery into the Attributes struct and some code imp…
…rovements
  • Loading branch information
GuillaumeGomez committed May 14, 2020
commit 883c177abb216fcef5b2d2369970394b0967f302
9 changes: 9 additions & 0 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,15 @@ impl Attributes {
})
.collect()
}

pub fn get_doc_aliases(&self) -> FxHashSet<String> {
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::<FxHashSet<_>>()
}
}

impl PartialEq for Attributes {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
41 changes: 4 additions & 37 deletions src/librustdoc/html/render/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<FxHashSet<_>>()
.into_iter()
{
for alias in item.attrs.get_doc_aliases() {
self.aliases
.entry(alias.to_lowercase())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that the case insensitive searching should be handled by the JavaScript so that the alias displayed in the results will have the same case as it was specified with.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because otherwise, we might have conflicts to handle on the JS side if we have Hello and hello. Also, I think it's better to lighten JS computations. In any case, we'll have to turn keys into lowercase, so I think it's better to do it directly on the Rust side once and for all.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it's not a big issue for now. It does seem weird that aliases are treated differently to the other search results though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The alias match is "exact", this is the base difference. But we can debate it in an issue if you want?

.or_insert(Vec::with_capacity(1))
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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::<FxHashSet<_>>()
.into_iter()
{
for alias in item.attrs.get_doc_aliases().into_iter() {
aliases
.entry(alias.to_lowercase())
.or_insert(Vec::with_capacity(1))
Expand Down Expand Up @@ -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::<Vec<_>>(),
)
})
.filter(|(_, values)| !values.is_empty())
.collect::<Vec<_>>();
let crate_aliases =
aliases.iter().map(|(k, values)| (k.clone(), values.clone())).collect::<Vec<_>>();

#[derive(Serialize)]
struct CrateData<'a> {
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/static/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ function getSearchElement() {
desc: item.desc,
ty: item.ty,
parent: item.parent,
type: item.parent,
type: item.type,
is_alias: true,
};
}
Expand Down