Skip to content

Commit f3e2973

Browse files
committed
Cleanup edit_text_range_for_record_field_expr_or_pat
1 parent cb60708 commit f3e2973

File tree

1 file changed

+59
-46
lines changed

1 file changed

+59
-46
lines changed

crates/ide/src/references/rename.rs

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ fn source_edit_from_reference(
126126
TextRange::new(reference.file_range.range.end(), reference.file_range.range.end())
127127
}
128128
ReferenceKind::RecordFieldExprOrPat => {
129+
mark::hit!(test_rename_field_expr_pat);
129130
replacement_text.push_str(new_name);
130131
edit_text_range_for_record_field_expr_or_pat(sema, reference.file_range, new_name)
131132
}
@@ -145,29 +146,27 @@ fn edit_text_range_for_record_field_expr_or_pat(
145146
file_range: FileRange,
146147
new_name: &str,
147148
) -> TextRange {
148-
let mut range = file_range.range;
149149
let source_file = sema.parse(file_range.file_id);
150150
let file_syntax = source_file.syntax();
151-
if let Some(field_expr) =
152-
syntax::algo::find_node_at_range::<ast::RecordExprField>(file_syntax, range)
153-
{
154-
match field_expr.expr().and_then(|e| e.name_ref()) {
155-
Some(name) if &name.to_string() == new_name => range = field_expr.syntax().text_range(),
156-
_ => (),
157-
}
158-
} else if let Some(field_pat) =
159-
syntax::algo::find_node_at_range::<ast::RecordPatField>(file_syntax, range)
160-
{
161-
match field_pat.pat() {
162-
Some(ast::Pat::IdentPat(pat))
163-
if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) =>
164-
{
165-
range = field_pat.syntax().text_range()
166-
}
167-
_ => (),
168-
}
169-
}
170-
range
151+
let original_range = file_range.range;
152+
153+
syntax::algo::find_node_at_range::<ast::RecordExprField>(file_syntax, original_range)
154+
.and_then(|field_expr| match field_expr.expr().and_then(|e| e.name_ref()) {
155+
Some(name) if &name.to_string() == new_name => Some(field_expr.syntax().text_range()),
156+
_ => None,
157+
})
158+
.or_else(|| {
159+
syntax::algo::find_node_at_range::<ast::RecordPatField>(file_syntax, original_range)
160+
.and_then(|field_pat| match field_pat.pat() {
161+
Some(ast::Pat::IdentPat(pat))
162+
if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) =>
163+
{
164+
Some(field_pat.syntax().text_range())
165+
}
166+
_ => None,
167+
})
168+
})
169+
.unwrap_or(original_range)
171170
}
172171

173172
fn rename_mod(
@@ -1140,6 +1139,7 @@ impl Foo {
11401139

11411140
#[test]
11421141
fn test_initializer_use_field_init_shorthand() {
1142+
mark::check!(test_rename_field_expr_pat);
11431143
check(
11441144
"bar",
11451145
r#"
@@ -1160,34 +1160,40 @@ fn foo(bar: i32) -> Foo {
11601160
}
11611161

11621162
#[test]
1163-
fn test_rename_binding_in_destructure_pat_shorthand() {
1163+
fn test_struct_field_destructure_into_shorthand() {
11641164
check(
1165-
"bar",
1165+
"baz",
11661166
r#"
1167-
struct Foo {
1168-
i: i32,
1169-
}
1167+
struct Foo { i<|>: i32 }
11701168
11711169
fn foo(foo: Foo) {
1172-
let Foo { i } = foo;
1173-
let _ = i<|>;
1170+
let Foo { i: baz } = foo;
1171+
let _ = baz;
11741172
}
11751173
"#,
11761174
r#"
1177-
struct Foo {
1178-
i: i32,
1179-
}
1175+
struct Foo { baz: i32 }
11801176
11811177
fn foo(foo: Foo) {
1182-
let Foo { i: bar } = foo;
1183-
let _ = bar;
1178+
let Foo { baz } = foo;
1179+
let _ = baz;
11841180
}
11851181
"#,
11861182
);
11871183
}
11881184

11891185
#[test]
11901186
fn test_rename_binding_in_destructure_pat() {
1187+
let expected_fixture = r#"
1188+
struct Foo {
1189+
i: i32,
1190+
}
1191+
1192+
fn foo(foo: Foo) {
1193+
let Foo { i: bar } = foo;
1194+
let _ = bar;
1195+
}
1196+
"#;
11911197
check(
11921198
"bar",
11931199
r#"
@@ -1200,39 +1206,46 @@ fn foo(foo: Foo) {
12001206
let _ = b<|>;
12011207
}
12021208
"#,
1209+
expected_fixture,
1210+
);
1211+
check(
1212+
"bar",
12031213
r#"
12041214
struct Foo {
12051215
i: i32,
12061216
}
12071217
12081218
fn foo(foo: Foo) {
1209-
let Foo { i: bar } = foo;
1210-
let _ = bar;
1219+
let Foo { i } = foo;
1220+
let _ = i<|>;
12111221
}
12121222
"#,
1223+
expected_fixture,
12131224
);
12141225
}
12151226

12161227
#[test]
1217-
fn test_struct_field_destructure_into_shorthand() {
1228+
fn test_rename_binding_in_destructure_param_pat() {
12181229
check(
1219-
"baz",
1230+
"bar",
12201231
r#"
1221-
struct Foo { i<|>: i32 }
1232+
struct Foo {
1233+
i: i32
1234+
}
12221235
1223-
fn foo(foo: Foo) {
1224-
let Foo { i: baz } = foo;
1225-
let _ = baz;
1236+
fn foo(Foo { i }: foo) -> i32 {
1237+
i<|>
12261238
}
12271239
"#,
12281240
r#"
1229-
struct Foo { baz: i32 }
1241+
struct Foo {
1242+
i: i32
1243+
}
12301244
1231-
fn foo(foo: Foo) {
1232-
let Foo { baz } = foo;
1233-
let _ = baz;
1245+
fn foo(Foo { i: bar }: foo) -> i32 {
1246+
bar
12341247
}
12351248
"#,
1236-
);
1249+
)
12371250
}
12381251
}

0 commit comments

Comments
 (0)