From 0fc8fd2bd589572964d4949cd35aac44659af334 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 26 Aug 2020 13:03:14 +0200 Subject: **Inline Variable** works with field shorthand --- .../assists/src/handlers/inline_local_variable.rs | 31 ++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'crates') diff --git a/crates/assists/src/handlers/inline_local_variable.rs b/crates/assists/src/handlers/inline_local_variable.rs index 164bbce86..587eb5feb 100644 --- a/crates/assists/src/handlers/inline_local_variable.rs +++ b/crates/assists/src/handlers/inline_local_variable.rs @@ -1,4 +1,4 @@ -use ide_db::defs::Definition; +use ide_db::{defs::Definition, search::ReferenceKind}; use syntax::{ ast::{self, AstNode, AstToken}, TextRange, @@ -119,7 +119,13 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) { let replacement = if should_wrap { init_in_paren.clone() } else { init_str.clone() }; - builder.replace(desc.file_range.range, replacement) + match desc.kind { + ReferenceKind::FieldShorthandForLocal => { + mark::hit!(inline_field_shorthand); + builder.insert(desc.file_range.range.end(), format!(": {}", replacement)) + } + _ => builder.replace(desc.file_range.range, replacement), + } } }, ) @@ -666,6 +672,27 @@ fn foo() { ); } + #[test] + fn inline_field_shorthand() { + mark::check!(inline_field_shorthand); + check_assist( + inline_local_variable, + r" +struct S { foo: i32} +fn main() { + let <|>foo = 92; + S { foo } +} +", + r" +struct S { foo: i32} +fn main() { + S { foo: 92 } +} +", + ); + } + #[test] fn test_not_applicable_if_variable_unused() { mark::check!(test_not_applicable_if_variable_unused); -- cgit v1.2.3