From 74406ca8ea45df8b44cb38ecba4a5b561038c4a0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 21 Dec 2018 14:02:14 +0300 Subject: introduce completion_item module --- crates/ra_analysis/src/completion.rs | 23 ++++------- .../ra_analysis/src/completion/completion_item.rs | 44 ++++++++++++++++++++++ .../src/completion/reference_completion.rs | 8 ++-- 3 files changed, 54 insertions(+), 21 deletions(-) create mode 100644 crates/ra_analysis/src/completion/completion_item.rs (limited to 'crates') diff --git a/crates/ra_analysis/src/completion.rs b/crates/ra_analysis/src/completion.rs index f480af611..ed1b6dd0c 100644 --- a/crates/ra_analysis/src/completion.rs +++ b/crates/ra_analysis/src/completion.rs @@ -1,3 +1,4 @@ +mod completion_item; mod reference_completion; use ra_editor::find_node_at_offset; @@ -17,15 +18,7 @@ use crate::{ Cancelable, FilePosition }; -#[derive(Debug)] -pub struct CompletionItem { - /// What user sees in pop-up - pub label: String, - /// What string is used for filtering, defaults to label - pub lookup: Option, - /// What is inserted, defaults to label - pub snippet: Option, -} +pub use crate::completion::completion_item::CompletionItem; pub(crate) fn completions( db: &db::RootDatabase, @@ -63,6 +56,10 @@ pub(crate) fn completions( Ok(res) } +/// Complete repeated parametes, both name and type. For example, if all +/// functions in a file have a `spam: &mut Spam` parameter, a completion with +/// `spam: &mut Spam` insert text/label and `spam` lookup string will be +/// suggested. fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec) { let mut params = FxHashMap::default(); for node in ctx.ancestors() { @@ -81,13 +78,7 @@ fn param_completions(ctx: SyntaxNodeRef, acc: &mut Vec) { Some((label, lookup)) } }) - .for_each(|(label, lookup)| { - acc.push(CompletionItem { - label, - lookup: Some(lookup), - snippet: None, - }) - }); + .for_each(|(label, lookup)| CompletionItem::new(label).lookup_by(lookup).add_to(acc)); fn process<'a, N: ast::FnDefOwner<'a>>( node: N, diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs new file mode 100644 index 000000000..309b0108d --- /dev/null +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -0,0 +1,44 @@ +#[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, +} + +impl CompletionItem { + pub(crate) fn new(label: impl Into) -> Builder { + let label = label.into(); + Builder { + label, + lookup: None, + snippet: None, + } + } +} + +pub(crate) struct Builder { + label: String, + lookup: Option, + snippet: Option, +} + +impl Builder { + pub fn add_to(self, acc: &mut Vec) { + acc.push(self.build()) + } + + pub fn build(self) -> CompletionItem { + CompletionItem { + label: self.label, + lookup: self.lookup, + snippet: self.snippet, + } + } + pub fn lookup_by(mut self, lookup: impl Into) -> Builder { + self.lookup = Some(lookup.into()); + self + } +} diff --git a/crates/ra_analysis/src/completion/reference_completion.rs b/crates/ra_analysis/src/completion/reference_completion.rs index f483ed045..457ca13cc 100644 --- a/crates/ra_analysis/src/completion/reference_completion.rs +++ b/crates/ra_analysis/src/completion/reference_completion.rs @@ -6,11 +6,9 @@ use ra_syntax::{ ast::{self, LoopBodyOwner}, SyntaxKind::*, }; -use hir::{ - self, - FnScopes, - Def, - Path, +use hir::{ + self, + FnScopes, Def, Path }; use crate::{ -- cgit v1.2.3