mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-28 22:13:39 +00:00
Merge #7332
7332: Rename reference search uses ide_db::search directly r=matklad a=Veykril https://github.com/rust-analyzer/rust-analyzer/pull/7251#discussion_r556644241 Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
commit
39282ec419
2 changed files with 211 additions and 165 deletions
|
@ -140,7 +140,7 @@ impl DiagnosticWithFix for IncorrectCase {
|
||||||
rename_with_semantics(sema, file_position, &self.suggested_text).ok()?;
|
rename_with_semantics(sema, file_position, &self.suggested_text).ok()?;
|
||||||
|
|
||||||
let label = format!("Rename to {}", self.suggested_text);
|
let label = format!("Rename to {}", self.suggested_text);
|
||||||
Some(Fix::new(&label, rename_changes.info, rename_changes.range))
|
Some(Fix::new(&label, rename_changes.info, frange.range))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,23 +1,24 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
|
|
||||||
use hir::{Module, ModuleDef, ModuleSource, Semantics};
|
use either::Either;
|
||||||
|
use hir::{HasSource, InFile, Module, ModuleDef, ModuleSource, Semantics};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::{AnchoredPathBuf, FileId, FileRange},
|
base_db::{AnchoredPathBuf, FileId, FileRange},
|
||||||
defs::{Definition, NameClass, NameRefClass},
|
defs::{Definition, NameClass, NameRefClass},
|
||||||
search::FileReference,
|
search::FileReference,
|
||||||
RootDatabase,
|
RootDatabase,
|
||||||
};
|
};
|
||||||
|
use stdx::assert_never;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
algo::find_node_at_offset,
|
|
||||||
ast::{self, NameOwner},
|
ast::{self, NameOwner},
|
||||||
lex_single_syntax_kind, match_ast, AstNode, SyntaxKind, SyntaxNode, T,
|
lex_single_syntax_kind, AstNode, SyntaxKind, SyntaxNode, T,
|
||||||
};
|
};
|
||||||
use test_utils::mark;
|
use test_utils::mark;
|
||||||
use text_edit::TextEdit;
|
use text_edit::TextEdit;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, ReferenceSearchResult, SourceChange,
|
display::TryToNav, FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, SourceChange,
|
||||||
TextRange,
|
TextRange,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -47,13 +48,15 @@ pub(crate) fn prepare_rename(
|
||||||
let sema = Semantics::new(db);
|
let sema = Semantics::new(db);
|
||||||
let source_file = sema.parse(position.file_id);
|
let source_file = sema.parse(position.file_id);
|
||||||
let syntax = source_file.syntax();
|
let syntax = source_file.syntax();
|
||||||
if let Some(module) = find_module_at_offset(&sema, position, syntax) {
|
let range = match &find_name_like(&sema, &syntax, position)
|
||||||
rename_mod(&sema, position, module, "dummy")
|
.ok_or_else(|| format_err!("No references found at position"))?
|
||||||
} else {
|
{
|
||||||
let RangeInfo { range, .. } = find_all_refs(&sema, position)?;
|
NameLike::Name(it) => it.syntax(),
|
||||||
Ok(RangeInfo::new(range, SourceChange::default()))
|
NameLike::NameRef(it) => it.syntax(),
|
||||||
|
NameLike::Lifetime(it) => it.syntax(),
|
||||||
}
|
}
|
||||||
.map(|info| RangeInfo::new(info.range, ()))
|
.text_range();
|
||||||
|
Ok(RangeInfo::new(range, ()))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn rename(
|
pub(crate) fn rename(
|
||||||
|
@ -73,10 +76,11 @@ pub(crate) fn rename_with_semantics(
|
||||||
let source_file = sema.parse(position.file_id);
|
let source_file = sema.parse(position.file_id);
|
||||||
let syntax = source_file.syntax();
|
let syntax = source_file.syntax();
|
||||||
|
|
||||||
if let Some(module) = find_module_at_offset(&sema, position, syntax) {
|
let def = find_definition(sema, syntax, position)
|
||||||
rename_mod(&sema, position, module, new_name)
|
.ok_or_else(|| format_err!("No references found at position"))?;
|
||||||
} else {
|
match def {
|
||||||
rename_reference(&sema, position, new_name)
|
Definition::ModuleDef(ModuleDef::Module(module)) => rename_mod(&sema, module, new_name),
|
||||||
|
def => rename_reference(sema, def, new_name),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,12 +91,7 @@ pub(crate) fn will_rename_file(
|
||||||
) -> Option<SourceChange> {
|
) -> Option<SourceChange> {
|
||||||
let sema = Semantics::new(db);
|
let sema = Semantics::new(db);
|
||||||
let module = sema.to_module_def(file_id)?;
|
let module = sema.to_module_def(file_id)?;
|
||||||
|
let mut change = rename_mod(&sema, module, new_name_stem).ok()?.info;
|
||||||
let decl = module.declaration_source(db)?;
|
|
||||||
let range = decl.value.name()?.syntax().text_range();
|
|
||||||
|
|
||||||
let position = FilePosition { file_id: decl.file_id.original_file(db), offset: range.start() };
|
|
||||||
let mut change = rename_mod(&sema, position, module, new_name_stem).ok()?.info;
|
|
||||||
change.file_system_edits.clear();
|
change.file_system_edits.clear();
|
||||||
Some(change)
|
Some(change)
|
||||||
}
|
}
|
||||||
|
@ -124,40 +123,51 @@ fn check_identifier(new_name: &str) -> RenameResult<IdentifierKind> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_module_at_offset(
|
enum NameLike {
|
||||||
sema: &Semantics<RootDatabase>,
|
Name(ast::Name),
|
||||||
position: FilePosition,
|
NameRef(ast::NameRef),
|
||||||
syntax: &SyntaxNode,
|
Lifetime(ast::Lifetime),
|
||||||
) -> Option<Module> {
|
|
||||||
let ident = syntax.token_at_offset(position.offset).find(|t| t.kind() == SyntaxKind::IDENT)?;
|
|
||||||
|
|
||||||
let module = match_ast! {
|
|
||||||
match (ident.parent()) {
|
|
||||||
ast::NameRef(name_ref) => {
|
|
||||||
match NameRefClass::classify(sema, &name_ref)? {
|
|
||||||
NameRefClass::Definition(Definition::ModuleDef(ModuleDef::Module(module))) => module,
|
|
||||||
_ => return None,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
ast::Name(name) => {
|
|
||||||
match NameClass::classify(&sema, &name)? {
|
|
||||||
NameClass::Definition(Definition::ModuleDef(ModuleDef::Module(module))) => module,
|
|
||||||
_ => return None,
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => return None,
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Some(module)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn find_all_refs(
|
fn find_name_like(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
|
syntax: &SyntaxNode,
|
||||||
position: FilePosition,
|
position: FilePosition,
|
||||||
) -> RenameResult<RangeInfo<ReferenceSearchResult>> {
|
) -> Option<NameLike> {
|
||||||
crate::references::find_all_refs(sema, position, None)
|
let namelike = if let Some(name_ref) =
|
||||||
.ok_or_else(|| format_err!("No references found at position"))
|
sema.find_node_at_offset_with_descend::<ast::NameRef>(syntax, position.offset)
|
||||||
|
{
|
||||||
|
NameLike::NameRef(name_ref)
|
||||||
|
} else if let Some(name) =
|
||||||
|
sema.find_node_at_offset_with_descend::<ast::Name>(syntax, position.offset)
|
||||||
|
{
|
||||||
|
NameLike::Name(name)
|
||||||
|
} else if let Some(lifetime) =
|
||||||
|
sema.find_node_at_offset_with_descend::<ast::Lifetime>(syntax, position.offset)
|
||||||
|
{
|
||||||
|
NameLike::Lifetime(lifetime)
|
||||||
|
} else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
Some(namelike)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_definition(
|
||||||
|
sema: &Semantics<RootDatabase>,
|
||||||
|
syntax: &SyntaxNode,
|
||||||
|
position: FilePosition,
|
||||||
|
) -> Option<Definition> {
|
||||||
|
let def = match find_name_like(sema, syntax, position)? {
|
||||||
|
NameLike::Name(name) => NameClass::classify(sema, &name)?.referenced_or_defined(sema.db),
|
||||||
|
NameLike::NameRef(name_ref) => NameRefClass::classify(sema, &name_ref)?.referenced(sema.db),
|
||||||
|
NameLike::Lifetime(lifetime) => NameRefClass::classify_lifetime(sema, &lifetime)
|
||||||
|
.map(|class| NameRefClass::referenced(class, sema.db))
|
||||||
|
.or_else(|| {
|
||||||
|
NameClass::classify_lifetime(sema, &lifetime)
|
||||||
|
.map(|it| it.referenced_or_defined(sema.db))
|
||||||
|
})?,
|
||||||
|
};
|
||||||
|
Some(def)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn source_edit_from_references(
|
fn source_edit_from_references(
|
||||||
|
@ -231,7 +241,6 @@ fn edit_text_range_for_record_field_expr_or_pat(
|
||||||
|
|
||||||
fn rename_mod(
|
fn rename_mod(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
position: FilePosition,
|
|
||||||
module: Module,
|
module: Module,
|
||||||
new_name: &str,
|
new_name: &str,
|
||||||
) -> RenameResult<RangeInfo<SourceChange>> {
|
) -> RenameResult<RangeInfo<SourceChange>> {
|
||||||
|
@ -241,10 +250,9 @@ fn rename_mod(
|
||||||
|
|
||||||
let mut source_change = SourceChange::default();
|
let mut source_change = SourceChange::default();
|
||||||
|
|
||||||
let src = module.definition_source(sema.db);
|
let InFile { file_id, value: def_source } = module.definition_source(sema.db);
|
||||||
let file_id = src.file_id.original_file(sema.db);
|
let file_id = file_id.original_file(sema.db);
|
||||||
match src.value {
|
if let ModuleSource::SourceFile(..) = def_source {
|
||||||
ModuleSource::SourceFile(..) => {
|
|
||||||
// mod is defined in path/to/dir/mod.rs
|
// mod is defined in path/to/dir/mod.rs
|
||||||
let path = if module.is_mod_rs(sema.db) {
|
let path = if module.is_mod_rs(sema.db) {
|
||||||
format!("../{}/mod.rs", new_name)
|
format!("../{}/mod.rs", new_name)
|
||||||
|
@ -255,48 +263,65 @@ fn rename_mod(
|
||||||
let move_file = FileSystemEdit::MoveFile { src: file_id, dst };
|
let move_file = FileSystemEdit::MoveFile { src: file_id, dst };
|
||||||
source_change.push_file_system_edit(move_file);
|
source_change.push_file_system_edit(move_file);
|
||||||
}
|
}
|
||||||
ModuleSource::Module(..) => {}
|
|
||||||
}
|
|
||||||
|
|
||||||
if let Some(src) = module.declaration_source(sema.db) {
|
if let Some(InFile { file_id, value: decl_source }) = module.declaration_source(sema.db) {
|
||||||
let file_id = src.file_id.original_file(sema.db);
|
let file_id = file_id.original_file(sema.db);
|
||||||
let name = src.value.name().unwrap();
|
match decl_source.name() {
|
||||||
source_change.insert_source_edit(
|
Some(name) => source_change.insert_source_edit(
|
||||||
file_id,
|
file_id,
|
||||||
TextEdit::replace(name.syntax().text_range(), new_name.into()),
|
TextEdit::replace(name.syntax().text_range(), new_name.to_string()),
|
||||||
);
|
),
|
||||||
|
_ => unreachable!(),
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
let def = Definition::ModuleDef(ModuleDef::Module(module));
|
||||||
let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
|
let usages = def.usages(sema).all();
|
||||||
let ref_edits = refs.references().iter().map(|(&file_id, references)| {
|
let ref_edits = usages.iter().map(|(&file_id, references)| {
|
||||||
source_edit_from_references(sema, file_id, references, new_name)
|
source_edit_from_references(sema, file_id, references, new_name)
|
||||||
});
|
});
|
||||||
source_change.extend(ref_edits);
|
source_change.extend(ref_edits);
|
||||||
|
|
||||||
Ok(RangeInfo::new(range, source_change))
|
Ok(RangeInfo::new(TextRange::default(), source_change))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename_to_self(
|
fn rename_to_self(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
position: FilePosition,
|
local: hir::Local,
|
||||||
) -> Result<RangeInfo<SourceChange>, RenameError> {
|
) -> RenameResult<RangeInfo<SourceChange>> {
|
||||||
let source_file = sema.parse(position.file_id);
|
if assert_never!(local.is_self(sema.db)) {
|
||||||
let syn = source_file.syntax();
|
bail!("rename_to_self invoked on self");
|
||||||
|
}
|
||||||
|
|
||||||
let (fn_def, fn_ast) = find_node_at_offset::<ast::Fn>(syn, position.offset)
|
let fn_def = match local.parent(sema.db) {
|
||||||
.and_then(|fn_ast| sema.to_def(&fn_ast).zip(Some(fn_ast)))
|
hir::DefWithBody::Function(func) => func,
|
||||||
.ok_or_else(|| format_err!("No surrounding method declaration found"))?;
|
_ => bail!("Cannot rename non-param local to self"),
|
||||||
let param_range = fn_ast
|
};
|
||||||
|
|
||||||
|
// FIXME: reimplement this on the hir instead
|
||||||
|
// as of the time of this writing params in hir don't keep their names
|
||||||
|
let fn_ast =
|
||||||
|
fn_def.source(sema.db).ok_or(format_err!("Cannot rename non-param local to self"))?.value;
|
||||||
|
|
||||||
|
let first_param_range = fn_ast
|
||||||
.param_list()
|
.param_list()
|
||||||
.and_then(|p| p.params().next())
|
.and_then(|p| p.params().next())
|
||||||
.ok_or_else(|| format_err!("Method has no parameters"))?
|
.ok_or_else(|| format_err!("Method has no parameters"))?
|
||||||
.syntax()
|
.syntax()
|
||||||
.text_range();
|
.text_range();
|
||||||
if !param_range.contains(position.offset) {
|
let InFile { file_id, value: local_source } = local.source(sema.db);
|
||||||
|
match local_source {
|
||||||
|
either::Either::Left(pat)
|
||||||
|
if !first_param_range.contains_range(pat.syntax().text_range()) =>
|
||||||
|
{
|
||||||
bail!("Only the first parameter can be self");
|
bail!("Only the first parameter can be self");
|
||||||
}
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
|
||||||
let impl_block = find_node_at_offset::<ast::Impl>(syn, position.offset)
|
let impl_block = fn_ast
|
||||||
|
.syntax()
|
||||||
|
.ancestors()
|
||||||
|
.find_map(|node| ast::Impl::cast(node))
|
||||||
.and_then(|def| sema.to_def(&def))
|
.and_then(|def| sema.to_def(&def))
|
||||||
.ok_or_else(|| format_err!("No impl block found for function"))?;
|
.ok_or_else(|| format_err!("No impl block found for function"))?;
|
||||||
if fn_def.self_param(sema.db).is_some() {
|
if fn_def.self_param(sema.db).is_some() {
|
||||||
|
@ -320,25 +345,21 @@ fn rename_to_self(
|
||||||
bail!("Parameter type differs from impl block type");
|
bail!("Parameter type differs from impl block type");
|
||||||
}
|
}
|
||||||
|
|
||||||
let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
|
let def = Definition::Local(local);
|
||||||
|
let usages = def.usages(sema).all();
|
||||||
let mut source_change = SourceChange::default();
|
let mut source_change = SourceChange::default();
|
||||||
source_change.extend(refs.references().iter().map(|(&file_id, references)| {
|
source_change.extend(usages.iter().map(|(&file_id, references)| {
|
||||||
source_edit_from_references(sema, file_id, references, "self")
|
source_edit_from_references(sema, file_id, references, "self")
|
||||||
}));
|
}));
|
||||||
source_change.insert_source_edit(
|
source_change.insert_source_edit(
|
||||||
position.file_id,
|
file_id.original_file(sema.db),
|
||||||
TextEdit::replace(param_range, String::from(self_param)),
|
TextEdit::replace(first_param_range, String::from(self_param)),
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(RangeInfo::new(range, source_change))
|
Ok(RangeInfo::new(TextRange::default(), source_change))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn text_edit_from_self_param(
|
fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Option<TextEdit> {
|
||||||
syn: &SyntaxNode,
|
|
||||||
self_param: &ast::SelfParam,
|
|
||||||
new_name: &str,
|
|
||||||
) -> Option<TextEdit> {
|
|
||||||
fn target_type_name(impl_def: &ast::Impl) -> Option<String> {
|
fn target_type_name(impl_def: &ast::Impl) -> Option<String> {
|
||||||
if let Some(ast::Type::PathType(p)) = impl_def.self_ty() {
|
if let Some(ast::Type::PathType(p)) = impl_def.self_ty() {
|
||||||
return Some(p.path()?.segment()?.name_ref()?.text().to_string());
|
return Some(p.path()?.segment()?.name_ref()?.text().to_string());
|
||||||
|
@ -346,7 +367,7 @@ fn text_edit_from_self_param(
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
let impl_def = find_node_at_offset::<ast::Impl>(syn, self_param.syntax().text_range().start())?;
|
let impl_def = self_param.syntax().ancestors().find_map(|it| ast::Impl::cast(it))?;
|
||||||
let type_name = target_type_name(&impl_def)?;
|
let type_name = target_type_name(&impl_def)?;
|
||||||
|
|
||||||
let mut replacement_text = String::from(new_name);
|
let mut replacement_text = String::from(new_name);
|
||||||
|
@ -363,94 +384,119 @@ fn text_edit_from_self_param(
|
||||||
|
|
||||||
fn rename_self_to_param(
|
fn rename_self_to_param(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
position: FilePosition,
|
local: hir::Local,
|
||||||
new_name: &str,
|
new_name: &str,
|
||||||
ident_kind: IdentifierKind,
|
identifier_kind: IdentifierKind,
|
||||||
range: TextRange,
|
) -> RenameResult<RangeInfo<SourceChange>> {
|
||||||
refs: ReferenceSearchResult,
|
let (file_id, self_param) = match local.source(sema.db) {
|
||||||
) -> Result<RangeInfo<SourceChange>, RenameError> {
|
InFile { file_id, value: Either::Right(self_param) } => (file_id, self_param),
|
||||||
match ident_kind {
|
_ => {
|
||||||
IdentifierKind::Lifetime => bail!("Invalid name `{}`: not an identifier", new_name),
|
assert_never!(true, "rename_self_to_param invoked on a non-self local");
|
||||||
IdentifierKind::ToSelf => {
|
bail!("rename_self_to_param invoked on a non-self local");
|
||||||
// no-op
|
|
||||||
mark::hit!(rename_self_to_self);
|
|
||||||
return Ok(RangeInfo::new(range, SourceChange::default()));
|
|
||||||
}
|
}
|
||||||
_ => (),
|
};
|
||||||
}
|
|
||||||
let source_file = sema.parse(position.file_id);
|
|
||||||
let syn = source_file.syntax();
|
|
||||||
|
|
||||||
let fn_def = find_node_at_offset::<ast::Fn>(syn, position.offset)
|
let def = Definition::Local(local);
|
||||||
.ok_or_else(|| format_err!("No surrounding method declaration found"))?;
|
let usages = def.usages(sema).all();
|
||||||
|
let edit = text_edit_from_self_param(&self_param, new_name)
|
||||||
let mut source_change = SourceChange::default();
|
|
||||||
if let Some(self_param) = fn_def.param_list().and_then(|it| it.self_param()) {
|
|
||||||
if self_param
|
|
||||||
.syntax()
|
|
||||||
.text_range()
|
|
||||||
.contains_range(refs.declaration().nav.focus_or_full_range())
|
|
||||||
{
|
|
||||||
let edit = 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"))?;
|
||||||
source_change.insert_source_edit(position.file_id, edit);
|
if usages.len() > 1 && identifier_kind == IdentifierKind::Underscore {
|
||||||
|
|
||||||
source_change.extend(refs.references().iter().map(|(&file_id, references)| {
|
|
||||||
source_edit_from_references(sema, file_id, &references, new_name)
|
|
||||||
}));
|
|
||||||
|
|
||||||
if source_change.source_file_edits.len() > 1 && ident_kind == IdentifierKind::Underscore
|
|
||||||
{
|
|
||||||
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
|
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
|
||||||
}
|
}
|
||||||
|
let mut source_change = SourceChange::default();
|
||||||
return Ok(RangeInfo::new(range, source_change));
|
source_change.insert_source_edit(file_id.original_file(sema.db), edit);
|
||||||
}
|
source_change.extend(usages.iter().map(|(&file_id, references)| {
|
||||||
}
|
source_edit_from_references(sema, file_id, &references, new_name)
|
||||||
Err(format_err!("Method has no self param"))
|
}));
|
||||||
|
Ok(RangeInfo::new(TextRange::default(), source_change))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rename_reference(
|
fn rename_reference(
|
||||||
sema: &Semantics<RootDatabase>,
|
sema: &Semantics<RootDatabase>,
|
||||||
position: FilePosition,
|
def: Definition,
|
||||||
new_name: &str,
|
new_name: &str,
|
||||||
) -> Result<RangeInfo<SourceChange>, RenameError> {
|
) -> RenameResult<RangeInfo<SourceChange>> {
|
||||||
let ident_kind = check_identifier(new_name)?;
|
let ident_kind = check_identifier(new_name)?;
|
||||||
let RangeInfo { range, info: refs } = find_all_refs(sema, position)?;
|
|
||||||
|
|
||||||
match (ident_kind, &refs.declaration.kind) {
|
let def_is_lbl_or_lt = matches!(def,
|
||||||
(IdentifierKind::ToSelf, ReferenceKind::Lifetime)
|
Definition::GenericParam(hir::GenericParam::LifetimeParam(_))
|
||||||
| (IdentifierKind::Underscore, ReferenceKind::Lifetime)
|
| Definition::Label(_)
|
||||||
| (IdentifierKind::Ident, ReferenceKind::Lifetime) => {
|
);
|
||||||
|
match (ident_kind, def) {
|
||||||
|
(IdentifierKind::ToSelf, _)
|
||||||
|
| (IdentifierKind::Underscore, _)
|
||||||
|
| (IdentifierKind::Ident, _)
|
||||||
|
if def_is_lbl_or_lt =>
|
||||||
|
{
|
||||||
mark::hit!(rename_not_a_lifetime_ident_ref);
|
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) => mark::hit!(rename_lifetime),
|
(IdentifierKind::Lifetime, _) if def_is_lbl_or_lt => mark::hit!(rename_lifetime),
|
||||||
(IdentifierKind::Lifetime, _) => {
|
(IdentifierKind::Lifetime, _) => {
|
||||||
mark::hit!(rename_not_an_ident_ref);
|
mark::hit!(rename_not_an_ident_ref);
|
||||||
bail!("Invalid name `{}`: not an identifier", new_name)
|
bail!("Invalid name `{}`: not an identifier", new_name)
|
||||||
}
|
}
|
||||||
(_, ReferenceKind::SelfParam) => {
|
(IdentifierKind::ToSelf, Definition::Local(local)) if local.is_self(sema.db) => {
|
||||||
|
// no-op
|
||||||
|
mark::hit!(rename_self_to_self);
|
||||||
|
return Ok(RangeInfo::new(TextRange::default(), SourceChange::default()));
|
||||||
|
}
|
||||||
|
(ident_kind, Definition::Local(local)) if local.is_self(sema.db) => {
|
||||||
mark::hit!(rename_self_to_param);
|
mark::hit!(rename_self_to_param);
|
||||||
return rename_self_to_param(sema, position, new_name, ident_kind, range, refs);
|
return rename_self_to_param(sema, local, new_name, ident_kind);
|
||||||
}
|
}
|
||||||
(IdentifierKind::ToSelf, _) => {
|
(IdentifierKind::ToSelf, Definition::Local(local)) => {
|
||||||
mark::hit!(rename_to_self);
|
mark::hit!(rename_to_self);
|
||||||
return rename_to_self(sema, position);
|
return rename_to_self(sema, local);
|
||||||
}
|
|
||||||
(IdentifierKind::Underscore, _) if !refs.references.is_empty() => {
|
|
||||||
mark::hit!(rename_underscore_multiple);
|
|
||||||
bail!("Cannot rename reference to `_` as it is being referenced multiple times")
|
|
||||||
}
|
}
|
||||||
|
(IdentifierKind::ToSelf, _) => bail!("Invalid name `{}`: not an identifier", new_name),
|
||||||
(IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident),
|
(IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let usages = def.usages(sema).all();
|
||||||
|
if !usages.is_empty() && ident_kind == IdentifierKind::Underscore {
|
||||||
|
mark::hit!(rename_underscore_multiple);
|
||||||
|
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
|
||||||
|
}
|
||||||
let mut source_change = SourceChange::default();
|
let mut source_change = SourceChange::default();
|
||||||
source_change.extend(refs.into_iter().map(|(file_id, references)| {
|
source_change.extend(usages.iter().map(|(&file_id, references)| {
|
||||||
source_edit_from_references(sema, file_id, &references, new_name)
|
source_edit_from_references(sema, file_id, &references, new_name)
|
||||||
}));
|
}));
|
||||||
|
|
||||||
Ok(RangeInfo::new(range, source_change))
|
let (file_id, edit) = source_edit_from_def(sema, def, new_name)?;
|
||||||
|
source_change.insert_source_edit(file_id, edit);
|
||||||
|
Ok(RangeInfo::new(TextRange::default(), source_change))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn source_edit_from_def(
|
||||||
|
sema: &Semantics<RootDatabase>,
|
||||||
|
def: Definition,
|
||||||
|
new_name: &str,
|
||||||
|
) -> RenameResult<(FileId, TextEdit)> {
|
||||||
|
let nav = def.try_to_nav(sema.db).unwrap();
|
||||||
|
|
||||||
|
let mut replacement_text = String::new();
|
||||||
|
let mut repl_range = nav.focus_or_full_range();
|
||||||
|
if let Definition::Local(local) = def {
|
||||||
|
if let Either::Left(pat) = local.source(sema.db).value {
|
||||||
|
if matches!(
|
||||||
|
pat.syntax().parent().and_then(ast::RecordPatField::cast),
|
||||||
|
Some(pat_field) if pat_field.name_ref().is_none()
|
||||||
|
) {
|
||||||
|
replacement_text.push_str(": ");
|
||||||
|
replacement_text.push_str(new_name);
|
||||||
|
repl_range = TextRange::new(
|
||||||
|
pat.syntax().text_range().end(),
|
||||||
|
pat.syntax().text_range().end(),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if replacement_text.is_empty() {
|
||||||
|
replacement_text.push_str(new_name);
|
||||||
|
}
|
||||||
|
let edit = TextEdit::replace(repl_range, replacement_text);
|
||||||
|
Ok((nav.file_id, edit))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -872,7 +918,7 @@ mod foo$0;
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
RangeInfo {
|
RangeInfo {
|
||||||
range: 4..7,
|
range: 0..0,
|
||||||
info: SourceChange {
|
info: SourceChange {
|
||||||
source_file_edits: {
|
source_file_edits: {
|
||||||
FileId(
|
FileId(
|
||||||
|
@ -924,7 +970,7 @@ use crate::foo$0::FooContent;
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
RangeInfo {
|
RangeInfo {
|
||||||
range: 11..14,
|
range: 0..0,
|
||||||
info: SourceChange {
|
info: SourceChange {
|
||||||
source_file_edits: {
|
source_file_edits: {
|
||||||
FileId(
|
FileId(
|
||||||
|
@ -980,7 +1026,7 @@ mod fo$0o;
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
RangeInfo {
|
RangeInfo {
|
||||||
range: 4..7,
|
range: 0..0,
|
||||||
info: SourceChange {
|
info: SourceChange {
|
||||||
source_file_edits: {
|
source_file_edits: {
|
||||||
FileId(
|
FileId(
|
||||||
|
@ -1027,7 +1073,7 @@ mod outer { mod fo$0o; }
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
RangeInfo {
|
RangeInfo {
|
||||||
range: 16..19,
|
range: 0..0,
|
||||||
info: SourceChange {
|
info: SourceChange {
|
||||||
source_file_edits: {
|
source_file_edits: {
|
||||||
FileId(
|
FileId(
|
||||||
|
@ -1097,7 +1143,7 @@ pub mod foo$0;
|
||||||
"#,
|
"#,
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
RangeInfo {
|
RangeInfo {
|
||||||
range: 8..11,
|
range: 0..0,
|
||||||
info: SourceChange {
|
info: SourceChange {
|
||||||
source_file_edits: {
|
source_file_edits: {
|
||||||
FileId(
|
FileId(
|
||||||
|
|
Loading…
Reference in a new issue