Skip to content

Commit 9d94ffa

Browse files
committed
Place cursor correctly when completing assoc fns with self
1 parent 0295566 commit 9d94ffa

File tree

4 files changed

+64
-31
lines changed

4 files changed

+64
-31
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/render/builder_ext.rs

Lines changed: 1 addition & 0 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),

crates/completion/src/render/function.rs

Lines changed: 42 additions & 11 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},
@@ -67,24 +68,32 @@ impl<'a> FunctionRender<'a> {
6768
}
6869

6970
fn params(&self) -> Params {
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();
7077
let params_ty = if self.ctx.completion.dot_receiver.is_some() {
7178
self.func.method_params(self.ctx.db()).unwrap_or_default()
7279
} 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+
}
7384
self.func.assoc_fn_params(self.ctx.db())
7485
};
75-
let params = self
76-
.ast_node
77-
.param_list()
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
7890
.into_iter()
79-
.flat_map(|it| it.params())
8091
.zip(params_ty)
81-
.flat_map(|(it, param_ty)| {
82-
if let Some(pat) = it.pat() {
83-
let name = pat.to_string();
84-
let arg = name.trim_start_matches("mut ").trim_start_matches('_');
85-
return Some(self.add_arg(arg, param_ty.ty()));
86-
}
87-
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()))
8897
})
8998
.collect();
9099
Params::Named(params)
@@ -176,6 +185,28 @@ fn bar(s: &S) {
176185
);
177186
}
178187

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+
179210
#[test]
180211
fn suppress_arg_snippets() {
181212
mark::check!(suppress_arg_snippets);

crates/hir/src/code_model.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,7 @@ impl From<Mutability> for Access {
806806
}
807807
}
808808

809+
#[derive(Debug)]
809810
pub struct Param {
810811
ty: Type,
811812
}

0 commit comments

Comments
 (0)