Place cursor correctly when completing assoc fns with self

This commit is contained in:
Aleksey Kladov 2020-12-01 14:23:00 +03:00
parent 02955661a0
commit 9d94ffad44
4 changed files with 64 additions and 31 deletions

View File

@ -356,7 +356,7 @@ fn foo() { let _ = S::<|> }
ct C const C: i32 = 42; ct C const C: i32 = 42;
ta T type T = i32; ta T type T = i32;
fn a() fn a() fn a() fn a()
me b() fn b(&self) me b() fn b(&self)
"#]], "#]],
); );
} }
@ -508,9 +508,9 @@ fn foo<T: Sub>() { T::<|> }
ta SubTy type SubTy; ta SubTy type SubTy;
ta Ty type Ty; ta Ty type Ty;
fn func() fn func() fn func() fn func()
me method() fn method(&self) me method() fn method(&self)
fn subfunc() fn subfunc() fn subfunc() fn subfunc()
me submethod() fn submethod(&self) me submethod() fn submethod(&self)
"#]], "#]],
); );
} }
@ -548,9 +548,9 @@ impl<T> Sub for Wrap<T> {
ta SubTy type SubTy; ta SubTy type SubTy;
ta Ty type Ty; ta Ty type Ty;
fn func() fn func() fn func() fn func()
me method() fn method(&self) me method() fn method(&self)
fn subfunc() fn subfunc() fn subfunc() fn subfunc()
me submethod() fn submethod(&self) me submethod() fn submethod(&self)
"#]], "#]],
); );
} }

View File

@ -5,6 +5,7 @@ use test_utils::mark;
use crate::{item::Builder, CompletionContext}; use crate::{item::Builder, CompletionContext};
#[derive(Debug)]
pub(super) enum Params { pub(super) enum Params {
Named(Vec<String>), Named(Vec<String>),
Anonymous(usize), Anonymous(usize),

View File

@ -2,6 +2,7 @@
use hir::{HasSource, Type}; use hir::{HasSource, Type};
use syntax::{ast::Fn, display::function_declaration}; use syntax::{ast::Fn, display::function_declaration};
use test_utils::mark;
use crate::{ use crate::{
item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd}, item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd},
@ -67,24 +68,32 @@ impl<'a> FunctionRender<'a> {
} }
fn params(&self) -> Params { 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() { let params_ty = if self.ctx.completion.dot_receiver.is_some() {
self.func.method_params(self.ctx.db()).unwrap_or_default() self.func.method_params(self.ctx.db()).unwrap_or_default()
} else { } 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()) self.func.assoc_fn_params(self.ctx.db())
}; };
let params = self params_pats
.ast_node .extend(ast_params.params().into_iter().map(|it| it.pat().map(|it| it.to_string())));
.param_list()
let params = params_pats
.into_iter() .into_iter()
.flat_map(|it| it.params())
.zip(params_ty) .zip(params_ty)
.flat_map(|(it, param_ty)| { .flat_map(|(pat, param_ty)| {
if let Some(pat) = it.pat() { let pat = pat?;
let name = pat.to_string(); let name = pat.to_string();
let arg = name.trim_start_matches("mut ").trim_start_matches('_'); let arg = name.trim_start_matches("mut ").trim_start_matches('_');
return Some(self.add_arg(arg, param_ty.ty())); Some(self.add_arg(arg, param_ty.ty()))
}
None
}) })
.collect(); .collect();
Params::Named(params) 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] #[test]
fn suppress_arg_snippets() { fn suppress_arg_snippets() {
mark::check!(suppress_arg_snippets); mark::check!(suppress_arg_snippets);

View File

@ -806,6 +806,7 @@ impl From<Mutability> for Access {
} }
} }
#[derive(Debug)]
pub struct Param { pub struct Param {
ty: Type, ty: Type,
} }