Skip to content

Commit dc91c0a

Browse files
Provide a setting to disable showing rename conflicts
1 parent f7a13f0 commit dc91c0a

File tree

14 files changed

+108
-28
lines changed

14 files changed

+108
-28
lines changed

crates/ide-assists/src/assist_config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use ide_db::{
99
SnippetCap,
1010
assists::ExprFillDefaultMode,
1111
imports::{import_assets::ImportPathConfig, insert_use::InsertUseConfig},
12+
rename::RenameConfig,
1213
};
1314

1415
use crate::AssistKind;
@@ -27,6 +28,7 @@ pub struct AssistConfig {
2728
pub code_action_grouping: bool,
2829
pub expr_fill_default: ExprFillDefaultMode,
2930
pub prefer_self_ty: bool,
31+
pub show_rename_conflicts: bool,
3032
}
3133

3234
impl AssistConfig {
@@ -46,4 +48,8 @@ impl AssistConfig {
4648
allow_unstable,
4749
}
4850
}
51+
52+
pub fn rename_config(&self) -> RenameConfig {
53+
RenameConfig { show_conflicts: self.show_rename_conflicts }
54+
}
4955
}

crates/ide-assists/src/handlers/remove_underscore.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ pub(crate) fn remove_underscore(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
6262
"Remove underscore from a used variable",
6363
text_range,
6464
|builder| {
65-
let changes = def.rename(&ctx.sema, new_name, RenameDefinition::Yes).unwrap();
65+
let changes = def
66+
.rename(&ctx.sema, new_name, RenameDefinition::Yes, &ctx.config.rename_config())
67+
.unwrap();
6668
builder.source_change = changes;
6769
},
6870
)

crates/ide-assists/src/tests.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
3838
code_action_grouping: true,
3939
expr_fill_default: ExprFillDefaultMode::Todo,
4040
prefer_self_ty: false,
41+
show_rename_conflicts: true,
4142
};
4243

4344
pub(crate) const TEST_CONFIG_NO_GROUPING: AssistConfig = AssistConfig {
@@ -59,6 +60,7 @@ pub(crate) const TEST_CONFIG_NO_GROUPING: AssistConfig = AssistConfig {
5960
code_action_grouping: false,
6061
expr_fill_default: ExprFillDefaultMode::Todo,
6162
prefer_self_ty: false,
63+
show_rename_conflicts: true,
6264
};
6365

6466
pub(crate) const TEST_CONFIG_NO_SNIPPET_CAP: AssistConfig = AssistConfig {
@@ -80,6 +82,7 @@ pub(crate) const TEST_CONFIG_NO_SNIPPET_CAP: AssistConfig = AssistConfig {
8082
code_action_grouping: true,
8183
expr_fill_default: ExprFillDefaultMode::Todo,
8284
prefer_self_ty: false,
85+
show_rename_conflicts: true,
8386
};
8487

8588
pub(crate) const TEST_CONFIG_IMPORT_ONE: AssistConfig = AssistConfig {
@@ -101,6 +104,7 @@ pub(crate) const TEST_CONFIG_IMPORT_ONE: AssistConfig = AssistConfig {
101104
code_action_grouping: true,
102105
expr_fill_default: ExprFillDefaultMode::Todo,
103106
prefer_self_ty: false,
107+
show_rename_conflicts: true,
104108
};
105109

106110
fn assists(

crates/ide-db/src/rename.rs

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ use crate::{
4545
traits::convert_to_def_in_trait,
4646
};
4747

48+
#[derive(Clone, Debug, PartialEq, Eq)]
49+
pub struct RenameConfig {
50+
pub show_conflicts: bool,
51+
}
52+
4853
pub type Result<T, E = RenameError> = std::result::Result<T, E>;
4954

5055
#[derive(Debug)]
@@ -81,6 +86,7 @@ impl Definition {
8186
sema: &Semantics<'_, RootDatabase>,
8287
new_name: &str,
8388
rename_definition: RenameDefinition,
89+
config: &RenameConfig,
8490
) -> Result<SourceChange> {
8591
// self.krate() returns None if
8692
// self is a built-in attr, built-in type or tool module.
@@ -109,10 +115,15 @@ impl Definition {
109115
bail!("Cannot rename a builtin attr.")
110116
}
111117
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
112-
Definition::Macro(mac) => {
113-
rename_reference(sema, Definition::Macro(mac), new_name, rename_definition, edition)
114-
}
115-
def => rename_reference(sema, def, new_name, rename_definition, edition),
118+
Definition::Macro(mac) => rename_reference(
119+
sema,
120+
Definition::Macro(mac),
121+
new_name,
122+
rename_definition,
123+
edition,
124+
config,
125+
),
126+
def => rename_reference(sema, def, new_name, rename_definition, edition, config),
116127
}
117128
}
118129

@@ -338,6 +349,7 @@ fn rename_reference(
338349
new_name: &str,
339350
rename_definition: RenameDefinition,
340351
edition: Edition,
352+
config: &RenameConfig,
341353
) -> Result<SourceChange> {
342354
let (mut new_name, ident_kind) = IdentifierKind::classify(edition, new_name)?;
343355

@@ -396,7 +408,8 @@ fn rename_reference(
396408
if rename_definition == RenameDefinition::Yes {
397409
// This needs to come after the references edits, because we change the annotation of existing edits
398410
// if a conflict is detected.
399-
let (file_id, edit) = source_edit_from_def(sema, def, &new_name, &mut source_change)?;
411+
let (file_id, edit) =
412+
source_edit_from_def(sema, config, def, &new_name, &mut source_change)?;
400413
source_change.insert_source_edit(file_id, edit);
401414
}
402415
Ok(source_change)
@@ -554,6 +567,7 @@ fn source_edit_from_name_ref(
554567

555568
fn source_edit_from_def(
556569
sema: &Semantics<'_, RootDatabase>,
570+
config: &RenameConfig,
557571
def: Definition,
558572
new_name: &Name,
559573
source_change: &mut SourceChange,
@@ -562,21 +576,22 @@ fn source_edit_from_def(
562576
if let Definition::Local(local) = def {
563577
let mut file_id = None;
564578

565-
let conflict_annotation = if !sema.rename_conflicts(&local, new_name).is_empty() {
566-
Some(
567-
source_change.insert_annotation(ChangeAnnotation {
568-
label: "This rename will change the program's meaning".to_owned(),
569-
needs_confirmation: true,
570-
description: Some(
571-
"Some variable(s) will shadow the renamed variable \
579+
let conflict_annotation =
580+
if config.show_conflicts && !sema.rename_conflicts(&local, new_name).is_empty() {
581+
Some(
582+
source_change.insert_annotation(ChangeAnnotation {
583+
label: "This rename will change the program's meaning".to_owned(),
584+
needs_confirmation: true,
585+
description: Some(
586+
"Some variable(s) will shadow the renamed variable \
572587
or be shadowed by it if the rename is performed"
573-
.to_owned(),
574-
),
575-
}),
576-
)
577-
} else {
578-
None
579-
};
588+
.to_owned(),
589+
),
590+
}),
591+
)
592+
} else {
593+
None
594+
};
580595

581596
for source in local.sources(sema.db) {
582597
let source = match source.source.clone().original_ast_node_rooted(sema.db) {

crates/ide-diagnostics/src/handlers/incorrect_case.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCase) -> Option<Vec<Ass
4444
let label = format!("Rename to {}", d.suggested_text);
4545
let mut res = unresolved_fix("change_case", &label, frange.range);
4646
if ctx.resolve.should_resolve(&res.id) {
47-
let source_change = def.rename(&ctx.sema, &d.suggested_text, RenameDefinition::Yes);
47+
let source_change = def.rename(
48+
&ctx.sema,
49+
&d.suggested_text,
50+
RenameDefinition::Yes,
51+
&ctx.config.rename_config(),
52+
);
4853
res.source_change = Some(source_change.ok().unwrap_or_default());
4954
}
5055

crates/ide-diagnostics/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ use ide_db::{
9696
generated::lints::{CLIPPY_LINT_GROUPS, DEFAULT_LINT_GROUPS, DEFAULT_LINTS, Lint, LintGroup},
9797
imports::insert_use::InsertUseConfig,
9898
label::Label,
99+
rename::RenameConfig,
99100
source_change::SourceChange,
100101
syntax_helpers::node_ext::parse_tt_as_comma_sep_paths,
101102
};
@@ -236,6 +237,7 @@ pub struct DiagnosticsConfig {
236237
pub prefer_absolute: bool,
237238
pub term_search_fuel: u64,
238239
pub term_search_borrowck: bool,
240+
pub show_rename_conflicts: bool,
239241
}
240242

241243
impl DiagnosticsConfig {
@@ -264,8 +266,13 @@ impl DiagnosticsConfig {
264266
prefer_absolute: false,
265267
term_search_fuel: 400,
266268
term_search_borrowck: true,
269+
show_rename_conflicts: true,
267270
}
268271
}
272+
273+
pub fn rename_config(&self) -> RenameConfig {
274+
RenameConfig { show_conflicts: self.show_rename_conflicts }
275+
}
269276
}
270277

271278
struct DiagnosticsContext<'a> {

crates/ide/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,8 +846,9 @@ impl Analysis {
846846
&self,
847847
file_id: FileId,
848848
new_name_stem: &str,
849+
config: &RenameConfig,
849850
) -> Cancellable<Option<SourceChange>> {
850-
self.with_db(|db| rename::will_rename_file(db, file_id, new_name_stem))
851+
self.with_db(|db| rename::will_rename_file(db, file_id, new_name_stem, config))
851852
}
852853

853854
pub fn structural_search_replace(

crates/ide/src/rename.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ pub struct RenameConfig {
3131
pub prefer_no_std: bool,
3232
pub prefer_prelude: bool,
3333
pub prefer_absolute: bool,
34+
pub show_conflicts: bool,
3435
}
3536

3637
impl RenameConfig {
@@ -42,6 +43,10 @@ impl RenameConfig {
4243
allow_unstable: true,
4344
}
4445
}
46+
47+
fn ide_db_config(&self) -> ide_db::rename::RenameConfig {
48+
ide_db::rename::RenameConfig { show_conflicts: self.show_conflicts }
49+
}
4550
}
4651

4752
/// This is similar to `collect::<Result<Vec<_>, _>>`, but unlike it, it succeeds if there is *any* `Ok` item.
@@ -190,7 +195,7 @@ pub(crate) fn rename(
190195
return rename_to_self(&sema, local);
191196
}
192197
}
193-
def.rename(&sema, new_name.as_str(), rename_def)
198+
def.rename(&sema, new_name.as_str(), rename_def, &config.ide_db_config())
194199
})),
195200
};
196201

@@ -205,11 +210,13 @@ pub(crate) fn will_rename_file(
205210
db: &RootDatabase,
206211
file_id: FileId,
207212
new_name_stem: &str,
213+
config: &RenameConfig,
208214
) -> Option<SourceChange> {
209215
let sema = Semantics::new(db);
210216
let module = sema.file_to_module_def(file_id)?;
211217
let def = Definition::Module(module);
212-
let mut change = def.rename(&sema, new_name_stem, RenameDefinition::Yes).ok()?;
218+
let mut change =
219+
def.rename(&sema, new_name_stem, RenameDefinition::Yes, &config.ide_db_config()).ok()?;
213220
change.file_system_edits.clear();
214221
Some(change)
215222
}
@@ -803,8 +810,12 @@ mod tests {
803810

804811
use super::{RangeInfo, RenameConfig, RenameError};
805812

806-
const TEST_CONFIG: RenameConfig =
807-
RenameConfig { prefer_no_std: false, prefer_prelude: true, prefer_absolute: false };
813+
const TEST_CONFIG: RenameConfig = RenameConfig {
814+
prefer_no_std: false,
815+
prefer_prelude: true,
816+
prefer_absolute: false,
817+
show_conflicts: true,
818+
};
808819

809820
#[track_caller]
810821
fn check(
@@ -893,7 +904,7 @@ mod tests {
893904
) {
894905
let (analysis, position) = fixture::position(ra_fixture);
895906
let source_change = analysis
896-
.will_rename_file(position.file_id, new_name)
907+
.will_rename_file(position.file_id, new_name, &TEST_CONFIG)
897908
.unwrap()
898909
.expect("Expect returned a RenameError");
899910
expect.assert_eq(&filter_expect(source_change))

crates/rust-analyzer/src/cli/analysis_stats.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1153,6 +1153,7 @@ impl flags::AnalysisStats {
11531153
style_lints: false,
11541154
term_search_fuel: 400,
11551155
term_search_borrowck: true,
1156+
show_rename_conflicts: true,
11561157
},
11571158
ide::AssistResolveStrategy::All,
11581159
analysis.editioned_file_id_to_vfs(file_id),

crates/rust-analyzer/src/config.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -707,6 +707,9 @@ config_data! {
707707
///
708708
/// E.g. `use ::std::io::Read;`.
709709
imports_prefixExternPrelude: bool = false,
710+
711+
/// Whether to warn when a rename will cause conflicts (change the meaning of the code).
712+
rename_showConflicts: bool = true,
710713
}
711714
}
712715

@@ -1702,6 +1705,7 @@ impl Config {
17021705
ExprFillDefaultDef::Underscore => ExprFillDefaultMode::Underscore,
17031706
},
17041707
prefer_self_ty: *self.assist_preferSelf(source_root),
1708+
show_rename_conflicts: *self.rename_showConflicts(source_root),
17051709
}
17061710
}
17071711

@@ -1710,6 +1714,7 @@ impl Config {
17101714
prefer_no_std: self.imports_preferNoStd(source_root).to_owned(),
17111715
prefer_prelude: self.imports_preferPrelude(source_root).to_owned(),
17121716
prefer_absolute: self.imports_prefixExternPrelude(source_root).to_owned(),
1717+
show_conflicts: *self.rename_showConflicts(source_root),
17131718
}
17141719
}
17151720

@@ -1809,6 +1814,7 @@ impl Config {
18091814
style_lints: self.diagnostics_styleLints_enable(source_root).to_owned(),
18101815
term_search_fuel: self.assist_termSearch_fuel(source_root).to_owned() as u64,
18111816
term_search_borrowck: self.assist_termSearch_borrowcheck(source_root).to_owned(),
1817+
show_rename_conflicts: *self.rename_showConflicts(source_root),
18121818
}
18131819
}
18141820

0 commit comments

Comments
 (0)