From 4092b8d0b58598d0b4b820fff37b1d8c741c47b9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 21 Dec 2018 15:19:46 +0300 Subject: make compleion item details private --- .../ra_analysis/src/completion/completion_item.rs | 34 ++++++++++++++++++---- .../src/completion/reference_completion.rs | 30 ++++++++----------- 2 files changed, 40 insertions(+), 24 deletions(-) (limited to 'crates/ra_analysis/src/completion') diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index 7edb86436..4280976e7 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -1,11 +1,13 @@ #[derive(Debug)] pub struct CompletionItem { - /// What user sees in pop-up in the UI. - pub label: String, - /// What string is used for filtering, defaults to label. - pub lookup: Option, - /// What is inserted, defaults to label. - pub snippet: Option, + label: String, + lookup: Option, + snippet: Option, +} + +pub enum InsertText { + PlainText { text: String }, + Snippet { text: String }, } impl CompletionItem { @@ -17,6 +19,26 @@ impl CompletionItem { snippet: None, } } + /// What user sees in pop-up in the UI. + pub fn label(&self) -> &str { + &self.label + } + /// What string is used for filtering. + pub fn lookup(&self) -> &str { + self.lookup + .as_ref() + .map(|it| it.as_str()) + .unwrap_or(self.label()) + } + /// What is inserted. + pub fn insert_text(&self) -> InsertText { + match &self.snippet { + None => InsertText::PlainText { + text: self.label.clone(), + }, + Some(it) => InsertText::Snippet { text: it.clone() }, + } + } } #[must_use] diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index 23052295c..f9f01a642 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs @@ -39,25 +39,19 @@ pub(super) fn completions( } let module_scope = module.scope(db)?; - acc.extend( - module_scope - .entries() - .filter(|(_name, res)| { - // Don't expose this item - match res.import { - None => true, - Some(import) => { - let range = import.range(db, module.source().file_id()); - !range.is_subrange(&name_ref.syntax().range()) - } + module_scope + .entries() + .filter(|(_name, res)| { + // Don't expose this item + match res.import { + None => true, + Some(import) => { + let range = import.range(db, module.source().file_id()); + !range.is_subrange(&name_ref.syntax().range()) } - }) - .map(|(name, _res)| CompletionItem { - label: name.to_string(), - lookup: None, - snippet: None, - }), - ); + } + }) + .for_each(|(name, _res)| CompletionItem::new(name.to_string()).add_to(acc)); } NameRefKind::Path(path) => complete_path(acc, db, module, path)?, NameRefKind::BareIdentInMod => { -- cgit v1.2.3