From d8b0379e1063941331253905795699a918233ef9 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 4 Dec 2018 23:44:00 +0300 Subject: Add functions to DefId --- crates/ra_hir/src/function/mod.rs | 41 ++++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 18 deletions(-) (limited to 'crates/ra_hir/src/function') diff --git a/crates/ra_hir/src/function/mod.rs b/crates/ra_hir/src/function/mod.rs index c8af2e54f..a6757601e 100644 --- a/crates/ra_hir/src/function/mod.rs +++ b/crates/ra_hir/src/function/mod.rs @@ -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> { + 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 { + ) -> Cancelable> { 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 { + ) -> Cancelable> { 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 { - let fn_def = node.ancestors().find_map(ast::FnDef::cast)?; - let res = Function::guess_from_source(db, file_id, fn_def); - Some(res) + ) -> Cancelable> { + 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 { -- cgit v1.2.3 From 54d053c881514b024ba399ce2edc678e3f710ab7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 4 Dec 2018 23:52:14 +0300 Subject: minor --- crates/ra_hir/src/function/mod.rs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'crates/ra_hir/src/function') diff --git a/crates/ra_hir/src/function/mod.rs b/crates/ra_hir/src/function/mod.rs index a6757601e..e00bca6e3 100644 --- a/crates/ra_hir/src/function/mod.rs +++ b/crates/ra_hir/src/function/mod.rs @@ -27,6 +27,11 @@ pub struct Function { } impl Function { + pub(crate) fn new(def_id: DefId) -> Function { + let fn_id = FnId(def_id); + Function { fn_id } + } + pub fn guess_from_source( db: &impl HirDatabase, file_id: FileId, @@ -42,8 +47,7 @@ impl Function { module_id: module.module_id, source_item_id, }; - let fn_id = FnId(def_loc.id(db)); - Ok(Some(Function { fn_id })) + Ok(Some(Function::new(def_loc.id(db)))) } pub fn guess_for_name_ref( -- cgit v1.2.3