mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-02 22:54:58 +00:00
replace trait by a bunch of functions
This commit is contained in:
parent
d26d0ada50
commit
835173d065
9 changed files with 227 additions and 330 deletions
|
@ -1,47 +1,90 @@
|
|||
use hir::{
|
||||
AssocItem, Either, EnumVariant, FromSource, Module, ModuleDef, ModuleSource, Path,
|
||||
PathResolution, Source, SourceAnalyzer, StructField,
|
||||
};
|
||||
use hir::{Either, FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer};
|
||||
use ra_db::FileId;
|
||||
use ra_syntax::{ast, match_ast, AstNode, AstPtr};
|
||||
|
||||
use super::{definition::HasDefinition, Definition, NameKind};
|
||||
use super::{
|
||||
name_definition::{from_assoc_item, from_module_def, from_pat, from_struct_field},
|
||||
NameDefinition, 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> {
|
||||
) -> Option<NameDefinition> {
|
||||
let parent = name.syntax().parent()?;
|
||||
let file_id = file_id.into();
|
||||
|
||||
// FIXME: add ast::MacroCall(it)
|
||||
match_ast! {
|
||||
match parent {
|
||||
ast::BindPat(it) => {
|
||||
decl_from_pat(db, file_id, AstPtr::new(&it))
|
||||
from_pat(db, file_id, AstPtr::new(&it))
|
||||
},
|
||||
ast::RecordFieldDef(it) => {
|
||||
StructField::from_def(db, file_id, it)
|
||||
let ast = hir::FieldSource::Named(it);
|
||||
let src = hir::Source { file_id, ast };
|
||||
let field = hir::StructField::from_source(db, src)?;
|
||||
Some(from_struct_field(db, field))
|
||||
},
|
||||
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::Module(it) => {
|
||||
let ast = hir::ModuleSource::Module(it);
|
||||
let src = hir::Source { file_id, ast };
|
||||
let def = hir::Module::from_definition(db, src)?;
|
||||
Some(from_module_def(db, def.into()))
|
||||
},
|
||||
ast::StructDef(it) => {
|
||||
let src = hir::Source { file_id, ast: it };
|
||||
let def = hir::Struct::from_source(db, src)?;
|
||||
Some(from_module_def(db, def.into()))
|
||||
},
|
||||
ast::EnumDef(it) => {
|
||||
let src = hir::Source { file_id, ast: it };
|
||||
let def = hir::Enum::from_source(db, src)?;
|
||||
Some(from_module_def(db, def.into()))
|
||||
},
|
||||
ast::TraitDef(it) => {
|
||||
let src = hir::Source { file_id, ast: it };
|
||||
let def = hir::Trait::from_source(db, src)?;
|
||||
Some(from_module_def(db, def.into()))
|
||||
},
|
||||
ast::StaticDef(it) => {
|
||||
let src = hir::Source { file_id, ast: it };
|
||||
let def = hir::Static::from_source(db, src)?;
|
||||
Some(from_module_def(db, def.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))
|
||||
let src = hir::Source { file_id, ast: it };
|
||||
let def = hir::EnumVariant::from_source(db, src)?;
|
||||
Some(from_module_def(db, def.into()))
|
||||
},
|
||||
ast::ModuleItem(it) => {
|
||||
ModuleDef::from_def(db, file_id, it)
|
||||
ast::FnDef(it) => {
|
||||
let src = hir::Source { file_id, ast: it };
|
||||
let def = hir::Function::from_source(db, src)?;
|
||||
if parent.parent().and_then(ast::ItemList::cast).is_some() {
|
||||
Some(from_assoc_item(db, def.into()))
|
||||
} else {
|
||||
Some(from_module_def(db, def.into()))
|
||||
}
|
||||
},
|
||||
ast::ConstDef(it) => {
|
||||
let src = hir::Source { file_id, ast: it };
|
||||
let def = hir::Const::from_source(db, src)?;
|
||||
if parent.parent().and_then(ast::ItemList::cast).is_some() {
|
||||
Some(from_assoc_item(db, def.into()))
|
||||
} else {
|
||||
Some(from_module_def(db, def.into()))
|
||||
}
|
||||
},
|
||||
ast::TypeAliasDef(it) => {
|
||||
let src = hir::Source { file_id, ast: it };
|
||||
let def = hir::TypeAlias::from_source(db, src)?;
|
||||
if parent.parent().and_then(ast::ItemList::cast).is_some() {
|
||||
Some(from_assoc_item(db, def.into()))
|
||||
} else {
|
||||
Some(from_module_def(db, def.into()))
|
||||
}
|
||||
},
|
||||
_ => None,
|
||||
}
|
||||
|
@ -52,92 +95,75 @@ 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);
|
||||
) -> Option<NameDefinition> {
|
||||
use PathResolution::*;
|
||||
|
||||
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 analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
|
||||
|
||||
if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
|
||||
let func = analyzer.resolve_method_call(&method_call)?;
|
||||
return Some(from_assoc_item(db, func.into()));
|
||||
}
|
||||
|
||||
if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
|
||||
if let Some(field) = analyzer.resolve_field(&field_expr) {
|
||||
return Some(from_struct_field(db, field));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(record_field) = ast::RecordField::cast(parent.clone()) {
|
||||
if let Some(record_lit) = record_field.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(from_struct_field(db, field));
|
||||
}
|
||||
}
|
||||
|
||||
let ast = ModuleSource::from_child_node(db, file_id, &parent);
|
||||
let file_id = file_id.into();
|
||||
// FIXME: find correct container and visibility for each case
|
||||
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 });
|
||||
if let Some(macro_def) = analyzer.resolve_macro_call(db, ¯o_call) {
|
||||
return Some(NameDefinition {
|
||||
item: NameKind::Macro(macro_def),
|
||||
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 })
|
||||
Def(def) => Some(from_module_def(db, def)),
|
||||
AssocItem(item) => Some(from_assoc_item(db, item)),
|
||||
LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat),
|
||||
LocalBinding(Either::B(par)) => {
|
||||
let item = NameKind::SelfParam(par);
|
||||
Some(NameDefinition { item, container, visibility })
|
||||
}
|
||||
PathResolution::GenericParam(par) => {
|
||||
GenericParam(par) => {
|
||||
// FIXME: get generic param def
|
||||
Some(Definition { item: NameKind::GenericParam(par), container, visibility })
|
||||
let item = NameKind::GenericParam(par);
|
||||
Some(NameDefinition { item, container, visibility })
|
||||
}
|
||||
PathResolution::Macro(def) => {
|
||||
Some(Definition { item: NameKind::Macro(def), container, visibility })
|
||||
Macro(def) => {
|
||||
let item = NameKind::Macro(def);
|
||||
Some(NameDefinition { item, container, visibility })
|
||||
}
|
||||
PathResolution::SelfType(impl_block) => {
|
||||
SelfType(impl_block) => {
|
||||
let ty = impl_block.target_ty(db);
|
||||
let item = NameKind::SelfType(ty);
|
||||
let container = impl_block.module();
|
||||
Some(Definition { item: NameKind::SelfType(ty), container, visibility })
|
||||
Some(NameDefinition { item, 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 })
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue