diff --git a/crates/hir-def/src/attr.rs b/crates/hir-def/src/attr.rs index d44f84e5691..db1c6bc8181 100644 --- a/crates/hir-def/src/attr.rs +++ b/crates/hir-def/src/attr.rs @@ -1,5 +1,8 @@ //! A higher level attributes based on TokenTree, with also some shortcuts. +#[cfg(test)] +mod tests; + use std::{hash::Hash, ops, sync::Arc}; use base_db::CrateId; @@ -238,12 +241,12 @@ pub fn has_doc_hidden(&self) -> bool { }) } - pub fn doc_exprs(&self) -> Vec { - self.by_key("doc").tt_values().map(DocExpr::parse).collect() + pub fn doc_exprs(&self) -> impl Iterator + '_ { + self.by_key("doc").tt_values().map(DocExpr::parse) } - pub fn doc_aliases(&self) -> Vec { - self.doc_exprs().into_iter().flat_map(|doc_expr| doc_expr.aliases()).collect() + pub fn doc_aliases(&self) -> impl Iterator + '_ { + self.doc_exprs().flat_map(|doc_expr| doc_expr.aliases().to_vec()) } pub fn is_proc_macro(&self) -> bool { @@ -288,17 +291,17 @@ fn from(atom: DocAtom) -> Self { } impl DocExpr { - pub fn parse(tt: &tt::Subtree) -> DocExpr { + fn parse(tt: &tt::Subtree) -> DocExpr { next_doc_expr(&mut tt.token_trees.iter()).unwrap_or(DocExpr::Invalid) } - pub fn aliases(self) -> Vec { + pub fn aliases(&self) -> &[SmolStr] { match self { DocExpr::Atom(DocAtom::KeyValue { key, value }) if key == "alias" => { - vec![value] + std::slice::from_ref(value) } DocExpr::Alias(aliases) => aliases, - _ => vec![], + _ => &[], } } } diff --git a/crates/hir-def/src/attr_tests.rs b/crates/hir-def/src/attr/tests.rs similarity index 100% rename from crates/hir-def/src/attr_tests.rs rename to crates/hir-def/src/attr/tests.rs diff --git a/crates/hir-def/src/lib.rs b/crates/hir-def/src/lib.rs index 59d672d0032..8c2e93f0905 100644 --- a/crates/hir-def/src/lib.rs +++ b/crates/hir-def/src/lib.rs @@ -53,8 +53,6 @@ macro_rules! eprintln { mod test_db; #[cfg(test)] mod macro_expansion_tests; -#[cfg(test)] -mod attr_tests; mod pretty; use std::{ diff --git a/crates/ide-completion/src/context.rs b/crates/ide-completion/src/context.rs index 072e8c52637..f6478d2ceb2 100644 --- a/crates/ide-completion/src/context.rs +++ b/crates/ide-completion/src/context.rs @@ -549,7 +549,7 @@ fn is_doc_hidden(&self, attrs: &hir::Attrs, defining_crate: hir::Crate) -> bool fn doc_aliases(&self, scope_def: ScopeDef) -> Vec { if let Some(attrs) = scope_def.attrs(self.db) { - attrs.doc_aliases() + attrs.doc_aliases().collect() } else { vec![] } diff --git a/crates/ide-completion/src/item.rs b/crates/ide-completion/src/item.rs index a3b46ef2bbc..c2c4a663c61 100644 --- a/crates/ide-completion/src/item.rs +++ b/crates/ide-completion/src/item.rs @@ -45,7 +45,7 @@ pub struct CompletionItem { /// /// That is, in `foo.bar$0` lookup of `abracadabra` will be accepted (it /// contains `bar` sub sequence), and `quux` will rejected. - pub lookup: Option, + pub lookup: SmolStr, /// Additional info to show in the UI pop up. pub detail: Option, @@ -359,7 +359,7 @@ pub(crate) fn new( /// What string is used for filtering. pub fn lookup(&self) -> &str { - self.lookup.as_deref().unwrap_or(&self.label) + self.lookup.as_str() } pub fn ref_match(&self) -> Option<(String, text_edit::Indel, CompletionRelevance)> { @@ -415,19 +415,20 @@ pub(crate) fn build(self) -> CompletionItem { let _p = profile::span("item::Builder::build"); let mut label = self.label; - let mut lookup = self.lookup; + let mut lookup = self.lookup.unwrap_or_else(|| label.clone()); let insert_text = self.insert_text.unwrap_or_else(|| label.to_string()); + if let Some(doc_aliases) = self.doc_aliases { + label = SmolStr::from(format!("{label} (alias {doc_aliases})")); + lookup = SmolStr::from(format!("{lookup} {doc_aliases}")); + } if let [import_edit] = &*self.imports_to_add { // snippets can have multiple imports, but normal completions only have up to one if let Some(original_path) = import_edit.original_path.as_ref() { - lookup = lookup.or_else(|| Some(label.clone())); label = SmolStr::from(format!("{label} (use {original_path})")); } } else if let Some(trait_name) = self.trait_name { label = SmolStr::from(format!("{label} (as {trait_name})")); - } else if let Some(doc_aliases) = self.doc_aliases { - label = SmolStr::from(format!("{label} (alias {doc_aliases})")); } let text_edit = match self.text_edit {