Renaming a local renames struct field shorthand
This commit is contained in:
parent
a9b6aec8a7
commit
ce8121bd65
2 changed files with 58 additions and 14 deletions
|
@ -48,17 +48,26 @@ fn find_name_and_module_at_offset(
|
|||
}
|
||||
|
||||
fn source_edit_from_reference(reference: Reference, new_name: &str) -> SourceFileEdit {
|
||||
let mut replacement_text = String::from(new_name);
|
||||
let mut replacement_text = String::new();
|
||||
let file_id = reference.file_range.file_id;
|
||||
let range = match reference.kind {
|
||||
ReferenceKind::StructFieldShorthand => {
|
||||
ReferenceKind::StructFieldShorthandForField => {
|
||||
replacement_text.push_str(new_name);
|
||||
replacement_text.push_str(": ");
|
||||
TextRange::from_to(
|
||||
reference.file_range.range.start(),
|
||||
reference.file_range.range.start(),
|
||||
)
|
||||
}
|
||||
_ => reference.file_range.range,
|
||||
ReferenceKind::StructFieldShorthandForLocal => {
|
||||
replacement_text.push_str(": ");
|
||||
replacement_text.push_str(new_name);
|
||||
TextRange::from_to(reference.file_range.range.end(), reference.file_range.range.end())
|
||||
}
|
||||
_ => {
|
||||
replacement_text.push_str(new_name);
|
||||
reference.file_range.range
|
||||
}
|
||||
};
|
||||
SourceFileEdit { file_id, edit: TextEdit::replace(range, replacement_text) }
|
||||
}
|
||||
|
@ -286,7 +295,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename_for_struct_field() {
|
||||
fn test_rename_struct_field() {
|
||||
test_rename(
|
||||
r#"
|
||||
struct Foo {
|
||||
|
@ -315,7 +324,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename_for_struct_field_shorthand() {
|
||||
fn test_rename_struct_field_for_shorthand() {
|
||||
test_rename(
|
||||
r#"
|
||||
struct Foo {
|
||||
|
@ -343,6 +352,35 @@ mod tests {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename_local_for_field_shorthand() {
|
||||
test_rename(
|
||||
r#"
|
||||
struct Foo {
|
||||
i: i32,
|
||||
}
|
||||
|
||||
impl Foo {
|
||||
fn new(i<|>: i32) -> Self {
|
||||
Self { i }
|
||||
}
|
||||
}
|
||||
"#,
|
||||
"j",
|
||||
r#"
|
||||
struct Foo {
|
||||
i: i32,
|
||||
}
|
||||
|
||||
impl Foo {
|
||||
fn new(j: i32) -> Self {
|
||||
Self { i: j }
|
||||
}
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rename_mod() {
|
||||
let (analysis, position) = analysis_and_position(
|
||||
|
|
|
@ -30,7 +30,8 @@ pub struct Reference {
|
|||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub enum ReferenceKind {
|
||||
StructFieldShorthand,
|
||||
StructFieldShorthandForField,
|
||||
StructFieldShorthandForLocal,
|
||||
StructLiteral,
|
||||
Other,
|
||||
}
|
||||
|
@ -238,8 +239,8 @@ impl Definition {
|
|||
// FIXME: reuse sb
|
||||
// See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098
|
||||
|
||||
match (classify_name_ref(&sema, &name_ref), self) {
|
||||
(Some(NameRefClass::Definition(def)), _) if &def == self => {
|
||||
match classify_name_ref(&sema, &name_ref) {
|
||||
Some(NameRefClass::Definition(def)) if &def == self => {
|
||||
let kind = if is_record_lit_name_ref(&name_ref)
|
||||
|| is_call_expr_name_ref(&name_ref)
|
||||
{
|
||||
|
@ -255,14 +256,19 @@ impl Definition {
|
|||
access: reference_access(&def, &name_ref),
|
||||
});
|
||||
}
|
||||
(
|
||||
Some(NameRefClass::FieldShorthand { local, field: _ }),
|
||||
Definition::StructField(_),
|
||||
) => {
|
||||
Some(NameRefClass::FieldShorthand { local, field: _ }) => {
|
||||
let kind = match self {
|
||||
Definition::StructField(_) => {
|
||||
ReferenceKind::StructFieldShorthandForField
|
||||
}
|
||||
Definition::Local(_) => ReferenceKind::StructFieldShorthandForLocal,
|
||||
_ => continue,
|
||||
};
|
||||
|
||||
let file_range = sema.original_range(name_ref.syntax());
|
||||
refs.push(Reference {
|
||||
file_range: file_range,
|
||||
kind: ReferenceKind::StructFieldShorthand,
|
||||
file_range,
|
||||
kind,
|
||||
access: reference_access(&Definition::Local(local), &name_ref),
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue