From c735b979ed6f60483a66808077e1e59062c8e36f Mon Sep 17 00:00:00 2001 From: cameron Date: Mon, 4 Apr 2022 13:51:51 +0100 Subject: [PATCH 1/4] complete pattern args based on type name --- crates/ide_completion/src/render/function.rs | 45 +++++++++++++++++++- 1 file 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 { @@ -516,6 +524,39 @@ fn take_mutably(mut x: &i32) {} fn main() { take_mutably(${1:x})$0 } +"#, + ); + } + + #[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 +} "#, ); } From 2137bdfca3adfb2cffb552eeec3f366c5950d223 Mon Sep 17 00:00:00 2001 From: cameron Date: Mon, 4 Apr 2022 14:28:15 +0100 Subject: [PATCH 2/4] remove unwarp on adt name --- crates/ide_completion/src/render/function.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs index 82688cfae6..6032b28fc6 100644 --- a/crates/ide_completion/src/render/function.rs +++ b/crates/ide_completion/src/render/function.rs @@ -137,10 +137,12 @@ pub(super) fn add_call_parens<'b>( } None => { let name = match param.ty().as_adt() { - Some(adt) => { - to_lower_snake_case(&adt.name(ctx.db).as_text().unwrap()) - } None => "_".to_string(), + Some(adt) => adt + .name(ctx.db) + .as_text() + .map(to_lower_snake_case) + .unwrap_or("_".to_string()), }; f(&format_args!("${{{}:{}}}", index + offset, name)) } From bb1c3a20b502c48b4d0952878f0280e579c8a6f7 Mon Sep 17 00:00:00 2001 From: cameron Date: Mon, 4 Apr 2022 14:30:49 +0100 Subject: [PATCH 3/4] fix type error --- crates/ide_completion/src/render/function.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs index 6032b28fc6..c0f38a8fcd 100644 --- a/crates/ide_completion/src/render/function.rs +++ b/crates/ide_completion/src/render/function.rs @@ -141,7 +141,7 @@ pub(super) fn add_call_parens<'b>( Some(adt) => adt .name(ctx.db) .as_text() - .map(to_lower_snake_case) + .map(|s| to_lower_snake_case(s.as_str())) .unwrap_or("_".to_string()), }; f(&format_args!("${{{}:{}}}", index + offset, name)) From 37d2a8243fc23d84490090803a428cf130894b6d Mon Sep 17 00:00:00 2001 From: cameron1024 Date: Mon, 4 Apr 2022 13:33:14 +0100 Subject: [PATCH 4/4] Update crates/ide_completion/src/render/function.rs Co-authored-by: Lukas Wirth --- crates/ide_completion/src/render/function.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/ide_completion/src/render/function.rs b/crates/ide_completion/src/render/function.rs index c0f38a8fcd..2031eef049 100644 --- a/crates/ide_completion/src/render/function.rs +++ b/crates/ide_completion/src/render/function.rs @@ -142,7 +142,7 @@ pub(super) fn add_call_parens<'b>( .name(ctx.db) .as_text() .map(|s| to_lower_snake_case(s.as_str())) - .unwrap_or("_".to_string()), + .unwrap_or_else(|| "_".to_string()), }; f(&format_args!("${{{}:{}}}", index + offset, name)) }