184 lines
7.2 KiB
Rust
Raw Normal View History

2019-10-22 23:46:53 +03:00
//! Functions that are used to classify an element from its definition or reference.
2019-10-14 14:59:02 +03:00
2019-10-12 20:30:53 +03:00
use hir::{Either, FromSource, Module, ModuleSource, Path, PathResolution, Source, SourceAnalyzer};
2019-10-12 18:47:17 +03:00
use ra_db::FileId;
2019-10-24 10:37:20 +03:00
use ra_prof::profile;
2019-10-12 18:47:17 +03:00
use ra_syntax::{ast, match_ast, AstNode, AstPtr};
2019-10-22 23:46:53 +03:00
use test_utils::tested_by;
2019-10-12 18:47:17 +03:00
2019-10-12 20:30:53 +03:00
use super::{
name_definition::{from_assoc_item, from_module_def, from_pat, from_struct_field},
NameDefinition, NameKind,
};
2019-10-12 18:47:17 +03:00
use crate::db::RootDatabase;
pub(crate) fn classify_name(
db: &RootDatabase,
file_id: FileId,
name: &ast::Name,
2019-10-12 20:30:53 +03:00
) -> Option<NameDefinition> {
2019-10-24 10:37:20 +03:00
let _p = profile("classify_name");
2019-10-12 18:47:17 +03:00
let parent = name.syntax().parent()?;
let file_id = file_id.into();
2019-10-12 20:30:53 +03:00
// FIXME: add ast::MacroCall(it)
2019-10-12 18:47:17 +03:00
match_ast! {
match parent {
ast::BindPat(it) => {
2019-10-12 20:30:53 +03:00
from_pat(db, file_id, AstPtr::new(&it))
2019-10-12 18:47:17 +03:00
},
ast::RecordFieldDef(it) => {
2019-10-12 20:30:53 +03:00
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::Module(it) => {
2019-10-15 22:50:28 +03:00
let def = {
if !it.has_semi() {
let ast = hir::ModuleSource::Module(it);
let src = hir::Source { file_id, ast };
hir::Module::from_definition(db, src)
} else {
let src = hir::Source { file_id, ast: it };
hir::Module::from_declaration(db, src)
}
}?;
2019-10-12 20:40:49 +03:00
Some(from_module_def(db, def.into(), None))
2019-10-12 18:47:17 +03:00
},
2019-10-12 20:30:53 +03:00
ast::StructDef(it) => {
let src = hir::Source { file_id, ast: it };
let def = hir::Struct::from_source(db, src)?;
2019-10-12 20:40:49 +03:00
Some(from_module_def(db, def.into(), None))
2019-10-12 20:30:53 +03:00
},
ast::EnumDef(it) => {
let src = hir::Source { file_id, ast: it };
let def = hir::Enum::from_source(db, src)?;
2019-10-12 20:40:49 +03:00
Some(from_module_def(db, def.into(), None))
2019-10-12 20:30:53 +03:00
},
ast::TraitDef(it) => {
let src = hir::Source { file_id, ast: it };
let def = hir::Trait::from_source(db, src)?;
2019-10-12 20:40:49 +03:00
Some(from_module_def(db, def.into(), None))
2019-10-12 20:30:53 +03:00
},
ast::StaticDef(it) => {
let src = hir::Source { file_id, ast: it };
let def = hir::Static::from_source(db, src)?;
2019-10-12 20:40:49 +03:00
Some(from_module_def(db, def.into(), None))
2019-10-12 18:47:17 +03:00
},
ast::EnumVariant(it) => {
2019-10-12 20:30:53 +03:00
let src = hir::Source { file_id, ast: it };
let def = hir::EnumVariant::from_source(db, src)?;
2019-10-12 20:40:49 +03:00
Some(from_module_def(db, def.into(), None))
2019-10-12 20:30:53 +03:00
},
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 {
2019-10-12 20:40:49 +03:00
Some(from_module_def(db, def.into(), None))
2019-10-12 20:30:53 +03:00
}
},
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 {
2019-10-12 20:40:49 +03:00
Some(from_module_def(db, def.into(), None))
2019-10-12 20:30:53 +03:00
}
2019-10-12 18:47:17 +03:00
},
2019-10-12 20:30:53 +03:00
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 {
2019-10-12 20:40:49 +03:00
Some(from_module_def(db, def.into(), None))
2019-10-12 20:30:53 +03:00
}
2019-10-12 18:47:17 +03:00
},
_ => None,
}
}
}
pub(crate) fn classify_name_ref(
db: &RootDatabase,
file_id: FileId,
name_ref: &ast::NameRef,
2019-10-12 20:30:53 +03:00
) -> Option<NameDefinition> {
use PathResolution::*;
2019-10-24 10:37:20 +03:00
let _p = profile("classify_name_ref");
2019-10-12 18:47:17 +03:00
let parent = name_ref.syntax().parent()?;
2019-10-12 20:30:53 +03:00
let analyzer = SourceAnalyzer::new(db, file_id, name_ref.syntax(), None);
if let Some(method_call) = ast::MethodCallExpr::cast(parent.clone()) {
2019-10-22 23:46:53 +03:00
tested_by!(goto_definition_works_for_methods);
2019-10-14 14:59:02 +03:00
if let Some(func) = analyzer.resolve_method_call(&method_call) {
return Some(from_assoc_item(db, func.into()));
}
2019-10-12 20:30:53 +03:00
}
if let Some(field_expr) = ast::FieldExpr::cast(parent.clone()) {
2019-10-22 23:46:53 +03:00
tested_by!(goto_definition_works_for_fields);
2019-10-12 20:30:53 +03:00
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()) {
2019-10-22 23:46:53 +03:00
tested_by!(goto_definition_works_for_record_fields);
2019-10-12 20:30:53 +03:00
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));
2019-10-12 18:47:17 +03:00
}
}
let ast = ModuleSource::from_child_node(db, file_id, &parent);
let file_id = file_id.into();
2019-10-12 20:30:53 +03:00
// FIXME: find correct container and visibility for each case
2019-10-12 18:47:17 +03:00
let container = Module::from_definition(db, Source { file_id, ast })?;
let visibility = None;
2019-10-14 14:59:02 +03:00
if let Some(macro_call) = parent.ancestors().find_map(ast::MacroCall::cast) {
2019-10-22 23:46:53 +03:00
tested_by!(goto_definition_works_for_macros);
2019-10-12 20:30:53 +03:00
if let Some(macro_def) = analyzer.resolve_macro_call(db, &macro_call) {
2019-10-14 14:59:02 +03:00
let kind = NameKind::Macro(macro_def);
return Some(NameDefinition { kind, container, visibility });
2019-10-12 18:47:17 +03:00
}
}
let path = name_ref.syntax().ancestors().find_map(ast::Path::cast)?;
let resolved = analyzer.resolve_path(db, &path)?;
match resolved {
2019-10-12 20:40:49 +03:00
Def(def) => Some(from_module_def(db, def, Some(container))),
2019-10-12 20:30:53 +03:00
AssocItem(item) => Some(from_assoc_item(db, item)),
LocalBinding(Either::A(pat)) => from_pat(db, file_id, pat),
LocalBinding(Either::B(par)) => {
2019-10-14 14:59:02 +03:00
let kind = NameKind::SelfParam(par);
Some(NameDefinition { kind, container, visibility })
2019-10-12 18:47:17 +03:00
}
2019-10-12 20:30:53 +03:00
GenericParam(par) => {
2019-10-12 18:47:17 +03:00
// FIXME: get generic param def
2019-10-14 14:59:02 +03:00
let kind = NameKind::GenericParam(par);
Some(NameDefinition { kind, container, visibility })
2019-10-12 18:47:17 +03:00
}
2019-10-12 20:30:53 +03:00
Macro(def) => {
2019-10-14 14:59:02 +03:00
let kind = NameKind::Macro(def);
Some(NameDefinition { kind, container, visibility })
2019-10-12 18:47:17 +03:00
}
2019-10-12 20:30:53 +03:00
SelfType(impl_block) => {
2019-10-12 18:47:17 +03:00
let ty = impl_block.target_ty(db);
2019-10-14 14:59:02 +03:00
let kind = NameKind::SelfType(ty);
2019-10-12 18:47:17 +03:00
let container = impl_block.module();
2019-10-14 14:59:02 +03:00
Some(NameDefinition { kind, container, visibility })
2019-10-12 18:47:17 +03:00
}
}
}