Skip to content

Commit 24f9209

Browse files
committed
Auto merge of rust-lang#12040 - rainy-me:improve-parameter-completion, r=jonas-schievink
fix: Improve parameter completion fix rust-lang/rust-analyzer#12016 and handles some extra cases.
2 parents 71d49d3 + 8f8f20f commit 24f9209

File tree

3 files changed

+113
-11
lines changed

3 files changed

+113
-11
lines changed

crates/ide_completion/src/completions/fn_param.rs

+16-11
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_hash::FxHashMap;
55
use syntax::{
66
algo,
77
ast::{self, HasModuleItem},
8-
match_ast, AstNode, Direction, SyntaxKind,
8+
match_ast, AstNode, Direction, SyntaxKind, TextRange,
99
};
1010

1111
use crate::{
@@ -27,14 +27,13 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
2727

2828
let comma_wrapper = comma_wrapper(ctx);
2929
let mut add_new_item_to_acc = |label: &str, lookup: String| {
30-
let mk_item = |label: &str| {
31-
CompletionItem::new(CompletionItemKind::Binding, ctx.source_range(), label)
30+
let mk_item = |label: &str, range: TextRange| {
31+
CompletionItem::new(CompletionItemKind::Binding, range, label)
3232
};
33-
let mut item = match &comma_wrapper {
34-
Some(fmt) => mk_item(&fmt(label)),
35-
None => mk_item(label),
33+
let item = match &comma_wrapper {
34+
Some((fmt, range, lookup)) => mk_item(&fmt(label), *range).lookup_by(lookup).to_owned(),
35+
None => mk_item(label, ctx.source_range()).lookup_by(lookup).to_owned(),
3636
};
37-
item.lookup_by(lookup);
3837
item.add_to(acc)
3938
};
4039

@@ -162,14 +161,16 @@ fn should_add_self_completions(ctx: &CompletionContext, param_list: &ast::ParamL
162161
inside_impl && no_params
163162
}
164163

165-
fn comma_wrapper(ctx: &CompletionContext) -> Option<impl Fn(&str) -> String> {
164+
fn comma_wrapper(ctx: &CompletionContext) -> Option<(impl Fn(&str) -> String, TextRange, String)> {
165+
let param = ctx.token.ancestors().find(|node| node.kind() == SyntaxKind::PARAM)?;
166+
166167
let next_token_kind = {
167-
let t = ctx.token.next_token()?;
168+
let t = param.last_token()?.next_token()?;
168169
let t = algo::skip_whitespace_token(t, Direction::Next)?;
169170
t.kind()
170171
};
171172
let prev_token_kind = {
172-
let t = ctx.previous_token.clone()?;
173+
let t = param.first_token()?.prev_token()?;
173174
let t = algo::skip_whitespace_token(t, Direction::Prev)?;
174175
t.kind()
175176
};
@@ -182,5 +183,9 @@ fn comma_wrapper(ctx: &CompletionContext) -> Option<impl Fn(&str) -> String> {
182183
matches!(prev_token_kind, SyntaxKind::COMMA | SyntaxKind::L_PAREN | SyntaxKind::PIPE);
183184
let leading = if has_leading_comma { "" } else { ", " };
184185

185-
Some(move |param: &_| format!("{}{}{}", leading, param, trailing))
186+
Some((
187+
move |label: &_| (format!("{}{}{}", leading, label, trailing)),
188+
param.text_range(),
189+
format!("{}{}", leading, param.text()),
190+
))
186191
}

crates/ide_completion/src/render/function.rs

+84
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,90 @@ fn qux(Foo { bar }: Foo) {
562562
fn main() {
563563
qux(${1:foo})$0
564564
}
565+
"#,
566+
);
567+
}
568+
569+
#[test]
570+
fn complete_fn_param() {
571+
// has mut kw
572+
check_edit(
573+
"mut ba",
574+
r#"
575+
fn f(foo: (), mut bar: u32) {}
576+
fn g(foo: (), mut ba$0)
577+
"#,
578+
r#"
579+
fn f(foo: (), mut bar: u32) {}
580+
fn g(foo: (), mut bar: u32)
581+
"#,
582+
);
583+
584+
// has type param
585+
check_edit(
586+
"mut ba: u32",
587+
r#"
588+
fn g(foo: (), mut ba$0: u32)
589+
fn f(foo: (), mut bar: u32) {}
590+
"#,
591+
r#"
592+
fn g(foo: (), mut bar: u32)
593+
fn f(foo: (), mut bar: u32) {}
594+
"#,
595+
);
596+
}
597+
598+
#[test]
599+
fn complete_fn_mut_param_add_comma() {
600+
// add leading and trailing comma
601+
check_edit(
602+
", mut ba",
603+
r#"
604+
fn f(foo: (), mut bar: u32) {}
605+
fn g(foo: ()mut ba$0 baz: ())
606+
"#,
607+
r#"
608+
fn f(foo: (), mut bar: u32) {}
609+
fn g(foo: (), mut bar: u32, baz: ())
610+
"#,
611+
);
612+
}
613+
614+
#[test]
615+
fn complete_fn_mut_param_has_attribute() {
616+
check_edit(
617+
"mut ba",
618+
r#"
619+
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
620+
fn g(foo: (), mut ba$0)
621+
"#,
622+
r#"
623+
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
624+
fn g(foo: (), #[baz = "qux"] mut bar: u32)
625+
"#,
626+
);
627+
628+
check_edit(
629+
r#"#[baz = "qux"] mut ba"#,
630+
r#"
631+
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
632+
fn g(foo: (), #[baz = "qux"] mut ba$0)
633+
"#,
634+
r#"
635+
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
636+
fn g(foo: (), #[baz = "qux"] mut bar: u32)
637+
"#,
638+
);
639+
640+
check_edit(
641+
r#", #[baz = "qux"] mut ba"#,
642+
r#"
643+
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
644+
fn g(foo: ()#[baz = "qux"] mut ba$0)
645+
"#,
646+
r#"
647+
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
648+
fn g(foo: (), #[baz = "qux"] mut bar: u32)
565649
"#,
566650
);
567651
}

crates/ide_completion/src/tests/fn_param.rs

+13
Original file line numberDiff line numberDiff line change
@@ -232,3 +232,16 @@ fn bar(bar$0) {}
232232
"#]],
233233
)
234234
}
235+
236+
#[test]
237+
fn completes_for_params_with_attributes() {
238+
check(
239+
r#"
240+
fn f(foo: (), #[baz = "qux"] mut bar: u32) {}
241+
fn g(foo: (), #[baz = "qux"] mut ba$0)
242+
"#,
243+
expect![[r##"
244+
bn #[baz = "qux"] mut bar: u32
245+
"##]],
246+
)
247+
}

0 commit comments

Comments
 (0)