From 26c978f258ed2af45a6979eefea9860c1eaeacda Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 14 Jun 2021 18:46:27 +0300 Subject: internal: adapt diagnostics to the new rename API --- crates/ide/src/references/rename.rs | 435 +------------------------- crates/ide_db/src/lib.rs | 4 +- crates/ide_db/src/rename.rs | 444 +++++++++++++++++++++++++++ crates/ide_diagnostics/src/incorrect_case.rs | 46 +-- 4 files changed, 474 insertions(+), 455 deletions(-) create mode 100644 crates/ide_db/src/rename.rs (limited to 'crates') diff --git a/crates/ide/src/references/rename.rs b/crates/ide/src/references/rename.rs index 6b3d02bf4..88b6b1260 100644 --- a/crates/ide/src/references/rename.rs +++ b/crates/ide/src/references/rename.rs @@ -2,44 +2,23 @@ //! //! All reference and file rename requests go through here where the corresponding [`SourceChange`]s //! will be calculated. -use std::fmt::{self, Display}; - -use either::Either; -use hir::{AsAssocItem, FieldSource, HasSource, InFile, ModuleSource, Semantics}; +use hir::{AsAssocItem, InFile, Semantics}; use ide_db::{ - base_db::{AnchoredPathBuf, FileId, FileRange}, + base_db::FileId, defs::{Definition, NameClass, NameRefClass}, - search::FileReference, + rename::{bail, format_err, source_edit_from_references, IdentifierKind}, RootDatabase, }; use stdx::never; -use syntax::{ - ast::{self, NameOwner}, - lex_single_syntax_kind, AstNode, SyntaxKind, SyntaxNode, T, -}; +use syntax::{ast, AstNode, SyntaxNode}; use text_edit::TextEdit; -use crate::{FilePosition, FileSystemEdit, RangeInfo, SourceChange, TextRange}; - -type RenameResult = Result; -#[derive(Debug)] -pub struct RenameError(String); - -impl fmt::Display for RenameError { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - Display::fmt(&self.0, f) - } -} +use crate::{FilePosition, RangeInfo, SourceChange}; -macro_rules! format_err { - ($fmt:expr) => {RenameError(format!($fmt))}; - ($fmt:expr, $($arg:tt)+) => {RenameError(format!($fmt, $($arg)+))} -} +pub use ide_db::rename::RenameError; -macro_rules! bail { - ($($tokens:tt)*) => {return Err(format_err!($($tokens)*))} -} +type RenameResult = Result; /// Prepares a rename. The sole job of this function is to return the TextRange of the thing that is /// being targeted for a rename. @@ -52,8 +31,8 @@ pub(crate) fn prepare_rename( let syntax = source_file.syntax(); let def = find_definition(&sema, syntax, position)?; - let frange = def_name_range(&&sema, def) - .ok_or_else(|| format_err!("No references found at position"))?; + let frange = + def.rename_range(&sema).ok_or_else(|| format_err!("No references found at position"))?; Ok(RangeInfo::new(frange.range, ())) } @@ -98,14 +77,7 @@ pub(crate) fn rename_with_semantics( } } - match def { - Definition::ModuleDef(hir::ModuleDef::Module(module)) => rename_mod(sema, module, new_name), - Definition::SelfType(_) => bail!("Cannot rename `Self`"), - Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => { - bail!("Cannot rename builtin type") - } - def => rename_reference(sema, def, new_name), - } + def.rename(sema, new_name) } /// Called by the client when it is about to rename a file. @@ -116,38 +88,12 @@ pub(crate) fn will_rename_file( ) -> Option { let sema = Semantics::new(db); let module = sema.to_module_def(file_id)?; - let mut change = rename_mod(&sema, module, new_name_stem).ok()?; + let def = Definition::ModuleDef(module.into()); + let mut change = def.rename(&sema, new_name_stem).ok()?; change.file_system_edits.clear(); Some(change) } -#[derive(Copy, Clone, Debug, PartialEq)] -enum IdentifierKind { - Ident, - Lifetime, - Underscore, -} - -impl IdentifierKind { - fn classify(new_name: &str) -> RenameResult { - match lex_single_syntax_kind(new_name) { - Some(res) => match res { - (SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident), - (T![_], _) => Ok(IdentifierKind::Underscore), - (SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => { - Ok(IdentifierKind::Lifetime) - } - (SyntaxKind::LIFETIME_IDENT, _) => { - bail!("Invalid name `{}`: not a lifetime identifier", new_name) - } - (_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error), - (_, None) => bail!("Invalid name `{}`: not an identifier", new_name), - }, - None => bail!("Invalid name `{}`: not an identifier", new_name), - } - } -} - fn find_definition( sema: &Semantics, syntax: &SyntaxNode, @@ -189,126 +135,6 @@ fn find_definition( .ok_or_else(|| format_err!("No references found at position")) } -fn rename_mod( - sema: &Semantics, - module: hir::Module, - new_name: &str, -) -> RenameResult { - if IdentifierKind::classify(new_name)? != IdentifierKind::Ident { - bail!("Invalid name `{0}`: cannot rename module to {0}", new_name); - } - - let mut source_change = SourceChange::default(); - - let InFile { file_id, value: def_source } = module.definition_source(sema.db); - let file_id = file_id.original_file(sema.db); - if let ModuleSource::SourceFile(..) = def_source { - // mod is defined in path/to/dir/mod.rs - let path = if module.is_mod_rs(sema.db) { - format!("../{}/mod.rs", new_name) - } else { - format!("{}.rs", new_name) - }; - let dst = AnchoredPathBuf { anchor: file_id, path }; - let move_file = FileSystemEdit::MoveFile { src: file_id, dst }; - source_change.push_file_system_edit(move_file); - } - - if let Some(InFile { file_id, value: decl_source }) = module.declaration_source(sema.db) { - let file_id = file_id.original_file(sema.db); - match decl_source.name() { - Some(name) => source_change.insert_source_edit( - file_id, - TextEdit::replace(name.syntax().text_range(), new_name.to_string()), - ), - _ => never!("Module source node is missing a name"), - } - } - let def = Definition::ModuleDef(hir::ModuleDef::Module(module)); - let usages = def.usages(sema).all(); - let ref_edits = usages.iter().map(|(&file_id, references)| { - (file_id, source_edit_from_references(references, def, new_name)) - }); - source_change.extend(ref_edits); - - Ok(source_change) -} - -fn rename_reference( - sema: &Semantics, - mut def: Definition, - new_name: &str, -) -> RenameResult { - let ident_kind = IdentifierKind::classify(new_name)?; - - if matches!( - def, // is target a lifetime? - Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_) - ) { - match ident_kind { - IdentifierKind::Ident | IdentifierKind::Underscore => { - cov_mark::hit!(rename_not_a_lifetime_ident_ref); - bail!("Invalid name `{}`: not a lifetime identifier", new_name); - } - IdentifierKind::Lifetime => cov_mark::hit!(rename_lifetime), - } - } else { - match (ident_kind, def) { - (IdentifierKind::Lifetime, _) => { - cov_mark::hit!(rename_not_an_ident_ref); - bail!("Invalid name `{}`: not an identifier", new_name); - } - (IdentifierKind::Ident, _) => cov_mark::hit!(rename_non_local), - (IdentifierKind::Underscore, _) => (), - } - } - - def = match def { - // HACK: resolve trait impl items to the item def of the trait definition - // so that we properly resolve all trait item references - Definition::ModuleDef(mod_def) => mod_def - .as_assoc_item(sema.db) - .and_then(|it| it.containing_trait_impl(sema.db)) - .and_then(|it| { - it.items(sema.db).into_iter().find_map(|it| match (it, mod_def) { - (hir::AssocItem::Function(trait_func), hir::ModuleDef::Function(func)) - if trait_func.name(sema.db) == func.name(sema.db) => - { - Some(Definition::ModuleDef(hir::ModuleDef::Function(trait_func))) - } - (hir::AssocItem::Const(trait_konst), hir::ModuleDef::Const(konst)) - if trait_konst.name(sema.db) == konst.name(sema.db) => - { - Some(Definition::ModuleDef(hir::ModuleDef::Const(trait_konst))) - } - ( - hir::AssocItem::TypeAlias(trait_type_alias), - hir::ModuleDef::TypeAlias(type_alias), - ) if trait_type_alias.name(sema.db) == type_alias.name(sema.db) => { - Some(Definition::ModuleDef(hir::ModuleDef::TypeAlias(trait_type_alias))) - } - _ => None, - }) - }) - .unwrap_or(def), - _ => def, - }; - let usages = def.usages(sema).all(); - - if !usages.is_empty() && ident_kind == IdentifierKind::Underscore { - cov_mark::hit!(rename_underscore_multiple); - bail!("Cannot rename reference to `_` as it is being referenced multiple times"); - } - let mut source_change = SourceChange::default(); - source_change.extend(usages.iter().map(|(&file_id, references)| { - (file_id, source_edit_from_references(references, def, new_name)) - })); - - let (file_id, edit) = source_edit_from_def(sema, def, new_name)?; - source_change.insert_source_edit(file_id, edit); - Ok(source_change) -} - fn rename_to_self(sema: &Semantics, local: hir::Local) -> RenameResult { if never!(local.is_self(sema.db)) { bail!("rename_to_self invoked on self"); @@ -426,243 +252,6 @@ fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Opt Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text)) } -fn source_edit_from_references( - references: &[FileReference], - def: Definition, - new_name: &str, -) -> TextEdit { - let mut edit = TextEdit::builder(); - for reference in references { - let (range, replacement) = match &reference.name { - // if the ranges differ then the node is inside a macro call, we can't really attempt - // to make special rewrites like shorthand syntax and such, so just rename the node in - // the macro input - ast::NameLike::NameRef(name_ref) - if name_ref.syntax().text_range() == reference.range => - { - source_edit_from_name_ref(name_ref, new_name, def) - } - ast::NameLike::Name(name) if name.syntax().text_range() == reference.range => { - source_edit_from_name(name, new_name) - } - _ => None, - } - .unwrap_or_else(|| (reference.range, new_name.to_string())); - edit.replace(range, replacement); - } - edit.finish() -} - -fn source_edit_from_name(name: &ast::Name, new_name: &str) -> Option<(TextRange, String)> { - if let Some(_) = ast::RecordPatField::for_field_name(name) { - if let Some(ident_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) { - return Some(( - TextRange::empty(ident_pat.syntax().text_range().start()), - [new_name, ": "].concat(), - )); - } - } - None -} - -fn source_edit_from_name_ref( - name_ref: &ast::NameRef, - new_name: &str, - def: Definition, -) -> Option<(TextRange, String)> { - if let Some(record_field) = ast::RecordExprField::for_name_ref(name_ref) { - let rcf_name_ref = record_field.name_ref(); - let rcf_expr = record_field.expr(); - match (rcf_name_ref, rcf_expr.and_then(|it| it.name_ref())) { - // field: init-expr, check if we can use a field init shorthand - (Some(field_name), Some(init)) => { - if field_name == *name_ref { - if init.text() == new_name { - cov_mark::hit!(test_rename_field_put_init_shorthand); - // same names, we can use a shorthand here instead. - // we do not want to erase attributes hence this range start - let s = field_name.syntax().text_range().start(); - let e = record_field.syntax().text_range().end(); - return Some((TextRange::new(s, e), new_name.to_owned())); - } - } else if init == *name_ref { - if field_name.text() == new_name { - cov_mark::hit!(test_rename_local_put_init_shorthand); - // same names, we can use a shorthand here instead. - // we do not want to erase attributes hence this range start - let s = field_name.syntax().text_range().start(); - let e = record_field.syntax().text_range().end(); - return Some((TextRange::new(s, e), new_name.to_owned())); - } - } - None - } - // init shorthand - // FIXME: instead of splitting the shorthand, recursively trigger a rename of the - // other name https://github.com/rust-analyzer/rust-analyzer/issues/6547 - (None, Some(_)) if matches!(def, Definition::Field(_)) => { - cov_mark::hit!(test_rename_field_in_field_shorthand); - let s = name_ref.syntax().text_range().start(); - Some((TextRange::empty(s), format!("{}: ", new_name))) - } - (None, Some(_)) if matches!(def, Definition::Local(_)) => { - cov_mark::hit!(test_rename_local_in_field_shorthand); - let s = name_ref.syntax().text_range().end(); - Some((TextRange::empty(s), format!(": {}", new_name))) - } - _ => None, - } - } else if let Some(record_field) = ast::RecordPatField::for_field_name_ref(name_ref) { - let rcf_name_ref = record_field.name_ref(); - let rcf_pat = record_field.pat(); - match (rcf_name_ref, rcf_pat) { - // field: rename - (Some(field_name), Some(ast::Pat::IdentPat(pat))) if field_name == *name_ref => { - // field name is being renamed - if pat.name().map_or(false, |it| it.text() == new_name) { - cov_mark::hit!(test_rename_field_put_init_shorthand_pat); - // same names, we can use a shorthand here instead/ - // we do not want to erase attributes hence this range start - let s = field_name.syntax().text_range().start(); - let e = record_field.syntax().text_range().end(); - Some((TextRange::new(s, e), pat.to_string())) - } else { - None - } - } - _ => None, - } - } else { - None - } -} - -fn source_edit_from_def( - sema: &Semantics, - def: Definition, - new_name: &str, -) -> RenameResult<(FileId, TextEdit)> { - let frange: FileRange = def_name_range(sema, def) - .ok_or_else(|| format_err!("No identifier available to rename"))?; - - let mut replacement_text = String::new(); - let mut repl_range = frange.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((frange.file_id, edit)) -} - -fn def_name_range(sema: &Semantics, def: Definition) -> Option { - // FIXME: the `original_file_range` calls here are wrong -- they never fail, - // and _fall back_ to the entirety of the macro call. Such fall back is - // incorrect for renames. The safe behavior would be to return an error for - // such cases. The correct behavior would be to return an auxiliary list of - // "can't rename these occurrences in macros" items, and then show some kind - // of a dialog to the user. - - let res = match def { - Definition::Macro(mac) => { - let src = mac.source(sema.db)?; - let name = match &src.value { - Either::Left(it) => it.name()?, - Either::Right(it) => it.name()?, - }; - src.with_value(name.syntax()).original_file_range(sema.db) - } - Definition::Field(field) => { - let src = field.source(sema.db)?; - - match &src.value { - FieldSource::Named(record_field) => { - let name = record_field.name()?; - src.with_value(name.syntax()).original_file_range(sema.db) - } - FieldSource::Pos(_) => { - return None; - } - } - } - Definition::ModuleDef(module_def) => match module_def { - hir::ModuleDef::Module(module) => { - let src = module.declaration_source(sema.db)?; - let name = src.value.name()?; - src.with_value(name.syntax()).original_file_range(sema.db) - } - hir::ModuleDef::Function(it) => name_range(it, sema)?, - hir::ModuleDef::Adt(adt) => match adt { - hir::Adt::Struct(it) => name_range(it, sema)?, - hir::Adt::Union(it) => name_range(it, sema)?, - hir::Adt::Enum(it) => name_range(it, sema)?, - }, - hir::ModuleDef::Variant(it) => name_range(it, sema)?, - hir::ModuleDef::Const(it) => name_range(it, sema)?, - hir::ModuleDef::Static(it) => name_range(it, sema)?, - hir::ModuleDef::Trait(it) => name_range(it, sema)?, - hir::ModuleDef::TypeAlias(it) => name_range(it, sema)?, - hir::ModuleDef::BuiltinType(_) => return None, - }, - Definition::SelfType(_) => return None, - Definition::Local(local) => { - let src = local.source(sema.db); - let name = match &src.value { - Either::Left(bind_pat) => bind_pat.name()?, - Either::Right(_) => return None, - }; - src.with_value(name.syntax()).original_file_range(sema.db) - } - Definition::GenericParam(generic_param) => match generic_param { - hir::GenericParam::TypeParam(type_param) => { - let src = type_param.source(sema.db)?; - let name = match &src.value { - Either::Left(_) => return None, - Either::Right(type_param) => type_param.name()?, - }; - src.with_value(name.syntax()).original_file_range(sema.db) - } - hir::GenericParam::LifetimeParam(lifetime_param) => { - let src = lifetime_param.source(sema.db)?; - let lifetime = src.value.lifetime()?; - src.with_value(lifetime.syntax()).original_file_range(sema.db) - } - hir::GenericParam::ConstParam(it) => name_range(it, sema)?, - }, - Definition::Label(label) => { - let src = label.source(sema.db); - let lifetime = src.value.lifetime()?; - src.with_value(lifetime.syntax()).original_file_range(sema.db) - } - }; - return Some(res); - - fn name_range(def: D, sema: &Semantics) -> Option - where - D: HasSource, - D::Ast: ast::NameOwner, - { - let src = def.source(sema.db)?; - let name = src.value.name()?; - let res = src.with_value(name.syntax()).original_file_range(sema.db); - Some(res) - } -} - #[cfg(test)] mod tests { use expect_test::{expect, Expect}; diff --git a/crates/ide_db/src/lib.rs b/crates/ide_db/src/lib.rs index 2ac215c06..7bbd08d6f 100644 --- a/crates/ide_db/src/lib.rs +++ b/crates/ide_db/src/lib.rs @@ -8,7 +8,6 @@ pub mod label; pub mod line_index; pub mod symbol_index; pub mod defs; -pub mod search; pub mod items_locator; pub mod source_change; pub mod ty_filter; @@ -16,6 +15,9 @@ pub mod traits; pub mod call_info; pub mod helpers; +pub mod search; +pub mod rename; + use std::{fmt, sync::Arc}; use base_db::{ diff --git a/crates/ide_db/src/rename.rs b/crates/ide_db/src/rename.rs new file mode 100644 index 000000000..877650df0 --- /dev/null +++ b/crates/ide_db/src/rename.rs @@ -0,0 +1,444 @@ +use std::fmt; + +use base_db::{AnchoredPathBuf, FileId, FileRange}; +use either::Either; +use hir::{AsAssocItem, FieldSource, HasSource, InFile, ModuleSource, Semantics}; +use stdx::never; +use syntax::{ + ast::{self, NameOwner}, + lex_single_syntax_kind, AstNode, SyntaxKind, TextRange, T, +}; +use text_edit::TextEdit; + +use crate::{ + defs::Definition, + search::FileReference, + source_change::{FileSystemEdit, SourceChange}, + RootDatabase, +}; + +pub type Result = std::result::Result; + +#[derive(Debug)] +pub struct RenameError(pub String); + +impl fmt::Display for RenameError { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + +#[macro_export] +macro_rules! _format_err { + ($fmt:expr) => { RenameError(format!($fmt)) }; + ($fmt:expr, $($arg:tt)+) => { RenameError(format!($fmt, $($arg)+)) } +} +pub use _format_err as format_err; + +#[macro_export] +macro_rules! _bail { + ($($tokens:tt)*) => { return Err(format_err!($($tokens)*)) } +} +pub use _bail as bail; + +impl Definition { + pub fn rename(&self, sema: &Semantics, new_name: &str) -> Result { + match *self { + Definition::ModuleDef(hir::ModuleDef::Module(module)) => { + rename_mod(sema, module, new_name) + } + Definition::ModuleDef(hir::ModuleDef::BuiltinType(_)) => { + bail!("Cannot rename builtin type") + } + Definition::SelfType(_) => bail!("Cannot rename `Self`"), + def => rename_reference(sema, def, new_name), + } + } + + /// Textual range of the identifier which will change when renaming this + /// `Definition`. Note that some definitions, like buitin types, can't be + /// renamed. + pub fn rename_range(self, sema: &Semantics) -> Option { + // FIXME: the `original_file_range` calls here are wrong -- they never fail, + // and _fall back_ to the entirety of the macro call. Such fall back is + // incorrect for renames. The safe behavior would be to return an error for + // such cases. The correct behavior would be to return an auxiliary list of + // "can't rename these occurrences in macros" items, and then show some kind + // of a dialog to the user. + + let res = match self { + Definition::Macro(mac) => { + let src = mac.source(sema.db)?; + let name = match &src.value { + Either::Left(it) => it.name()?, + Either::Right(it) => it.name()?, + }; + src.with_value(name.syntax()).original_file_range(sema.db) + } + Definition::Field(field) => { + let src = field.source(sema.db)?; + + match &src.value { + FieldSource::Named(record_field) => { + let name = record_field.name()?; + src.with_value(name.syntax()).original_file_range(sema.db) + } + FieldSource::Pos(_) => { + return None; + } + } + } + Definition::ModuleDef(module_def) => match module_def { + hir::ModuleDef::Module(module) => { + let src = module.declaration_source(sema.db)?; + let name = src.value.name()?; + src.with_value(name.syntax()).original_file_range(sema.db) + } + hir::ModuleDef::Function(it) => name_range(it, sema)?, + hir::ModuleDef::Adt(adt) => match adt { + hir::Adt::Struct(it) => name_range(it, sema)?, + hir::Adt::Union(it) => name_range(it, sema)?, + hir::Adt::Enum(it) => name_range(it, sema)?, + }, + hir::ModuleDef::Variant(it) => name_range(it, sema)?, + hir::ModuleDef::Const(it) => name_range(it, sema)?, + hir::ModuleDef::Static(it) => name_range(it, sema)?, + hir::ModuleDef::Trait(it) => name_range(it, sema)?, + hir::ModuleDef::TypeAlias(it) => name_range(it, sema)?, + hir::ModuleDef::BuiltinType(_) => return None, + }, + Definition::SelfType(_) => return None, + Definition::Local(local) => { + let src = local.source(sema.db); + let name = match &src.value { + Either::Left(bind_pat) => bind_pat.name()?, + Either::Right(_) => return None, + }; + src.with_value(name.syntax()).original_file_range(sema.db) + } + Definition::GenericParam(generic_param) => match generic_param { + hir::GenericParam::TypeParam(type_param) => { + let src = type_param.source(sema.db)?; + let name = match &src.value { + Either::Left(_) => return None, + Either::Right(type_param) => type_param.name()?, + }; + src.with_value(name.syntax()).original_file_range(sema.db) + } + hir::GenericParam::LifetimeParam(lifetime_param) => { + let src = lifetime_param.source(sema.db)?; + let lifetime = src.value.lifetime()?; + src.with_value(lifetime.syntax()).original_file_range(sema.db) + } + hir::GenericParam::ConstParam(it) => name_range(it, sema)?, + }, + Definition::Label(label) => { + let src = label.source(sema.db); + let lifetime = src.value.lifetime()?; + src.with_value(lifetime.syntax()).original_file_range(sema.db) + } + }; + return Some(res); + + fn name_range(def: D, sema: &Semantics) -> Option + where + D: HasSource, + D::Ast: ast::NameOwner, + { + let src = def.source(sema.db)?; + let name = src.value.name()?; + let res = src.with_value(name.syntax()).original_file_range(sema.db); + Some(res) + } + } +} + +fn rename_mod( + sema: &Semantics, + module: hir::Module, + new_name: &str, +) -> Result { + if IdentifierKind::classify(new_name)? != IdentifierKind::Ident { + bail!("Invalid name `{0}`: cannot rename module to {0}", new_name); + } + + let mut source_change = SourceChange::default(); + + let InFile { file_id, value: def_source } = module.definition_source(sema.db); + let file_id = file_id.original_file(sema.db); + if let ModuleSource::SourceFile(..) = def_source { + // mod is defined in path/to/dir/mod.rs + let path = if module.is_mod_rs(sema.db) { + format!("../{}/mod.rs", new_name) + } else { + format!("{}.rs", new_name) + }; + let dst = AnchoredPathBuf { anchor: file_id, path }; + let move_file = FileSystemEdit::MoveFile { src: file_id, dst }; + source_change.push_file_system_edit(move_file); + } + + if let Some(InFile { file_id, value: decl_source }) = module.declaration_source(sema.db) { + let file_id = file_id.original_file(sema.db); + match decl_source.name() { + Some(name) => source_change.insert_source_edit( + file_id, + TextEdit::replace(name.syntax().text_range(), new_name.to_string()), + ), + _ => never!("Module source node is missing a name"), + } + } + let def = Definition::ModuleDef(hir::ModuleDef::Module(module)); + let usages = def.usages(sema).all(); + let ref_edits = usages.iter().map(|(&file_id, references)| { + (file_id, source_edit_from_references(references, def, new_name)) + }); + source_change.extend(ref_edits); + + Ok(source_change) +} + +fn rename_reference( + sema: &Semantics, + mut def: Definition, + new_name: &str, +) -> Result { + let ident_kind = IdentifierKind::classify(new_name)?; + + if matches!( + def, // is target a lifetime? + Definition::GenericParam(hir::GenericParam::LifetimeParam(_)) | Definition::Label(_) + ) { + match ident_kind { + IdentifierKind::Ident | IdentifierKind::Underscore => { + cov_mark::hit!(rename_not_a_lifetime_ident_ref); + bail!("Invalid name `{}`: not a lifetime identifier", new_name); + } + IdentifierKind::Lifetime => cov_mark::hit!(rename_lifetime), + } + } else { + match (ident_kind, def) { + (IdentifierKind::Lifetime, _) => { + cov_mark::hit!(rename_not_an_ident_ref); + bail!("Invalid name `{}`: not an identifier", new_name); + } + (IdentifierKind::Ident, _) => cov_mark::hit!(rename_non_local), + (IdentifierKind::Underscore, _) => (), + } + } + + def = match def { + // HACK: resolve trait impl items to the item def of the trait definition + // so that we properly resolve all trait item references + Definition::ModuleDef(mod_def) => mod_def + .as_assoc_item(sema.db) + .and_then(|it| it.containing_trait_impl(sema.db)) + .and_then(|it| { + it.items(sema.db).into_iter().find_map(|it| match (it, mod_def) { + (hir::AssocItem::Function(trait_func), hir::ModuleDef::Function(func)) + if trait_func.name(sema.db) == func.name(sema.db) => + { + Some(Definition::ModuleDef(hir::ModuleDef::Function(trait_func))) + } + (hir::AssocItem::Const(trait_konst), hir::ModuleDef::Const(konst)) + if trait_konst.name(sema.db) == konst.name(sema.db) => + { + Some(Definition::ModuleDef(hir::ModuleDef::Const(trait_konst))) + } + ( + hir::AssocItem::TypeAlias(trait_type_alias), + hir::ModuleDef::TypeAlias(type_alias), + ) if trait_type_alias.name(sema.db) == type_alias.name(sema.db) => { + Some(Definition::ModuleDef(hir::ModuleDef::TypeAlias(trait_type_alias))) + } + _ => None, + }) + }) + .unwrap_or(def), + _ => def, + }; + let usages = def.usages(sema).all(); + + if !usages.is_empty() && ident_kind == IdentifierKind::Underscore { + cov_mark::hit!(rename_underscore_multiple); + bail!("Cannot rename reference to `_` as it is being referenced multiple times"); + } + let mut source_change = SourceChange::default(); + source_change.extend(usages.iter().map(|(&file_id, references)| { + (file_id, source_edit_from_references(references, def, new_name)) + })); + + let (file_id, edit) = source_edit_from_def(sema, def, new_name)?; + source_change.insert_source_edit(file_id, edit); + Ok(source_change) +} + +pub fn source_edit_from_references( + references: &[FileReference], + def: Definition, + new_name: &str, +) -> TextEdit { + let mut edit = TextEdit::builder(); + for reference in references { + let (range, replacement) = match &reference.name { + // if the ranges differ then the node is inside a macro call, we can't really attempt + // to make special rewrites like shorthand syntax and such, so just rename the node in + // the macro input + ast::NameLike::NameRef(name_ref) + if name_ref.syntax().text_range() == reference.range => + { + source_edit_from_name_ref(name_ref, new_name, def) + } + ast::NameLike::Name(name) if name.syntax().text_range() == reference.range => { + source_edit_from_name(name, new_name) + } + _ => None, + } + .unwrap_or_else(|| (reference.range, new_name.to_string())); + edit.replace(range, replacement); + } + edit.finish() +} + +fn source_edit_from_name(name: &ast::Name, new_name: &str) -> Option<(TextRange, String)> { + if let Some(_) = ast::RecordPatField::for_field_name(name) { + if let Some(ident_pat) = name.syntax().parent().and_then(ast::IdentPat::cast) { + return Some(( + TextRange::empty(ident_pat.syntax().text_range().start()), + [new_name, ": "].concat(), + )); + } + } + None +} + +fn source_edit_from_name_ref( + name_ref: &ast::NameRef, + new_name: &str, + def: Definition, +) -> Option<(TextRange, String)> { + if let Some(record_field) = ast::RecordExprField::for_name_ref(name_ref) { + let rcf_name_ref = record_field.name_ref(); + let rcf_expr = record_field.expr(); + match (rcf_name_ref, rcf_expr.and_then(|it| it.name_ref())) { + // field: init-expr, check if we can use a field init shorthand + (Some(field_name), Some(init)) => { + if field_name == *name_ref { + if init.text() == new_name { + cov_mark::hit!(test_rename_field_put_init_shorthand); + // same names, we can use a shorthand here instead. + // we do not want to erase attributes hence this range start + let s = field_name.syntax().text_range().start(); + let e = record_field.syntax().text_range().end(); + return Some((TextRange::new(s, e), new_name.to_owned())); + } + } else if init == *name_ref { + if field_name.text() == new_name { + cov_mark::hit!(test_rename_local_put_init_shorthand); + // same names, we can use a shorthand here instead. + // we do not want to erase attributes hence this range start + let s = field_name.syntax().text_range().start(); + let e = record_field.syntax().text_range().end(); + return Some((TextRange::new(s, e), new_name.to_owned())); + } + } + None + } + // init shorthand + // FIXME: instead of splitting the shorthand, recursively trigger a rename of the + // other name https://github.com/rust-analyzer/rust-analyzer/issues/6547 + (None, Some(_)) if matches!(def, Definition::Field(_)) => { + cov_mark::hit!(test_rename_field_in_field_shorthand); + let s = name_ref.syntax().text_range().start(); + Some((TextRange::empty(s), format!("{}: ", new_name))) + } + (None, Some(_)) if matches!(def, Definition::Local(_)) => { + cov_mark::hit!(test_rename_local_in_field_shorthand); + let s = name_ref.syntax().text_range().end(); + Some((TextRange::empty(s), format!(": {}", new_name))) + } + _ => None, + } + } else if let Some(record_field) = ast::RecordPatField::for_field_name_ref(name_ref) { + let rcf_name_ref = record_field.name_ref(); + let rcf_pat = record_field.pat(); + match (rcf_name_ref, rcf_pat) { + // field: rename + (Some(field_name), Some(ast::Pat::IdentPat(pat))) if field_name == *name_ref => { + // field name is being renamed + if pat.name().map_or(false, |it| it.text() == new_name) { + cov_mark::hit!(test_rename_field_put_init_shorthand_pat); + // same names, we can use a shorthand here instead/ + // we do not want to erase attributes hence this range start + let s = field_name.syntax().text_range().start(); + let e = record_field.syntax().text_range().end(); + Some((TextRange::new(s, e), pat.to_string())) + } else { + None + } + } + _ => None, + } + } else { + None + } +} + +fn source_edit_from_def( + sema: &Semantics, + def: Definition, + new_name: &str, +) -> Result<(FileId, TextEdit)> { + let frange = + def.rename_range(sema).ok_or_else(|| format_err!("No identifier available to rename"))?; + + let mut replacement_text = String::new(); + let mut repl_range = frange.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((frange.file_id, edit)) +} + +#[derive(Copy, Clone, Debug, PartialEq)] +pub enum IdentifierKind { + Ident, + Lifetime, + Underscore, +} + +impl IdentifierKind { + pub fn classify(new_name: &str) -> Result { + match lex_single_syntax_kind(new_name) { + Some(res) => match res { + (SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident), + (T![_], _) => Ok(IdentifierKind::Underscore), + (SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => { + Ok(IdentifierKind::Lifetime) + } + (SyntaxKind::LIFETIME_IDENT, _) => { + bail!("Invalid name `{}`: not a lifetime identifier", new_name) + } + (_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error), + (_, None) => bail!("Invalid name `{}`: not an identifier", new_name), + }, + None => bail!("Invalid name `{}`: not an identifier", new_name), + } + } +} diff --git a/crates/ide_diagnostics/src/incorrect_case.rs b/crates/ide_diagnostics/src/incorrect_case.rs index 8e1a93aa7..2cf232d56 100644 --- a/crates/ide_diagnostics/src/incorrect_case.rs +++ b/crates/ide_diagnostics/src/incorrect_case.rs @@ -1,5 +1,5 @@ use hir::{db::AstDatabase, InFile}; -use ide_db::{assists::Assist, base_db::FilePosition}; +use ide_db::{assists::Assist, defs::NameClass}; use syntax::AstNode; use crate::{ @@ -27,35 +27,26 @@ pub(super) fn incorrect_case(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCas } fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::IncorrectCase) -> Option> { - if true { - return None; - } - let root = ctx.sema.db.parse_or_expand(d.file)?; let name_node = d.ident.to_node(&root); + let def = NameClass::classify(&ctx.sema, &name_node)?.defined(ctx.sema.db)?; let name_node = InFile::new(d.file, name_node.syntax()); let frange = name_node.original_file_range(ctx.sema.db); - let _file_position = FilePosition { file_id: frange.file_id, offset: frange.range.start() }; let label = format!("Rename to {}", d.suggested_text); - let res = unresolved_fix("change_case", &label, frange.range); + let mut res = unresolved_fix("change_case", &label, frange.range); if ctx.resolve.should_resolve(&res.id) { - //let source_change = rename_with_semantics(&ctx.sema, file_position, &d.suggested_text); - //res.source_change = Some(source_change.ok().unwrap_or_default()); - todo!() + let source_change = def.rename(&ctx.sema, &d.suggested_text); + res.source_change = Some(source_change.ok().unwrap_or_default()); } Some(vec![res]) } -#[cfg(TODO)] +#[cfg(test)] mod change_case { - use crate::{ - fixture, - tests::{check_diagnostics, check_fix}, - AssistResolveStrategy, DiagnosticsConfig, - }; + use crate::tests::{check_diagnostics, check_fix}; #[test] fn test_rename_incorrect_case() { @@ -123,7 +114,7 @@ fn some_fn() { check_diagnostics( r#" fn foo() { - const ANOTHER_ITEM$0: &str = "some_item"; + const ANOTHER_ITEM: &str = "some_item"; } "#, ); @@ -155,20 +146,13 @@ impl TestStruct { #[test] fn test_single_incorrect_case_diagnostic_in_function_name_issue_6970() { - let input = r#"fn FOO$0() {}"#; - let expected = r#"fn foo() {}"#; - - let (analysis, file_position) = fixture::position(input); - let diagnostics = analysis - .diagnostics( - &DiagnosticsConfig::default(), - AssistResolveStrategy::All, - file_position.file_id, - ) - .unwrap(); - assert_eq!(diagnostics.len(), 1); - - check_fix(input, expected); + check_diagnostics( + r#" +fn FOO() {} +// ^^^ Function `FOO` should have snake_case name, e.g. `foo` +"#, + ); + check_fix(r#"fn FOO$0() {}"#, r#"fn foo() {}"#); } #[test] -- cgit v1.2.3