From 3ee7a95315e510e719dae01bdc0b4cb47561938f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 30 Dec 2018 16:20:17 +0300 Subject: use completion context when creating completion --- crates/ra_analysis/src/completion/complete_path.rs | 2 +- crates/ra_analysis/src/completion/complete_scope.rs | 2 +- crates/ra_analysis/src/completion/completion_item.rs | 12 ++++++------ 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/complete_path.rs b/crates/ra_analysis/src/completion/complete_path.rs index c73a083a4..99fe9aa94 100644 --- a/crates/ra_analysis/src/completion/complete_path.rs +++ b/crates/ra_analysis/src/completion/complete_path.rs @@ -17,7 +17,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> C let module_scope = module.scope(ctx.db)?; module_scope.entries().for_each(|(name, res)| { CompletionItem::new(CompletionKind::Reference, name.to_string()) - .from_resolution(ctx.db, res) + .from_resolution(ctx, res) .add_to(acc) }); } diff --git a/crates/ra_analysis/src/completion/complete_scope.rs b/crates/ra_analysis/src/completion/complete_scope.rs index cd98efe95..daf666505 100644 --- a/crates/ra_analysis/src/completion/complete_scope.rs +++ b/crates/ra_analysis/src/completion/complete_scope.rs @@ -34,7 +34,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) -> }) .for_each(|(name, res)| { CompletionItem::new(CompletionKind::Reference, name.to_string()) - .from_resolution(ctx.db, res) + .from_resolution(ctx, res) .add_to(acc) }); Ok(()) diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index b8fa39ae3..a3d947e5b 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -1,7 +1,7 @@ -use crate::db; - use hir::PerNs; +use crate::completion::CompletionContext; + /// `CompletionItem` describes a single completion variant in the editor pop-up. /// It is basically a POD with various properties. To construct a /// `CompletionItem`, use `new` method and the `Builder` struct. @@ -118,12 +118,12 @@ impl Builder { self.kind = Some(kind); self } - pub(crate) fn from_resolution( + pub(super) fn from_resolution( mut self, - db: &db::RootDatabase, + ctx: &CompletionContext, resolution: &hir::Resolution, ) -> Builder { - let resolved = resolution.def_id.and_then(|d| d.resolve(db).ok()); + let resolved = resolution.def_id.and_then(|d| d.resolve(ctx.db).ok()); let kind = match resolved { PerNs { types: Some(hir::Def::Module(..)), @@ -141,7 +141,7 @@ impl Builder { values: Some(hir::Def::Function(function)), .. } => { - if let Some(sig_info) = function.signature_info(db) { + if let Some(sig_info) = function.signature_info(ctx.db) { if sig_info.params.is_empty() { self.snippet = Some(format!("{}()$0", self.label)); } else { -- cgit v1.2.3 From c182aab5460fb5dc14b8c0e47fae45a09ccf0ac8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 30 Dec 2018 16:29:10 +0300 Subject: don't add () in use items --- crates/ra_analysis/src/completion/complete_path.rs | 12 ++++++++++ .../src/completion/completion_context.rs | 4 ++++ .../ra_analysis/src/completion/completion_item.rs | 26 +++++++++++++--------- 3 files changed, 32 insertions(+), 10 deletions(-) (limited to 'crates') diff --git a/crates/ra_analysis/src/completion/complete_path.rs b/crates/ra_analysis/src/completion/complete_path.rs index 99fe9aa94..4723a65a6 100644 --- a/crates/ra_analysis/src/completion/complete_path.rs +++ b/crates/ra_analysis/src/completion/complete_path.rs @@ -113,4 +113,16 @@ mod tests { "Foo;Bar", ); } + + #[test] + fn dont_render_function_parens_in_use_item() { + check_reference_completion( + " + //- /lib.rs + mod m { pub fn foo() {} } + use crate::m::f<|>; + ", + "foo", + ) + } } diff --git a/crates/ra_analysis/src/completion/completion_context.rs b/crates/ra_analysis/src/completion/completion_context.rs index 71bf7fd32..949b8135e 100644 --- a/crates/ra_analysis/src/completion/completion_context.rs +++ b/crates/ra_analysis/src/completion/completion_context.rs @@ -24,6 +24,7 @@ pub(super) struct CompletionContext<'a> { pub(super) module: Option, pub(super) function: Option, pub(super) function_syntax: Option>, + pub(super) use_item_syntax: Option>, pub(super) is_param: bool, /// A single-indent path, like `foo`. pub(super) is_trivial_path: bool, @@ -55,6 +56,7 @@ impl<'a> CompletionContext<'a> { module, function: None, function_syntax: None, + use_item_syntax: None, is_param: false, is_trivial_path: false, path_prefix: None, @@ -114,6 +116,8 @@ impl<'a> CompletionContext<'a> { _ => (), } + self.use_item_syntax = self.leaf.ancestors().find_map(ast::UseItem::cast); + self.function_syntax = self .leaf .ancestors() diff --git a/crates/ra_analysis/src/completion/completion_item.rs b/crates/ra_analysis/src/completion/completion_item.rs index a3d947e5b..cd4d529f9 100644 --- a/crates/ra_analysis/src/completion/completion_item.rs +++ b/crates/ra_analysis/src/completion/completion_item.rs @@ -140,21 +140,27 @@ impl Builder { PerNs { values: Some(hir::Def::Function(function)), .. - } => { - if let Some(sig_info) = function.signature_info(ctx.db) { - if sig_info.params.is_empty() { - self.snippet = Some(format!("{}()$0", self.label)); - } else { - self.snippet = Some(format!("{}($0)", self.label)); - } - } - CompletionItemKind::Function - } + } => return self.from_function(ctx, function), _ => return self, }; self.kind = Some(kind); self } + + fn from_function(mut self, ctx: &CompletionContext, function: hir::Function) -> Builder { + // If not an import, add parenthesis automatically. + if ctx.use_item_syntax.is_none() { + if let Some(sig_info) = function.signature_info(ctx.db) { + if sig_info.params.is_empty() { + self.snippet = Some(format!("{}()$0", self.label)); + } else { + self.snippet = Some(format!("{}($0)", self.label)); + } + } + } + self.kind = Some(CompletionItemKind::Function); + self + } } impl Into for Builder { -- cgit v1.2.3