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 --- .../completion/src/completions/qualified_path.rs | 40 ++++++++-------- crates/completion/src/render/builder_ext.rs | 1 + crates/completion/src/render/function.rs | 53 +++++++++++++++++----- 3 files changed, 63 insertions(+), 31 deletions(-) (limited to 'crates/completion') diff --git a/crates/completion/src/completions/qualified_path.rs b/crates/completion/src/completions/qualified_path.rs index d9387054d..bc23bea3f 100644 --- a/crates/completion/src/completions/qualified_path.rs +++ b/crates/completion/src/completions/qualified_path.rs @@ -353,10 +353,10 @@ impl S { fn foo() { let _ = S::<|> } "#, expect![[r#" - ct C const C: i32 = 42; - ta T type T = i32; - fn a() fn a() - me b() fn b(&self) + ct C const C: i32 = 42; + ta T type T = i32; + fn a() fn a() + me b(…) fn b(&self) "#]], ); } @@ -503,14 +503,14 @@ trait Sub: Super { fn foo() { T::<|> } "#, expect![[r#" - ct C2 const C2: (); - ct CONST const CONST: u8; - ta SubTy type SubTy; - ta Ty type Ty; - fn func() fn func() - me method() fn method(&self) - fn subfunc() fn subfunc() - me submethod() fn submethod(&self) + ct C2 const C2: (); + ct CONST const CONST: u8; + ta SubTy type SubTy; + ta Ty type Ty; + fn func() fn func() + me method(…) fn method(&self) + fn subfunc() fn subfunc() + me submethod(…) fn submethod(&self) "#]], ); } @@ -543,14 +543,14 @@ impl Sub for Wrap { } "#, expect![[r#" - ct C2 const C2: () = (); - ct CONST const CONST: u8 = 0; - ta SubTy type SubTy; - ta Ty type Ty; - fn func() fn func() - me method() fn method(&self) - fn subfunc() fn subfunc() - me submethod() fn submethod(&self) + ct C2 const C2: () = (); + ct CONST const CONST: u8 = 0; + ta SubTy type SubTy; + ta Ty type Ty; + fn func() fn func() + me method(…) fn method(&self) + fn subfunc() fn subfunc() + me submethod(…) fn submethod(&self) "#]], ); } 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