2019-10-14 11:59:02 +00:00
|
|
|
//! FIXME: write short doc here
|
2021-01-15 20:07:38 +00:00
|
|
|
use std::fmt::{self, Display};
|
2019-10-14 11:59:02 +00:00
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
use either::Either;
|
|
|
|
use hir::{HasSource, InFile, Module, ModuleDef, ModuleSource, Semantics};
|
2020-08-13 14:39:16 +00:00
|
|
|
use ide_db::{
|
2021-01-15 20:07:38 +00:00
|
|
|
base_db::{AnchoredPathBuf, FileId, FileRange},
|
2020-10-15 15:27:50 +00:00
|
|
|
defs::{Definition, NameClass, NameRefClass},
|
2021-01-12 14:51:02 +00:00
|
|
|
search::FileReference,
|
2020-06-11 21:40:59 +00:00
|
|
|
RootDatabase,
|
|
|
|
};
|
2021-01-17 20:00:55 +00:00
|
|
|
use stdx::assert_never;
|
2020-08-12 16:26:51 +00:00
|
|
|
use syntax::{
|
2020-07-30 18:51:43 +00:00
|
|
|
ast::{self, NameOwner},
|
2021-01-17 20:00:55 +00:00
|
|
|
lex_single_syntax_kind, AstNode, SyntaxKind, SyntaxNode, T,
|
2020-01-28 05:09:13 +00:00
|
|
|
};
|
2020-05-20 10:59:20 +00:00
|
|
|
use test_utils::mark;
|
2020-08-12 15:03:06 +00:00
|
|
|
use text_edit::TextEdit;
|
2019-10-12 15:47:17 +00:00
|
|
|
|
|
|
|
use crate::{
|
2021-01-17 20:00:55 +00:00
|
|
|
display::TryToNav, FilePosition, FileSystemEdit, RangeInfo, ReferenceKind, SourceChange,
|
2021-01-15 20:07:38 +00:00
|
|
|
TextRange,
|
2019-10-12 15:47:17 +00:00
|
|
|
};
|
|
|
|
|
2021-01-09 15:59:00 +00:00
|
|
|
type RenameResult<T> = Result<T, RenameError>;
|
2020-08-24 14:37:45 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct RenameError(pub(crate) String);
|
|
|
|
|
|
|
|
impl fmt::Display for RenameError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
|
|
Display::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-10 20:59:44 +00:00
|
|
|
macro_rules! format_err {
|
|
|
|
($fmt:expr) => {RenameError(format!($fmt))};
|
|
|
|
($fmt:expr, $($arg:tt)+) => {RenameError(format!($fmt, $($arg)+))}
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! bail {
|
|
|
|
($($tokens:tt)*) => {return Err(format_err!($($tokens)*))}
|
|
|
|
}
|
|
|
|
|
2020-12-16 20:35:15 +00:00
|
|
|
pub(crate) fn prepare_rename(
|
|
|
|
db: &RootDatabase,
|
|
|
|
position: FilePosition,
|
2021-01-09 15:59:00 +00:00
|
|
|
) -> RenameResult<RangeInfo<()>> {
|
2020-12-16 20:35:15 +00:00
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let source_file = sema.parse(position.file_id);
|
|
|
|
let syntax = source_file.syntax();
|
2021-01-17 20:00:55 +00:00
|
|
|
let range = match &find_name_like(&sema, &syntax, position)
|
|
|
|
.ok_or_else(|| format_err!("No references found at position"))?
|
|
|
|
{
|
|
|
|
NameLike::Name(it) => it.syntax(),
|
|
|
|
NameLike::NameRef(it) => it.syntax(),
|
|
|
|
NameLike::Lifetime(it) => it.syntax(),
|
2020-12-16 20:35:15 +00:00
|
|
|
}
|
2021-01-17 20:00:55 +00:00
|
|
|
.text_range();
|
|
|
|
Ok(RangeInfo::new(range, ()))
|
2020-12-16 20:35:15 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 15:47:17 +00:00
|
|
|
pub(crate) fn rename(
|
|
|
|
db: &RootDatabase,
|
|
|
|
position: FilePosition,
|
|
|
|
new_name: &str,
|
2021-01-18 19:25:40 +00:00
|
|
|
) -> RenameResult<SourceChange> {
|
2020-07-01 12:11:34 +00:00
|
|
|
let sema = Semantics::new(db);
|
2020-10-03 14:34:52 +00:00
|
|
|
rename_with_semantics(&sema, position, new_name)
|
|
|
|
}
|
2020-07-01 12:11:34 +00:00
|
|
|
|
2020-10-03 14:34:52 +00:00
|
|
|
pub(crate) fn rename_with_semantics(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
position: FilePosition,
|
|
|
|
new_name: &str,
|
2021-01-18 19:25:40 +00:00
|
|
|
) -> RenameResult<SourceChange> {
|
2020-02-18 17:35:10 +00:00
|
|
|
let source_file = sema.parse(position.file_id);
|
2020-05-04 05:12:18 +00:00
|
|
|
let syntax = source_file.syntax();
|
2021-01-09 15:59:00 +00:00
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
let def = find_definition(sema, syntax, position)
|
|
|
|
.ok_or_else(|| format_err!("No references found at position"))?;
|
|
|
|
match def {
|
|
|
|
Definition::ModuleDef(ModuleDef::Module(module)) => rename_mod(&sema, module, new_name),
|
|
|
|
def => rename_reference(sema, def, new_name),
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-22 19:19:51 +00:00
|
|
|
pub(crate) fn will_rename_file(
|
|
|
|
db: &RootDatabase,
|
|
|
|
file_id: FileId,
|
|
|
|
new_name_stem: &str,
|
|
|
|
) -> Option<SourceChange> {
|
|
|
|
let sema = Semantics::new(db);
|
|
|
|
let module = sema.to_module_def(file_id)?;
|
2021-01-18 19:25:40 +00:00
|
|
|
let mut change = rename_mod(&sema, module, new_name_stem).ok()?;
|
2020-12-22 19:19:51 +00:00
|
|
|
change.file_system_edits.clear();
|
|
|
|
Some(change)
|
|
|
|
}
|
|
|
|
|
2021-01-15 20:07:38 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
2021-01-09 15:59:00 +00:00
|
|
|
enum IdentifierKind {
|
|
|
|
Ident,
|
|
|
|
Lifetime,
|
|
|
|
ToSelf,
|
|
|
|
Underscore,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_identifier(new_name: &str) -> RenameResult<IdentifierKind> {
|
|
|
|
match lex_single_syntax_kind(new_name) {
|
|
|
|
Some(res) => match res {
|
|
|
|
(SyntaxKind::IDENT, _) => Ok(IdentifierKind::Ident),
|
2021-01-10 21:27:43 +00:00
|
|
|
(T![_], _) => Ok(IdentifierKind::Underscore),
|
|
|
|
(T![self], _) => Ok(IdentifierKind::ToSelf),
|
2021-01-09 15:59:00 +00:00
|
|
|
(SyntaxKind::LIFETIME_IDENT, _) if new_name != "'static" && new_name != "'_" => {
|
|
|
|
Ok(IdentifierKind::Lifetime)
|
|
|
|
}
|
|
|
|
(SyntaxKind::LIFETIME_IDENT, _) => {
|
2021-01-10 20:59:44 +00:00
|
|
|
bail!("Invalid name `{0}`: Cannot rename lifetime to {0}", new_name)
|
2021-01-09 15:59:00 +00:00
|
|
|
}
|
2021-01-10 20:59:44 +00:00
|
|
|
(_, Some(syntax_error)) => bail!("Invalid name `{}`: {}", new_name, syntax_error),
|
|
|
|
(_, None) => bail!("Invalid name `{}`: not an identifier", new_name),
|
2021-01-09 15:59:00 +00:00
|
|
|
},
|
2021-01-10 20:59:44 +00:00
|
|
|
None => bail!("Invalid name `{}`: not an identifier", new_name),
|
2021-01-09 15:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
enum NameLike {
|
|
|
|
Name(ast::Name),
|
|
|
|
NameRef(ast::NameRef),
|
|
|
|
Lifetime(ast::Lifetime),
|
|
|
|
}
|
|
|
|
|
|
|
|
fn find_name_like(
|
2020-06-11 21:40:59 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
syntax: &SyntaxNode,
|
2021-01-17 20:00:55 +00:00
|
|
|
position: FilePosition,
|
|
|
|
) -> Option<NameLike> {
|
|
|
|
let namelike = if let Some(name_ref) =
|
|
|
|
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;
|
2020-06-11 21:40:59 +00:00
|
|
|
};
|
2021-01-17 20:00:55 +00:00
|
|
|
Some(namelike)
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
fn find_definition(
|
2021-01-09 15:59:00 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2021-01-17 20:00:55 +00:00
|
|
|
syntax: &SyntaxNode,
|
2021-01-09 15:59:00 +00:00
|
|
|
position: FilePosition,
|
2021-01-17 20:00:55 +00:00
|
|
|
) -> 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)
|
2021-01-09 15:59:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 23:05:07 +00:00
|
|
|
fn source_edit_from_references(
|
2020-11-14 16:49:36 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2021-01-12 14:51:02 +00:00
|
|
|
file_id: FileId,
|
|
|
|
references: &[FileReference],
|
2020-11-14 16:49:36 +00:00
|
|
|
new_name: &str,
|
2021-01-14 17:35:22 +00:00
|
|
|
) -> (FileId, TextEdit) {
|
2021-01-11 23:05:07 +00:00
|
|
|
let mut edit = TextEdit::builder();
|
|
|
|
for reference in references {
|
|
|
|
let mut replacement_text = String::new();
|
|
|
|
let range = match reference.kind {
|
|
|
|
ReferenceKind::FieldShorthandForField => {
|
|
|
|
mark::hit!(test_rename_struct_field_for_shorthand);
|
|
|
|
replacement_text.push_str(new_name);
|
|
|
|
replacement_text.push_str(": ");
|
|
|
|
TextRange::new(reference.range.start(), reference.range.start())
|
|
|
|
}
|
|
|
|
ReferenceKind::FieldShorthandForLocal => {
|
|
|
|
mark::hit!(test_rename_local_for_field_shorthand);
|
|
|
|
replacement_text.push_str(": ");
|
|
|
|
replacement_text.push_str(new_name);
|
|
|
|
TextRange::new(reference.range.end(), reference.range.end())
|
|
|
|
}
|
|
|
|
ReferenceKind::RecordFieldExprOrPat => {
|
|
|
|
mark::hit!(test_rename_field_expr_pat);
|
|
|
|
replacement_text.push_str(new_name);
|
|
|
|
edit_text_range_for_record_field_expr_or_pat(
|
|
|
|
sema,
|
|
|
|
FileRange { file_id, range: reference.range },
|
|
|
|
new_name,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
replacement_text.push_str(new_name);
|
|
|
|
reference.range
|
|
|
|
}
|
|
|
|
};
|
|
|
|
edit.replace(range, replacement_text);
|
2020-11-14 18:57:47 +00:00
|
|
|
}
|
2021-01-14 17:35:22 +00:00
|
|
|
(file_id, edit.finish())
|
2020-11-14 18:57:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn edit_text_range_for_record_field_expr_or_pat(
|
|
|
|
sema: &Semantics<RootDatabase>,
|
|
|
|
file_range: FileRange,
|
|
|
|
new_name: &str,
|
|
|
|
) -> TextRange {
|
|
|
|
let source_file = sema.parse(file_range.file_id);
|
|
|
|
let file_syntax = source_file.syntax();
|
2020-11-15 15:11:06 +00:00
|
|
|
let original_range = file_range.range;
|
|
|
|
|
|
|
|
syntax::algo::find_node_at_range::<ast::RecordExprField>(file_syntax, original_range)
|
|
|
|
.and_then(|field_expr| match field_expr.expr().and_then(|e| e.name_ref()) {
|
|
|
|
Some(name) if &name.to_string() == new_name => Some(field_expr.syntax().text_range()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.or_else(|| {
|
|
|
|
syntax::algo::find_node_at_range::<ast::RecordPatField>(file_syntax, original_range)
|
|
|
|
.and_then(|field_pat| match field_pat.pat() {
|
|
|
|
Some(ast::Pat::IdentPat(pat))
|
|
|
|
if pat.name().map(|n| n.to_string()).as_deref() == Some(new_name) =>
|
|
|
|
{
|
|
|
|
Some(field_pat.syntax().text_range())
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.unwrap_or(original_range)
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn rename_mod(
|
2020-07-01 12:11:34 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2020-06-11 21:40:59 +00:00
|
|
|
module: Module,
|
2019-10-12 15:47:17 +00:00
|
|
|
new_name: &str,
|
2021-01-18 19:25:40 +00:00
|
|
|
) -> RenameResult<SourceChange> {
|
2021-01-09 15:59:00 +00:00
|
|
|
if IdentifierKind::Ident != check_identifier(new_name)? {
|
2021-01-10 20:59:44 +00:00
|
|
|
bail!("Invalid name `{0}`: cannot rename module to {0}", new_name);
|
2021-01-09 15:59:00 +00:00
|
|
|
}
|
2021-01-14 21:43:36 +00:00
|
|
|
|
|
|
|
let mut source_change = SourceChange::default();
|
2020-06-11 21:40:59 +00:00
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
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);
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
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()),
|
|
|
|
),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2020-02-19 21:51:59 +00:00
|
|
|
}
|
2021-01-17 20:00:55 +00:00
|
|
|
let def = Definition::ModuleDef(ModuleDef::Module(module));
|
|
|
|
let usages = def.usages(sema).all();
|
|
|
|
let ref_edits = usages.iter().map(|(&file_id, references)| {
|
2021-01-12 14:51:02 +00:00
|
|
|
source_edit_from_references(sema, file_id, references, new_name)
|
|
|
|
});
|
2021-01-14 21:43:36 +00:00
|
|
|
source_change.extend(ref_edits);
|
2020-06-11 21:40:59 +00:00
|
|
|
|
2021-01-18 19:25:40 +00:00
|
|
|
Ok(source_change)
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|
|
|
|
|
2021-01-18 19:25:40 +00:00
|
|
|
fn rename_to_self(sema: &Semantics<RootDatabase>, local: hir::Local) -> RenameResult<SourceChange> {
|
2021-01-17 20:00:55 +00:00
|
|
|
if assert_never!(local.is_self(sema.db)) {
|
|
|
|
bail!("rename_to_self invoked on self");
|
|
|
|
}
|
|
|
|
|
|
|
|
let fn_def = match local.parent(sema.db) {
|
|
|
|
hir::DefWithBody::Function(func) => func,
|
|
|
|
_ => bail!("Cannot rename non-param local to self"),
|
|
|
|
};
|
2020-05-04 05:12:18 +00:00
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
// 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
|
2020-11-29 17:44:26 +00:00
|
|
|
.param_list()
|
|
|
|
.and_then(|p| p.params().next())
|
2021-01-10 20:59:44 +00:00
|
|
|
.ok_or_else(|| format_err!("Method has no parameters"))?
|
2020-11-29 17:44:26 +00:00
|
|
|
.syntax()
|
|
|
|
.text_range();
|
2021-01-17 20:00:55 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
_ => (),
|
2020-11-29 17:44:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
let impl_block = fn_ast
|
|
|
|
.syntax()
|
|
|
|
.ancestors()
|
|
|
|
.find_map(|node| ast::Impl::cast(node))
|
2020-11-29 17:44:26 +00:00
|
|
|
.and_then(|def| sema.to_def(&def))
|
2021-01-10 20:59:44 +00:00
|
|
|
.ok_or_else(|| format_err!("No impl block found for function"))?;
|
2020-11-29 17:44:26 +00:00
|
|
|
if fn_def.self_param(sema.db).is_some() {
|
2021-01-10 20:59:44 +00:00
|
|
|
bail!("Method already has a self parameter");
|
2020-05-04 05:12:18 +00:00
|
|
|
}
|
2020-11-29 17:44:26 +00:00
|
|
|
|
2020-12-01 10:53:12 +00:00
|
|
|
let params = fn_def.assoc_fn_params(sema.db);
|
2021-01-10 20:59:44 +00:00
|
|
|
let first_param = params.first().ok_or_else(|| format_err!("Method has no parameters"))?;
|
2020-11-29 17:44:26 +00:00
|
|
|
let first_param_ty = first_param.ty();
|
|
|
|
let impl_ty = impl_block.target_ty(sema.db);
|
|
|
|
let (ty, self_param) = if impl_ty.remove_ref().is_some() {
|
|
|
|
// if the impl is a ref to the type we can just match the `&T` with self directly
|
|
|
|
(first_param_ty.clone(), "self")
|
|
|
|
} else {
|
|
|
|
first_param_ty.remove_ref().map_or((first_param_ty.clone(), "self"), |ty| {
|
|
|
|
(ty, if first_param_ty.is_mutable_reference() { "&mut self" } else { "&self" })
|
|
|
|
})
|
2020-05-04 05:12:18 +00:00
|
|
|
};
|
|
|
|
|
2020-11-29 17:44:26 +00:00
|
|
|
if ty != impl_ty {
|
2021-01-10 20:59:44 +00:00
|
|
|
bail!("Parameter type differs from impl block type");
|
2020-11-29 17:44:26 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
let def = Definition::Local(local);
|
|
|
|
let usages = def.usages(sema).all();
|
2021-01-14 21:43:36 +00:00
|
|
|
let mut source_change = SourceChange::default();
|
2021-01-17 20:00:55 +00:00
|
|
|
source_change.extend(usages.iter().map(|(&file_id, references)| {
|
2021-01-14 17:35:22 +00:00
|
|
|
source_edit_from_references(sema, file_id, references, "self")
|
|
|
|
}));
|
2021-01-14 21:43:36 +00:00
|
|
|
source_change.insert_source_edit(
|
2021-01-17 20:00:55 +00:00
|
|
|
file_id.original_file(sema.db),
|
|
|
|
TextEdit::replace(first_param_range, String::from(self_param)),
|
2021-01-14 21:43:36 +00:00
|
|
|
);
|
2020-05-04 05:12:18 +00:00
|
|
|
|
2021-01-18 19:25:40 +00:00
|
|
|
Ok(source_change)
|
2020-05-04 05:12:18 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
fn text_edit_from_self_param(self_param: &ast::SelfParam, new_name: &str) -> Option<TextEdit> {
|
2020-07-30 16:28:28 +00:00
|
|
|
fn target_type_name(impl_def: &ast::Impl) -> Option<String> {
|
2020-07-31 18:23:52 +00:00
|
|
|
if let Some(ast::Type::PathType(p)) = impl_def.self_ty() {
|
2020-05-04 05:12:18 +00:00
|
|
|
return Some(p.path()?.segment()?.name_ref()?.text().to_string());
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
let impl_def = self_param.syntax().ancestors().find_map(|it| ast::Impl::cast(it))?;
|
2020-05-04 05:12:18 +00:00
|
|
|
let type_name = target_type_name(&impl_def)?;
|
|
|
|
|
|
|
|
let mut replacement_text = String::from(new_name);
|
|
|
|
replacement_text.push_str(": ");
|
2020-11-29 16:56:36 +00:00
|
|
|
match (self_param.amp_token(), self_param.mut_token()) {
|
|
|
|
(None, None) => (),
|
|
|
|
(Some(_), None) => replacement_text.push('&'),
|
|
|
|
(_, Some(_)) => replacement_text.push_str("&mut "),
|
|
|
|
};
|
2020-05-04 05:12:18 +00:00
|
|
|
replacement_text.push_str(type_name.as_str());
|
|
|
|
|
|
|
|
Some(TextEdit::replace(self_param.syntax().text_range(), replacement_text))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rename_self_to_param(
|
2020-07-01 12:11:34 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2021-01-17 20:00:55 +00:00
|
|
|
local: hir::Local,
|
2020-05-04 05:12:18 +00:00
|
|
|
new_name: &str,
|
2021-01-17 20:00:55 +00:00
|
|
|
identifier_kind: IdentifierKind,
|
2021-01-18 19:25:40 +00:00
|
|
|
) -> RenameResult<SourceChange> {
|
2021-01-17 20:00:55 +00:00
|
|
|
let (file_id, self_param) = match local.source(sema.db) {
|
|
|
|
InFile { file_id, value: Either::Right(self_param) } => (file_id, self_param),
|
|
|
|
_ => {
|
|
|
|
assert_never!(true, "rename_self_to_param invoked on a non-self local");
|
|
|
|
bail!("rename_self_to_param invoked on a non-self local");
|
2021-01-09 15:59:00 +00:00
|
|
|
}
|
2021-01-17 20:00:55 +00:00
|
|
|
};
|
2020-05-04 05:12:18 +00:00
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
let def = Definition::Local(local);
|
|
|
|
let usages = def.usages(sema).all();
|
|
|
|
let edit = text_edit_from_self_param(&self_param, new_name)
|
|
|
|
.ok_or_else(|| format_err!("No target type found"))?;
|
|
|
|
if usages.len() > 1 && identifier_kind == IdentifierKind::Underscore {
|
|
|
|
bail!("Cannot rename reference to `_` as it is being referenced multiple times");
|
2021-01-15 20:07:38 +00:00
|
|
|
}
|
2021-01-17 20:00:55 +00:00
|
|
|
let mut source_change = SourceChange::default();
|
|
|
|
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)
|
|
|
|
}));
|
2021-01-18 19:25:40 +00:00
|
|
|
Ok(source_change)
|
2020-05-04 05:12:18 +00:00
|
|
|
}
|
|
|
|
|
2019-10-12 15:47:17 +00:00
|
|
|
fn rename_reference(
|
2020-07-01 12:11:34 +00:00
|
|
|
sema: &Semantics<RootDatabase>,
|
2021-01-17 20:00:55 +00:00
|
|
|
def: Definition,
|
2019-10-12 15:47:17 +00:00
|
|
|
new_name: &str,
|
2021-01-18 19:25:40 +00:00
|
|
|
) -> RenameResult<SourceChange> {
|
2021-01-09 15:59:00 +00:00
|
|
|
let ident_kind = check_identifier(new_name)?;
|
2019-10-12 15:47:17 +00:00
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
let def_is_lbl_or_lt = matches!(def,
|
|
|
|
Definition::GenericParam(hir::GenericParam::LifetimeParam(_))
|
|
|
|
| Definition::Label(_)
|
|
|
|
);
|
|
|
|
match (ident_kind, def) {
|
|
|
|
(IdentifierKind::ToSelf, _)
|
|
|
|
| (IdentifierKind::Underscore, _)
|
|
|
|
| (IdentifierKind::Ident, _)
|
|
|
|
if def_is_lbl_or_lt =>
|
|
|
|
{
|
2021-01-10 21:27:43 +00:00
|
|
|
mark::hit!(rename_not_a_lifetime_ident_ref);
|
2021-01-10 20:59:44 +00:00
|
|
|
bail!("Invalid name `{}`: not a lifetime identifier", new_name)
|
2020-12-16 20:35:15 +00:00
|
|
|
}
|
2021-01-17 20:00:55 +00:00
|
|
|
(IdentifierKind::Lifetime, _) if def_is_lbl_or_lt => mark::hit!(rename_lifetime),
|
2021-01-10 21:27:43 +00:00
|
|
|
(IdentifierKind::Lifetime, _) => {
|
|
|
|
mark::hit!(rename_not_an_ident_ref);
|
|
|
|
bail!("Invalid name `{}`: not an identifier", new_name)
|
2020-12-16 20:35:15 +00:00
|
|
|
}
|
2021-01-17 20:00:55 +00:00
|
|
|
(IdentifierKind::ToSelf, Definition::Local(local)) if local.is_self(sema.db) => {
|
|
|
|
// no-op
|
|
|
|
mark::hit!(rename_self_to_self);
|
2021-01-18 19:25:40 +00:00
|
|
|
return Ok(SourceChange::default());
|
2021-01-17 20:00:55 +00:00
|
|
|
}
|
|
|
|
(ident_kind, Definition::Local(local)) if local.is_self(sema.db) => {
|
2021-01-15 20:07:38 +00:00
|
|
|
mark::hit!(rename_self_to_param);
|
2021-01-17 20:00:55 +00:00
|
|
|
return rename_self_to_param(sema, local, new_name, ident_kind);
|
2021-01-09 15:59:00 +00:00
|
|
|
}
|
2021-01-17 20:00:55 +00:00
|
|
|
(IdentifierKind::ToSelf, Definition::Local(local)) => {
|
2021-01-10 21:27:43 +00:00
|
|
|
mark::hit!(rename_to_self);
|
2021-01-17 20:00:55 +00:00
|
|
|
return rename_to_self(sema, local);
|
2021-01-09 15:59:00 +00:00
|
|
|
}
|
2021-01-17 20:00:55 +00:00
|
|
|
(IdentifierKind::ToSelf, _) => bail!("Invalid name `{}`: not an identifier", new_name),
|
2021-01-10 21:27:43 +00:00
|
|
|
(IdentifierKind::Ident, _) | (IdentifierKind::Underscore, _) => mark::hit!(rename_ident),
|
2020-12-16 20:35:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
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");
|
|
|
|
}
|
2021-01-14 21:43:36 +00:00
|
|
|
let mut source_change = SourceChange::default();
|
2021-01-17 20:00:55 +00:00
|
|
|
source_change.extend(usages.iter().map(|(&file_id, references)| {
|
2021-01-14 17:35:22 +00:00
|
|
|
source_edit_from_references(sema, file_id, &references, new_name)
|
|
|
|
}));
|
2019-10-12 15:47:17 +00:00
|
|
|
|
2021-01-17 20:00:55 +00:00
|
|
|
let (file_id, edit) = source_edit_from_def(sema, def, new_name)?;
|
|
|
|
source_change.insert_source_edit(file_id, edit);
|
2021-01-18 19:25:40 +00:00
|
|
|
Ok(source_change)
|
2021-01-17 20:00:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-21 11:19:31 +00:00
|
|
|
use expect_test::{expect, Expect};
|
2020-06-24 09:31:30 +00:00
|
|
|
use stdx::trim_indent;
|
2020-05-20 10:59:20 +00:00
|
|
|
use test_utils::{assert_eq_text, mark};
|
2020-08-12 15:03:06 +00:00
|
|
|
use text_edit::TextEdit;
|
2019-10-26 17:07:24 +00:00
|
|
|
|
2020-10-02 15:34:31 +00:00
|
|
|
use crate::{fixture, FileId};
|
2019-10-12 15:47:17 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
fn check(new_name: &str, ra_fixture_before: &str, ra_fixture_after: &str) {
|
|
|
|
let ra_fixture_after = &trim_indent(ra_fixture_after);
|
2020-10-02 15:34:31 +00:00
|
|
|
let (analysis, position) = fixture::position(ra_fixture_before);
|
2020-08-24 14:37:45 +00:00
|
|
|
let rename_result = analysis
|
|
|
|
.rename(position, new_name)
|
|
|
|
.unwrap_or_else(|err| panic!("Rename to '{}' was cancelled: {}", new_name, err));
|
|
|
|
match rename_result {
|
|
|
|
Ok(source_change) => {
|
|
|
|
let mut text_edit_builder = TextEdit::builder();
|
|
|
|
let mut file_id: Option<FileId> = None;
|
2021-01-18 19:25:40 +00:00
|
|
|
for edit in source_change.source_file_edits {
|
2021-01-14 17:35:22 +00:00
|
|
|
file_id = Some(edit.0);
|
|
|
|
for indel in edit.1.into_iter() {
|
2020-08-24 14:37:45 +00:00
|
|
|
text_edit_builder.replace(indel.delete, indel.insert);
|
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
2021-01-10 21:27:43 +00:00
|
|
|
if let Some(file_id) = file_id {
|
|
|
|
let mut result = analysis.file_text(file_id).unwrap().to_string();
|
|
|
|
text_edit_builder.finish().apply(&mut result);
|
|
|
|
assert_eq_text!(ra_fixture_after, &*result);
|
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
2020-08-24 14:37:45 +00:00
|
|
|
Err(err) => {
|
|
|
|
if ra_fixture_after.starts_with("error:") {
|
|
|
|
let error_message = ra_fixture_after
|
|
|
|
.chars()
|
|
|
|
.into_iter()
|
|
|
|
.skip("error:".len())
|
|
|
|
.collect::<String>();
|
|
|
|
assert_eq!(error_message.trim(), err.to_string());
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
panic!("Rename to '{}' failed unexpectedly: {}", new_name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
|
2020-07-03 16:35:43 +00:00
|
|
|
fn check_expect(new_name: &str, ra_fixture: &str, expect: Expect) {
|
2020-10-02 15:34:31 +00:00
|
|
|
let (analysis, position) = fixture::position(ra_fixture);
|
2020-08-24 14:37:45 +00:00
|
|
|
let source_change = analysis
|
|
|
|
.rename(position, new_name)
|
|
|
|
.unwrap()
|
|
|
|
.expect("Expect returned RangeInfo to be Some, but was None");
|
2020-07-03 16:35:43 +00:00
|
|
|
expect.assert_debug_eq(&source_change)
|
|
|
|
}
|
|
|
|
|
2019-11-29 15:03:39 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_to_underscore() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check("_", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let _ = 1; }"#);
|
2019-11-29 15:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_to_raw_identifier() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check("r#fn", r#"fn main() { let i$0 = 1; }"#, r#"fn main() { let r#fn = 1; }"#);
|
2019-11-29 15:03:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-08-24 14:37:45 +00:00
|
|
|
fn test_rename_to_invalid_identifier1() {
|
|
|
|
check(
|
|
|
|
"invalid!",
|
2021-01-06 20:15:48 +00:00
|
|
|
r#"fn main() { let i$0 = 1; }"#,
|
2020-08-24 14:37:45 +00:00
|
|
|
"error: Invalid name `invalid!`: not an identifier",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_to_invalid_identifier2() {
|
|
|
|
check(
|
|
|
|
"multiple tokens",
|
2021-01-06 20:15:48 +00:00
|
|
|
r#"fn main() { let i$0 = 1; }"#,
|
2020-08-24 14:37:45 +00:00
|
|
|
"error: Invalid name `multiple tokens`: not an identifier",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_to_invalid_identifier3() {
|
|
|
|
check(
|
|
|
|
"let",
|
2021-01-06 20:15:48 +00:00
|
|
|
r#"fn main() { let i$0 = 1; }"#,
|
2020-08-24 14:37:45 +00:00
|
|
|
"error: Invalid name `let`: not an identifier",
|
|
|
|
);
|
2019-11-29 15:03:39 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 20:35:15 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_to_invalid_identifier_lifetime() {
|
2021-01-10 21:27:43 +00:00
|
|
|
mark::check!(rename_not_an_ident_ref);
|
2020-12-16 20:35:15 +00:00
|
|
|
check(
|
|
|
|
"'foo",
|
2021-01-06 20:15:48 +00:00
|
|
|
r#"fn main() { let i$0 = 1; }"#,
|
2020-12-16 20:35:15 +00:00
|
|
|
"error: Invalid name `'foo`: not an identifier",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_to_invalid_identifier_lifetime2() {
|
2021-01-10 21:27:43 +00:00
|
|
|
mark::check!(rename_not_a_lifetime_ident_ref);
|
2020-12-16 20:35:15 +00:00
|
|
|
check(
|
|
|
|
"foo",
|
2021-01-06 20:15:48 +00:00
|
|
|
r#"fn main<'a>(_: &'a$0 ()) {}"#,
|
2020-12-16 20:35:15 +00:00
|
|
|
"error: Invalid name `foo`: not a lifetime identifier",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-09 15:59:00 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_to_underscore_invalid() {
|
2021-01-10 21:27:43 +00:00
|
|
|
mark::check!(rename_underscore_multiple);
|
2021-01-09 15:59:00 +00:00
|
|
|
check(
|
|
|
|
"_",
|
|
|
|
r#"fn main(foo$0: ()) {foo;}"#,
|
|
|
|
"error: Cannot rename reference to `_` as it is being referenced multiple times",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-01-10 21:27:43 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_mod_invalid() {
|
|
|
|
check(
|
|
|
|
"'foo",
|
|
|
|
r#"mod foo$0 {}"#,
|
|
|
|
"error: Invalid name `'foo`: cannot rename module to 'foo",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-12 15:47:17 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_for_local() {
|
2021-01-10 21:27:43 +00:00
|
|
|
mark::check!(rename_ident);
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"k",
|
2019-10-12 15:47:17 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
fn main() {
|
|
|
|
let mut i = 1;
|
|
|
|
let j = 1;
|
2021-01-06 20:15:48 +00:00
|
|
|
i = i$0 + j;
|
2019-10-12 15:47:17 +00:00
|
|
|
|
2020-07-03 16:39:36 +00:00
|
|
|
{ i = 0; }
|
2019-10-12 15:47:17 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
i = 5;
|
|
|
|
}
|
|
|
|
"#,
|
2019-10-12 15:47:17 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
fn main() {
|
|
|
|
let mut k = 1;
|
|
|
|
let j = 1;
|
|
|
|
k = k + j;
|
2019-10-12 15:47:17 +00:00
|
|
|
|
2020-07-03 16:39:36 +00:00
|
|
|
{ k = 0; }
|
2019-10-12 15:47:17 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
k = 5;
|
|
|
|
}
|
|
|
|
"#,
|
2019-10-12 15:47:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-24 14:37:45 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_unresolved_reference() {
|
|
|
|
check(
|
|
|
|
"new_name",
|
2021-01-06 20:15:48 +00:00
|
|
|
r#"fn main() { let _ = unresolved_ref$0; }"#,
|
2020-08-24 14:37:45 +00:00
|
|
|
"error: No references found at position",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-09 09:17:56 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_for_macro_args() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
2020-02-09 09:17:56 +00:00
|
|
|
"b",
|
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
macro_rules! foo {($i:ident) => {$i} }
|
|
|
|
fn main() {
|
2021-01-06 20:15:48 +00:00
|
|
|
let a$0 = "test";
|
2020-07-03 16:29:37 +00:00
|
|
|
foo!(a);
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
macro_rules! foo {($i:ident) => {$i} }
|
|
|
|
fn main() {
|
|
|
|
let b = "test";
|
|
|
|
foo!(b);
|
|
|
|
}
|
|
|
|
"#,
|
2020-02-09 09:17:56 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-22 11:53:34 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_for_macro_args_rev() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
2020-03-22 11:53:34 +00:00
|
|
|
"b",
|
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
macro_rules! foo {($i:ident) => {$i} }
|
|
|
|
fn main() {
|
|
|
|
let a = "test";
|
2021-01-06 20:15:48 +00:00
|
|
|
foo!(a$0);
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
macro_rules! foo {($i:ident) => {$i} }
|
|
|
|
fn main() {
|
|
|
|
let b = "test";
|
|
|
|
foo!(b);
|
|
|
|
}
|
|
|
|
"#,
|
2020-03-22 11:53:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_for_macro_define_fn() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
2020-03-22 11:53:34 +00:00
|
|
|
"bar",
|
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
macro_rules! define_fn {($id:ident) => { fn $id{} }}
|
|
|
|
define_fn!(foo);
|
|
|
|
fn main() {
|
2021-01-06 20:15:48 +00:00
|
|
|
fo$0o();
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
macro_rules! define_fn {($id:ident) => { fn $id{} }}
|
|
|
|
define_fn!(bar);
|
|
|
|
fn main() {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
"#,
|
2020-03-22 11:53:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_for_macro_define_fn_rev() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
2020-03-22 11:53:34 +00:00
|
|
|
"bar",
|
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
macro_rules! define_fn {($id:ident) => { fn $id{} }}
|
2021-01-06 20:15:48 +00:00
|
|
|
define_fn!(fo$0o);
|
2020-07-03 16:29:37 +00:00
|
|
|
fn main() {
|
|
|
|
foo();
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
macro_rules! define_fn {($id:ident) => { fn $id{} }}
|
|
|
|
define_fn!(bar);
|
|
|
|
fn main() {
|
|
|
|
bar();
|
|
|
|
}
|
|
|
|
"#,
|
2020-03-22 11:53:34 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-12 15:47:17 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_for_param_inside() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check("j", r#"fn foo(i : u32) -> u32 { i$0 }"#, r#"fn foo(j : u32) -> u32 { j }"#);
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_refs_for_fn_param() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check("j", r#"fn foo(i$0 : u32) -> u32 { i }"#, r#"fn foo(j : u32) -> u32 { j }"#);
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_for_mut_param() {
|
2021-01-06 20:15:48 +00:00
|
|
|
check("j", r#"fn foo(mut i$0 : u32) -> u32 { i }"#, r#"fn foo(mut j : u32) -> u32 { j }"#);
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|
|
|
|
|
2020-03-10 02:18:55 +00:00
|
|
|
#[test]
|
2020-03-10 02:34:33 +00:00
|
|
|
fn test_rename_struct_field() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"j",
|
2020-03-10 02:18:55 +00:00
|
|
|
r#"
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo { i$0: i32 }
|
2020-03-10 02:18:55 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
|
|
|
fn new(i: i32) -> Self {
|
|
|
|
Self { i: i }
|
2020-03-10 02:18:55 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-10 02:18:55 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { j: i32 }
|
2020-03-10 02:18:55 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
|
|
|
fn new(i: i32) -> Self {
|
|
|
|
Self { j: i }
|
2020-03-10 02:18:55 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-10 02:18:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-03-10 02:34:33 +00:00
|
|
|
fn test_rename_struct_field_for_shorthand() {
|
2020-05-20 10:59:20 +00:00
|
|
|
mark::check!(test_rename_struct_field_for_shorthand);
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"j",
|
2020-03-10 02:18:55 +00:00
|
|
|
r#"
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo { i$0: i32 }
|
2020-03-10 02:18:55 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
|
|
|
fn new(i: i32) -> Self {
|
|
|
|
Self { i }
|
2020-03-10 02:18:55 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-10 02:18:55 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { j: i32 }
|
2020-03-10 02:18:55 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
|
|
|
fn new(i: i32) -> Self {
|
|
|
|
Self { j: i }
|
2020-03-10 02:18:55 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-10 02:18:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-10 02:34:33 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_local_for_field_shorthand() {
|
2020-05-20 10:59:20 +00:00
|
|
|
mark::check!(test_rename_local_for_field_shorthand);
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"j",
|
2020-03-10 02:34:33 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { i: i32 }
|
2020-03-10 02:34:33 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn new(i$0: i32) -> Self {
|
2020-07-03 16:29:37 +00:00
|
|
|
Self { i }
|
2020-03-10 02:34:33 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-10 02:34:33 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { i: i32 }
|
2020-03-10 02:34:33 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
|
|
|
fn new(j: i32) -> Self {
|
|
|
|
Self { i: j }
|
2020-03-10 02:34:33 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-10 02:34:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-03-11 03:27:38 +00:00
|
|
|
#[test]
|
|
|
|
fn test_field_shorthand_correct_struct() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"j",
|
2020-03-11 03:27:38 +00:00
|
|
|
r#"
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo { i$0: i32 }
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Bar { i: i32 }
|
2020-03-11 03:27:38 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Bar {
|
|
|
|
fn new(i: i32) -> Self {
|
|
|
|
Self { i }
|
2020-03-11 03:27:38 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-11 03:27:38 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { j: i32 }
|
|
|
|
struct Bar { i: i32 }
|
2020-03-11 03:27:38 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Bar {
|
|
|
|
fn new(i: i32) -> Self {
|
|
|
|
Self { i }
|
2020-03-11 03:27:38 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-11 03:27:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_shadow_local_for_struct_shorthand() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"j",
|
2020-03-11 03:27:38 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { i: i32 }
|
2020-03-11 03:27:38 +00:00
|
|
|
|
2021-01-06 20:15:48 +00:00
|
|
|
fn baz(i$0: i32) -> Self {
|
2020-07-03 16:29:37 +00:00
|
|
|
let x = Foo { i };
|
|
|
|
{
|
|
|
|
let i = 0;
|
|
|
|
Foo { i }
|
2020-03-11 03:27:38 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-11 03:27:38 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { i: i32 }
|
2020-03-11 03:27:38 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
fn baz(j: i32) -> Self {
|
|
|
|
let x = Foo { i: j };
|
|
|
|
{
|
|
|
|
let i = 0;
|
|
|
|
Foo { i }
|
2020-03-11 03:27:38 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-03-11 03:27:38 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-12 15:47:17 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_mod() {
|
2020-07-03 16:35:43 +00:00
|
|
|
check_expect(
|
|
|
|
"foo2",
|
2020-06-16 16:45:58 +00:00
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod bar;
|
2019-10-12 15:47:17 +00:00
|
|
|
|
2020-06-16 16:45:58 +00:00
|
|
|
//- /bar.rs
|
2021-01-06 20:15:48 +00:00
|
|
|
mod foo$0;
|
2019-10-12 15:47:17 +00:00
|
|
|
|
2020-06-16 16:45:58 +00:00
|
|
|
//- /bar/foo.rs
|
2020-07-03 16:35:43 +00:00
|
|
|
// empty
|
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-01-18 19:25:40 +00:00
|
|
|
SourceChange {
|
|
|
|
source_file_edits: {
|
|
|
|
FileId(
|
|
|
|
1,
|
|
|
|
): TextEdit {
|
|
|
|
indels: [
|
|
|
|
Indel {
|
|
|
|
insert: "foo2",
|
|
|
|
delete: 4..7,
|
|
|
|
},
|
|
|
|
],
|
2021-01-14 17:35:22 +00:00
|
|
|
},
|
2021-01-18 19:25:40 +00:00
|
|
|
},
|
|
|
|
file_system_edits: [
|
|
|
|
MoveFile {
|
|
|
|
src: FileId(
|
|
|
|
2,
|
|
|
|
),
|
|
|
|
dst: AnchoredPathBuf {
|
|
|
|
anchor: FileId(
|
2020-10-02 14:13:48 +00:00
|
|
|
2,
|
2020-07-03 16:35:43 +00:00
|
|
|
),
|
2021-01-18 19:25:40 +00:00
|
|
|
path: "foo2.rs",
|
2020-07-03 16:35:43 +00:00
|
|
|
},
|
2021-01-18 19:25:40 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
is_snippet: false,
|
2020-07-03 16:35:43 +00:00
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
2020-06-11 21:40:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_mod_in_use_tree() {
|
2020-07-03 16:35:43 +00:00
|
|
|
check_expect(
|
|
|
|
"quux",
|
2020-06-11 21:40:59 +00:00
|
|
|
r#"
|
|
|
|
//- /main.rs
|
|
|
|
pub mod foo;
|
|
|
|
pub mod bar;
|
|
|
|
fn main() {}
|
|
|
|
|
|
|
|
//- /foo.rs
|
|
|
|
pub struct FooContent;
|
|
|
|
|
|
|
|
//- /bar.rs
|
2021-01-06 20:15:48 +00:00
|
|
|
use crate::foo$0::FooContent;
|
2020-07-03 16:35:43 +00:00
|
|
|
"#,
|
2020-07-03 16:39:36 +00:00
|
|
|
expect![[r#"
|
2021-01-18 19:25:40 +00:00
|
|
|
SourceChange {
|
|
|
|
source_file_edits: {
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
): TextEdit {
|
|
|
|
indels: [
|
|
|
|
Indel {
|
|
|
|
insert: "quux",
|
|
|
|
delete: 8..11,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
FileId(
|
|
|
|
2,
|
|
|
|
): TextEdit {
|
|
|
|
indels: [
|
|
|
|
Indel {
|
|
|
|
insert: "quux",
|
|
|
|
delete: 11..14,
|
|
|
|
},
|
|
|
|
],
|
2021-01-14 17:35:22 +00:00
|
|
|
},
|
2021-01-18 19:25:40 +00:00
|
|
|
},
|
|
|
|
file_system_edits: [
|
|
|
|
MoveFile {
|
|
|
|
src: FileId(
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
dst: AnchoredPathBuf {
|
|
|
|
anchor: FileId(
|
2020-10-02 14:13:48 +00:00
|
|
|
1,
|
2020-07-03 16:39:36 +00:00
|
|
|
),
|
2021-01-18 19:25:40 +00:00
|
|
|
path: "quux.rs",
|
2020-07-03 16:39:36 +00:00
|
|
|
},
|
2021-01-18 19:25:40 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
is_snippet: false,
|
2020-07-03 16:39:36 +00:00
|
|
|
}
|
|
|
|
"#]],
|
2020-06-11 21:40:59 +00:00
|
|
|
);
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_mod_in_dir() {
|
2020-07-03 16:35:43 +00:00
|
|
|
check_expect(
|
|
|
|
"foo2",
|
2020-06-16 16:45:58 +00:00
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2021-01-06 20:15:48 +00:00
|
|
|
mod fo$0o;
|
2020-06-16 16:45:58 +00:00
|
|
|
//- /foo/mod.rs
|
2021-01-08 14:46:48 +00:00
|
|
|
// empty
|
2020-07-03 16:35:43 +00:00
|
|
|
"#,
|
2020-07-03 16:39:36 +00:00
|
|
|
expect![[r#"
|
2021-01-18 19:25:40 +00:00
|
|
|
SourceChange {
|
|
|
|
source_file_edits: {
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
): TextEdit {
|
|
|
|
indels: [
|
|
|
|
Indel {
|
|
|
|
insert: "foo2",
|
|
|
|
delete: 4..7,
|
|
|
|
},
|
|
|
|
],
|
2021-01-14 17:35:22 +00:00
|
|
|
},
|
2021-01-18 19:25:40 +00:00
|
|
|
},
|
|
|
|
file_system_edits: [
|
|
|
|
MoveFile {
|
|
|
|
src: FileId(
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
dst: AnchoredPathBuf {
|
|
|
|
anchor: FileId(
|
2020-10-02 14:13:48 +00:00
|
|
|
1,
|
2020-07-03 16:39:36 +00:00
|
|
|
),
|
2021-01-18 19:25:40 +00:00
|
|
|
path: "../foo2/mod.rs",
|
2020-07-03 16:39:36 +00:00
|
|
|
},
|
2021-01-18 19:25:40 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
is_snippet: false,
|
2020-07-03 16:39:36 +00:00
|
|
|
}
|
|
|
|
"#]],
|
2019-10-12 15:47:17 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-07-03 16:45:18 +00:00
|
|
|
#[test]
|
|
|
|
fn test_rename_unusually_nested_mod() {
|
|
|
|
check_expect(
|
|
|
|
"bar",
|
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
2021-01-06 20:15:48 +00:00
|
|
|
mod outer { mod fo$0o; }
|
2020-07-03 16:45:18 +00:00
|
|
|
|
|
|
|
//- /outer/foo.rs
|
2021-01-08 14:46:48 +00:00
|
|
|
// empty
|
2020-07-03 16:45:18 +00:00
|
|
|
"#,
|
|
|
|
expect![[r#"
|
2021-01-18 19:25:40 +00:00
|
|
|
SourceChange {
|
|
|
|
source_file_edits: {
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
): TextEdit {
|
|
|
|
indels: [
|
|
|
|
Indel {
|
|
|
|
insert: "bar",
|
|
|
|
delete: 16..19,
|
|
|
|
},
|
|
|
|
],
|
2021-01-14 17:35:22 +00:00
|
|
|
},
|
2021-01-18 19:25:40 +00:00
|
|
|
},
|
|
|
|
file_system_edits: [
|
|
|
|
MoveFile {
|
|
|
|
src: FileId(
|
|
|
|
1,
|
|
|
|
),
|
|
|
|
dst: AnchoredPathBuf {
|
|
|
|
anchor: FileId(
|
2020-10-02 14:13:48 +00:00
|
|
|
1,
|
2020-07-03 16:45:18 +00:00
|
|
|
),
|
2021-01-18 19:25:40 +00:00
|
|
|
path: "bar.rs",
|
2020-07-03 16:45:18 +00:00
|
|
|
},
|
2021-01-18 19:25:40 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
is_snippet: false,
|
2020-07-03 16:45:18 +00:00
|
|
|
}
|
|
|
|
"#]],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-19 21:51:59 +00:00
|
|
|
#[test]
|
|
|
|
fn test_module_rename_in_path() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"baz",
|
2020-02-19 21:51:59 +00:00
|
|
|
r#"
|
2021-01-06 20:15:48 +00:00
|
|
|
mod $0foo { pub fn bar() {} }
|
2020-02-19 21:51:59 +00:00
|
|
|
|
2020-07-03 16:39:36 +00:00
|
|
|
fn main() { foo::bar(); }
|
2020-07-03 16:29:37 +00:00
|
|
|
"#,
|
2020-02-19 21:51:59 +00:00
|
|
|
r#"
|
2020-07-03 16:39:36 +00:00
|
|
|
mod baz { pub fn bar() {} }
|
2020-02-19 21:51:59 +00:00
|
|
|
|
2020-07-03 16:39:36 +00:00
|
|
|
fn main() { baz::bar(); }
|
2020-07-03 16:29:37 +00:00
|
|
|
"#,
|
2020-02-19 21:51:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_mod_filename_and_path() {
|
2020-07-03 16:35:43 +00:00
|
|
|
check_expect(
|
|
|
|
"foo2",
|
2020-06-16 16:45:58 +00:00
|
|
|
r#"
|
|
|
|
//- /lib.rs
|
|
|
|
mod bar;
|
|
|
|
fn f() {
|
|
|
|
bar::foo::fun()
|
|
|
|
}
|
2020-02-19 21:51:59 +00:00
|
|
|
|
2020-06-16 16:45:58 +00:00
|
|
|
//- /bar.rs
|
2021-01-06 20:15:48 +00:00
|
|
|
pub mod foo$0;
|
2020-02-19 21:51:59 +00:00
|
|
|
|
2020-06-16 16:45:58 +00:00
|
|
|
//- /bar/foo.rs
|
|
|
|
// pub fn fun() {}
|
2020-07-03 16:35:43 +00:00
|
|
|
"#,
|
2020-07-03 16:39:36 +00:00
|
|
|
expect![[r#"
|
2021-01-18 19:25:40 +00:00
|
|
|
SourceChange {
|
|
|
|
source_file_edits: {
|
|
|
|
FileId(
|
|
|
|
0,
|
|
|
|
): TextEdit {
|
|
|
|
indels: [
|
|
|
|
Indel {
|
|
|
|
insert: "foo2",
|
|
|
|
delete: 27..30,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
FileId(
|
|
|
|
1,
|
|
|
|
): TextEdit {
|
|
|
|
indels: [
|
|
|
|
Indel {
|
|
|
|
insert: "foo2",
|
|
|
|
delete: 8..11,
|
|
|
|
},
|
|
|
|
],
|
2021-01-14 17:35:22 +00:00
|
|
|
},
|
2021-01-18 19:25:40 +00:00
|
|
|
},
|
|
|
|
file_system_edits: [
|
|
|
|
MoveFile {
|
|
|
|
src: FileId(
|
|
|
|
2,
|
|
|
|
),
|
|
|
|
dst: AnchoredPathBuf {
|
|
|
|
anchor: FileId(
|
2020-10-02 14:13:48 +00:00
|
|
|
2,
|
2020-07-03 16:39:36 +00:00
|
|
|
),
|
2021-01-18 19:25:40 +00:00
|
|
|
path: "foo2.rs",
|
2020-07-03 16:39:36 +00:00
|
|
|
},
|
2021-01-18 19:25:40 +00:00
|
|
|
},
|
|
|
|
],
|
|
|
|
is_snippet: false,
|
2020-07-03 16:39:36 +00:00
|
|
|
}
|
|
|
|
"#]],
|
2020-02-19 21:51:59 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-06 14:17:35 +00:00
|
|
|
#[test]
|
|
|
|
fn test_enum_variant_from_module_1() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"Baz",
|
2020-05-06 14:17:35 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
mod foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
pub enum Foo { Bar$0 }
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
2020-05-06 14:17:35 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
fn func(f: foo::Foo) {
|
|
|
|
match f {
|
|
|
|
foo::Foo::Bar => {}
|
2020-05-06 14:17:35 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-05-06 14:17:35 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
mod foo {
|
2020-07-03 16:39:36 +00:00
|
|
|
pub enum Foo { Baz }
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
2020-05-06 14:17:35 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
fn func(f: foo::Foo) {
|
|
|
|
match f {
|
|
|
|
foo::Foo::Baz => {}
|
2020-05-06 14:17:35 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-05-06 14:17:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_enum_variant_from_module_2() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"baz",
|
2020-05-06 14:17:35 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
mod foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
pub struct Foo { pub bar$0: uint }
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
2020-05-06 14:17:35 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
fn foo(f: foo::Foo) {
|
|
|
|
let _ = f.bar;
|
|
|
|
}
|
|
|
|
"#,
|
2020-05-06 14:17:35 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
mod foo {
|
|
|
|
pub struct Foo { pub baz: uint }
|
|
|
|
}
|
2020-05-06 14:17:35 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
fn foo(f: foo::Foo) {
|
|
|
|
let _ = f.baz;
|
|
|
|
}
|
|
|
|
"#,
|
2020-05-06 14:17:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-04 05:12:18 +00:00
|
|
|
#[test]
|
|
|
|
fn test_parameter_to_self() {
|
2021-01-10 21:27:43 +00:00
|
|
|
mark::check!(rename_to_self);
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"self",
|
2020-05-04 05:12:18 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { i: i32 }
|
2020-05-04 05:12:18 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn f(foo$0: &mut Foo) -> i32 {
|
2020-07-03 16:29:37 +00:00
|
|
|
foo.i
|
2020-05-04 05:12:18 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-05-04 05:12:18 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { i: i32 }
|
2020-05-04 05:12:18 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
|
|
|
fn f(&mut self) -> i32 {
|
|
|
|
self.i
|
2020-05-04 05:12:18 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
2020-11-29 17:44:26 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"self",
|
|
|
|
r#"
|
|
|
|
struct Foo { i: i32 }
|
|
|
|
|
|
|
|
impl Foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn f(foo$0: Foo) -> i32 {
|
2020-11-29 17:44:26 +00:00
|
|
|
foo.i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo { i: i32 }
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn f(self) -> i32 {
|
|
|
|
self.i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parameter_to_self_error_no_impl() {
|
|
|
|
check(
|
|
|
|
"self",
|
|
|
|
r#"
|
|
|
|
struct Foo { i: i32 }
|
|
|
|
|
2021-01-06 20:15:48 +00:00
|
|
|
fn f(foo$0: &mut Foo) -> i32 {
|
2020-11-29 17:44:26 +00:00
|
|
|
foo.i
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"error: No impl block found for function",
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"self",
|
|
|
|
r#"
|
|
|
|
struct Foo { i: i32 }
|
|
|
|
struct Bar;
|
|
|
|
|
|
|
|
impl Bar {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn f(foo$0: &mut Foo) -> i32 {
|
2020-11-29 17:44:26 +00:00
|
|
|
foo.i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"error: Parameter type differs from impl block type",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parameter_to_self_error_not_first() {
|
|
|
|
check(
|
|
|
|
"self",
|
|
|
|
r#"
|
|
|
|
struct Foo { i: i32 }
|
|
|
|
impl Foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn f(x: (), foo$0: &mut Foo) -> i32 {
|
2020-11-29 17:44:26 +00:00
|
|
|
foo.i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
"error: Only the first parameter can be self",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_parameter_to_self_impl_ref() {
|
|
|
|
check(
|
|
|
|
"self",
|
|
|
|
r#"
|
|
|
|
struct Foo { i: i32 }
|
|
|
|
impl &Foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn f(foo$0: &Foo) -> i32 {
|
2020-11-29 17:44:26 +00:00
|
|
|
foo.i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo { i: i32 }
|
|
|
|
impl &Foo {
|
|
|
|
fn f(self) -> i32 {
|
|
|
|
self.i
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
"#,
|
2020-05-04 05:12:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_self_to_parameter() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"foo",
|
2020-05-04 05:12:18 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { i: i32 }
|
2020-05-04 05:12:18 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn f(&mut $0self) -> i32 {
|
2020-07-03 16:29:37 +00:00
|
|
|
self.i
|
2020-05-04 05:12:18 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-05-04 05:12:18 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { i: i32 }
|
2020-05-04 05:12:18 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
|
|
|
fn f(foo: &mut Foo) -> i32 {
|
|
|
|
foo.i
|
2020-05-04 05:12:18 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-05-04 05:12:18 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:56:36 +00:00
|
|
|
#[test]
|
|
|
|
fn test_owned_self_to_parameter() {
|
2021-01-15 20:07:38 +00:00
|
|
|
mark::check!(rename_self_to_param);
|
2020-11-29 16:56:36 +00:00
|
|
|
check(
|
|
|
|
"foo",
|
|
|
|
r#"
|
|
|
|
struct Foo { i: i32 }
|
|
|
|
|
|
|
|
impl Foo {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn f($0self) -> i32 {
|
2020-11-29 16:56:36 +00:00
|
|
|
self.i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo { i: i32 }
|
|
|
|
|
|
|
|
impl Foo {
|
|
|
|
fn f(foo: Foo) -> i32 {
|
|
|
|
foo.i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-04 05:12:18 +00:00
|
|
|
#[test]
|
|
|
|
fn test_self_in_path_to_parameter() {
|
2020-07-03 16:29:37 +00:00
|
|
|
check(
|
|
|
|
"foo",
|
2020-05-04 05:12:18 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { i: i32 }
|
2020-05-04 05:12:18 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
|
|
|
fn f(&self) -> i32 {
|
|
|
|
let self_var = 1;
|
2021-01-06 20:15:48 +00:00
|
|
|
self$0.i
|
2020-05-04 05:12:18 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-05-04 05:12:18 +00:00
|
|
|
r#"
|
2020-07-03 16:29:37 +00:00
|
|
|
struct Foo { i: i32 }
|
2020-05-04 05:12:18 +00:00
|
|
|
|
2020-07-03 16:29:37 +00:00
|
|
|
impl Foo {
|
|
|
|
fn f(foo: &Foo) -> i32 {
|
|
|
|
let self_var = 1;
|
|
|
|
foo.i
|
2020-05-04 05:12:18 +00:00
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
}
|
2020-11-14 16:49:36 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_initializer_use_field_init_shorthand() {
|
2020-11-15 15:11:06 +00:00
|
|
|
mark::check!(test_rename_field_expr_pat);
|
2020-11-14 16:49:36 +00:00
|
|
|
check(
|
|
|
|
"bar",
|
|
|
|
r#"
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo { i$0: i32 }
|
2020-11-14 16:49:36 +00:00
|
|
|
|
|
|
|
fn foo(bar: i32) -> Foo {
|
|
|
|
Foo { i: bar }
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo { bar: i32 }
|
|
|
|
|
|
|
|
fn foo(bar: i32) -> Foo {
|
|
|
|
Foo { bar }
|
|
|
|
}
|
2020-11-14 18:11:09 +00:00
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-15 15:11:06 +00:00
|
|
|
fn test_struct_field_destructure_into_shorthand() {
|
2020-11-14 18:11:09 +00:00
|
|
|
check(
|
2020-11-15 15:11:06 +00:00
|
|
|
"baz",
|
2020-11-14 18:11:09 +00:00
|
|
|
r#"
|
2021-01-06 20:15:48 +00:00
|
|
|
struct Foo { i$0: i32 }
|
2020-11-14 18:11:09 +00:00
|
|
|
|
|
|
|
fn foo(foo: Foo) {
|
2020-11-15 15:11:06 +00:00
|
|
|
let Foo { i: baz } = foo;
|
|
|
|
let _ = baz;
|
2020-11-14 18:11:09 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2020-11-15 15:11:06 +00:00
|
|
|
struct Foo { baz: i32 }
|
2020-11-14 18:11:09 +00:00
|
|
|
|
|
|
|
fn foo(foo: Foo) {
|
2020-11-15 15:11:06 +00:00
|
|
|
let Foo { baz } = foo;
|
|
|
|
let _ = baz;
|
2020-11-14 18:11:09 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_binding_in_destructure_pat() {
|
2020-11-15 15:11:06 +00:00
|
|
|
let expected_fixture = r#"
|
|
|
|
struct Foo {
|
|
|
|
i: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(foo: Foo) {
|
|
|
|
let Foo { i: bar } = foo;
|
|
|
|
let _ = bar;
|
|
|
|
}
|
|
|
|
"#;
|
2020-11-14 18:11:09 +00:00
|
|
|
check(
|
|
|
|
"bar",
|
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
i: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(foo: Foo) {
|
|
|
|
let Foo { i: b } = foo;
|
2021-01-06 20:15:48 +00:00
|
|
|
let _ = b$0;
|
2020-11-14 18:11:09 +00:00
|
|
|
}
|
|
|
|
"#,
|
2020-11-15 15:11:06 +00:00
|
|
|
expected_fixture,
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
"bar",
|
2020-11-14 18:11:09 +00:00
|
|
|
r#"
|
|
|
|
struct Foo {
|
|
|
|
i: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn foo(foo: Foo) {
|
2020-11-15 15:11:06 +00:00
|
|
|
let Foo { i } = foo;
|
2021-01-06 20:15:48 +00:00
|
|
|
let _ = i$0;
|
2020-11-14 18:11:09 +00:00
|
|
|
}
|
2020-11-14 18:57:47 +00:00
|
|
|
"#,
|
2020-11-15 15:11:06 +00:00
|
|
|
expected_fixture,
|
2020-11-14 18:57:47 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-11-15 15:11:06 +00:00
|
|
|
fn test_rename_binding_in_destructure_param_pat() {
|
2020-11-14 18:57:47 +00:00
|
|
|
check(
|
2020-11-15 15:11:06 +00:00
|
|
|
"bar",
|
2020-11-14 18:57:47 +00:00
|
|
|
r#"
|
2020-11-15 15:11:06 +00:00
|
|
|
struct Foo {
|
|
|
|
i: i32
|
|
|
|
}
|
2020-11-14 18:57:47 +00:00
|
|
|
|
2020-11-15 15:11:06 +00:00
|
|
|
fn foo(Foo { i }: foo) -> i32 {
|
2021-01-06 20:15:48 +00:00
|
|
|
i$0
|
2020-11-14 18:57:47 +00:00
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
2020-11-15 15:11:06 +00:00
|
|
|
struct Foo {
|
|
|
|
i: i32
|
|
|
|
}
|
2020-11-14 18:57:47 +00:00
|
|
|
|
2020-11-15 15:11:06 +00:00
|
|
|
fn foo(Foo { i: bar }: foo) -> i32 {
|
|
|
|
bar
|
2020-11-14 18:57:47 +00:00
|
|
|
}
|
2020-12-16 20:35:15 +00:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_lifetimes() {
|
2021-01-10 21:27:43 +00:00
|
|
|
mark::check!(rename_lifetime);
|
2020-12-16 20:35:15 +00:00
|
|
|
check(
|
|
|
|
"'yeeee",
|
|
|
|
r#"
|
|
|
|
trait Foo<'a> {
|
|
|
|
fn foo() -> &'a ();
|
|
|
|
}
|
|
|
|
impl<'a> Foo<'a> for &'a () {
|
2021-01-06 20:15:48 +00:00
|
|
|
fn foo() -> &'a$0 () {
|
2020-12-16 20:35:15 +00:00
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
trait Foo<'a> {
|
|
|
|
fn foo() -> &'a ();
|
|
|
|
}
|
|
|
|
impl<'yeeee> Foo<'yeeee> for &'yeeee () {
|
|
|
|
fn foo() -> &'yeeee () {
|
|
|
|
unimplemented!()
|
|
|
|
}
|
|
|
|
}
|
2020-07-03 16:29:37 +00:00
|
|
|
"#,
|
2020-11-15 15:11:06 +00:00
|
|
|
)
|
2020-05-04 05:12:18 +00:00
|
|
|
}
|
2020-12-19 18:37:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_bind_pat() {
|
|
|
|
check(
|
|
|
|
"new_name",
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
enum CustomOption<T> {
|
|
|
|
None,
|
|
|
|
Some(T),
|
|
|
|
}
|
|
|
|
|
|
|
|
let test_variable = CustomOption::Some(22);
|
|
|
|
|
|
|
|
match test_variable {
|
2021-01-06 20:15:48 +00:00
|
|
|
CustomOption::Some(foo$0) if foo == 11 => {}
|
2020-12-19 18:37:19 +00:00
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
r#"
|
|
|
|
fn main() {
|
|
|
|
enum CustomOption<T> {
|
|
|
|
None,
|
|
|
|
Some(T),
|
|
|
|
}
|
|
|
|
|
|
|
|
let test_variable = CustomOption::Some(22);
|
|
|
|
|
|
|
|
match test_variable {
|
|
|
|
CustomOption::Some(new_name) if new_name == 11 => {}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}"#,
|
|
|
|
);
|
|
|
|
}
|
2020-12-23 16:15:01 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_rename_label() {
|
|
|
|
check(
|
|
|
|
"'foo",
|
|
|
|
r#"
|
|
|
|
fn foo<'a>() -> &'a () {
|
|
|
|
'a: {
|
|
|
|
'b: loop {
|
2021-01-06 20:15:48 +00:00
|
|
|
break 'a$0;
|
2020-12-23 16:15:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
fn foo<'a>() -> &'a () {
|
|
|
|
'foo: {
|
|
|
|
'b: loop {
|
|
|
|
break 'foo;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-01-10 21:27:43 +00:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_self_to_self() {
|
|
|
|
mark::check!(rename_self_to_self);
|
|
|
|
check(
|
|
|
|
"self",
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(self$0) {}
|
|
|
|
}
|
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct Foo;
|
|
|
|
impl Foo {
|
|
|
|
fn foo(self) {}
|
|
|
|
}
|
2020-12-23 16:15:01 +00:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
}
|
2019-10-12 15:47:17 +00:00
|
|
|
}
|