diff options
author | Aleksey Kladov <[email protected]> | 2020-08-26 12:03:14 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-08-26 12:03:14 +0100 |
commit | 0fc8fd2bd589572964d4949cd35aac44659af334 (patch) | |
tree | fc5745bf0264d14ab5d0346b25d6b14b1ae0da45 /crates/assists/src | |
parent | f647edcb080f50e01762a31eebd9ca94c982c768 (diff) |
**Inline Variable** works with field shorthand
Diffstat (limited to 'crates/assists/src')
-rw-r--r-- | crates/assists/src/handlers/inline_local_variable.rs | 31 |
1 files changed, 29 insertions, 2 deletions
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 @@ | |||
1 | use ide_db::defs::Definition; | 1 | use ide_db::{defs::Definition, search::ReferenceKind}; |
2 | use syntax::{ | 2 | use syntax::{ |
3 | ast::{self, AstNode, AstToken}, | 3 | ast::{self, AstNode, AstToken}, |
4 | TextRange, | 4 | TextRange, |
@@ -119,7 +119,13 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O | |||
119 | for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) { | 119 | for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) { |
120 | let replacement = | 120 | let replacement = |
121 | if should_wrap { init_in_paren.clone() } else { init_str.clone() }; | 121 | if should_wrap { init_in_paren.clone() } else { init_str.clone() }; |
122 | builder.replace(desc.file_range.range, replacement) | 122 | match desc.kind { |
123 | ReferenceKind::FieldShorthandForLocal => { | ||
124 | mark::hit!(inline_field_shorthand); | ||
125 | builder.insert(desc.file_range.range.end(), format!(": {}", replacement)) | ||
126 | } | ||
127 | _ => builder.replace(desc.file_range.range, replacement), | ||
128 | } | ||
123 | } | 129 | } |
124 | }, | 130 | }, |
125 | ) | 131 | ) |
@@ -667,6 +673,27 @@ fn foo() { | |||
667 | } | 673 | } |
668 | 674 | ||
669 | #[test] | 675 | #[test] |
676 | fn inline_field_shorthand() { | ||
677 | mark::check!(inline_field_shorthand); | ||
678 | check_assist( | ||
679 | inline_local_variable, | ||
680 | r" | ||
681 | struct S { foo: i32} | ||
682 | fn main() { | ||
683 | let <|>foo = 92; | ||
684 | S { foo } | ||
685 | } | ||
686 | ", | ||
687 | r" | ||
688 | struct S { foo: i32} | ||
689 | fn main() { | ||
690 | S { foo: 92 } | ||
691 | } | ||
692 | ", | ||
693 | ); | ||
694 | } | ||
695 | |||
696 | #[test] | ||
670 | fn test_not_applicable_if_variable_unused() { | 697 | fn test_not_applicable_if_variable_unused() { |
671 | mark::check!(test_not_applicable_if_variable_unused); | 698 | mark::check!(test_not_applicable_if_variable_unused); |
672 | check_assist_not_applicable( | 699 | check_assist_not_applicable( |