aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-08-26 12:04:00 +0100
committerGitHub <[email protected]>2020-08-26 12:04:00 +0100
commit51f5af223f01e7938fb07821027afe511d480826 (patch)
treefc5745bf0264d14ab5d0346b25d6b14b1ae0da45 /crates
parentf647edcb080f50e01762a31eebd9ca94c982c768 (diff)
parent0fc8fd2bd589572964d4949cd35aac44659af334 (diff)
Merge #5888
5888: **Inline Variable** works with field shorthand r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/assists/src/handlers/inline_local_variable.rs31
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 @@
1use ide_db::defs::Definition; 1use ide_db::{defs::Definition, search::ReferenceKind};
2use syntax::{ 2use 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"
681struct S { foo: i32}
682fn main() {
683 let <|>foo = 92;
684 S { foo }
685}
686",
687 r"
688struct S { foo: i32}
689fn 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(