5888: **Inline Variable** works with field shorthand
 r=matklad a=matklad

bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-08-26 11:04:00 +00:00 committed by GitHub
commit 51f5af223f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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);