Skip to content

Commit 75e037f

Browse files
bors[bot]matklad
andauthored
Merge #6688
6688: Place cursor correctly when completing assoc fns with self r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 455a0cf + 9d94ffa commit 75e037f

File tree

6 files changed

+87
-43
lines changed

6 files changed

+87
-43
lines changed

crates/completion/src/completions/qualified_path.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,10 @@ impl S {
353353
fn foo() { let _ = S::<|> }
354354
"#,
355355
expect![[r#"
356-
ct C const C: i32 = 42;
357-
ta T type T = i32;
358-
fn a() fn a()
359-
me b() fn b(&self)
356+
ct C const C: i32 = 42;
357+
ta T type T = i32;
358+
fn a() fn a()
359+
me b() fn b(&self)
360360
"#]],
361361
);
362362
}
@@ -503,14 +503,14 @@ trait Sub: Super {
503503
fn foo<T: Sub>() { T::<|> }
504504
"#,
505505
expect![[r#"
506-
ct C2 const C2: ();
507-
ct CONST const CONST: u8;
508-
ta SubTy type SubTy;
509-
ta Ty type Ty;
510-
fn func() fn func()
511-
me method() fn method(&self)
512-
fn subfunc() fn subfunc()
513-
me submethod() fn submethod(&self)
506+
ct C2 const C2: ();
507+
ct CONST const CONST: u8;
508+
ta SubTy type SubTy;
509+
ta Ty type Ty;
510+
fn func() fn func()
511+
me method() fn method(&self)
512+
fn subfunc() fn subfunc()
513+
me submethod() fn submethod(&self)
514514
"#]],
515515
);
516516
}
@@ -543,14 +543,14 @@ impl<T> Sub for Wrap<T> {
543543
}
544544
"#,
545545
expect![[r#"
546-
ct C2 const C2: () = ();
547-
ct CONST const CONST: u8 = 0;
548-
ta SubTy type SubTy;
549-
ta Ty type Ty;
550-
fn func() fn func()
551-
me method() fn method(&self)
552-
fn subfunc() fn subfunc()
553-
me submethod() fn submethod(&self)
546+
ct C2 const C2: () = ();
547+
ct CONST const CONST: u8 = 0;
548+
ta SubTy type SubTy;
549+
ta Ty type Ty;
550+
fn func() fn func()
551+
me method() fn method(&self)
552+
fn subfunc() fn subfunc()
553+
me submethod() fn submethod(&self)
554554
"#]],
555555
);
556556
}

crates/completion/src/completions/trait_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ fn add_function_impl(
139139
) {
140140
let fn_name = func.name(ctx.db).to_string();
141141

142-
let label = if func.params(ctx.db).is_empty() {
142+
let label = if func.assoc_fn_params(ctx.db).is_empty() {
143143
format!("fn {}()", fn_name)
144144
} else {
145145
format!("fn {}(..)", fn_name)

crates/completion/src/render/builder_ext.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use test_utils::mark;
55

66
use crate::{item::Builder, CompletionContext};
77

8+
#[derive(Debug)]
89
pub(super) enum Params {
910
Named(Vec<String>),
1011
Anonymous(usize),
@@ -24,7 +25,7 @@ impl Params {
2425
}
2526

2627
impl Builder {
27-
pub(super) fn should_add_parems(&self, ctx: &CompletionContext) -> bool {
28+
fn should_add_parens(&self, ctx: &CompletionContext) -> bool {
2829
if !ctx.config.add_call_parenthesis {
2930
return false;
3031
}
@@ -58,7 +59,7 @@ impl Builder {
5859
name: String,
5960
params: Params,
6061
) -> Builder {
61-
if !self.should_add_parems(ctx) {
62+
if !self.should_add_parens(ctx) {
6263
return self;
6364
}
6465

crates/completion/src/render/function.rs

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use hir::{HasSource, Type};
44
use syntax::{ast::Fn, display::function_declaration};
5+
use test_utils::mark;
56

67
use crate::{
78
item::{CompletionItem, CompletionItemKind, CompletionKind, ImportToAdd},
@@ -22,7 +23,7 @@ pub(crate) fn render_fn<'a>(
2223
struct FunctionRender<'a> {
2324
ctx: RenderContext<'a>,
2425
name: String,
25-
fn_: hir::Function,
26+
func: hir::Function,
2627
ast_node: Fn,
2728
}
2829

@@ -35,15 +36,15 @@ impl<'a> FunctionRender<'a> {
3536
let name = local_name.unwrap_or_else(|| fn_.name(ctx.db()).to_string());
3637
let ast_node = fn_.source(ctx.db()).value;
3738

38-
FunctionRender { ctx, name, fn_, ast_node }
39+
FunctionRender { ctx, name, func: fn_, ast_node }
3940
}
4041

4142
fn render(self, import_to_add: Option<ImportToAdd>) -> CompletionItem {
4243
let params = self.params();
4344
CompletionItem::new(CompletionKind::Reference, self.ctx.source_range(), self.name.clone())
4445
.kind(self.kind())
45-
.set_documentation(self.ctx.docs(self.fn_))
46-
.set_deprecated(self.ctx.is_deprecated(self.fn_))
46+
.set_documentation(self.ctx.docs(self.func))
47+
.set_deprecated(self.ctx.is_deprecated(self.func))
4748
.detail(self.detail())
4849
.add_call_parens(self.ctx.completion, self.name, params)
4950
.add_import(import_to_add)
@@ -67,27 +68,39 @@ impl<'a> FunctionRender<'a> {
6768
}
6869

6970
fn params(&self) -> Params {
70-
let params_ty = self.fn_.params(self.ctx.db());
71-
let params = self
72-
.ast_node
73-
.param_list()
71+
let ast_params = match self.ast_node.param_list() {
72+
Some(it) => it,
73+
None => return Params::Named(Vec::new()),
74+
};
75+
76+
let mut params_pats = Vec::new();
77+
let params_ty = if self.ctx.completion.dot_receiver.is_some() {
78+
self.func.method_params(self.ctx.db()).unwrap_or_default()
79+
} else {
80+
if let Some(s) = ast_params.self_param() {
81+
mark::hit!(parens_for_method_call_as_assoc_fn);
82+
params_pats.push(Some(s.to_string()));
83+
}
84+
self.func.assoc_fn_params(self.ctx.db())
85+
};
86+
params_pats
87+
.extend(ast_params.params().into_iter().map(|it| it.pat().map(|it| it.to_string())));
88+
89+
let params = params_pats
7490
.into_iter()
75-
.flat_map(|it| it.params())
7691
.zip(params_ty)
77-
.flat_map(|(it, param_ty)| {
78-
if let Some(pat) = it.pat() {
79-
let name = pat.to_string();
80-
let arg = name.trim_start_matches("mut ").trim_start_matches('_');
81-
return Some(self.add_arg(arg, param_ty.ty()));
82-
}
83-
None
92+
.flat_map(|(pat, param_ty)| {
93+
let pat = pat?;
94+
let name = pat.to_string();
95+
let arg = name.trim_start_matches("mut ").trim_start_matches('_');
96+
Some(self.add_arg(arg, param_ty.ty()))
8497
})
8598
.collect();
8699
Params::Named(params)
87100
}
88101

89102
fn kind(&self) -> CompletionItemKind {
90-
if self.fn_.self_param(self.ctx.db()).is_some() {
103+
if self.func.self_param(self.ctx.db()).is_some() {
91104
CompletionItemKind::Method
92105
} else {
93106
CompletionItemKind::Function
@@ -172,6 +185,28 @@ fn bar(s: &S) {
172185
);
173186
}
174187

188+
#[test]
189+
fn parens_for_method_call_as_assoc_fn() {
190+
mark::check!(parens_for_method_call_as_assoc_fn);
191+
check_edit(
192+
"foo",
193+
r#"
194+
struct S;
195+
impl S {
196+
fn foo(&self) {}
197+
}
198+
fn main() { S::f<|> }
199+
"#,
200+
r#"
201+
struct S;
202+
impl S {
203+
fn foo(&self) {}
204+
}
205+
fn main() { S::foo(${1:&self})$0 }
206+
"#,
207+
);
208+
}
209+
175210
#[test]
176211
fn suppress_arg_snippets() {
177212
mark::check!(suppress_arg_snippets);

crates/hir/src/code_model.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -744,14 +744,13 @@ impl Function {
744744
Some(SelfParam { func: self.id })
745745
}
746746

747-
pub fn params(self, db: &dyn HirDatabase) -> Vec<Param> {
747+
pub fn assoc_fn_params(self, db: &dyn HirDatabase) -> Vec<Param> {
748748
let resolver = self.id.resolver(db.upcast());
749749
let ctx = hir_ty::TyLoweringContext::new(db, &resolver);
750750
let environment = TraitEnvironment::lower(db, &resolver);
751751
db.function_data(self.id)
752752
.params
753753
.iter()
754-
.skip(if self.self_param(db).is_some() { 1 } else { 0 })
755754
.map(|type_ref| {
756755
let ty = Type {
757756
krate: self.id.lookup(db.upcast()).container.module(db.upcast()).krate,
@@ -764,6 +763,14 @@ impl Function {
764763
})
765764
.collect()
766765
}
766+
pub fn method_params(self, db: &dyn HirDatabase) -> Option<Vec<Param>> {
767+
if self.self_param(db).is_none() {
768+
return None;
769+
}
770+
let mut res = self.assoc_fn_params(db);
771+
res.remove(0);
772+
Some(res)
773+
}
767774

768775
pub fn is_unsafe(self, db: &dyn HirDatabase) -> bool {
769776
db.function_data(self.id).is_unsafe
@@ -799,6 +806,7 @@ impl From<Mutability> for Access {
799806
}
800807
}
801808

809+
#[derive(Debug)]
802810
pub struct Param {
803811
ty: Type,
804812
}

crates/ide/src/references/rename.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ fn rename_to_self(
241241
return Err(RenameError("Method already has a self parameter".to_string()));
242242
}
243243

244-
let params = fn_def.params(sema.db);
244+
let params = fn_def.assoc_fn_params(sema.db);
245245
let first_param =
246246
params.first().ok_or_else(|| RenameError("Method has no parameters".into()))?;
247247
let first_param_ty = first_param.ty();

0 commit comments

Comments
 (0)