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

@ -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: Sub>() { 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<T> Sub for Wrap<T> {
}
"#,
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)
"#]],
);
}

View file

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

View file

@ -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);

View file

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