mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 22:24:14 +00:00
restructure a bit
This commit is contained in:
parent
0dd08b8023
commit
d26d0ada50
11 changed files with 869 additions and 699 deletions
|
@ -10,7 +10,7 @@ use ra_syntax::{
|
||||||
use crate::{
|
use crate::{
|
||||||
db::RootDatabase,
|
db::RootDatabase,
|
||||||
display::ShortLabel,
|
display::ShortLabel,
|
||||||
name_kind::{classify_name_ref, NameKind::*},
|
references::{classify_name_ref, NameKind::*},
|
||||||
FilePosition, NavigationTarget, RangeInfo,
|
FilePosition, NavigationTarget, RangeInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -54,8 +54,7 @@ pub(crate) fn reference_definition(
|
||||||
) -> ReferenceResult {
|
) -> ReferenceResult {
|
||||||
use self::ReferenceResult::*;
|
use self::ReferenceResult::*;
|
||||||
|
|
||||||
let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
|
let name_kind = classify_name_ref(db, file_id, &name_ref).and_then(|d| Some(d.item));
|
||||||
let name_kind = classify_name_ref(db, file_id, &analyzer, &name_ref).and_then(|d| Some(d.item));
|
|
||||||
match name_kind {
|
match name_kind {
|
||||||
Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)),
|
Some(Macro(mac)) => return Exact(NavigationTarget::from_macro_def(db, mac)),
|
||||||
Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)),
|
Some(FieldAccess(field)) => return Exact(NavigationTarget::from_field(db, field)),
|
||||||
|
|
|
@ -14,7 +14,7 @@ use crate::{
|
||||||
description_from_symbol, docs_from_symbol, macro_label, rust_code_markup,
|
description_from_symbol, docs_from_symbol, macro_label, rust_code_markup,
|
||||||
rust_code_markup_with_doc, ShortLabel,
|
rust_code_markup_with_doc, ShortLabel,
|
||||||
},
|
},
|
||||||
name_kind::{classify_name_ref, NameKind::*},
|
references::{classify_name_ref, NameKind::*},
|
||||||
FilePosition, FileRange, RangeInfo,
|
FilePosition, FileRange, RangeInfo,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -99,11 +99,9 @@ pub(crate) fn hover(db: &RootDatabase, position: FilePosition) -> Option<RangeIn
|
||||||
|
|
||||||
let mut range = None;
|
let mut range = None;
|
||||||
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
|
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(file.syntax(), position.offset) {
|
||||||
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
|
|
||||||
|
|
||||||
let mut no_fallback = false;
|
let mut no_fallback = false;
|
||||||
let name_kind = classify_name_ref(db, position.file_id, &analyzer, &name_ref)
|
let name_kind =
|
||||||
.and_then(|d| Some(d.item));
|
classify_name_ref(db, position.file_id, &name_ref).and_then(|d| Some(d.item));
|
||||||
match name_kind {
|
match name_kind {
|
||||||
Some(Macro(it)) => {
|
Some(Macro(it)) => {
|
||||||
let src = it.source(db);
|
let src = it.source(db);
|
||||||
|
|
|
@ -19,7 +19,6 @@ mod feature_flags;
|
||||||
mod status;
|
mod status;
|
||||||
mod completion;
|
mod completion;
|
||||||
mod runnables;
|
mod runnables;
|
||||||
mod name_kind;
|
|
||||||
mod goto_definition;
|
mod goto_definition;
|
||||||
mod goto_type_definition;
|
mod goto_type_definition;
|
||||||
mod extend_selection;
|
mod extend_selection;
|
||||||
|
@ -41,7 +40,6 @@ mod matching_brace;
|
||||||
mod display;
|
mod display;
|
||||||
mod inlay_hints;
|
mod inlay_hints;
|
||||||
mod wasm_shims;
|
mod wasm_shims;
|
||||||
mod search_scope;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod marks;
|
mod marks;
|
||||||
|
|
|
@ -1,287 +0,0 @@
|
||||||
//! FIXME: write short doc here
|
|
||||||
|
|
||||||
use hir::{
|
|
||||||
db::AstDatabase, Adt, AssocItem, DefWithBody, Either, EnumVariant, FromSource, HasSource,
|
|
||||||
HirFileId, MacroDef, Module, ModuleDef, ModuleSource, Path, PathResolution, Source,
|
|
||||||
SourceAnalyzer, StructField, Ty, VariantDef,
|
|
||||||
};
|
|
||||||
use ra_db::FileId;
|
|
||||||
use ra_syntax::{ast, ast::VisibilityOwner, match_ast, AstNode, AstPtr};
|
|
||||||
|
|
||||||
use crate::db::RootDatabase;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq)]
|
|
||||||
pub enum NameKind {
|
|
||||||
Macro(MacroDef),
|
|
||||||
FieldAccess(StructField),
|
|
||||||
AssocItem(AssocItem),
|
|
||||||
Def(ModuleDef),
|
|
||||||
SelfType(Ty),
|
|
||||||
Pat((DefWithBody, AstPtr<ast::BindPat>)),
|
|
||||||
SelfParam(AstPtr<ast::SelfParam>),
|
|
||||||
GenericParam(u32),
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(PartialEq, Eq)]
|
|
||||||
pub(crate) struct Definition {
|
|
||||||
pub visibility: Option<ast::Visibility>,
|
|
||||||
pub container: Module,
|
|
||||||
pub item: NameKind,
|
|
||||||
}
|
|
||||||
|
|
||||||
trait HasDefinition {
|
|
||||||
type Def;
|
|
||||||
type Ref;
|
|
||||||
|
|
||||||
fn definition(self, db: &RootDatabase) -> Definition;
|
|
||||||
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition>;
|
|
||||||
fn from_ref(
|
|
||||||
db: &RootDatabase,
|
|
||||||
analyzer: &SourceAnalyzer,
|
|
||||||
refer: Self::Ref,
|
|
||||||
) -> Option<Definition>;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn classify_name_ref(
|
|
||||||
db: &RootDatabase,
|
|
||||||
file_id: FileId,
|
|
||||||
analyzer: &SourceAnalyzer,
|
|
||||||
name_ref: &ast::NameRef,
|
|
||||||
) -> Option<Definition> {
|
|
||||||
let parent = name_ref.syntax().parent()?;
|
|
||||||
match_ast! {
|
|
||||||
match parent {
|
|
||||||
ast::MethodCallExpr(it) => {
|
|
||||||
return AssocItem::from_ref(db, analyzer, it);
|
|
||||||
},
|
|
||||||
ast::FieldExpr(it) => {
|
|
||||||
if let Some(field) = analyzer.resolve_field(&it) {
|
|
||||||
return Some(field.definition(db));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
ast::RecordField(it) => {
|
|
||||||
if let Some(record_lit) = it.syntax().ancestors().find_map(ast::RecordLit::cast) {
|
|
||||||
let variant_def = analyzer.resolve_record_literal(&record_lit)?;
|
|
||||||
let hir_path = Path::from_name_ref(name_ref);
|
|
||||||
let hir_name = hir_path.as_ident()?;
|
|
||||||
let field = variant_def.field(db, hir_name)?;
|
|
||||||
return Some(field.definition(db));
|
|
||||||
}
|
|
||||||
},
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let ast = ModuleSource::from_child_node(db, file_id, &parent);
|
|
||||||
let file_id = file_id.into();
|
|
||||||
let container = Module::from_definition(db, Source { file_id, ast })?;
|
|
||||||
let visibility = None;
|
|
||||||
|
|
||||||
if let Some(macro_call) =
|
|
||||||
parent.parent().and_then(|node| node.parent()).and_then(ast::MacroCall::cast)
|
|
||||||
{
|
|
||||||
if let Some(mac) = analyzer.resolve_macro_call(db, ¯o_call) {
|
|
||||||
return Some(Definition { item: NameKind::Macro(mac), container, visibility });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// General case, a path or a local:
|
|
||||||
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
|
|
||||||
let resolved = analyzer.resolve_path(db, &path)?;
|
|
||||||
match resolved {
|
|
||||||
PathResolution::Def(def) => Some(def.definition(db)),
|
|
||||||
PathResolution::LocalBinding(Either::A(pat)) => decl_from_pat(db, file_id, pat),
|
|
||||||
PathResolution::LocalBinding(Either::B(par)) => {
|
|
||||||
Some(Definition { item: NameKind::SelfParam(par), container, visibility })
|
|
||||||
}
|
|
||||||
PathResolution::GenericParam(par) => {
|
|
||||||
// FIXME: get generic param def
|
|
||||||
Some(Definition { item: NameKind::GenericParam(par), container, visibility })
|
|
||||||
}
|
|
||||||
PathResolution::Macro(def) => {
|
|
||||||
Some(Definition { item: NameKind::Macro(def), container, visibility })
|
|
||||||
}
|
|
||||||
PathResolution::SelfType(impl_block) => {
|
|
||||||
let ty = impl_block.target_ty(db);
|
|
||||||
let container = impl_block.module();
|
|
||||||
Some(Definition { item: NameKind::SelfType(ty), container, visibility })
|
|
||||||
}
|
|
||||||
PathResolution::AssocItem(assoc) => Some(assoc.definition(db)),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn classify_name(
|
|
||||||
db: &RootDatabase,
|
|
||||||
file_id: FileId,
|
|
||||||
name: &ast::Name,
|
|
||||||
) -> Option<Definition> {
|
|
||||||
let parent = name.syntax().parent()?;
|
|
||||||
let file_id = file_id.into();
|
|
||||||
|
|
||||||
match_ast! {
|
|
||||||
match parent {
|
|
||||||
ast::BindPat(it) => {
|
|
||||||
decl_from_pat(db, file_id, AstPtr::new(&it))
|
|
||||||
},
|
|
||||||
ast::RecordFieldDef(it) => {
|
|
||||||
StructField::from_def(db, file_id, it)
|
|
||||||
},
|
|
||||||
ast::ImplItem(it) => {
|
|
||||||
AssocItem::from_def(db, file_id, it.clone()).or_else(|| {
|
|
||||||
match it {
|
|
||||||
ast::ImplItem::FnDef(f) => ModuleDef::from_def(db, file_id, f.into()),
|
|
||||||
ast::ImplItem::ConstDef(c) => ModuleDef::from_def(db, file_id, c.into()),
|
|
||||||
ast::ImplItem::TypeAliasDef(a) => ModuleDef::from_def(db, file_id, a.into()),
|
|
||||||
}
|
|
||||||
})
|
|
||||||
},
|
|
||||||
ast::EnumVariant(it) => {
|
|
||||||
let src = hir::Source { file_id, ast: it.clone() };
|
|
||||||
let def: ModuleDef = EnumVariant::from_source(db, src)?.into();
|
|
||||||
Some(def.definition(db))
|
|
||||||
},
|
|
||||||
ast::ModuleItem(it) => {
|
|
||||||
ModuleDef::from_def(db, file_id, it)
|
|
||||||
},
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn decl_from_pat(
|
|
||||||
db: &RootDatabase,
|
|
||||||
file_id: HirFileId,
|
|
||||||
pat: AstPtr<ast::BindPat>,
|
|
||||||
) -> Option<Definition> {
|
|
||||||
let root = db.parse_or_expand(file_id)?;
|
|
||||||
// FIXME: use match_ast!
|
|
||||||
let def = pat.to_node(&root).syntax().ancestors().find_map(|node| {
|
|
||||||
if let Some(it) = ast::FnDef::cast(node.clone()) {
|
|
||||||
let src = hir::Source { file_id, ast: it };
|
|
||||||
Some(hir::Function::from_source(db, src)?.into())
|
|
||||||
} else if let Some(it) = ast::ConstDef::cast(node.clone()) {
|
|
||||||
let src = hir::Source { file_id, ast: it };
|
|
||||||
Some(hir::Const::from_source(db, src)?.into())
|
|
||||||
} else if let Some(it) = ast::StaticDef::cast(node.clone()) {
|
|
||||||
let src = hir::Source { file_id, ast: it };
|
|
||||||
Some(hir::Static::from_source(db, src)?.into())
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
})?;
|
|
||||||
let item = NameKind::Pat((def, pat));
|
|
||||||
let container = def.module(db);
|
|
||||||
Some(Definition { item, container, visibility: None })
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasDefinition for StructField {
|
|
||||||
type Def = ast::RecordFieldDef;
|
|
||||||
type Ref = ast::FieldExpr;
|
|
||||||
|
|
||||||
fn definition(self, db: &RootDatabase) -> Definition {
|
|
||||||
let item = NameKind::FieldAccess(self);
|
|
||||||
let parent = self.parent_def(db);
|
|
||||||
let container = parent.module(db);
|
|
||||||
let visibility = match parent {
|
|
||||||
VariantDef::Struct(s) => s.source(db).ast.visibility(),
|
|
||||||
VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(),
|
|
||||||
};
|
|
||||||
Definition { item, container, visibility }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition> {
|
|
||||||
let src = hir::Source { file_id, ast: hir::FieldSource::Named(def) };
|
|
||||||
let field = StructField::from_source(db, src)?;
|
|
||||||
Some(field.definition(db))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_ref(
|
|
||||||
db: &RootDatabase,
|
|
||||||
analyzer: &SourceAnalyzer,
|
|
||||||
refer: Self::Ref,
|
|
||||||
) -> Option<Definition> {
|
|
||||||
let field = analyzer.resolve_field(&refer)?;
|
|
||||||
Some(field.definition(db))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasDefinition for AssocItem {
|
|
||||||
type Def = ast::ImplItem;
|
|
||||||
type Ref = ast::MethodCallExpr;
|
|
||||||
|
|
||||||
fn definition(self, db: &RootDatabase) -> Definition {
|
|
||||||
let item = NameKind::AssocItem(self);
|
|
||||||
let container = self.module(db);
|
|
||||||
let visibility = match self {
|
|
||||||
AssocItem::Function(f) => f.source(db).ast.visibility(),
|
|
||||||
AssocItem::Const(c) => c.source(db).ast.visibility(),
|
|
||||||
AssocItem::TypeAlias(a) => a.source(db).ast.visibility(),
|
|
||||||
};
|
|
||||||
Definition { item, container, visibility }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition> {
|
|
||||||
if def.syntax().parent().and_then(ast::ItemList::cast).is_none() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
let src = hir::Source { file_id, ast: def };
|
|
||||||
let item = AssocItem::from_source(db, src)?;
|
|
||||||
Some(item.definition(db))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_ref(
|
|
||||||
db: &RootDatabase,
|
|
||||||
analyzer: &SourceAnalyzer,
|
|
||||||
refer: Self::Ref,
|
|
||||||
) -> Option<Definition> {
|
|
||||||
let func: AssocItem = analyzer.resolve_method_call(&refer)?.into();
|
|
||||||
Some(func.definition(db))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl HasDefinition for ModuleDef {
|
|
||||||
type Def = ast::ModuleItem;
|
|
||||||
type Ref = ast::Path;
|
|
||||||
|
|
||||||
fn definition(self, db: &RootDatabase) -> Definition {
|
|
||||||
let (container, visibility) = match self {
|
|
||||||
ModuleDef::Module(it) => {
|
|
||||||
let container = it.parent(db).or_else(|| Some(it)).unwrap();
|
|
||||||
let visibility = it.declaration_source(db).and_then(|s| s.ast.visibility());
|
|
||||||
(container, visibility)
|
|
||||||
}
|
|
||||||
ModuleDef::EnumVariant(it) => {
|
|
||||||
let container = it.module(db);
|
|
||||||
let visibility = it.source(db).ast.parent_enum().visibility();
|
|
||||||
(container, visibility)
|
|
||||||
}
|
|
||||||
ModuleDef::Function(it) => (it.module(db), it.source(db).ast.visibility()),
|
|
||||||
ModuleDef::Const(it) => (it.module(db), it.source(db).ast.visibility()),
|
|
||||||
ModuleDef::Static(it) => (it.module(db), it.source(db).ast.visibility()),
|
|
||||||
ModuleDef::Trait(it) => (it.module(db), it.source(db).ast.visibility()),
|
|
||||||
ModuleDef::TypeAlias(it) => (it.module(db), it.source(db).ast.visibility()),
|
|
||||||
ModuleDef::Adt(Adt::Struct(it)) => (it.module(db), it.source(db).ast.visibility()),
|
|
||||||
ModuleDef::Adt(Adt::Union(it)) => (it.module(db), it.source(db).ast.visibility()),
|
|
||||||
ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).ast.visibility()),
|
|
||||||
ModuleDef::BuiltinType(..) => unreachable!(),
|
|
||||||
};
|
|
||||||
let item = NameKind::Def(self);
|
|
||||||
Definition { item, container, visibility }
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition> {
|
|
||||||
let src = hir::Source { file_id, ast: def };
|
|
||||||
let def = ModuleDef::from_source(db, src)?;
|
|
||||||
Some(def.definition(db))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn from_ref(
|
|
||||||
db: &RootDatabase,
|
|
||||||
analyzer: &SourceAnalyzer,
|
|
||||||
refer: Self::Ref,
|
|
||||||
) -> Option<Definition> {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME: impl HasDefinition for hir::MacroDef
|
|
|
@ -1,16 +1,19 @@
|
||||||
//! FIXME: write short doc here
|
//! FIXME: write short doc here
|
||||||
|
|
||||||
use hir::ModuleSource;
|
mod classify;
|
||||||
use ra_db::{SourceDatabase, SourceDatabaseExt};
|
mod definition;
|
||||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode};
|
mod rename;
|
||||||
use relative_path::{RelativePath, RelativePathBuf};
|
mod search_scope;
|
||||||
|
|
||||||
use crate::{
|
use ra_db::{SourceDatabase, SourceDatabaseExt};
|
||||||
db::RootDatabase,
|
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, SyntaxNode, TextUnit};
|
||||||
name_kind::{classify_name, classify_name_ref, Definition, NameKind::*},
|
|
||||||
search_scope::find_refs,
|
use crate::{db::RootDatabase, FileId, FilePosition, FileRange, NavigationTarget, RangeInfo};
|
||||||
FileId, FilePosition, FileRange, FileSystemEdit, NavigationTarget, RangeInfo, SourceChange,
|
|
||||||
SourceFileEdit, TextRange,
|
pub(crate) use self::{
|
||||||
|
classify::{classify_name, classify_name_ref},
|
||||||
|
definition::{Definition, NameKind},
|
||||||
|
rename::rename,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
|
@ -59,30 +62,23 @@ pub(crate) fn find_all_refs(
|
||||||
let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?;
|
let RangeInfo { range, info: (name, def) } = find_name(db, &syntax, position)?;
|
||||||
|
|
||||||
let declaration = match def.item {
|
let declaration = match def.item {
|
||||||
Macro(mac) => NavigationTarget::from_macro_def(db, mac),
|
NameKind::Macro(mac) => NavigationTarget::from_macro_def(db, mac),
|
||||||
FieldAccess(field) => NavigationTarget::from_field(db, field),
|
NameKind::FieldAccess(field) => NavigationTarget::from_field(db, field),
|
||||||
AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc),
|
NameKind::AssocItem(assoc) => NavigationTarget::from_assoc_item(db, assoc),
|
||||||
Def(def) => NavigationTarget::from_def(db, def)?,
|
NameKind::Def(def) => NavigationTarget::from_def(db, def)?,
|
||||||
SelfType(ref ty) => match ty.as_adt() {
|
NameKind::SelfType(ref ty) => match ty.as_adt() {
|
||||||
Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id),
|
Some((def_id, _)) => NavigationTarget::from_adt_def(db, def_id),
|
||||||
None => return None,
|
None => return None,
|
||||||
},
|
},
|
||||||
Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat),
|
NameKind::Pat((_, pat)) => NavigationTarget::from_pat(db, position.file_id, pat),
|
||||||
SelfParam(par) => NavigationTarget::from_self_param(position.file_id, par),
|
NameKind::SelfParam(par) => NavigationTarget::from_self_param(position.file_id, par),
|
||||||
GenericParam(_) => return None,
|
NameKind::GenericParam(_) => return None,
|
||||||
};
|
};
|
||||||
|
|
||||||
// let references = match name_kind {
|
let references = process_definition(db, def, name);
|
||||||
// Pat((_, pat)) => analyzer
|
|
||||||
// .find_all_refs(&pat.to_node(&syntax))
|
|
||||||
// .into_iter()
|
|
||||||
// .map(move |ref_desc| FileRange { file_id: position.file_id, range: ref_desc.range })
|
|
||||||
// .collect::<Vec<_>>(),
|
|
||||||
// _ => vec![],
|
|
||||||
// };
|
|
||||||
let references = find_refs(db, def, name);
|
|
||||||
|
|
||||||
return Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }));
|
Some(RangeInfo::new(range, ReferenceSearchResult { declaration, references }))
|
||||||
|
}
|
||||||
|
|
||||||
fn find_name<'a>(
|
fn find_name<'a>(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
|
@ -95,133 +91,55 @@ pub(crate) fn find_all_refs(
|
||||||
return Some(RangeInfo::new(range, (name.text().to_string(), def)));
|
return Some(RangeInfo::new(range, (name.text().to_string(), def)));
|
||||||
}
|
}
|
||||||
let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?;
|
let name_ref = find_node_at_offset::<ast::NameRef>(&syntax, position.offset)?;
|
||||||
|
let def = classify_name_ref(db, position.file_id, &name_ref)?;
|
||||||
let range = name_ref.syntax().text_range();
|
let range = name_ref.syntax().text_range();
|
||||||
let analyzer = hir::SourceAnalyzer::new(db, position.file_id, name_ref.syntax(), None);
|
|
||||||
let def = classify_name_ref(db, position.file_id, &analyzer, &name_ref)?;
|
|
||||||
Some(RangeInfo::new(range, (name_ref.text().to_string(), def)))
|
Some(RangeInfo::new(range, (name_ref.text().to_string(), def)))
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn rename(
|
fn process_definition(db: &RootDatabase, def: Definition, name: String) -> Vec<FileRange> {
|
||||||
db: &RootDatabase,
|
let pat = name.as_str();
|
||||||
position: FilePosition,
|
let scope = def.scope(db).scope;
|
||||||
new_name: &str,
|
let mut refs = vec![];
|
||||||
) -> Option<RangeInfo<SourceChange>> {
|
|
||||||
let parse = db.parse(position.file_id);
|
let is_match = |file_id: FileId, name_ref: &ast::NameRef| -> bool {
|
||||||
if let Some((ast_name, ast_module)) =
|
let classified = classify_name_ref(db, file_id, &name_ref);
|
||||||
find_name_and_module_at_offset(parse.tree().syntax(), position)
|
if let Some(d) = classified {
|
||||||
{
|
d == def
|
||||||
let range = ast_name.syntax().text_range();
|
|
||||||
rename_mod(db, &ast_name, &ast_module, position, new_name)
|
|
||||||
.map(|info| RangeInfo::new(range, info))
|
|
||||||
} else {
|
} else {
|
||||||
rename_reference(db, position, new_name)
|
false
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn find_name_and_module_at_offset(
|
|
||||||
syntax: &SyntaxNode,
|
|
||||||
position: FilePosition,
|
|
||||||
) -> Option<(ast::Name, ast::Module)> {
|
|
||||||
let ast_name = find_node_at_offset::<ast::Name>(syntax, position.offset)?;
|
|
||||||
let ast_module = ast::Module::cast(ast_name.syntax().parent()?)?;
|
|
||||||
Some((ast_name, ast_module))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn source_edit_from_file_id_range(
|
|
||||||
file_id: FileId,
|
|
||||||
range: TextRange,
|
|
||||||
new_name: &str,
|
|
||||||
) -> SourceFileEdit {
|
|
||||||
SourceFileEdit {
|
|
||||||
file_id,
|
|
||||||
edit: {
|
|
||||||
let mut builder = ra_text_edit::TextEditBuilder::default();
|
|
||||||
builder.replace(range, new_name.into());
|
|
||||||
builder.finish()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rename_mod(
|
|
||||||
db: &RootDatabase,
|
|
||||||
ast_name: &ast::Name,
|
|
||||||
ast_module: &ast::Module,
|
|
||||||
position: FilePosition,
|
|
||||||
new_name: &str,
|
|
||||||
) -> Option<SourceChange> {
|
|
||||||
let mut source_file_edits = Vec::new();
|
|
||||||
let mut file_system_edits = Vec::new();
|
|
||||||
let module_src = hir::Source { file_id: position.file_id.into(), ast: ast_module.clone() };
|
|
||||||
if let Some(module) = hir::Module::from_declaration(db, module_src) {
|
|
||||||
let src = module.definition_source(db);
|
|
||||||
let file_id = src.file_id.original_file(db);
|
|
||||||
match src.ast {
|
|
||||||
ModuleSource::SourceFile(..) => {
|
|
||||||
let mod_path: RelativePathBuf = db.file_relative_path(file_id);
|
|
||||||
// mod is defined in path/to/dir/mod.rs
|
|
||||||
let dst_path = if mod_path.file_stem() == Some("mod") {
|
|
||||||
mod_path
|
|
||||||
.parent()
|
|
||||||
.and_then(|p| p.parent())
|
|
||||||
.or_else(|| Some(RelativePath::new("")))
|
|
||||||
.map(|p| p.join(new_name).join("mod.rs"))
|
|
||||||
} else {
|
|
||||||
Some(mod_path.with_file_name(new_name).with_extension("rs"))
|
|
||||||
};
|
};
|
||||||
if let Some(path) = dst_path {
|
|
||||||
let move_file = FileSystemEdit::MoveFile {
|
for (file_id, text_range) in scope {
|
||||||
src: file_id,
|
let text = db.file_text(file_id);
|
||||||
dst_source_root: db.file_source_root(position.file_id),
|
let parse = SourceFile::parse(&text);
|
||||||
dst_path: path,
|
let syntax = parse.tree().syntax().clone();
|
||||||
};
|
|
||||||
file_system_edits.push(move_file);
|
for (idx, _) in text.match_indices(pat) {
|
||||||
|
let offset = TextUnit::from_usize(idx);
|
||||||
|
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&syntax, offset) {
|
||||||
|
let range = name_ref.syntax().text_range();
|
||||||
|
|
||||||
|
if let Some(text_range) = text_range {
|
||||||
|
if range.is_subrange(&text_range) && is_match(file_id, &name_ref) {
|
||||||
|
refs.push(FileRange { file_id, range });
|
||||||
|
}
|
||||||
|
} else if is_match(file_id, &name_ref) {
|
||||||
|
refs.push(FileRange { file_id, range });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ModuleSource::Module(..) => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let edit = SourceFileEdit {
|
return refs;
|
||||||
file_id: position.file_id,
|
|
||||||
edit: {
|
|
||||||
let mut builder = ra_text_edit::TextEditBuilder::default();
|
|
||||||
builder.replace(ast_name.syntax().text_range(), new_name.into());
|
|
||||||
builder.finish()
|
|
||||||
},
|
|
||||||
};
|
|
||||||
source_file_edits.push(edit);
|
|
||||||
|
|
||||||
Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn rename_reference(
|
|
||||||
db: &RootDatabase,
|
|
||||||
position: FilePosition,
|
|
||||||
new_name: &str,
|
|
||||||
) -> Option<RangeInfo<SourceChange>> {
|
|
||||||
let RangeInfo { range, info: refs } = find_all_refs(db, position)?;
|
|
||||||
|
|
||||||
let edit = refs
|
|
||||||
.into_iter()
|
|
||||||
.map(|range| source_edit_from_file_id_range(range.file_id, range.range, new_name))
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
if edit.is_empty() {
|
|
||||||
return None;
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(RangeInfo::new(range, SourceChange::source_file_edits("rename", edit)))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use crate::{
|
use crate::{
|
||||||
mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId,
|
mock_analysis::analysis_and_position, mock_analysis::single_file_with_position,
|
||||||
ReferenceSearchResult,
|
ReferenceSearchResult,
|
||||||
};
|
};
|
||||||
use insta::assert_debug_snapshot;
|
|
||||||
use test_utils::assert_eq_text;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_find_all_refs_for_local() {
|
fn test_find_all_refs_for_local() {
|
||||||
|
@ -353,207 +271,4 @@ mod tests {
|
||||||
let (analysis, position) = single_file_with_position(text);
|
let (analysis, position) = single_file_with_position(text);
|
||||||
analysis.find_all_refs(position).unwrap().unwrap()
|
analysis.find_all_refs(position).unwrap().unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_rename_for_local() {
|
|
||||||
test_rename(
|
|
||||||
r#"
|
|
||||||
fn main() {
|
|
||||||
let mut i = 1;
|
|
||||||
let j = 1;
|
|
||||||
i = i<|> + j;
|
|
||||||
|
|
||||||
{
|
|
||||||
i = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
i = 5;
|
|
||||||
}"#,
|
|
||||||
"k",
|
|
||||||
r#"
|
|
||||||
fn main() {
|
|
||||||
let mut k = 1;
|
|
||||||
let j = 1;
|
|
||||||
k = k + j;
|
|
||||||
|
|
||||||
{
|
|
||||||
k = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
k = 5;
|
|
||||||
}"#,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_rename_for_param_inside() {
|
|
||||||
test_rename(
|
|
||||||
r#"
|
|
||||||
fn foo(i : u32) -> u32 {
|
|
||||||
i<|>
|
|
||||||
}"#,
|
|
||||||
"j",
|
|
||||||
r#"
|
|
||||||
fn foo(j : u32) -> u32 {
|
|
||||||
j
|
|
||||||
}"#,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_rename_refs_for_fn_param() {
|
|
||||||
test_rename(
|
|
||||||
r#"
|
|
||||||
fn foo(i<|> : u32) -> u32 {
|
|
||||||
i
|
|
||||||
}"#,
|
|
||||||
"new_name",
|
|
||||||
r#"
|
|
||||||
fn foo(new_name : u32) -> u32 {
|
|
||||||
new_name
|
|
||||||
}"#,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_rename_for_mut_param() {
|
|
||||||
test_rename(
|
|
||||||
r#"
|
|
||||||
fn foo(mut i<|> : u32) -> u32 {
|
|
||||||
i
|
|
||||||
}"#,
|
|
||||||
"new_name",
|
|
||||||
r#"
|
|
||||||
fn foo(mut new_name : u32) -> u32 {
|
|
||||||
new_name
|
|
||||||
}"#,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_rename_mod() {
|
|
||||||
let (analysis, position) = analysis_and_position(
|
|
||||||
"
|
|
||||||
//- /lib.rs
|
|
||||||
mod bar;
|
|
||||||
|
|
||||||
//- /bar.rs
|
|
||||||
mod foo<|>;
|
|
||||||
|
|
||||||
//- /bar/foo.rs
|
|
||||||
// emtpy
|
|
||||||
",
|
|
||||||
);
|
|
||||||
let new_name = "foo2";
|
|
||||||
let source_change = analysis.rename(position, new_name).unwrap();
|
|
||||||
assert_debug_snapshot!(&source_change,
|
|
||||||
@r###"
|
|
||||||
Some(
|
|
||||||
RangeInfo {
|
|
||||||
range: [4; 7),
|
|
||||||
info: SourceChange {
|
|
||||||
label: "rename",
|
|
||||||
source_file_edits: [
|
|
||||||
SourceFileEdit {
|
|
||||||
file_id: FileId(
|
|
||||||
2,
|
|
||||||
),
|
|
||||||
edit: TextEdit {
|
|
||||||
atoms: [
|
|
||||||
AtomTextEdit {
|
|
||||||
delete: [4; 7),
|
|
||||||
insert: "foo2",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
file_system_edits: [
|
|
||||||
MoveFile {
|
|
||||||
src: FileId(
|
|
||||||
3,
|
|
||||||
),
|
|
||||||
dst_source_root: SourceRootId(
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
dst_path: "bar/foo2.rs",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
cursor_position: None,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
"###);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_rename_mod_in_dir() {
|
|
||||||
let (analysis, position) = analysis_and_position(
|
|
||||||
"
|
|
||||||
//- /lib.rs
|
|
||||||
mod fo<|>o;
|
|
||||||
//- /foo/mod.rs
|
|
||||||
// emtpy
|
|
||||||
",
|
|
||||||
);
|
|
||||||
let new_name = "foo2";
|
|
||||||
let source_change = analysis.rename(position, new_name).unwrap();
|
|
||||||
assert_debug_snapshot!(&source_change,
|
|
||||||
@r###"
|
|
||||||
Some(
|
|
||||||
RangeInfo {
|
|
||||||
range: [4; 7),
|
|
||||||
info: SourceChange {
|
|
||||||
label: "rename",
|
|
||||||
source_file_edits: [
|
|
||||||
SourceFileEdit {
|
|
||||||
file_id: FileId(
|
|
||||||
1,
|
|
||||||
),
|
|
||||||
edit: TextEdit {
|
|
||||||
atoms: [
|
|
||||||
AtomTextEdit {
|
|
||||||
delete: [4; 7),
|
|
||||||
insert: "foo2",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
file_system_edits: [
|
|
||||||
MoveFile {
|
|
||||||
src: FileId(
|
|
||||||
2,
|
|
||||||
),
|
|
||||||
dst_source_root: SourceRootId(
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
dst_path: "foo2/mod.rs",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
cursor_position: None,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
)
|
|
||||||
"###
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn test_rename(text: &str, new_name: &str, expected: &str) {
|
|
||||||
let (analysis, position) = single_file_with_position(text);
|
|
||||||
let source_change = analysis.rename(position, new_name).unwrap();
|
|
||||||
let mut text_edit_builder = ra_text_edit::TextEditBuilder::default();
|
|
||||||
let mut file_id: Option<FileId> = None;
|
|
||||||
if let Some(change) = source_change {
|
|
||||||
for edit in change.info.source_file_edits {
|
|
||||||
file_id = Some(edit.file_id);
|
|
||||||
for atom in edit.edit.as_atoms() {
|
|
||||||
text_edit_builder.replace(atom.delete, atom.insert.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let result =
|
|
||||||
text_edit_builder.finish().apply(&*analysis.file_text(file_id.unwrap()).unwrap());
|
|
||||||
assert_eq_text!(expected, &*result);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
143
crates/ra_ide_api/src/references/classify.rs
Normal file
143
crates/ra_ide_api/src/references/classify.rs
Normal file
|
@ -0,0 +1,143 @@
|
||||||
|
use hir::{
|
||||||
|
AssocItem, Either, EnumVariant, FromSource, Module, ModuleDef, ModuleSource, Path,
|
||||||
|
PathResolution, Source, SourceAnalyzer, StructField,
|
||||||
|
};
|
||||||
|
use ra_db::FileId;
|
||||||
|
use ra_syntax::{ast, match_ast, AstNode, AstPtr};
|
||||||
|
|
||||||
|
use super::{definition::HasDefinition, Definition, NameKind};
|
||||||
|
use crate::db::RootDatabase;
|
||||||
|
|
||||||
|
use hir::{db::AstDatabase, HirFileId};
|
||||||
|
|
||||||
|
pub(crate) fn classify_name(
|
||||||
|
db: &RootDatabase,
|
||||||
|
file_id: FileId,
|
||||||
|
name: &ast::Name,
|
||||||
|
) -> Option<Definition> {
|
||||||
|
let parent = name.syntax().parent()?;
|
||||||
|
let file_id = file_id.into();
|
||||||
|
|
||||||
|
match_ast! {
|
||||||
|
match parent {
|
||||||
|
ast::BindPat(it) => {
|
||||||
|
decl_from_pat(db, file_id, AstPtr::new(&it))
|
||||||
|
},
|
||||||
|
ast::RecordFieldDef(it) => {
|
||||||
|
StructField::from_def(db, file_id, it)
|
||||||
|
},
|
||||||
|
ast::ImplItem(it) => {
|
||||||
|
AssocItem::from_def(db, file_id, it.clone()).or_else(|| {
|
||||||
|
match it {
|
||||||
|
ast::ImplItem::FnDef(f) => ModuleDef::from_def(db, file_id, f.into()),
|
||||||
|
ast::ImplItem::ConstDef(c) => ModuleDef::from_def(db, file_id, c.into()),
|
||||||
|
ast::ImplItem::TypeAliasDef(a) => ModuleDef::from_def(db, file_id, a.into()),
|
||||||
|
}
|
||||||
|
})
|
||||||
|
},
|
||||||
|
ast::EnumVariant(it) => {
|
||||||
|
let src = hir::Source { file_id, ast: it.clone() };
|
||||||
|
let def: ModuleDef = EnumVariant::from_source(db, src)?.into();
|
||||||
|
Some(def.definition(db))
|
||||||
|
},
|
||||||
|
ast::ModuleItem(it) => {
|
||||||
|
ModuleDef::from_def(db, file_id, it)
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn classify_name_ref(
|
||||||
|
db: &RootDatabase,
|
||||||
|
file_id: FileId,
|
||||||
|
name_ref: &ast::NameRef,
|
||||||
|
) -> Option<Definition> {
|
||||||
|
let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
|
||||||
|
let parent = name_ref.syntax().parent()?;
|
||||||
|
match_ast! {
|
||||||
|
match parent {
|
||||||
|
ast::MethodCallExpr(it) => {
|
||||||
|
return AssocItem::from_ref(db, &analyzer, it);
|
||||||
|
},
|
||||||
|
ast::FieldExpr(it) => {
|
||||||
|
if let Some(field) = analyzer.resolve_field(&it) {
|
||||||
|
return Some(field.definition(db));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ast::RecordField(it) => {
|
||||||
|
if let Some(record_lit) = it.syntax().ancestors().find_map(ast::RecordLit::cast) {
|
||||||
|
let variant_def = analyzer.resolve_record_literal(&record_lit)?;
|
||||||
|
let hir_path = Path::from_name_ref(name_ref);
|
||||||
|
let hir_name = hir_path.as_ident()?;
|
||||||
|
let field = variant_def.field(db, hir_name)?;
|
||||||
|
return Some(field.definition(db));
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let ast = ModuleSource::from_child_node(db, file_id, &parent);
|
||||||
|
let file_id = file_id.into();
|
||||||
|
let container = Module::from_definition(db, Source { file_id, ast })?;
|
||||||
|
let visibility = None;
|
||||||
|
|
||||||
|
if let Some(macro_call) =
|
||||||
|
parent.parent().and_then(|node| node.parent()).and_then(ast::MacroCall::cast)
|
||||||
|
{
|
||||||
|
if let Some(mac) = analyzer.resolve_macro_call(db, ¯o_call) {
|
||||||
|
return Some(Definition { item: NameKind::Macro(mac), container, visibility });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// General case, a path or a local:
|
||||||
|
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
|
||||||
|
let resolved = analyzer.resolve_path(db, &path)?;
|
||||||
|
match resolved {
|
||||||
|
PathResolution::Def(def) => Some(def.definition(db)),
|
||||||
|
PathResolution::LocalBinding(Either::A(pat)) => decl_from_pat(db, file_id, pat),
|
||||||
|
PathResolution::LocalBinding(Either::B(par)) => {
|
||||||
|
Some(Definition { item: NameKind::SelfParam(par), container, visibility })
|
||||||
|
}
|
||||||
|
PathResolution::GenericParam(par) => {
|
||||||
|
// FIXME: get generic param def
|
||||||
|
Some(Definition { item: NameKind::GenericParam(par), container, visibility })
|
||||||
|
}
|
||||||
|
PathResolution::Macro(def) => {
|
||||||
|
Some(Definition { item: NameKind::Macro(def), container, visibility })
|
||||||
|
}
|
||||||
|
PathResolution::SelfType(impl_block) => {
|
||||||
|
let ty = impl_block.target_ty(db);
|
||||||
|
let container = impl_block.module();
|
||||||
|
Some(Definition { item: NameKind::SelfType(ty), container, visibility })
|
||||||
|
}
|
||||||
|
PathResolution::AssocItem(assoc) => Some(assoc.definition(db)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn decl_from_pat(
|
||||||
|
db: &RootDatabase,
|
||||||
|
file_id: HirFileId,
|
||||||
|
pat: AstPtr<ast::BindPat>,
|
||||||
|
) -> Option<Definition> {
|
||||||
|
let root = db.parse_or_expand(file_id)?;
|
||||||
|
// FIXME: use match_ast!
|
||||||
|
let def = pat.to_node(&root).syntax().ancestors().find_map(|node| {
|
||||||
|
if let Some(it) = ast::FnDef::cast(node.clone()) {
|
||||||
|
let src = hir::Source { file_id, ast: it };
|
||||||
|
Some(hir::Function::from_source(db, src)?.into())
|
||||||
|
} else if let Some(it) = ast::ConstDef::cast(node.clone()) {
|
||||||
|
let src = hir::Source { file_id, ast: it };
|
||||||
|
Some(hir::Const::from_source(db, src)?.into())
|
||||||
|
} else if let Some(it) = ast::StaticDef::cast(node.clone()) {
|
||||||
|
let src = hir::Source { file_id, ast: it };
|
||||||
|
Some(hir::Static::from_source(db, src)?.into())
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
let item = NameKind::Pat((def, pat));
|
||||||
|
let container = def.module(db);
|
||||||
|
Some(Definition { item, container, visibility: None })
|
||||||
|
}
|
177
crates/ra_ide_api/src/references/definition.rs
Normal file
177
crates/ra_ide_api/src/references/definition.rs
Normal file
|
@ -0,0 +1,177 @@
|
||||||
|
use hir::{
|
||||||
|
db::AstDatabase, Adt, AssocItem, DefWithBody, FromSource, HasSource, HirFileId, MacroDef,
|
||||||
|
Module, ModuleDef, SourceAnalyzer, StructField, Ty, VariantDef,
|
||||||
|
};
|
||||||
|
use ra_syntax::{ast, ast::VisibilityOwner, AstNode, AstPtr};
|
||||||
|
|
||||||
|
use crate::db::RootDatabase;
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq, Eq)]
|
||||||
|
pub enum NameKind {
|
||||||
|
Macro(MacroDef),
|
||||||
|
FieldAccess(StructField),
|
||||||
|
AssocItem(AssocItem),
|
||||||
|
Def(ModuleDef),
|
||||||
|
SelfType(Ty),
|
||||||
|
Pat((DefWithBody, AstPtr<ast::BindPat>)),
|
||||||
|
SelfParam(AstPtr<ast::SelfParam>),
|
||||||
|
GenericParam(u32),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Eq)]
|
||||||
|
pub(crate) struct Definition {
|
||||||
|
pub visibility: Option<ast::Visibility>,
|
||||||
|
pub container: Module,
|
||||||
|
pub item: NameKind,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) trait HasDefinition {
|
||||||
|
type Def;
|
||||||
|
type Ref;
|
||||||
|
|
||||||
|
fn definition(self, db: &RootDatabase) -> Definition;
|
||||||
|
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition>;
|
||||||
|
fn from_ref(
|
||||||
|
db: &RootDatabase,
|
||||||
|
analyzer: &SourceAnalyzer,
|
||||||
|
refer: Self::Ref,
|
||||||
|
) -> Option<Definition>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// fn decl_from_pat(
|
||||||
|
// db: &RootDatabase,
|
||||||
|
// file_id: HirFileId,
|
||||||
|
// pat: AstPtr<ast::BindPat>,
|
||||||
|
// ) -> Option<Definition> {
|
||||||
|
// let root = db.parse_or_expand(file_id)?;
|
||||||
|
// // FIXME: use match_ast!
|
||||||
|
// let def = pat.to_node(&root).syntax().ancestors().find_map(|node| {
|
||||||
|
// if let Some(it) = ast::FnDef::cast(node.clone()) {
|
||||||
|
// let src = hir::Source { file_id, ast: it };
|
||||||
|
// Some(hir::Function::from_source(db, src)?.into())
|
||||||
|
// } else if let Some(it) = ast::ConstDef::cast(node.clone()) {
|
||||||
|
// let src = hir::Source { file_id, ast: it };
|
||||||
|
// Some(hir::Const::from_source(db, src)?.into())
|
||||||
|
// } else if let Some(it) = ast::StaticDef::cast(node.clone()) {
|
||||||
|
// let src = hir::Source { file_id, ast: it };
|
||||||
|
// Some(hir::Static::from_source(db, src)?.into())
|
||||||
|
// } else {
|
||||||
|
// None
|
||||||
|
// }
|
||||||
|
// })?;
|
||||||
|
// let item = NameKind::Pat((def, pat));
|
||||||
|
// let container = def.module(db);
|
||||||
|
// Some(Definition { item, container, visibility: None })
|
||||||
|
// }
|
||||||
|
|
||||||
|
impl HasDefinition for StructField {
|
||||||
|
type Def = ast::RecordFieldDef;
|
||||||
|
type Ref = ast::FieldExpr;
|
||||||
|
|
||||||
|
fn definition(self, db: &RootDatabase) -> Definition {
|
||||||
|
let item = NameKind::FieldAccess(self);
|
||||||
|
let parent = self.parent_def(db);
|
||||||
|
let container = parent.module(db);
|
||||||
|
let visibility = match parent {
|
||||||
|
VariantDef::Struct(s) => s.source(db).ast.visibility(),
|
||||||
|
VariantDef::EnumVariant(e) => e.source(db).ast.parent_enum().visibility(),
|
||||||
|
};
|
||||||
|
Definition { item, container, visibility }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition> {
|
||||||
|
let src = hir::Source { file_id, ast: hir::FieldSource::Named(def) };
|
||||||
|
let field = StructField::from_source(db, src)?;
|
||||||
|
Some(field.definition(db))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_ref(
|
||||||
|
db: &RootDatabase,
|
||||||
|
analyzer: &SourceAnalyzer,
|
||||||
|
refer: Self::Ref,
|
||||||
|
) -> Option<Definition> {
|
||||||
|
let field = analyzer.resolve_field(&refer)?;
|
||||||
|
Some(field.definition(db))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasDefinition for AssocItem {
|
||||||
|
type Def = ast::ImplItem;
|
||||||
|
type Ref = ast::MethodCallExpr;
|
||||||
|
|
||||||
|
fn definition(self, db: &RootDatabase) -> Definition {
|
||||||
|
let item = NameKind::AssocItem(self);
|
||||||
|
let container = self.module(db);
|
||||||
|
let visibility = match self {
|
||||||
|
AssocItem::Function(f) => f.source(db).ast.visibility(),
|
||||||
|
AssocItem::Const(c) => c.source(db).ast.visibility(),
|
||||||
|
AssocItem::TypeAlias(a) => a.source(db).ast.visibility(),
|
||||||
|
};
|
||||||
|
Definition { item, container, visibility }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition> {
|
||||||
|
if def.syntax().parent().and_then(ast::ItemList::cast).is_none() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
let src = hir::Source { file_id, ast: def };
|
||||||
|
let item = AssocItem::from_source(db, src)?;
|
||||||
|
Some(item.definition(db))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_ref(
|
||||||
|
db: &RootDatabase,
|
||||||
|
analyzer: &SourceAnalyzer,
|
||||||
|
refer: Self::Ref,
|
||||||
|
) -> Option<Definition> {
|
||||||
|
let func: AssocItem = analyzer.resolve_method_call(&refer)?.into();
|
||||||
|
Some(func.definition(db))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl HasDefinition for ModuleDef {
|
||||||
|
type Def = ast::ModuleItem;
|
||||||
|
type Ref = ast::Path;
|
||||||
|
|
||||||
|
fn definition(self, db: &RootDatabase) -> Definition {
|
||||||
|
let (container, visibility) = match self {
|
||||||
|
ModuleDef::Module(it) => {
|
||||||
|
let container = it.parent(db).or_else(|| Some(it)).unwrap();
|
||||||
|
let visibility = it.declaration_source(db).and_then(|s| s.ast.visibility());
|
||||||
|
(container, visibility)
|
||||||
|
}
|
||||||
|
ModuleDef::EnumVariant(it) => {
|
||||||
|
let container = it.module(db);
|
||||||
|
let visibility = it.source(db).ast.parent_enum().visibility();
|
||||||
|
(container, visibility)
|
||||||
|
}
|
||||||
|
ModuleDef::Function(it) => (it.module(db), it.source(db).ast.visibility()),
|
||||||
|
ModuleDef::Const(it) => (it.module(db), it.source(db).ast.visibility()),
|
||||||
|
ModuleDef::Static(it) => (it.module(db), it.source(db).ast.visibility()),
|
||||||
|
ModuleDef::Trait(it) => (it.module(db), it.source(db).ast.visibility()),
|
||||||
|
ModuleDef::TypeAlias(it) => (it.module(db), it.source(db).ast.visibility()),
|
||||||
|
ModuleDef::Adt(Adt::Struct(it)) => (it.module(db), it.source(db).ast.visibility()),
|
||||||
|
ModuleDef::Adt(Adt::Union(it)) => (it.module(db), it.source(db).ast.visibility()),
|
||||||
|
ModuleDef::Adt(Adt::Enum(it)) => (it.module(db), it.source(db).ast.visibility()),
|
||||||
|
ModuleDef::BuiltinType(..) => unreachable!(),
|
||||||
|
};
|
||||||
|
let item = NameKind::Def(self);
|
||||||
|
Definition { item, container, visibility }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_def(db: &RootDatabase, file_id: HirFileId, def: Self::Def) -> Option<Definition> {
|
||||||
|
let src = hir::Source { file_id, ast: def };
|
||||||
|
let def = ModuleDef::from_source(db, src)?;
|
||||||
|
Some(def.definition(db))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn from_ref(
|
||||||
|
db: &RootDatabase,
|
||||||
|
analyzer: &SourceAnalyzer,
|
||||||
|
refer: Self::Ref,
|
||||||
|
) -> Option<Definition> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FIXME: impl HasDefinition for hir::MacroDef
|
467
crates/ra_ide_api/src/references/rename.rs
Normal file
467
crates/ra_ide_api/src/references/rename.rs
Normal file
|
@ -0,0 +1,467 @@
|
||||||
|
use hir::ModuleSource;
|
||||||
|
use ra_db::SourceDatabase;
|
||||||
|
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SyntaxNode};
|
||||||
|
use relative_path::{RelativePath, RelativePathBuf};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
db::RootDatabase, FileId, FilePosition, FileSystemEdit, RangeInfo, SourceChange,
|
||||||
|
SourceFileEdit, TextRange,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::find_all_refs;
|
||||||
|
|
||||||
|
pub(crate) fn rename(
|
||||||
|
db: &RootDatabase,
|
||||||
|
position: FilePosition,
|
||||||
|
new_name: &str,
|
||||||
|
) -> Option<RangeInfo<SourceChange>> {
|
||||||
|
let parse = db.parse(position.file_id);
|
||||||
|
if let Some((ast_name, ast_module)) =
|
||||||
|
find_name_and_module_at_offset(parse.tree().syntax(), position)
|
||||||
|
{
|
||||||
|
let range = ast_name.syntax().text_range();
|
||||||
|
rename_mod(db, &ast_name, &ast_module, position, new_name)
|
||||||
|
.map(|info| RangeInfo::new(range, info))
|
||||||
|
} else {
|
||||||
|
rename_reference(db, position, new_name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_name_and_module_at_offset(
|
||||||
|
syntax: &SyntaxNode,
|
||||||
|
position: FilePosition,
|
||||||
|
) -> Option<(ast::Name, ast::Module)> {
|
||||||
|
let ast_name = find_node_at_offset::<ast::Name>(syntax, position.offset)?;
|
||||||
|
let ast_module = ast::Module::cast(ast_name.syntax().parent()?)?;
|
||||||
|
Some((ast_name, ast_module))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn source_edit_from_file_id_range(
|
||||||
|
file_id: FileId,
|
||||||
|
range: TextRange,
|
||||||
|
new_name: &str,
|
||||||
|
) -> SourceFileEdit {
|
||||||
|
SourceFileEdit {
|
||||||
|
file_id,
|
||||||
|
edit: {
|
||||||
|
let mut builder = ra_text_edit::TextEditBuilder::default();
|
||||||
|
builder.replace(range, new_name.into());
|
||||||
|
builder.finish()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rename_mod(
|
||||||
|
db: &RootDatabase,
|
||||||
|
ast_name: &ast::Name,
|
||||||
|
ast_module: &ast::Module,
|
||||||
|
position: FilePosition,
|
||||||
|
new_name: &str,
|
||||||
|
) -> Option<SourceChange> {
|
||||||
|
let mut source_file_edits = Vec::new();
|
||||||
|
let mut file_system_edits = Vec::new();
|
||||||
|
let module_src = hir::Source { file_id: position.file_id.into(), ast: ast_module.clone() };
|
||||||
|
if let Some(module) = hir::Module::from_declaration(db, module_src) {
|
||||||
|
let src = module.definition_source(db);
|
||||||
|
let file_id = src.file_id.original_file(db);
|
||||||
|
match src.ast {
|
||||||
|
ModuleSource::SourceFile(..) => {
|
||||||
|
let mod_path: RelativePathBuf = db.file_relative_path(file_id);
|
||||||
|
// mod is defined in path/to/dir/mod.rs
|
||||||
|
let dst_path = if mod_path.file_stem() == Some("mod") {
|
||||||
|
mod_path
|
||||||
|
.parent()
|
||||||
|
.and_then(|p| p.parent())
|
||||||
|
.or_else(|| Some(RelativePath::new("")))
|
||||||
|
.map(|p| p.join(new_name).join("mod.rs"))
|
||||||
|
} else {
|
||||||
|
Some(mod_path.with_file_name(new_name).with_extension("rs"))
|
||||||
|
};
|
||||||
|
if let Some(path) = dst_path {
|
||||||
|
let move_file = FileSystemEdit::MoveFile {
|
||||||
|
src: file_id,
|
||||||
|
dst_source_root: db.file_source_root(position.file_id),
|
||||||
|
dst_path: path,
|
||||||
|
};
|
||||||
|
file_system_edits.push(move_file);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ModuleSource::Module(..) => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let edit = SourceFileEdit {
|
||||||
|
file_id: position.file_id,
|
||||||
|
edit: {
|
||||||
|
let mut builder = ra_text_edit::TextEditBuilder::default();
|
||||||
|
builder.replace(ast_name.syntax().text_range(), new_name.into());
|
||||||
|
builder.finish()
|
||||||
|
},
|
||||||
|
};
|
||||||
|
source_file_edits.push(edit);
|
||||||
|
|
||||||
|
Some(SourceChange::from_edits("rename", source_file_edits, file_system_edits))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn rename_reference(
|
||||||
|
db: &RootDatabase,
|
||||||
|
position: FilePosition,
|
||||||
|
new_name: &str,
|
||||||
|
) -> Option<RangeInfo<SourceChange>> {
|
||||||
|
let RangeInfo { range, info: refs } = find_all_refs(db, position)?;
|
||||||
|
|
||||||
|
let edit = refs
|
||||||
|
.into_iter()
|
||||||
|
.map(|range| source_edit_from_file_id_range(range.file_id, range.range, new_name))
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
|
if edit.is_empty() {
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(RangeInfo::new(range, SourceChange::source_file_edits("rename", edit)))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use crate::{
|
||||||
|
mock_analysis::analysis_and_position, mock_analysis::single_file_with_position, FileId,
|
||||||
|
ReferenceSearchResult,
|
||||||
|
};
|
||||||
|
use insta::assert_debug_snapshot;
|
||||||
|
use test_utils::assert_eq_text;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_all_refs_for_local() {
|
||||||
|
let code = r#"
|
||||||
|
fn main() {
|
||||||
|
let mut i = 1;
|
||||||
|
let j = 1;
|
||||||
|
i = i<|> + j;
|
||||||
|
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
i = 5;
|
||||||
|
}"#;
|
||||||
|
|
||||||
|
let refs = get_all_refs(code);
|
||||||
|
assert_eq!(refs.len(), 5);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_all_refs_for_param_inside() {
|
||||||
|
let code = r#"
|
||||||
|
fn foo(i : u32) -> u32 {
|
||||||
|
i<|>
|
||||||
|
}"#;
|
||||||
|
|
||||||
|
let refs = get_all_refs(code);
|
||||||
|
assert_eq!(refs.len(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_all_refs_for_fn_param() {
|
||||||
|
let code = r#"
|
||||||
|
fn foo(i<|> : u32) -> u32 {
|
||||||
|
i
|
||||||
|
}"#;
|
||||||
|
|
||||||
|
let refs = get_all_refs(code);
|
||||||
|
assert_eq!(refs.len(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_all_refs_field_name() {
|
||||||
|
let code = r#"
|
||||||
|
//- /lib.rs
|
||||||
|
struct Foo {
|
||||||
|
pub spam<|>: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main(s: Foo) {
|
||||||
|
let f = s.spam;
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let refs = get_all_refs(code);
|
||||||
|
assert_eq!(refs.len(), 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_all_refs_impl_item_name() {
|
||||||
|
let code = r#"
|
||||||
|
//- /lib.rs
|
||||||
|
struct Foo;
|
||||||
|
impl Foo {
|
||||||
|
fn f<|>(&self) { }
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let refs = get_all_refs(code);
|
||||||
|
assert_eq!(refs.len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_all_refs_enum_var_name() {
|
||||||
|
let code = r#"
|
||||||
|
//- /lib.rs
|
||||||
|
enum Foo {
|
||||||
|
A,
|
||||||
|
B<|>,
|
||||||
|
C,
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let refs = get_all_refs(code);
|
||||||
|
assert_eq!(refs.len(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_find_all_refs_modules() {
|
||||||
|
let code = r#"
|
||||||
|
//- /lib.rs
|
||||||
|
pub mod foo;
|
||||||
|
pub mod bar;
|
||||||
|
|
||||||
|
fn f() {
|
||||||
|
let i = foo::Foo { n: 5 };
|
||||||
|
}
|
||||||
|
|
||||||
|
//- /foo.rs
|
||||||
|
use crate::bar;
|
||||||
|
|
||||||
|
pub struct Foo {
|
||||||
|
pub n: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn f() {
|
||||||
|
let i = bar::Bar { n: 5 };
|
||||||
|
}
|
||||||
|
|
||||||
|
//- /bar.rs
|
||||||
|
use crate::foo;
|
||||||
|
|
||||||
|
pub struct Bar {
|
||||||
|
pub n: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn f() {
|
||||||
|
let i = foo::Foo<|> { n: 5 };
|
||||||
|
}
|
||||||
|
"#;
|
||||||
|
|
||||||
|
let (analysis, pos) = analysis_and_position(code);
|
||||||
|
let refs = analysis.find_all_refs(pos).unwrap().unwrap();
|
||||||
|
assert_eq!(refs.len(), 3);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_all_refs(text: &str) -> ReferenceSearchResult {
|
||||||
|
let (analysis, position) = single_file_with_position(text);
|
||||||
|
analysis.find_all_refs(position).unwrap().unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rename_for_local() {
|
||||||
|
test_rename(
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
let mut i = 1;
|
||||||
|
let j = 1;
|
||||||
|
i = i<|> + j;
|
||||||
|
|
||||||
|
{
|
||||||
|
i = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
i = 5;
|
||||||
|
}"#,
|
||||||
|
"k",
|
||||||
|
r#"
|
||||||
|
fn main() {
|
||||||
|
let mut k = 1;
|
||||||
|
let j = 1;
|
||||||
|
k = k + j;
|
||||||
|
|
||||||
|
{
|
||||||
|
k = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
k = 5;
|
||||||
|
}"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rename_for_param_inside() {
|
||||||
|
test_rename(
|
||||||
|
r#"
|
||||||
|
fn foo(i : u32) -> u32 {
|
||||||
|
i<|>
|
||||||
|
}"#,
|
||||||
|
"j",
|
||||||
|
r#"
|
||||||
|
fn foo(j : u32) -> u32 {
|
||||||
|
j
|
||||||
|
}"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rename_refs_for_fn_param() {
|
||||||
|
test_rename(
|
||||||
|
r#"
|
||||||
|
fn foo(i<|> : u32) -> u32 {
|
||||||
|
i
|
||||||
|
}"#,
|
||||||
|
"new_name",
|
||||||
|
r#"
|
||||||
|
fn foo(new_name : u32) -> u32 {
|
||||||
|
new_name
|
||||||
|
}"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rename_for_mut_param() {
|
||||||
|
test_rename(
|
||||||
|
r#"
|
||||||
|
fn foo(mut i<|> : u32) -> u32 {
|
||||||
|
i
|
||||||
|
}"#,
|
||||||
|
"new_name",
|
||||||
|
r#"
|
||||||
|
fn foo(mut new_name : u32) -> u32 {
|
||||||
|
new_name
|
||||||
|
}"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rename_mod() {
|
||||||
|
let (analysis, position) = analysis_and_position(
|
||||||
|
"
|
||||||
|
//- /lib.rs
|
||||||
|
mod bar;
|
||||||
|
|
||||||
|
//- /bar.rs
|
||||||
|
mod foo<|>;
|
||||||
|
|
||||||
|
//- /bar/foo.rs
|
||||||
|
// emtpy
|
||||||
|
",
|
||||||
|
);
|
||||||
|
let new_name = "foo2";
|
||||||
|
let source_change = analysis.rename(position, new_name).unwrap();
|
||||||
|
assert_debug_snapshot!(&source_change,
|
||||||
|
@r###"
|
||||||
|
Some(
|
||||||
|
RangeInfo {
|
||||||
|
range: [4; 7),
|
||||||
|
info: SourceChange {
|
||||||
|
label: "rename",
|
||||||
|
source_file_edits: [
|
||||||
|
SourceFileEdit {
|
||||||
|
file_id: FileId(
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
edit: TextEdit {
|
||||||
|
atoms: [
|
||||||
|
AtomTextEdit {
|
||||||
|
delete: [4; 7),
|
||||||
|
insert: "foo2",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
file_system_edits: [
|
||||||
|
MoveFile {
|
||||||
|
src: FileId(
|
||||||
|
3,
|
||||||
|
),
|
||||||
|
dst_source_root: SourceRootId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
dst_path: "bar/foo2.rs",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
cursor_position: None,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
"###);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rename_mod_in_dir() {
|
||||||
|
let (analysis, position) = analysis_and_position(
|
||||||
|
"
|
||||||
|
//- /lib.rs
|
||||||
|
mod fo<|>o;
|
||||||
|
//- /foo/mod.rs
|
||||||
|
// emtpy
|
||||||
|
",
|
||||||
|
);
|
||||||
|
let new_name = "foo2";
|
||||||
|
let source_change = analysis.rename(position, new_name).unwrap();
|
||||||
|
assert_debug_snapshot!(&source_change,
|
||||||
|
@r###"
|
||||||
|
Some(
|
||||||
|
RangeInfo {
|
||||||
|
range: [4; 7),
|
||||||
|
info: SourceChange {
|
||||||
|
label: "rename",
|
||||||
|
source_file_edits: [
|
||||||
|
SourceFileEdit {
|
||||||
|
file_id: FileId(
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
edit: TextEdit {
|
||||||
|
atoms: [
|
||||||
|
AtomTextEdit {
|
||||||
|
delete: [4; 7),
|
||||||
|
insert: "foo2",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
file_system_edits: [
|
||||||
|
MoveFile {
|
||||||
|
src: FileId(
|
||||||
|
2,
|
||||||
|
),
|
||||||
|
dst_source_root: SourceRootId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
dst_path: "foo2/mod.rs",
|
||||||
|
},
|
||||||
|
],
|
||||||
|
cursor_position: None,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
"###
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_rename(text: &str, new_name: &str, expected: &str) {
|
||||||
|
let (analysis, position) = single_file_with_position(text);
|
||||||
|
let source_change = analysis.rename(position, new_name).unwrap();
|
||||||
|
let mut text_edit_builder = ra_text_edit::TextEditBuilder::default();
|
||||||
|
let mut file_id: Option<FileId> = None;
|
||||||
|
if let Some(change) = source_change {
|
||||||
|
for edit in change.info.source_file_edits {
|
||||||
|
file_id = Some(edit.file_id);
|
||||||
|
for atom in edit.edit.as_atoms() {
|
||||||
|
text_edit_builder.replace(atom.delete, atom.insert.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let result =
|
||||||
|
text_edit_builder.finish().apply(&*analysis.file_text(file_id.unwrap()).unwrap());
|
||||||
|
assert_eq_text!(expected, &*result);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,55 +1,15 @@
|
||||||
use hir::{DefWithBody, HasSource, ModuleSource, SourceAnalyzer};
|
use hir::{DefWithBody, HasSource, ModuleSource};
|
||||||
use ra_db::{FileId, FileRange, SourceDatabase};
|
use ra_db::{FileId, SourceDatabase};
|
||||||
use ra_syntax::{algo::find_node_at_offset, ast, AstNode, SourceFile, TextRange, TextUnit};
|
use ra_syntax::{AstNode, TextRange};
|
||||||
|
|
||||||
use crate::{
|
use crate::db::RootDatabase;
|
||||||
db::RootDatabase,
|
|
||||||
name_kind::{classify_name_ref, Definition, NameKind},
|
use super::{Definition, NameKind};
|
||||||
};
|
|
||||||
|
|
||||||
pub(crate) struct SearchScope {
|
pub(crate) struct SearchScope {
|
||||||
pub scope: Vec<(FileId, Option<TextRange>)>,
|
pub scope: Vec<(FileId, Option<TextRange>)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn find_refs(db: &RootDatabase, def: Definition, name: String) -> Vec<FileRange> {
|
|
||||||
let pat = name.as_str();
|
|
||||||
let scope = def.scope(db).scope;
|
|
||||||
let mut refs = vec![];
|
|
||||||
|
|
||||||
let is_match = |file_id: FileId, name_ref: &ast::NameRef| -> bool {
|
|
||||||
let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
|
|
||||||
let classified = classify_name_ref(db, file_id, &analyzer, &name_ref);
|
|
||||||
if let Some(d) = classified {
|
|
||||||
d == def
|
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
for (file_id, text_range) in scope {
|
|
||||||
let text = db.file_text(file_id);
|
|
||||||
let parse = SourceFile::parse(&text);
|
|
||||||
let syntax = parse.tree().syntax().clone();
|
|
||||||
|
|
||||||
for (idx, _) in text.match_indices(pat) {
|
|
||||||
let offset = TextUnit::from_usize(idx);
|
|
||||||
if let Some(name_ref) = find_node_at_offset::<ast::NameRef>(&syntax, offset) {
|
|
||||||
let range = name_ref.syntax().text_range();
|
|
||||||
|
|
||||||
if let Some(text_range) = text_range {
|
|
||||||
if range.is_subrange(&text_range) && is_match(file_id, &name_ref) {
|
|
||||||
refs.push(FileRange { file_id, range });
|
|
||||||
}
|
|
||||||
} else if is_match(file_id, &name_ref) {
|
|
||||||
refs.push(FileRange { file_id, range });
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return refs;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Definition {
|
impl Definition {
|
||||||
pub fn scope(&self, db: &RootDatabase) -> SearchScope {
|
pub fn scope(&self, db: &RootDatabase) -> SearchScope {
|
||||||
let module_src = self.container.definition_source(db);
|
let module_src = self.container.definition_source(db);
|
|
@ -14,7 +14,7 @@ use ra_syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
db::RootDatabase,
|
db::RootDatabase,
|
||||||
name_kind::{classify_name_ref, NameKind::*},
|
references::{classify_name_ref, NameKind::*},
|
||||||
FileId,
|
FileId,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -101,10 +101,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) {
|
if let Some(name_ref) = node.as_node().cloned().and_then(ast::NameRef::cast) {
|
||||||
// FIXME: try to reuse the SourceAnalyzers
|
let name_kind =
|
||||||
let analyzer = hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
|
classify_name_ref(db, file_id, &name_ref).and_then(|d| Some(d.item));
|
||||||
let name_kind = classify_name_ref(db, file_id, &analyzer, &name_ref)
|
|
||||||
.and_then(|d| Some(d.item));
|
|
||||||
match name_kind {
|
match name_kind {
|
||||||
Some(Macro(_)) => "macro",
|
Some(Macro(_)) => "macro",
|
||||||
Some(FieldAccess(_)) => "field",
|
Some(FieldAccess(_)) => "field",
|
||||||
|
@ -131,6 +129,8 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
|
||||||
Some(calc_binding_hash(file_id, &text, *shadow_count))
|
Some(calc_binding_hash(file_id, &text, *shadow_count))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let analyzer =
|
||||||
|
hir::SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
|
||||||
if is_variable_mutable(db, &analyzer, ptr.to_node(&root)) {
|
if is_variable_mutable(db, &analyzer, ptr.to_node(&root)) {
|
||||||
"variable.mut"
|
"variable.mut"
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Reference in a new issue