From d8b0379e1063941331253905795699a918233ef9 Mon Sep 17 00:00:00 2001
From: Aleksey Kladov <aleksey.kladov@gmail.com>
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<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> {
-- 
cgit v1.2.3