Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle shorthands in destructure patterns when renaming #6552

Merged
merged 4 commits into from
Nov 15, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
105 changes: 59 additions & 46 deletions crates/ide/src/references/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ fn source_edit_from_reference(
TextRange::new(reference.file_range.range.end(), reference.file_range.range.end())
}
ReferenceKind::RecordFieldExprOrPat => {
mark::hit!(test_rename_field_expr_pat);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

replacement_text.push_str(new_name);
edit_text_range_for_record_field_expr_or_pat(sema, reference.file_range, new_name)
}
Expand All @@ -145,29 +146,27 @@ fn edit_text_range_for_record_field_expr_or_pat(
file_range: FileRange,
new_name: &str,
) -> TextRange {
let mut range = file_range.range;
let source_file = sema.parse(file_range.file_id);
let file_syntax = source_file.syntax();
if let Some(field_expr) =
syntax::algo::find_node_at_range::<ast::RecordExprField>(file_syntax, range)
{
match field_expr.expr().and_then(|e| e.name_ref()) {
Some(name) if &name.to_string() == new_name => range = field_expr.syntax().text_range(),
_ => (),
}
} else if let Some(field_pat) =
syntax::algo::find_node_at_range::<ast::RecordPatField>(file_syntax, range)
{
match field_pat.pat() {
Some(ast::Pat::IdentPat(pat))
if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) =>
{
range = field_pat.syntax().text_range()
}
_ => (),
}
}
range
let original_range = file_range.range;

syntax::algo::find_node_at_range::<ast::RecordExprField>(file_syntax, original_range)
.and_then(|field_expr| match field_expr.expr().and_then(|e| e.name_ref()) {
Some(name) if &name.to_string() == new_name => Some(field_expr.syntax().text_range()),
_ => None,
})
.or_else(|| {
syntax::algo::find_node_at_range::<ast::RecordPatField>(file_syntax, original_range)
.and_then(|field_pat| match field_pat.pat() {
Some(ast::Pat::IdentPat(pat))
if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) =>
{
Some(field_pat.syntax().text_range())
}
_ => None,
})
})
.unwrap_or(original_range)
}

fn rename_mod(
Expand Down Expand Up @@ -1140,6 +1139,7 @@ impl Foo {

#[test]
fn test_initializer_use_field_init_shorthand() {
mark::check!(test_rename_field_expr_pat);
check(
"bar",
r#"
Expand All @@ -1160,34 +1160,40 @@ fn foo(bar: i32) -> Foo {
}

#[test]
fn test_rename_binding_in_destructure_pat_shorthand() {
fn test_struct_field_destructure_into_shorthand() {
check(
"bar",
"baz",
r#"
struct Foo {
i: i32,
}
struct Foo { i<|>: i32 }

fn foo(foo: Foo) {
let Foo { i } = foo;
let _ = i<|>;
let Foo { i: baz } = foo;
let _ = baz;
}
"#,
r#"
struct Foo {
i: i32,
}
struct Foo { baz: i32 }

fn foo(foo: Foo) {
let Foo { i: bar } = foo;
let _ = bar;
let Foo { baz } = foo;
let _ = baz;
}
"#,
);
}

#[test]
fn test_rename_binding_in_destructure_pat() {
let expected_fixture = r#"
struct Foo {
i: i32,
}

fn foo(foo: Foo) {
let Foo { i: bar } = foo;
let _ = bar;
}
"#;
check(
"bar",
r#"
Expand All @@ -1200,39 +1206,46 @@ fn foo(foo: Foo) {
let _ = b<|>;
}
"#,
expected_fixture,
);
check(
"bar",
r#"
struct Foo {
i: i32,
}

fn foo(foo: Foo) {
let Foo { i: bar } = foo;
let _ = bar;
let Foo { i } = foo;
let _ = i<|>;
}
"#,
expected_fixture,
);
}

#[test]
fn test_struct_field_destructure_into_shorthand() {
fn test_rename_binding_in_destructure_param_pat() {
check(
"baz",
"bar",
r#"
struct Foo { i<|>: i32 }
struct Foo {
i: i32
}

fn foo(foo: Foo) {
let Foo { i: baz } = foo;
let _ = baz;
fn foo(Foo { i }: foo) -> i32 {
i<|>
}
"#,
r#"
struct Foo { baz: i32 }
struct Foo {
i: i32
}

fn foo(foo: Foo) {
let Foo { baz } = foo;
let _ = baz;
fn foo(Foo { i: bar }: foo) -> i32 {
bar
}
"#,
);
)
}
}