Skip to content

Commit 51f5af2

Browse files
bors[bot]matklad
andauthored
Merge #5888
5888: **Inline Variable** works with field shorthand r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents f647edc + 0fc8fd2 commit 51f5af2

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

crates/assists/src/handlers/inline_local_variable.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use ide_db::defs::Definition;
1+
use ide_db::{defs::Definition, search::ReferenceKind};
22
use syntax::{
33
ast::{self, AstNode, AstToken},
44
TextRange,
@@ -119,7 +119,13 @@ pub(crate) fn inline_local_variable(acc: &mut Assists, ctx: &AssistContext) -> O
119119
for (desc, should_wrap) in refs.iter().zip(wrap_in_parens) {
120120
let replacement =
121121
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+
}
123129
}
124130
},
125131
)
@@ -666,6 +672,27 @@ fn foo() {
666672
);
667673
}
668674

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+
669696
#[test]
670697
fn test_not_applicable_if_variable_unused() {
671698
mark::check!(test_not_applicable_if_variable_unused);

0 commit comments

Comments
 (0)