mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 12:54:58 +00:00
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 {
|
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 file_id = reference.file_range.file_id;
|
||||||
let range = match reference.kind {
|
let range = match reference.kind {
|
||||||
ReferenceKind::StructFieldShorthand => {
|
ReferenceKind::StructFieldShorthandForField => {
|
||||||
|
replacement_text.push_str(new_name);
|
||||||
replacement_text.push_str(": ");
|
replacement_text.push_str(": ");
|
||||||
TextRange::from_to(
|
TextRange::from_to(
|
||||||
reference.file_range.range.start(),
|
reference.file_range.range.start(),
|
||||||
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) }
|
SourceFileEdit { file_id, edit: TextEdit::replace(range, replacement_text) }
|
||||||
}
|
}
|
||||||
|
@ -286,7 +295,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rename_for_struct_field() {
|
fn test_rename_struct_field() {
|
||||||
test_rename(
|
test_rename(
|
||||||
r#"
|
r#"
|
||||||
struct Foo {
|
struct Foo {
|
||||||
|
@ -315,7 +324,7 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rename_for_struct_field_shorthand() {
|
fn test_rename_struct_field_for_shorthand() {
|
||||||
test_rename(
|
test_rename(
|
||||||
r#"
|
r#"
|
||||||
struct Foo {
|
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]
|
#[test]
|
||||||
fn test_rename_mod() {
|
fn test_rename_mod() {
|
||||||
let (analysis, position) = analysis_and_position(
|
let (analysis, position) = analysis_and_position(
|
||||||
|
|
|
@ -30,7 +30,8 @@ pub struct Reference {
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum ReferenceKind {
|
pub enum ReferenceKind {
|
||||||
StructFieldShorthand,
|
StructFieldShorthandForField,
|
||||||
|
StructFieldShorthandForLocal,
|
||||||
StructLiteral,
|
StructLiteral,
|
||||||
Other,
|
Other,
|
||||||
}
|
}
|
||||||
|
@ -238,8 +239,8 @@ impl Definition {
|
||||||
// FIXME: reuse sb
|
// FIXME: reuse sb
|
||||||
// See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098
|
// See https://github.com/rust-lang/rust/pull/68198#issuecomment-574269098
|
||||||
|
|
||||||
match (classify_name_ref(&sema, &name_ref), self) {
|
match classify_name_ref(&sema, &name_ref) {
|
||||||
(Some(NameRefClass::Definition(def)), _) if &def == self => {
|
Some(NameRefClass::Definition(def)) if &def == self => {
|
||||||
let kind = if is_record_lit_name_ref(&name_ref)
|
let kind = if is_record_lit_name_ref(&name_ref)
|
||||||
|| is_call_expr_name_ref(&name_ref)
|
|| is_call_expr_name_ref(&name_ref)
|
||||||
{
|
{
|
||||||
|
@ -255,14 +256,19 @@ impl Definition {
|
||||||
access: reference_access(&def, &name_ref),
|
access: reference_access(&def, &name_ref),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
(
|
Some(NameRefClass::FieldShorthand { local, field: _ }) => {
|
||||||
Some(NameRefClass::FieldShorthand { local, field: _ }),
|
let kind = match self {
|
||||||
Definition::StructField(_),
|
Definition::StructField(_) => {
|
||||||
) => {
|
ReferenceKind::StructFieldShorthandForField
|
||||||
|
}
|
||||||
|
Definition::Local(_) => ReferenceKind::StructFieldShorthandForLocal,
|
||||||
|
_ => continue,
|
||||||
|
};
|
||||||
|
|
||||||
let file_range = sema.original_range(name_ref.syntax());
|
let file_range = sema.original_range(name_ref.syntax());
|
||||||
refs.push(Reference {
|
refs.push(Reference {
|
||||||
file_range: file_range,
|
file_range,
|
||||||
kind: ReferenceKind::StructFieldShorthand,
|
kind,
|
||||||
access: reference_access(&Definition::Local(local), &name_ref),
|
access: reference_access(&Definition::Local(local), &name_ref),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue