Skip to content

Commit 4d7b1a4

Browse files
Veykrilshepmaster
andcommitted
Enhance renaming to include identifiers that are generated from the original symbol
Co-authored-by: Jake Goulding <[email protected]>
1 parent 1099b63 commit 4d7b1a4

File tree

6 files changed

+251
-44
lines changed

6 files changed

+251
-44
lines changed

src/tools/rust-analyzer/crates/hir/src/semantics.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,21 @@ impl<DB: HirDatabase> Semantics<'_, DB> {
222222
self.imp.descend_node_at_offset(node, offset).filter_map(|mut it| it.find_map(N::cast))
223223
}
224224

225+
// FIXME: Rethink this API
226+
pub fn find_namelike_at_offset_with_descend<'slf>(
227+
&'slf self,
228+
node: &SyntaxNode,
229+
offset: TextSize,
230+
) -> impl Iterator<Item = ast::NameLike> + 'slf {
231+
node.token_at_offset(offset)
232+
.map(move |token| self.descend_into_macros_no_opaque(token))
233+
.map(|descendants| descendants.into_iter().filter_map(move |it| it.value.parent()))
234+
// re-order the tokens from token_at_offset by returning the ancestors with the smaller first nodes first
235+
// See algo::ancestors_at_offset, which uses the same approach
236+
.kmerge_by(|left, right| left.text_range().len().lt(&right.text_range().len()))
237+
.filter_map(ast::NameLike::cast)
238+
}
239+
225240
pub fn resolve_range_pat(&self, range_pat: &ast::RangePat) -> Option<Struct> {
226241
self.imp.resolve_range_pat(range_pat).map(Struct::from)
227242
}

src/tools/rust-analyzer/crates/ide-assists/src/handlers/remove_underscore.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use ide_db::{
22
assists::AssistId,
33
defs::{Definition, NameClass, NameRefClass},
4+
rename::RenameDefinition,
45
};
56
use syntax::{AstNode, ast};
67

@@ -61,7 +62,7 @@ pub(crate) fn remove_underscore(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
6162
"Remove underscore from a used variable",
6263
text_range,
6364
|builder| {
64-
let changes = def.rename(&ctx.sema, new_name).unwrap();
65+
let changes = def.rename(&ctx.sema, new_name, RenameDefinition::Yes).unwrap();
6566
builder.source_change = changes;
6667
},
6768
)

src/tools/rust-analyzer/crates/ide-db/src/rename.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,19 @@ macro_rules! _bail {
7070
}
7171
pub use _bail as bail;
7272

73+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
74+
pub enum RenameDefinition {
75+
Yes,
76+
No,
77+
}
78+
7379
impl Definition {
7480
pub fn rename(
7581
&self,
7682
sema: &Semantics<'_, RootDatabase>,
7783
new_name: &str,
84+
rename_definition: RenameDefinition,
7885
) -> Result<SourceChange> {
79-
// We append `r#` if needed.
80-
let new_name = new_name.trim_start_matches("r#");
81-
8286
// self.krate() returns None if
8387
// self is a built-in attr, built-in type or tool module.
8488
// it is not allowed for these defs to be renamed.
@@ -103,8 +107,10 @@ impl Definition {
103107
bail!("Cannot rename a builtin attr.")
104108
}
105109
Definition::SelfType(_) => bail!("Cannot rename `Self`"),
106-
Definition::Macro(mac) => rename_reference(sema, Definition::Macro(mac), new_name),
107-
def => rename_reference(sema, def, new_name),
110+
Definition::Macro(mac) => {
111+
rename_reference(sema, Definition::Macro(mac), new_name, rename_definition)
112+
}
113+
def => rename_reference(sema, def, new_name, rename_definition),
108114
}
109115
}
110116

@@ -328,6 +334,7 @@ fn rename_reference(
328334
sema: &Semantics<'_, RootDatabase>,
329335
def: Definition,
330336
new_name: &str,
337+
rename_definition: RenameDefinition,
331338
) -> Result<SourceChange> {
332339
let ident_kind = IdentifierKind::classify(new_name)?;
333340

@@ -366,11 +373,12 @@ fn rename_reference(
366373
source_edit_from_references(references, def, new_name, file_id.edition(sema.db)),
367374
)
368375
}));
369-
370-
// This needs to come after the references edits, because we change the annotation of existing edits
371-
// if a conflict is detected.
372-
let (file_id, edit) = source_edit_from_def(sema, def, new_name, &mut source_change)?;
373-
source_change.insert_source_edit(file_id, edit);
376+
if rename_definition == RenameDefinition::Yes {
377+
// This needs to come after the references edits, because we change the annotation of existing edits
378+
// if a conflict is detected.
379+
let (file_id, edit) = source_edit_from_def(sema, def, new_name, &mut source_change)?;
380+
source_change.insert_source_edit(file_id, edit);
381+
}
374382
Ok(source_change)
375383
}
376384

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_case.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use hir::{CaseType, InFile, db::ExpandDatabase};
2-
use ide_db::{assists::Assist, defs::NameClass};
2+
use ide_db::{assists::Assist, defs::NameClass, rename::RenameDefinition};
33
use syntax::AstNode;
44

55
use crate::{
@@ -44,7 +44,7 @@ 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);
47+
let source_change = def.rename(&ctx.sema, &d.suggested_text, RenameDefinition::Yes);
4848
res.source_change = Some(source_change.ok().unwrap_or_default());
4949
}
5050

0 commit comments

Comments
 (0)