From 6f51f728a114078a0c3a029fc66cfb8c4daf9a28 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 1 Dec 2020 13:53:12 +0300 Subject: Type-safer API for dealing with parameter lists with optional self --- crates/completion/src/render/function.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) (limited to 'crates/completion/src/render') diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 542383d7e..09d9f85bc 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -22,7 +22,7 @@ pub(crate) fn render_fn<'a>( struct FunctionRender<'a> { ctx: RenderContext<'a>, name: String, - fn_: hir::Function, + func: hir::Function, ast_node: Fn, } @@ -35,15 +35,15 @@ impl<'a> FunctionRender<'a> { let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string()); let ast_node = fn_.source(ctx.db()).value; - FunctionRender { ctx, name, fn_, ast_node } + FunctionRender { ctx, name, func: fn_, ast_node } } fn render(self, import_to_add: Option) -> CompletionItem { let params = self.params(); CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone()) .kind(self.kind()) - .set_documentation(self.ctx.docs(self.fn_)) - .set_deprecated(self.ctx.is_deprecated(self.fn_)) + .set_documentation(self.ctx.docs(self.func)) + .set_deprecated(self.ctx.is_deprecated(self.func)) .detail(self.detail()) .add_call_parens(self.ctx.completion, self.name, params) .add_import(import_to_add) @@ -67,7 +67,11 @@ impl<'a> FunctionRender<'a> { } fn params(&self) -> Params { - let params_ty = self.fn_.params(self.ctx.db()); + let params_ty = if self.ctx.completion.dot_receiver.is_some() { + self.func.method_params(self.ctx.db()).unwrap_or_default() + } else { + self.func.assoc_fn_params(self.ctx.db()) + }; let params = self .ast_node .param_list() @@ -87,7 +91,7 @@ impl<'a> FunctionRender<'a> { } fn kind(&self) -> CompletionItemKind { - if self.fn_.self_param(self.ctx.db()).is_some() { + if self.func.self_param(self.ctx.db()).is_some() { CompletionItemKind::Method } else { CompletionItemKind::Function -- cgit v1.2.3 From 02955661a0e8b39fd0b887b245f0e1284ea8f504 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Nov 2020 13:45:32 +0300 Subject: Fix typo --- crates/completion/src/render/builder_ext.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'crates/completion/src/render') diff --git a/crates/completion/src/render/builder_ext.rs b/crates/completion/src/render/builder_ext.rs index 37b0d0459..79599de4b 100644 --- a/crates/completion/src/render/builder_ext.rs +++ b/crates/completion/src/render/builder_ext.rs @@ -24,7 +24,7 @@ impl Params { } impl Builder { - pub(super) fn should_add_parems(&self, ctx: &CompletionContext) -> bool { + fn should_add_parens(&self, ctx: &CompletionContext) -> bool { if !ctx.config.add_call_parenthesis { return false; } @@ -58,7 +58,7 @@ impl Builder { name: String, params: Params, ) -> Builder { - if !self.should_add_parems(ctx) { + if !self.should_add_parens(ctx) { return self; } -- cgit v1.2.3 From 9d94ffad44f2b250e498f162cd498aed62877c8e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 1 Dec 2020 14:23:00 +0300 Subject: Place cursor correctly when completing assoc fns with self --- crates/completion/src/render/builder_ext.rs | 1 + crates/completion/src/render/function.rs | 53 +++++++++++++++++++++++------ 2 files changed, 43 insertions(+), 11 deletions(-) (limited to 'crates/completion/src/render') diff --git a/crates/completion/src/render/builder_ext.rs b/crates/completion/src/render/builder_ext.rs index 79599de4b..ce8718bd5 100644 --- a/crates/completion/src/render/builder_ext.rs +++ b/crates/completion/src/render/builder_ext.rs @@ -5,6 +5,7 @@ use test_utils::mark; use crate::{item::Builder, CompletionContext}; +#[derive(Debug)] pub(super) enum Params { Named(Vec), Anonymous(usize), diff --git a/crates/completion/src/render/function.rs b/crates/completion/src/render/function.rs index 09d9f85bc..00e3eb203 100644 --- a/crates/completion/src/render/function.rs +++ b/crates/completion/src/render/function.rs @@ -2,6 +2,7 @@ use hir::{HasSource, Type}; use syntax::{ast::Fn, display::function_declaration}; +use test_utils::mark; use crate::{ item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd}, @@ -67,24 +68,32 @@ impl<'a> FunctionRender<'a> { } fn params(&self) -> Params { + let ast_params = match self.ast_node.param_list() { + Some(it) => it, + None => return Params::Named(Vec::new()), + }; + + let mut params_pats = Vec::new(); let params_ty = if self.ctx.completion.dot_receiver.is_some() { self.func.method_params(self.ctx.db()).unwrap_or_default() } else { + if let Some(s) = ast_params.self_param() { + mark::hit!(parens_for_method_call_as_assoc_fn); + params_pats.push(Some(s.to_string())); + } self.func.assoc_fn_params(self.ctx.db()) }; - let params = self - .ast_node - .param_list() + params_pats + .extend(ast_params.params().into_iter().map(|it| it.pat().map(|it| it.to_string()))); + + let params = params_pats .into_iter() - .flat_map(|it| it.params()) .zip(params_ty) - .flat_map(|(it, param_ty)| { - if let Some(pat) = it.pat() { - let name = pat.to_string(); - let arg = name.trim_start_matches("mut ").trim_start_matches('_'); - return Some(self.add_arg(arg, param_ty.ty())); - } - None + .flat_map(|(pat, param_ty)| { + let pat = pat?; + let name = pat.to_string(); + let arg = name.trim_start_matches("mut ").trim_start_matches('_'); + Some(self.add_arg(arg, param_ty.ty())) }) .collect(); Params::Named(params) @@ -176,6 +185,28 @@ fn bar(s: &S) { ); } + #[test] + fn parens_for_method_call_as_assoc_fn() { + mark::check!(parens_for_method_call_as_assoc_fn); + check_edit( + "foo", + r#" +struct S; +impl S { + fn foo(&self) {} +} +fn main() { S::f<|> } +"#, + r#" +struct S; +impl S { + fn foo(&self) {} +} +fn main() { S::foo(${1:&self})$0 } +"#, + ); + } + #[test] fn suppress_arg_snippets() { mark::check!(suppress_arg_snippets); -- cgit v1.2.3