From 25dda42f3773b1d002a5809c0182c2adc6c47027 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 22 Dec 2018 01:27:07 +0300 Subject: introduce ComletionItemKind --- .../ra_analysis/src/completion/complete_fn_param.rs | 3 +-- .../ra_analysis/src/completion/complete_keyword.rs | 5 +---- crates/ra_analysis/src/completion/complete_path.rs | 8 +++----- crates/ra_analysis/src/completion/complete_scope.rs | 14 +++----------- .../ra_analysis/src/completion/complete_snippet.rs | 12 ++++-------- crates/ra_analysis/src/completion/completion_item.rs | 20 ++++++++++++++++---- 6 files changed, 28 insertions(+), 34 deletions(-) diff --git a/crates/ra_analysis/src/completion/complete_fn_param.rs b/crates/ra_analysis/src/completion/complete_fn_param.rs index 6a6213e67..bb5fdfda0 100644 --- a/crates/ra_analysis/src/completion/complete_fn_param.rs +++ b/crates/ra_analysis/src/completion/complete_fn_param.rs @@ -34,9 +34,8 @@ pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) } }) .for_each(|(label, lookup)| { - CompletionItem::new(label) + CompletionItem::new(CompletionKind::Magic, label) .lookup_by(lookup) - .kind(CompletionKind::Magic) .add_to(acc) }); diff --git a/crates/ra_analysis/src/completion/complete_keyword.rs b/crates/ra_analysis/src/completion/complete_keyword.rs index dead15bb6..1e6d7008d 100644 --- a/crates/ra_analysis/src/completion/complete_keyword.rs +++ b/crates/ra_analysis/src/completion/complete_keyword.rs @@ -61,10 +61,7 @@ fn complete_return(fn_def: ast::FnDef, is_stmt: bool) -> Option } fn keyword(kw: &str, snippet: &str) -> CompletionItem { - CompletionItem::new(kw) - .kind(Keyword) - .snippet(snippet) - .build() + CompletionItem::new(Keyword, kw).snippet(snippet).build() } #[cfg(test)] diff --git a/crates/ra_analysis/src/completion/complete_path.rs b/crates/ra_analysis/src/completion/complete_path.rs index 5fc24af72..692502bb2 100644 --- a/crates/ra_analysis/src/completion/complete_path.rs +++ b/crates/ra_analysis/src/completion/complete_path.rs @@ -17,11 +17,9 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C _ => return Ok(()), }; let module_scope = target_module.scope(ctx.db)?; - module_scope.entries().for_each(|(name, _res)| { - CompletionItem::new(name.to_string()) - .kind(Reference) - .add_to(acc) - }); + module_scope + .entries() + .for_each(|(name, _res)| CompletionItem::new(Reference, name.to_string()).add_to(acc)); Ok(()) } diff --git a/crates/ra_analysis/src/completion/complete_scope.rs b/crates/ra_analysis/src/completion/complete_scope.rs index d07c0e46d..f39b98d62 100644 --- a/crates/ra_analysis/src/completion/complete_scope.rs +++ b/crates/ra_analysis/src/completion/complete_scope.rs @@ -29,11 +29,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> } } }) - .for_each(|(name, _res)| { - CompletionItem::new(name.to_string()) - .kind(Reference) - .add_to(acc) - }); + .for_each(|(name, _res)| CompletionItem::new(Reference, name.to_string()).add_to(acc)); } Ok(()) @@ -45,13 +41,9 @@ fn complete_fn(acc: &mut Completions, scopes: &hir::FnScopes, offset: TextUnit) .scope_chain_for_offset(offset) .flat_map(|scope| scopes.entries(scope).iter()) .filter(|entry| shadowed.insert(entry.name())) - .for_each(|entry| { - CompletionItem::new(entry.name().to_string()) - .kind(Reference) - .add_to(acc) - }); + .for_each(|entry| CompletionItem::new(Reference, entry.name().to_string()).add_to(acc)); if scopes.self_param.is_some() { - CompletionItem::new("self").kind(Reference).add_to(acc); + CompletionItem::new(Reference, "self").add_to(acc); } } diff --git a/crates/ra_analysis/src/completion/complete_snippet.rs b/crates/ra_analysis/src/completion/complete_snippet.rs index ccd68832b..f0ad45fec 100644 --- a/crates/ra_analysis/src/completion/complete_snippet.rs +++ b/crates/ra_analysis/src/completion/complete_snippet.rs @@ -4,13 +4,11 @@ pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte if !(ctx.is_trivial_path && ctx.enclosing_fn.is_some()) { return; } - CompletionItem::new("pd") + CompletionItem::new(Snippet, "pd") .snippet("eprintln!(\"$0 = {:?}\", $0);") - .kind(Snippet) .add_to(acc); - CompletionItem::new("ppd") + CompletionItem::new(Snippet, "ppd") .snippet("eprintln!(\"$0 = {:#?}\", $0);") - .kind(Snippet) .add_to(acc); } @@ -18,7 +16,7 @@ pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionConte if !ctx.is_new_item { return; } - CompletionItem::new("Test function") + CompletionItem::new(Snippet, "Test function") .lookup_by("tfn") .snippet( "\ @@ -27,11 +25,9 @@ fn ${1:feature}() { $0 }", ) - .kind(Snippet) .add_to(acc); - CompletionItem::new("pub(crate)") + CompletionItem::new(Snippet, "pub(crate)") .snippet("pub(crate) $0") - .kind(Snippet) .add_to(acc); } diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index 30581c8a5..423d5a48b 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -6,7 +6,9 @@ pub struct CompletionItem { label: String, lookup: Option, snippet: Option, - /// Used only internally in test, to check only specific kind of completion. + kind: Option, + /// Used only internally in tests, to check only specific kind of + /// completion. completion_kind: CompletionKind, } @@ -15,6 +17,12 @@ pub enum InsertText { Snippet { text: String }, } +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum CompletionItemKind { + Snippet, + Keyword, +} + #[derive(Debug, PartialEq, Eq)] pub(crate) enum CompletionKind { /// Parser-based keyword completion. @@ -24,17 +32,16 @@ pub(crate) enum CompletionKind { /// "Secret sauce" completions. Magic, Snippet, - Unspecified, } impl CompletionItem { - pub(crate) fn new(label: impl Into) -> Builder { + pub(crate) fn new(completion_kind: CompletionKind, label: impl Into) -> Builder { let label = label.into(); Builder { label, lookup: None, snippet: None, - completion_kind: CompletionKind::Unspecified, + completion_kind, } } /// What user sees in pop-up in the UI. @@ -57,6 +64,10 @@ impl CompletionItem { Some(it) => InsertText::Snippet { text: it.clone() }, } } + + pub fn kind(&self) -> Option { + self.kind + } } /// A helper to make `CompletionItem`s. @@ -78,6 +89,7 @@ impl Builder { label: self.label, lookup: self.lookup, snippet: self.snippet, + kind: None, completion_kind: self.completion_kind, } } -- cgit v1.2.3