Skip to content

Commit f5c069c

Browse files
Merge #11894
11894: complete pattern args based on type name r=Veykril a=cameron1024 Addresses #11892 Changes function argument completion to cover a case like this: ```rust struct Foo { bar: i32 } fn qux(Foo { bar }: Foo) { println!("{bar}"); } ``` When completing the function call for `qux`, instead of expanding to `qux(_)`, it will now expand to `qux(foo)` (based on the snake-cased version of the name of the ADT) Non ADTs are unaffected Co-authored-by: cameron <[email protected]> Co-authored-by: cameron1024 <[email protected]>
2 parents 46d7ee6 + 37d2a82 commit f5c069c

File tree

1 file changed

+45
-2
lines changed

1 file changed

+45
-2
lines changed

crates/ide_completion/src/render/function.rs

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use hir::{db::HirDatabase, AsAssocItem, HirDisplay};
44
use ide_db::{SnippetCap, SymbolKind};
55
use itertools::Itertools;
6-
use stdx::format_to;
6+
use stdx::{format_to, to_lower_snake_case};
77
use syntax::SmolStr;
88

99
use crate::{
@@ -135,7 +135,17 @@ pub(super) fn add_call_parens<'b>(
135135
let ref_ = ref_of_param(ctx, text, param.ty());
136136
f(&format_args!("${{{}:{}{}}}", index + offset, ref_, text))
137137
}
138-
None => f(&format_args!("${{{}:_}}", index + offset,)),
138+
None => {
139+
let name = match param.ty().as_adt() {
140+
None => "_".to_string(),
141+
Some(adt) => adt
142+
.name(ctx.db)
143+
.as_text()
144+
.map(|s| to_lower_snake_case(s.as_str()))
145+
.unwrap_or_else(|| "_".to_string()),
146+
};
147+
f(&format_args!("${{{}:{}}}", index + offset, name))
148+
}
139149
}
140150
});
141151
match self_param {
@@ -516,6 +526,39 @@ fn take_mutably(mut x: &i32) {}
516526
fn main() {
517527
take_mutably(${1:x})$0
518528
}
529+
"#,
530+
);
531+
}
532+
533+
#[test]
534+
fn complete_pattern_args_with_type_name_if_adt() {
535+
check_edit(
536+
"qux",
537+
r#"
538+
struct Foo {
539+
bar: i32
540+
}
541+
542+
fn qux(Foo { bar }: Foo) {
543+
println!("{}", bar);
544+
}
545+
546+
fn main() {
547+
qu$0
548+
}
549+
"#,
550+
r#"
551+
struct Foo {
552+
bar: i32
553+
}
554+
555+
fn qux(Foo { bar }: Foo) {
556+
println!("{}", bar);
557+
}
558+
559+
fn main() {
560+
qux(${1:foo})$0
561+
}
519562
"#,
520563
);
521564
}

0 commit comments

Comments
 (0)