Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 8678b92

Browse files
committed
Allow renaming lifetiems and labels without leading apostrophe
1 parent daa0138 commit 8678b92

File tree

2 files changed

+52
-12
lines changed

2 files changed

+52
-12
lines changed

crates/ide-db/src/rename.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,10 @@ fn rename_reference(
263263
Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_)
264264
) {
265265
match ident_kind {
266-
IdentifierKind::Ident | IdentifierKind::Underscore => {
267-
cov_mark::hit!(rename_not_a_lifetime_ident_ref);
266+
IdentifierKind::Underscore => {
268267
bail!("Invalid name `{}`: not a lifetime identifier", new_name);
269268
}
270-
IdentifierKind::Lifetime => cov_mark::hit!(rename_lifetime),
269+
_ => cov_mark::hit!(rename_lifetime),
271270
}
272271
} else {
273272
match ident_kind {
@@ -335,7 +334,14 @@ pub fn source_edit_from_references(
335334
_ => false,
336335
};
337336
if !has_emitted_edit && !edited_ranges.contains(&range.start()) {
338-
edit.replace(range, new_name.to_string());
337+
let new_name = match name {
338+
ast::NameLike::Lifetime(_) => {
339+
format!("'{}", new_name.trim_start_matches("'"))
340+
}
341+
_ => new_name.into(),
342+
};
343+
344+
edit.replace(range, new_name);
339345
edited_ranges.push(range.start());
340346
}
341347
}
@@ -501,7 +507,15 @@ fn source_edit_from_def(
501507
}
502508
}
503509
if edit.is_empty() {
504-
edit.replace(range, new_name.to_string());
510+
let new_name = match def {
511+
Definition::GenericParam(hir::GenericParam::LifetimeParam(_))
512+
| Definition::Label(_) => {
513+
format!("'{}", new_name.trim_start_matches("'"))
514+
}
515+
_ => new_name.into(),
516+
};
517+
518+
edit.replace(range, new_name);
505519
}
506520
Ok((file_id, edit.finish()))
507521
}
@@ -522,9 +536,6 @@ impl IdentifierKind {
522536
(SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => {
523537
Ok(IdentifierKind::Lifetime)
524538
}
525-
(SyntaxKind::LIFETIME_IDENT, _) => {
526-
bail!("Invalid name `{}`: not a lifetime identifier", new_name)
527-
}
528539
(_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error),
529540
(_, None) => bail!("Invalid name `{}`: not an identifier", new_name),
530541
},

crates/ide/src/rename.rs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -521,14 +521,18 @@ impl Foo {
521521

522522
#[test]
523523
fn test_rename_to_invalid_identifier_lifetime2() {
524-
cov_mark::check!(rename_not_a_lifetime_ident_ref);
525524
check(
526-
"foo",
525+
"_",
527526
r#"fn main<'a>(_: &'a$0 ()) {}"#,
528-
"error: Invalid name `foo`: not a lifetime identifier",
527+
r#"error: Invalid name `_`: not a lifetime identifier"#,
529528
);
530529
}
531530

531+
#[test]
532+
fn test_rename_accepts_lifetime_without_apostrophe() {
533+
check("foo", r#"fn main<'a>(_: &'a$0 ()) {}"#, r#"fn main<'foo>(_: &'foo ()) {}"#);
534+
}
535+
532536
#[test]
533537
fn test_rename_to_underscore_invalid() {
534538
cov_mark::check!(rename_underscore_multiple);
@@ -1745,7 +1749,7 @@ fn foo(foo: Foo) {
17451749

17461750
#[test]
17471751
fn test_rename_lifetimes() {
1748-
cov_mark::check!(rename_lifetime);
1752+
// cov_mark::check!(rename_lifetime);
17491753
check(
17501754
"'yeeee",
17511755
r#"
@@ -1831,6 +1835,31 @@ fn foo<'a>() -> &'a () {
18311835
)
18321836
}
18331837

1838+
#[test]
1839+
fn test_rename_label_new_name_without_apostrophe() {
1840+
check(
1841+
"foo",
1842+
r#"
1843+
fn main() {
1844+
'outer$0: loop {
1845+
'inner: loop {
1846+
break 'outer;
1847+
}
1848+
}
1849+
}
1850+
"#,
1851+
r#"
1852+
fn main() {
1853+
'foo: loop {
1854+
'inner: loop {
1855+
break 'foo;
1856+
}
1857+
}
1858+
}
1859+
"#,
1860+
);
1861+
}
1862+
18341863
#[test]
18351864
fn test_self_to_self() {
18361865
cov_mark::check!(rename_self_to_self);

0 commit comments

Comments
 (0)