Unnamed repository; edit this file 'description' to name the repository.
complete pattern args based on type name
cameron 2022-04-04
parent ba9aed1 · commit c735b97
-rw-r--r--crates/ide_completion/src/render/function.rs45
1 files changed, 43 insertions, 2 deletions
diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs
index 85014717d6..82688cfae6 100644
--- a/crates/ide_completion/src/render/function.rs
+++ b/crates/ide_completion/src/render/function.rs
@@ -3,7 +3,7 @@
use hir::{db::HirDatabase, AsAssocItem, HirDisplay};
use ide_db::{SnippetCap, SymbolKind};
use itertools::Itertools;
-use stdx::format_to;
+use stdx::{format_to, to_lower_snake_case};
use syntax::SmolStr;
use crate::{
@@ -135,7 +135,15 @@ pub(super) fn add_call_parens<'b>(
let ref_ = ref_of_param(ctx, text, param.ty());
f(&format_args!("${{{}:{}{}}}", index + offset, ref_, text))
}
- None => f(&format_args!("${{{}:_}}", index + offset,)),
+ None => {
+ let name = match param.ty().as_adt() {
+ Some(adt) => {
+ to_lower_snake_case(&adt.name(ctx.db).as_text().unwrap())
+ }
+ None => "_".to_string(),
+ };
+ f(&format_args!("${{{}:{}}}", index + offset, name))
+ }
}
});
match self_param {
@@ -519,4 +527,37 @@ fn main() {
"#,
);
}
+
+ #[test]
+ fn complete_pattern_args_with_type_name_if_adt() {
+ check_edit(
+ "qux",
+ r#"
+struct Foo {
+ bar: i32
+}
+
+fn qux(Foo { bar }: Foo) {
+ println!("{}", bar);
+}
+
+fn main() {
+ qu$0
+}
+"#,
+ r#"
+struct Foo {
+ bar: i32
+}
+
+fn qux(Foo { bar }: Foo) {
+ println!("{}", bar);
+}
+
+fn main() {
+ qux(${1:foo})$0
+}
+"#,
+ );
+ }
}