Skip to content

Commit ce8121b

Browse files
committed
Renaming a local renames struct field shorthand
1 parent a9b6aec commit ce8121b

File tree

2 files changed

+58
-14
lines changed

2 files changed

+58
-14
lines changed

crates/ra_ide/src/references/rename.rs

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,26 @@ fn find_name_and_module_at_offset(
4848
}
4949

5050
fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFileEdit {
51-
let mut replacement_text = String::from(new_name);
51+
let mut replacement_text = String::new();
5252
let file_id = reference.file_range.file_id;
5353
let range = match reference.kind {
54-
ReferenceKind::StructFieldShorthand => {
54+
ReferenceKind::StructFieldShorthandForField => {
55+
replacement_text.push_str(new_name);
5556
replacement_text.push_str(": ");
5657
TextRange::from_to(
5758
reference.file_range.range.start(),
5859
reference.file_range.range.start(),
5960
)
6061
}
61-
_ => reference.file_range.range,
62+
ReferenceKind::StructFieldShorthandForLocal => {
63+
replacement_text.push_str(": ");
64+
replacement_text.push_str(new_name);
65+
TextRange::from_to(reference.file_range.range.end(), reference.file_range.range.end())
66+
}
67+
_ => {
68+
replacement_text.push_str(new_name);
69+
reference.file_range.range
70+
}
6271
};
6372
SourceFileEdit { file_id, edit: TextEdit::replace(range, replacement_text) }
6473
}
@@ -286,7 +295,7 @@ mod tests {
286295
}
287296

288297
#[test]
289-
fn test_rename_for_struct_field() {
298+
fn test_rename_struct_field() {
290299
test_rename(
291300
r#"
292301
struct Foo {
@@ -315,7 +324,7 @@ mod tests {
315324
}
316325

317326
#[test]
318-
fn test_rename_for_struct_field_shorthand() {
327+
fn test_rename_struct_field_for_shorthand() {
319328
test_rename(
320329
r#"
321330
struct Foo {
@@ -343,6 +352,35 @@ mod tests {
343352
);
344353
}
345354

355+
#[test]
356+
fn test_rename_local_for_field_shorthand() {
357+
test_rename(
358+
r#"
359+
struct Foo {
360+
i: i32,
361+
}
362+
363+
impl Foo {
364+
fn new(i<|>: i32) -> Self {
365+
Self { i }
366+
}
367+
}
368+
"#,
369+
"j",
370+
r#"
371+
struct Foo {
372+
i: i32,
373+
}
374+
375+
impl Foo {
376+
fn new(j: i32) -> Self {
377+
Self { i: j }
378+
}
379+
}
380+
"#,
381+
);
382+
}
383+
346384
#[test]
347385
fn test_rename_mod() {
348386
let (analysis, position) = analysis_and_position(

crates/ra_ide_db/src/search.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ pub struct Reference {
3030

3131
#[derive(Debug, Clone, PartialEq)]
3232
pub enum ReferenceKind {
33-
StructFieldShorthand,
33+
StructFieldShorthandForField,
34+
StructFieldShorthandForLocal,
3435
StructLiteral,
3536
Other,
3637
}
@@ -238,8 +239,8 @@ impl Definition {
238239
// FIXME: reuse sb
239240
// See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098
240241

241-
match (classify_name_ref(&sema, &name_ref), self) {
242-
(Some(NameRefClass::Definition(def)), _) if &def == self => {
242+
match classify_name_ref(&sema, &name_ref) {
243+
Some(NameRefClass::Definition(def)) if &def == self => {
243244
let kind = if is_record_lit_name_ref(&name_ref)
244245
|| is_call_expr_name_ref(&name_ref)
245246
{
@@ -255,14 +256,19 @@ impl Definition {
255256
access: reference_access(&def, &name_ref),
256257
});
257258
}
258-
(
259-
Some(NameRefClass::FieldShorthand { local, field: _ }),
260-
Definition::StructField(_),
261-
) => {
259+
Some(NameRefClass::FieldShorthand { local, field: _ }) => {
260+
let kind = match self {
261+
Definition::StructField(_) => {
262+
ReferenceKind::StructFieldShorthandForField
263+
}
264+
Definition::Local(_) => ReferenceKind::StructFieldShorthandForLocal,
265+
_ => continue,
266+
};
267+
262268
let file_range = sema.original_range(name_ref.syntax());
263269
refs.push(Reference {
264-
file_range: file_range,
265-
kind: ReferenceKind::StructFieldShorthand,
270+
file_range,
271+
kind,
266272
access: reference_access(&Definition::Local(local), &name_ref),
267273
});
268274
}

0 commit comments

Comments
 (0)