mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-27 12:29:21 +00:00
Add a new config to allow renaming of non-local items
With #15656 we started disallowing renaming of non-local items. Although this makes sense there are some false positives that impacted users' workflows. So this config aims to mitigate this by giving users the liberty to disable this feature.
This commit is contained in:
parent
5b62ebc23f
commit
9bd9a17ce5
8 changed files with 65 additions and 17 deletions
|
@ -84,6 +84,7 @@ pub(crate) fn rename(
|
|||
db: &RootDatabase,
|
||||
position: FilePosition,
|
||||
new_name: &str,
|
||||
rename_external: bool,
|
||||
) -> RenameResult<SourceChange> {
|
||||
let sema = Semantics::new(db);
|
||||
let source_file = sema.parse(position.file_id);
|
||||
|
@ -103,7 +104,7 @@ pub(crate) fn rename(
|
|||
return rename_to_self(&sema, local);
|
||||
}
|
||||
}
|
||||
def.rename(&sema, new_name)
|
||||
def.rename(&sema, new_name, rename_external)
|
||||
})
|
||||
.collect();
|
||||
|
||||
|
@ -122,9 +123,9 @@ pub(crate) fn will_rename_file(
|
|||
let module = sema.to_module_def(file_id)?;
|
||||
let def = Definition::Module(module);
|
||||
let mut change = if is_raw_identifier(new_name_stem) {
|
||||
def.rename(&sema, &SmolStr::from_iter(["r#", new_name_stem])).ok()?
|
||||
def.rename(&sema, &SmolStr::from_iter(["r#", new_name_stem]), true).ok()?
|
||||
} else {
|
||||
def.rename(&sema, new_name_stem).ok()?
|
||||
def.rename(&sema, new_name_stem, true).ok()?
|
||||
};
|
||||
change.file_system_edits.clear();
|
||||
Some(change)
|
||||
|
@ -375,8 +376,17 @@ mod tests {
|
|||
|
||||
use super::{RangeInfo, RenameError};
|
||||
|
||||
#[track_caller]
|
||||
fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
||||
check_with_rename_config(new_name, ra_fixture_before, ra_fixture_after, true);
|
||||
}
|
||||
|
||||
#[track_caller]
|
||||
fn check_with_rename_config(
|
||||
new_name: &str,
|
||||
ra_fixture_before: &str,
|
||||
ra_fixture_after: &str,
|
||||
rename_external: bool,
|
||||
) {
|
||||
let ra_fixture_after = &trim_indent(ra_fixture_after);
|
||||
let (analysis, position) = fixture::position(ra_fixture_before);
|
||||
if !ra_fixture_after.starts_with("error: ") {
|
||||
|
@ -385,7 +395,7 @@ mod tests {
|
|||
}
|
||||
}
|
||||
let rename_result = analysis
|
||||
.rename(position, new_name)
|
||||
.rename(position, new_name, rename_external)
|
||||
.unwrap_or_else(|err| panic!("Rename to '{new_name}' was cancelled: {err}"));
|
||||
match rename_result {
|
||||
Ok(source_change) => {
|
||||
|
@ -417,8 +427,10 @@ mod tests {
|
|||
|
||||
fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) {
|
||||
let (analysis, position) = fixture::position(ra_fixture);
|
||||
let source_change =
|
||||
analysis.rename(position, new_name).unwrap().expect("Expect returned a RenameError");
|
||||
let source_change = analysis
|
||||
.rename(position, new_name, true)
|
||||
.unwrap()
|
||||
.expect("Expect returned a RenameError");
|
||||
expect.assert_eq(&filter_expect(source_change))
|
||||
}
|
||||
|
||||
|
@ -2617,6 +2629,18 @@ use qux as frob;
|
|||
|
||||
#[test]
|
||||
fn disallow_renaming_for_non_local_definition() {
|
||||
check_with_rename_config(
|
||||
"Baz",
|
||||
r#"
|
||||
//- /lib.rs crate:lib new_source_root:library
|
||||
pub struct S;
|
||||
//- /main.rs crate:main deps:lib new_source_root:local
|
||||
use lib::S$0;
|
||||
"#,
|
||||
"error: Cannot rename a non-local definition. Set `renameExternalItems` to `true` to allow renaming for this item.",
|
||||
false,
|
||||
);
|
||||
|
||||
check(
|
||||
"Baz",
|
||||
r#"
|
||||
|
@ -2625,13 +2649,13 @@ pub struct S;
|
|||
//- /main.rs crate:main deps:lib new_source_root:local
|
||||
use lib::S$0;
|
||||
"#,
|
||||
"error: Cannot rename a non-local definition.",
|
||||
"use lib::Baz;",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn disallow_renaming_for_builtin_macros() {
|
||||
check(
|
||||
check_with_rename_config(
|
||||
"Baz",
|
||||
r#"
|
||||
//- minicore: derive, hash
|
||||
|
@ -2640,8 +2664,9 @@ use core::hash::Hash;
|
|||
#[derive(H$0ash)]
|
||||
struct A;
|
||||
"#,
|
||||
"error: Cannot rename a non-local definition.",
|
||||
)
|
||||
"error: Cannot rename a non-local definition. Set `renameExternalItems` to `true` to allow renaming for this item.",
|
||||
false,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue