mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-16 07:03:57 +00:00
Allow renaming lifetiems and labels without leading apostrophe
This commit is contained in:
parent
daa0138e4c
commit
8678b92218
2 changed files with 52 additions and 12 deletions
|
@ -263,11 +263,10 @@ fn rename_reference(
|
||||||
Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_)
|
Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_)
|
||||||
) {
|
) {
|
||||||
match ident_kind {
|
match ident_kind {
|
||||||
IdentifierKind::Ident | IdentifierKind::Underscore => {
|
IdentifierKind::Underscore => {
|
||||||
cov_mark::hit!(rename_not_a_lifetime_ident_ref);
|
|
||||||
bail!("Invalid name `{}`: not a lifetime identifier", new_name);
|
bail!("Invalid name `{}`: not a lifetime identifier", new_name);
|
||||||
}
|
}
|
||||||
IdentifierKind::Lifetime => cov_mark::hit!(rename_lifetime),
|
_ => cov_mark::hit!(rename_lifetime),
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match ident_kind {
|
match ident_kind {
|
||||||
|
@ -335,7 +334,14 @@ pub fn source_edit_from_references(
|
||||||
_ => false,
|
_ => false,
|
||||||
};
|
};
|
||||||
if !has_emitted_edit && !edited_ranges.contains(&range.start()) {
|
if !has_emitted_edit && !edited_ranges.contains(&range.start()) {
|
||||||
edit.replace(range, new_name.to_string());
|
let new_name = match name {
|
||||||
|
ast::NameLike::Lifetime(_) => {
|
||||||
|
format!("'{}", new_name.trim_start_matches("'"))
|
||||||
|
}
|
||||||
|
_ => new_name.into(),
|
||||||
|
};
|
||||||
|
|
||||||
|
edit.replace(range, new_name);
|
||||||
edited_ranges.push(range.start());
|
edited_ranges.push(range.start());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -501,7 +507,15 @@ fn source_edit_from_def(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if edit.is_empty() {
|
if edit.is_empty() {
|
||||||
edit.replace(range, new_name.to_string());
|
let new_name = match def {
|
||||||
|
Definition::GenericParam(hir::GenericParam::LifetimeParam(_))
|
||||||
|
| Definition::Label(_) => {
|
||||||
|
format!("'{}", new_name.trim_start_matches("'"))
|
||||||
|
}
|
||||||
|
_ => new_name.into(),
|
||||||
|
};
|
||||||
|
|
||||||
|
edit.replace(range, new_name);
|
||||||
}
|
}
|
||||||
Ok((file_id, edit.finish()))
|
Ok((file_id, edit.finish()))
|
||||||
}
|
}
|
||||||
|
@ -522,9 +536,6 @@ impl IdentifierKind {
|
||||||
(SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => {
|
(SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => {
|
||||||
Ok(IdentifierKind::Lifetime)
|
Ok(IdentifierKind::Lifetime)
|
||||||
}
|
}
|
||||||
(SyntaxKind::LIFETIME_IDENT, _) => {
|
|
||||||
bail!("Invalid name `{}`: not a lifetime identifier", new_name)
|
|
||||||
}
|
|
||||||
(_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error),
|
(_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error),
|
||||||
(_, None) => bail!("Invalid name `{}`: not an identifier", new_name),
|
(_, None) => bail!("Invalid name `{}`: not an identifier", new_name),
|
||||||
},
|
},
|
||||||
|
|
|
@ -521,14 +521,18 @@ impl Foo {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rename_to_invalid_identifier_lifetime2() {
|
fn test_rename_to_invalid_identifier_lifetime2() {
|
||||||
cov_mark::check!(rename_not_a_lifetime_ident_ref);
|
|
||||||
check(
|
check(
|
||||||
"foo",
|
"_",
|
||||||
r#"fn main<'a>(_: &'a$0 ()) {}"#,
|
r#"fn main<'a>(_: &'a$0 ()) {}"#,
|
||||||
"error: Invalid name `foo`: not a lifetime identifier",
|
r#"error: Invalid name `_`: not a lifetime identifier"#,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rename_accepts_lifetime_without_apostrophe() {
|
||||||
|
check("foo", r#"fn main<'a>(_: &'a$0 ()) {}"#, r#"fn main<'foo>(_: &'foo ()) {}"#);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rename_to_underscore_invalid() {
|
fn test_rename_to_underscore_invalid() {
|
||||||
cov_mark::check!(rename_underscore_multiple);
|
cov_mark::check!(rename_underscore_multiple);
|
||||||
|
@ -1745,7 +1749,7 @@ fn foo(foo: Foo) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rename_lifetimes() {
|
fn test_rename_lifetimes() {
|
||||||
cov_mark::check!(rename_lifetime);
|
// cov_mark::check!(rename_lifetime);
|
||||||
check(
|
check(
|
||||||
"'yeeee",
|
"'yeeee",
|
||||||
r#"
|
r#"
|
||||||
|
@ -1831,6 +1835,31 @@ fn foo<'a>() -> &'a () {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rename_label_new_name_without_apostrophe() {
|
||||||
|
check(
|
||||||
|
"foo",
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
'outer$0: loop {
|
||||||
|
'inner: loop {
|
||||||
|
break 'outer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
'foo: loop {
|
||||||
|
'inner: loop {
|
||||||
|
break 'foo;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_self_to_self() {
|
fn test_self_to_self() {
|
||||||
cov_mark::check!(rename_self_to_self);
|
cov_mark::check!(rename_self_to_self);
|
||||||
|
|
Loading…
Reference in a new issue