Add some more marks to rename tests

This commit is contained in:
Lukas Wirth 2021-01-10 22:27:43 +01:00
parent 2347944aae
commit f7af0b4b96

View file

@ -14,7 +14,7 @@ use ide_db::{
use syntax::{ use syntax::{
algo::find_node_at_offset, algo::find_node_at_offset,
ast::{self, NameOwner}, ast::{self, NameOwner},
lex_single_syntax_kind, match_ast, AstNode, SyntaxKind, SyntaxNode, SyntaxToken, lex_single_syntax_kind, match_ast, AstNode, SyntaxKind, SyntaxNode, SyntaxToken, T,
}; };
use test_utils::mark; use test_utils::mark;
use text_edit::TextEdit; use text_edit::TextEdit;
@ -55,7 +55,7 @@ pub(crate) fn prepare_rename(
if let Some(module) = find_module_at_offset(&sema, position, syntax) { if let Some(module) = find_module_at_offset(&sema, position, syntax) {
rename_mod(&sema, position, module, "dummy") rename_mod(&sema, position, module, "dummy")
} else if let Some(self_token) = } else if let Some(self_token) =
syntax.token_at_offset(position.offset).find(|t| t.kind() == SyntaxKind::SELF_KW) syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self])
{ {
rename_self_to_param(&sema, position, self_token, "dummy") rename_self_to_param(&sema, position, self_token, "dummy")
} else { } else {
@ -85,7 +85,7 @@ pub(crate) fn rename_with_semantics(
if let Some(module) = find_module_at_offset(&sema, position, syntax) { if let Some(module) = find_module_at_offset(&sema, position, syntax) {
rename_mod(&sema, position, module, new_name) rename_mod(&sema, position, module, new_name)
} else if let Some(self_token) = } else if let Some(self_token) =
syntax.token_at_offset(position.offset).find(|t| t.kind() == SyntaxKind::SELF_KW) syntax.token_at_offset(position.offset).find(|t| t.kind() == T![self])
{ {
rename_self_to_param(&sema, position, self_token, new_name) rename_self_to_param(&sema, position, self_token, new_name)
} else { } else {
@ -110,7 +110,7 @@ pub(crate) fn will_rename_file(
Some(change) Some(change)
} }
#[derive(PartialEq)] #[derive(Debug, PartialEq)]
enum IdentifierKind { enum IdentifierKind {
Ident, Ident,
Lifetime, Lifetime,
@ -122,8 +122,8 @@ fn check_identifier(new_name: &str) -> RenameResult<IdentifierKind> {
match lex_single_syntax_kind(new_name) { match lex_single_syntax_kind(new_name) {
Some(res) => match res { Some(res) => match res {
(SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident), (SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident),
(SyntaxKind::UNDERSCORE, _) => Ok(IdentifierKind::Underscore), (T![_], _) => Ok(IdentifierKind::Underscore),
(SyntaxKind::SELF_KW, _) => Ok(IdentifierKind::ToSelf), (T![self], _) => Ok(IdentifierKind::ToSelf),
(SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => { (SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => {
Ok(IdentifierKind::Lifetime) Ok(IdentifierKind::Lifetime)
} }
@ -390,6 +390,7 @@ fn rename_self_to_param(
IdentifierKind::Lifetime => bail!("Invalid name `{}`: not an identifier", new_name), IdentifierKind::Lifetime => bail!("Invalid name `{}`: not an identifier", new_name),
IdentifierKind::ToSelf => { IdentifierKind::ToSelf => {
// no-op // no-op
mark::hit!(rename_self_to_self);
return Ok(RangeInfo::new(self_token.text_range(), SourceChange::default())); return Ok(RangeInfo::new(self_token.text_range(), SourceChange::default()));
} }
_ => (), _ => (),
@ -409,9 +410,7 @@ fn rename_self_to_param(
if !search_range.contains_inclusive(offset) { if !search_range.contains_inclusive(offset) {
continue; continue;
} }
if let Some(ref usage) = if let Some(ref usage) = syn.token_at_offset(offset).find(|t| t.kind() == T![self]) {
syn.token_at_offset(offset).find(|t| t.kind() == SyntaxKind::SELF_KW)
{
let edit = if let Some(ref self_param) = ast::SelfParam::cast(usage.parent()) { let edit = if let Some(ref self_param) = ast::SelfParam::cast(usage.parent()) {
text_edit_from_self_param(syn, self_param, new_name) text_edit_from_self_param(syn, self_param, new_name)
.ok_or_else(|| format_err!("No target type found"))? .ok_or_else(|| format_err!("No target type found"))?
@ -444,21 +443,26 @@ fn rename_reference(
(IdentifierKind::ToSelf, ReferenceKind::Lifetime) (IdentifierKind::ToSelf, ReferenceKind::Lifetime)
| (IdentifierKind::Underscore, ReferenceKind::Lifetime) | (IdentifierKind::Underscore, ReferenceKind::Lifetime)
| (IdentifierKind::Ident, ReferenceKind::Lifetime) => { | (IdentifierKind::Ident, ReferenceKind::Lifetime) => {
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, ReferenceKind::Lifetime) => (), (IdentifierKind::Lifetime, ReferenceKind::Lifetime) => mark::hit!(rename_lifetime),
(IdentifierKind::Lifetime, _) => bail!("Invalid name `{}`: not an identifier", new_name), (IdentifierKind::Lifetime, _) => {
mark::hit!(rename_not_an_ident_ref);
bail!("Invalid name `{}`: not an identifier", new_name)
}
(IdentifierKind::ToSelf, ReferenceKind::SelfKw) => { (IdentifierKind::ToSelf, ReferenceKind::SelfKw) => {
//no-op unreachable!("rename_self_to_param should've been called instead")
return Ok(RangeInfo::new(range, SourceChange::default()));
} }
(IdentifierKind::ToSelf, _) => { (IdentifierKind::ToSelf, _) => {
mark::hit!(rename_to_self);
return rename_to_self(sema, position); return rename_to_self(sema, position);
} }
(IdentifierKind::Underscore, _) if !refs.references.is_empty() => { (IdentifierKind::Underscore, _) if !refs.references.is_empty() => {
mark::hit!(rename_underscore_multiple);
bail!("Cannot rename reference to `_` as it is being referenced multiple times") bail!("Cannot rename reference to `_` as it is being referenced multiple times")
} }
(IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => (), (IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident),
} }
let edit = refs let edit = refs
@ -494,9 +498,11 @@ mod tests {
text_edit_builder.replace(indel.delete, indel.insert); text_edit_builder.replace(indel.delete, indel.insert);
} }
} }
let mut result = analysis.file_text(file_id.unwrap()).unwrap().to_string(); if let Some(file_id) = file_id {
text_edit_builder.finish().apply(&mut result); let mut result = analysis.file_text(file_id).unwrap().to_string();
assert_eq_text!(ra_fixture_after, &*result); text_edit_builder.finish().apply(&mut result);
assert_eq_text!(ra_fixture_after, &*result);
}
} }
Err(err) => { Err(err) => {
if ra_fixture_after.starts_with("error:") { if ra_fixture_after.starts_with("error:") {
@ -562,6 +568,7 @@ mod tests {
#[test] #[test]
fn test_rename_to_invalid_identifier_lifetime() { fn test_rename_to_invalid_identifier_lifetime() {
mark::check!(rename_not_an_ident_ref);
check( check(
"'foo", "'foo",
r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let i$0 = 1; }"#,
@ -571,6 +578,7 @@ mod tests {
#[test] #[test]
fn test_rename_to_invalid_identifier_lifetime2() { fn test_rename_to_invalid_identifier_lifetime2() {
mark::check!(rename_not_a_lifetime_ident_ref);
check( check(
"foo", "foo",
r#"fn main<'a>(_: &'a$0 ()) {}"#, r#"fn main<'a>(_: &'a$0 ()) {}"#,
@ -580,6 +588,7 @@ mod tests {
#[test] #[test]
fn test_rename_to_underscore_invalid() { fn test_rename_to_underscore_invalid() {
mark::check!(rename_underscore_multiple);
check( check(
"_", "_",
r#"fn main(foo$0: ()) {foo;}"#, r#"fn main(foo$0: ()) {foo;}"#,
@ -587,8 +596,18 @@ mod tests {
); );
} }
#[test]
fn test_rename_mod_invalid() {
check(
"'foo",
r#"mod foo$0 {}"#,
"error: Invalid name `'foo`: cannot rename module to 'foo",
);
}
#[test] #[test]
fn test_rename_for_local() { fn test_rename_for_local() {
mark::check!(rename_ident);
check( check(
"k", "k",
r#" r#"
@ -1219,6 +1238,7 @@ fn foo(f: foo::Foo) {
#[test] #[test]
fn test_parameter_to_self() { fn test_parameter_to_self() {
mark::check!(rename_to_self);
check( check(
"self", "self",
r#" r#"
@ -1522,6 +1542,7 @@ fn foo(Foo { i: bar }: foo) -> i32 {
#[test] #[test]
fn test_rename_lifetimes() { fn test_rename_lifetimes() {
mark::check!(rename_lifetime);
check( check(
"'yeeee", "'yeeee",
r#" r#"
@ -1603,6 +1624,26 @@ fn foo<'a>() -> &'a () {
} }
} }
} }
"#,
)
}
#[test]
fn test_self_to_self() {
mark::check!(rename_self_to_self);
check(
"self",
r#"
struct Foo;
impl Foo {
fn foo(self$0) {}
}
"#,
r#"
struct Foo;
impl Foo {
fn foo(self) {}
}
"#, "#,
) )
} }