Add functions to DefId

This commit is contained in:
Aleksey Kladov 2018-12-04 23:44:00 +03:00
parent 947e3350e0
commit d8b0379e10
10 changed files with 90 additions and 83 deletions

View file

@ -12,19 +12,15 @@ use ra_syntax::{
use ra_db::FileId;
use crate::{
FnId, HirDatabase, SourceItemId,
Cancelable,
DefLoc, DefKind, DefId, HirDatabase, SourceItemId,
Module,
};
pub use self::scope::FnScopes;
impl FnId {
pub fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId {
let file_items = db.file_items(file_id);
let item_id = file_items.id_of(fn_def.syntax());
let item_id = SourceItemId { file_id, item_id };
FnId::from_loc(db, &item_id)
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct FnId(pub(crate) DefId);
pub struct Function {
fn_id: FnId,
@ -35,16 +31,26 @@ impl Function {
db: &impl HirDatabase,
file_id: FileId,
fn_def: ast::FnDef,
) -> Function {
let fn_id = FnId::get(db, file_id, fn_def);
Function { fn_id }
) -> Cancelable<Option<Function>> {
let module = ctry!(Module::guess_from_child_node(db, file_id, fn_def.syntax())?);
let file_items = db.file_items(file_id);
let item_id = file_items.id_of(fn_def.syntax());
let source_item_id = SourceItemId { file_id, item_id };
let def_loc = DefLoc {
kind: DefKind::Function,
source_root_id: module.source_root_id,
module_id: module.module_id,
source_item_id,
};
let fn_id = FnId(def_loc.id(db));
Ok(Some(Function { fn_id }))
}
pub fn guess_for_name_ref(
db: &impl HirDatabase,
file_id: FileId,
name_ref: ast::NameRef,
) -> Option<Function> {
) -> Cancelable<Option<Function>> {
Function::guess_for_node(db, file_id, name_ref.syntax())
}
@ -52,7 +58,7 @@ impl Function {
db: &impl HirDatabase,
file_id: FileId,
bind_pat: ast::BindPat,
) -> Option<Function> {
) -> Cancelable<Option<Function>> {
Function::guess_for_node(db, file_id, bind_pat.syntax())
}
@ -60,10 +66,9 @@ impl Function {
db: &impl HirDatabase,
file_id: FileId,
node: SyntaxNodeRef,
) -> Option<Function> {
let fn_def = node.ancestors().find_map(ast::FnDef::cast)?;
let res = Function::guess_from_source(db, file_id, fn_def);
Some(res)
) -> Cancelable<Option<Function>> {
let fn_def = ctry!(node.ancestors().find_map(ast::FnDef::cast));
Function::guess_from_source(db, file_id, fn_def)
}
pub fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> {