Skip to content

feat: Provide a setting to disable showing rename conflicts #20193

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/ide-assists/src/assist_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! assists if we are allowed to.

use hir::ImportPathConfig;
use ide_db::rename::RenameConfig;
use ide_db::{SnippetCap, assists::ExprFillDefaultMode, imports::insert_use::InsertUseConfig};

use crate::AssistKind;
Expand All @@ -23,6 +24,7 @@ pub struct AssistConfig {
pub code_action_grouping: bool,
pub expr_fill_default: ExprFillDefaultMode,
pub prefer_self_ty: bool,
pub rename_config: RenameConfig,
}

impl AssistConfig {
Expand Down
4 changes: 3 additions & 1 deletion crates/ide-assists/src/handlers/remove_underscore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ pub(crate) fn remove_underscore(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
"Remove underscore from a used variable",
text_range,
|builder| {
let changes = def.rename(&ctx.sema, new_name, RenameDefinition::Yes).unwrap();
let changes = def
.rename(&ctx.sema, &ctx.config.rename_config, new_name, RenameDefinition::Yes)
.unwrap();
builder.source_change = changes;
},
)
Expand Down
5 changes: 5 additions & 0 deletions crates/ide-assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use ide_db::{
assists::ExprFillDefaultMode,
base_db::SourceDatabase,
imports::insert_use::{ImportGranularity, InsertUseConfig},
rename::RenameConfig,
source_change::FileSystemEdit,
};
use stdx::{format_to, trim_indent};
Expand Down Expand Up @@ -38,6 +39,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
code_action_grouping: true,
expr_fill_default: ExprFillDefaultMode::Todo,
prefer_self_ty: false,
rename_config: RenameConfig { show_conflicts: true },
};

pub(crate) const TEST_CONFIG_NO_GROUPING: AssistConfig = AssistConfig {
Expand All @@ -59,6 +61,7 @@ pub(crate) const TEST_CONFIG_NO_GROUPING: AssistConfig = AssistConfig {
code_action_grouping: false,
expr_fill_default: ExprFillDefaultMode::Todo,
prefer_self_ty: false,
rename_config: RenameConfig { show_conflicts: true },
};

pub(crate) const TEST_CONFIG_NO_SNIPPET_CAP: AssistConfig = AssistConfig {
Expand All @@ -80,6 +83,7 @@ pub(crate) const TEST_CONFIG_NO_SNIPPET_CAP: AssistConfig = AssistConfig {
code_action_grouping: true,
expr_fill_default: ExprFillDefaultMode::Todo,
prefer_self_ty: false,
rename_config: RenameConfig { show_conflicts: true },
};

pub(crate) const TEST_CONFIG_IMPORT_ONE: AssistConfig = AssistConfig {
Expand All @@ -101,6 +105,7 @@ pub(crate) const TEST_CONFIG_IMPORT_ONE: AssistConfig = AssistConfig {
code_action_grouping: true,
expr_fill_default: ExprFillDefaultMode::Todo,
prefer_self_ty: false,
rename_config: RenameConfig { show_conflicts: true },
};

pub(crate) fn with_single_file(text: &str) -> (RootDatabase, EditionedFileId) {
Expand Down
53 changes: 34 additions & 19 deletions crates/ide-db/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ use crate::{
traits::convert_to_def_in_trait,
};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RenameConfig {
pub show_conflicts: bool,
}

pub type Result<T, E = RenameError> = std::result::Result<T, E>;

#[derive(Debug)]
Expand Down Expand Up @@ -79,6 +84,7 @@ impl Definition {
pub fn rename(
&self,
sema: &Semantics<'_, RootDatabase>,
config: &RenameConfig,
new_name: &str,
rename_definition: RenameDefinition,
) -> Result<SourceChange> {
Expand Down Expand Up @@ -109,10 +115,15 @@ impl Definition {
bail!("Cannot rename a builtin attr.")
}
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
Definition::Macro(mac) => {
rename_reference(sema, Definition::Macro(mac), new_name, rename_definition, edition)
}
def => rename_reference(sema, def, new_name, rename_definition, edition),
Definition::Macro(mac) => rename_reference(
sema,
config,
Definition::Macro(mac),
new_name,
rename_definition,
edition,
),
def => rename_reference(sema, config, def, new_name, rename_definition, edition),
}
}

Expand Down Expand Up @@ -335,6 +346,7 @@ fn rename_mod(

fn rename_reference(
sema: &Semantics<'_, RootDatabase>,
config: &RenameConfig,
def: Definition,
new_name: &str,
rename_definition: RenameDefinition,
Expand Down Expand Up @@ -397,7 +409,8 @@ fn rename_reference(
if rename_definition == RenameDefinition::Yes {
// This needs to come after the references edits, because we change the annotation of existing edits
// if a conflict is detected.
let (file_id, edit) = source_edit_from_def(sema, def, &new_name, &mut source_change)?;
let (file_id, edit) =
source_edit_from_def(sema, config, def, &new_name, &mut source_change)?;
source_change.insert_source_edit(file_id, edit);
}
Ok(source_change)
Expand Down Expand Up @@ -555,6 +568,7 @@ fn source_edit_from_name_ref(

fn source_edit_from_def(
sema: &Semantics<'_, RootDatabase>,
config: &RenameConfig,
def: Definition,
new_name: &Name,
source_change: &mut SourceChange,
Expand All @@ -563,21 +577,22 @@ fn source_edit_from_def(
if let Definition::Local(local) = def {
let mut file_id = None;

let conflict_annotation = if !sema.rename_conflicts(&local, new_name).is_empty() {
Some(
source_change.insert_annotation(ChangeAnnotation {
label: "This rename will change the program's meaning".to_owned(),
needs_confirmation: true,
description: Some(
"Some variable(s) will shadow the renamed variable \
let conflict_annotation =
if config.show_conflicts && !sema.rename_conflicts(&local, new_name).is_empty() {
Some(
source_change.insert_annotation(ChangeAnnotation {
label: "This rename will change the program's meaning".to_owned(),
needs_confirmation: true,
description: Some(
"Some variable(s) will shadow the renamed variable \
or be shadowed by it if the rename is performed"
.to_owned(),
),
}),
)
} else {
None
};
.to_owned(),
),
}),
)
} else {
None
};

for source in local.sources(sema.db) {
let source = match source.source.clone().original_ast_node_rooted(sema.db) {
Expand Down
7 changes: 6 additions & 1 deletion crates/ide-diagnostics/src/handlers/incorrect_case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCase) -> Option<Vec<Ass
let label = format!("Rename to {}", d.suggested_text);
let mut res = unresolved_fix("change_case", &label, frange.range);
if ctx.resolve.should_resolve(&res.id) {
let source_change = def.rename(&ctx.sema, &d.suggested_text, RenameDefinition::Yes);
let source_change = def.rename(
&ctx.sema,
&ctx.config.rename_config,
&d.suggested_text,
RenameDefinition::Yes,
);
res.source_change = Some(source_change.ok().unwrap_or_default());
}

Expand Down
3 changes: 3 additions & 0 deletions crates/ide-diagnostics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ use ide_db::{
generated::lints::{CLIPPY_LINT_GROUPS, DEFAULT_LINT_GROUPS, DEFAULT_LINTS, Lint, LintGroup},
imports::insert_use::InsertUseConfig,
label::Label,
rename::RenameConfig,
source_change::SourceChange,
syntax_helpers::node_ext::parse_tt_as_comma_sep_paths,
};
Expand Down Expand Up @@ -236,6 +237,7 @@ pub struct DiagnosticsConfig {
pub prefer_absolute: bool,
pub term_search_fuel: u64,
pub term_search_borrowck: bool,
pub rename_config: RenameConfig,
}

impl DiagnosticsConfig {
Expand Down Expand Up @@ -264,6 +266,7 @@ impl DiagnosticsConfig {
prefer_absolute: false,
term_search_fuel: 400,
term_search_borrowck: true,
rename_config: RenameConfig { show_conflicts: true },
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ use ide_db::{
CrateOrigin, CrateWorkspaceData, Env, FileSet, RootQueryDb, SourceDatabase, VfsPath,
salsa::Cancelled,
},
prime_caches, symbol_index,
prime_caches,
rename::RenameConfig,
symbol_index,
};
use syntax::SourceFile;
use triomphe::Arc;
Expand Down Expand Up @@ -777,10 +779,11 @@ impl Analysis {
/// name.
pub fn rename(
&self,
config: &RenameConfig,
position: FilePosition,
new_name: &str,
) -> Cancellable<Result<SourceChange, RenameError>> {
self.with_db(|db| rename::rename(db, position, new_name))
self.with_db(|db| rename::rename(db, config, position, new_name))
}

pub fn prepare_rename(
Expand All @@ -792,10 +795,11 @@ impl Analysis {

pub fn will_rename_file(
&self,
config: &RenameConfig,
file_id: FileId,
new_name_stem: &str,
) -> Cancellable<Option<SourceChange>> {
self.with_db(|db| rename::will_rename_file(db, file_id, new_name_stem))
self.with_db(|db| rename::will_rename_file(db, config, file_id, new_name_stem))
}

pub fn structural_search_replace(
Expand Down
26 changes: 18 additions & 8 deletions crates/ide/src/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use hir::{AsAssocItem, InFile, Name, Semantics, sym};
use ide_db::{
FileId, FileRange, RootDatabase,
defs::{Definition, NameClass, NameRefClass},
rename::{IdentifierKind, RenameDefinition, bail, format_err, source_edit_from_references},
rename::{
IdentifierKind, RenameConfig, RenameDefinition, bail, format_err,
source_edit_from_references,
},
source_change::SourceChangeBuilder,
};
use itertools::Itertools;
Expand Down Expand Up @@ -78,6 +81,7 @@ pub(crate) fn prepare_rename(
// ![Rename](https://user-images.githubusercontent.com/48062697/113065582-055aae80-91b1-11eb-8ade-2b58e6d81883.gif)
pub(crate) fn rename(
db: &RootDatabase,
config: &RenameConfig,
position: FilePosition,
new_name: &str,
) -> RenameResult<SourceChange> {
Expand Down Expand Up @@ -145,7 +149,7 @@ pub(crate) fn rename(
return rename_to_self(&sema, local);
}
}
def.rename(&sema, new_name.as_str(), rename_def)
def.rename(&sema, config, new_name.as_str(), rename_def)
})
.collect(),
};
Expand All @@ -159,13 +163,14 @@ pub(crate) fn rename(
/// Called by the client when it is about to rename a file.
pub(crate) fn will_rename_file(
db: &RootDatabase,
config: &RenameConfig,
file_id: FileId,
new_name_stem: &str,
) -> Option<SourceChange> {
let sema = Semantics::new(db);
let module = sema.file_to_module_def(file_id)?;
let def = Definition::Module(module);
let mut change = def.rename(&sema, new_name_stem, RenameDefinition::Yes).ok()?;
let mut change = def.rename(&sema, config, new_name_stem, RenameDefinition::Yes).ok()?;
change.file_system_edits.clear();
Some(change)
}
Expand Down Expand Up @@ -493,6 +498,7 @@ fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: String) -> O
#[cfg(test)]
mod tests {
use expect_test::{Expect, expect};
use ide_db::rename::RenameConfig;
use ide_db::source_change::SourceChange;
use ide_db::text_edit::TextEdit;
use itertools::Itertools;
Expand All @@ -503,6 +509,8 @@ mod tests {

use super::{RangeInfo, RenameError};

const TEST_CONFIG: RenameConfig = RenameConfig { show_conflicts: true };

#[track_caller]
fn check(
new_name: &str,
Expand All @@ -517,7 +525,7 @@ mod tests {
}
}
let rename_result = analysis
.rename(position, new_name)
.rename(&TEST_CONFIG, position, new_name)
.unwrap_or_else(|err| panic!("Rename to '{new_name}' was cancelled: {err}"));
match rename_result {
Ok(source_change) => {
Expand Down Expand Up @@ -549,7 +557,7 @@ mod tests {
#[track_caller]
fn check_conflicts(new_name: &str, #[rust_analyzer::rust_fixture] ra_fixture: &str) {
let (analysis, position, conflicts) = fixture::annotations(ra_fixture);
let source_change = analysis.rename(position, new_name).unwrap().unwrap();
let source_change = analysis.rename(&TEST_CONFIG, position, new_name).unwrap().unwrap();
let expected_conflicts = conflicts
.into_iter()
.map(|(file_range, _)| (file_range.file_id, file_range.range))
Expand All @@ -576,8 +584,10 @@ mod tests {
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(&TEST_CONFIG, position, new_name)
.unwrap()
.expect("Expect returned a RenameError");
expect.assert_eq(&filter_expect(source_change))
}

Expand All @@ -588,7 +598,7 @@ mod tests {
) {
let (analysis, position) = fixture::position(ra_fixture);
let source_change = analysis
.will_rename_file(position.file_id, new_name)
.will_rename_file(&TEST_CONFIG, position.file_id, new_name)
.unwrap()
.expect("Expect returned a RenameError");
expect.assert_eq(&filter_expect(source_change))
Expand Down
2 changes: 2 additions & 0 deletions crates/rust-analyzer/src/cli/analysis_stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use ide::{
use ide_db::{
EditionedFileId, LineIndexDatabase, SnippetCap,
base_db::{SourceDatabase, salsa::Database},
rename::RenameConfig,
};
use itertools::Itertools;
use load_cargo::{LoadCargoConfig, ProcMacroServerChoice, load_workspace};
Expand Down Expand Up @@ -1140,6 +1141,7 @@ impl flags::AnalysisStats {
style_lints: false,
term_search_fuel: 400,
term_search_borrowck: true,
rename_config: RenameConfig { show_conflicts: true },
},
ide::AssistResolveStrategy::All,
analysis.editioned_file_id_to_vfs(file_id),
Expand Down
Loading