Skip to content

Commit 995c8f5

Browse files
committed
some code docs for the ide_db/rename module
1 parent 5e533e5 commit 995c8f5

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

crates/ide/src/rename.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,6 +1470,7 @@ fn foo(Foo { i: bar }: Foo) -> i32 {
14701470

14711471
#[test]
14721472
fn test_struct_field_complex_ident_pat() {
1473+
cov_mark::check!(rename_record_pat_field_name_split);
14731474
check(
14741475
"baz",
14751476
r#"

crates/ide_db/src/rename.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,16 @@ pub fn source_edit_from_references(
326326

327327
fn source_edit_from_name(name: &ast::Name, new_name: &str) -> Option<(TextRange, String)> {
328328
if let Some(_) = ast::RecordPatField::for_field_name(name) {
329-
// FIXME: instead of splitting the shorthand, recursively trigger a rename of the
330-
// other name https://github.com/rust-analyzer/rust-analyzer/issues/6547
331329
if let Some(ident_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) {
330+
cov_mark::hit!(rename_record_pat_field_name_split);
331+
// Foo { ref mut field } -> Foo { new_name: ref mut field }
332+
// ^ insert `new_name: `
333+
334+
// FIXME: instead of splitting the shorthand, recursively trigger a rename of the
335+
// other name https://github.com/rust-analyzer/rust-analyzer/issues/6547
332336
return Some((
333337
TextRange::empty(ident_pat.syntax().text_range().start()),
334-
[new_name, ": "].concat(),
338+
format!("{}: ", new_name),
335339
));
336340
}
337341
}
@@ -347,21 +351,29 @@ fn source_edit_from_name_ref(
347351
if let Some(record_field) = ast::RecordExprField::for_name_ref(name_ref) {
348352
let rcf_name_ref = record_field.name_ref();
349353
let rcf_expr = record_field.expr();
350-
match (rcf_name_ref, rcf_expr.and_then(|it| it.name_ref())) {
354+
match &(rcf_name_ref, rcf_expr.and_then(|it| it.name_ref())) {
351355
// field: init-expr, check if we can use a field init shorthand
352356
(Some(field_name), Some(init)) => {
353-
if field_name == *name_ref {
357+
if field_name == name_ref {
354358
if init.text() == new_name {
355359
cov_mark::hit!(test_rename_field_put_init_shorthand);
360+
// Foo { field: local } -> Foo { local }
361+
// ^^^^^^^^ delete this
362+
// FIXME: Actually delete this instead of replacing the entire thing
363+
356364
// same names, we can use a shorthand here instead.
357365
// we do not want to erase attributes hence this range start
358366
let s = field_name.syntax().text_range().start();
359367
let e = record_field.syntax().text_range().end();
360368
return Some((TextRange::new(s, e), new_name.to_owned()));
361369
}
362-
} else if init == *name_ref {
370+
} else if init == name_ref {
363371
if field_name.text() == new_name {
364372
cov_mark::hit!(test_rename_local_put_init_shorthand);
373+
// Foo { field: local } -> Foo { field }
374+
// ^^^^^^^ delete this
375+
// FIXME: Actually delete this instead of replacing the entire thing
376+
365377
// same names, we can use a shorthand here instead.
366378
// we do not want to erase attributes hence this range start
367379
let s = field_name.syntax().text_range().start();
@@ -374,11 +386,15 @@ fn source_edit_from_name_ref(
374386
// init shorthand
375387
(None, Some(_)) if matches!(def, Definition::Field(_)) => {
376388
cov_mark::hit!(test_rename_field_in_field_shorthand);
389+
// Foo { field } -> Foo { new_name: field }
390+
// ^ insert `new_name: `
377391
let s = name_ref.syntax().text_range().start();
378392
Some((TextRange::empty(s), format!("{}: ", new_name)))
379393
}
380394
(None, Some(_)) if matches!(def, Definition::Local(_)) => {
381395
cov_mark::hit!(test_rename_local_in_field_shorthand);
396+
// Foo { field } -> Foo { field: new_name }
397+
// ^ insert `: new_name`
382398
let s = name_ref.syntax().text_range().end();
383399
Some((TextRange::empty(s), format!(": {}", new_name)))
384400
}
@@ -395,6 +411,11 @@ fn source_edit_from_name_ref(
395411
// field name is being renamed
396412
if pat.name().map_or(false, |it| it.text() == new_name) {
397413
cov_mark::hit!(test_rename_field_put_init_shorthand_pat);
414+
// Foo { field: ref mut local } -> Foo { ref mut field }
415+
// ^^^^^^^ delete this
416+
// ^^^^^ replace this with `field`
417+
// FIXME: do this the way its written here
418+
398419
// same names, we can use a shorthand here instead/
399420
// we do not want to erase attributes hence this range start
400421
let s = field_name.syntax().text_range().start();

0 commit comments

Comments
 (0)