diff options
Diffstat (limited to 'crates/assists')
-rw-r--r-- | crates/assists/src/handlers/extract_struct_from_enum_variant.rs | 3 | ||||
-rw-r--r-- | crates/assists/src/handlers/inline_local_variable.rs | 31 |
2 files changed, 30 insertions, 4 deletions
diff --git a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs index c1124b9e2..8ac20210a 100644 --- a/crates/assists/src/handlers/extract_struct_from_enum_variant.rs +++ b/crates/assists/src/handlers/extract_struct_from_enum_variant.rs | |||
@@ -1,6 +1,7 @@ | |||
1 | use base_db::FileId; | 1 | use base_db::FileId; |
2 | use hir::{EnumVariant, Module, ModuleDef, Name}; | 2 | use hir::{EnumVariant, Module, ModuleDef, Name}; |
3 | use ide_db::{defs::Definition, search::Reference, RootDatabase}; | 3 | use ide_db::{defs::Definition, search::Reference, RootDatabase}; |
4 | use itertools::Itertools; | ||
4 | use rustc_hash::FxHashSet; | 5 | use rustc_hash::FxHashSet; |
5 | use syntax::{ | 6 | use syntax::{ |
6 | algo::find_node_at_offset, | 7 | algo::find_node_at_offset, |
@@ -203,13 +204,11 @@ fn list_with_visibility(list: &str) -> String { | |||
203 | mod_part.insert_str(index, "pub "); | 204 | mod_part.insert_str(index, "pub "); |
204 | mod_part | 205 | mod_part |
205 | }) | 206 | }) |
206 | .collect::<Vec<String>>() | ||
207 | .join(", ") | 207 | .join(", ") |
208 | } | 208 | } |
209 | 209 | ||
210 | #[cfg(test)] | 210 | #[cfg(test)] |
211 | mod tests { | 211 | mod tests { |
212 | |||
213 | use crate::{ | 212 | use crate::{ |
214 | tests::{check_assist, check_assist_not_applicable}, | 213 | tests::{check_assist, check_assist_not_applicable}, |
215 | utils::FamousDefs, | 214 | utils::FamousDefs, |
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( |