diff options
Diffstat (limited to 'crates/ra_hir/src/function/mod.rs')
-rw-r--r-- | crates/ra_hir/src/function/mod.rs | 45 |
1 files changed, 27 insertions, 18 deletions
diff --git a/crates/ra_hir/src/function/mod.rs b/crates/ra_hir/src/function/mod.rs index c8af2e54f..e00bca6e3 100644 --- a/crates/ra_hir/src/function/mod.rs +++ b/crates/ra_hir/src/function/mod.rs | |||
@@ -12,39 +12,49 @@ use ra_syntax::{ | |||
12 | use ra_db::FileId; | 12 | use ra_db::FileId; |
13 | 13 | ||
14 | use crate::{ | 14 | use crate::{ |
15 | FnId, HirDatabase, SourceItemId, | 15 | Cancelable, |
16 | DefLoc, DefKind, DefId, HirDatabase, SourceItemId, | ||
17 | Module, | ||
16 | }; | 18 | }; |
17 | 19 | ||
18 | pub use self::scope::FnScopes; | 20 | pub use self::scope::FnScopes; |
19 | 21 | ||
20 | impl FnId { | 22 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
21 | pub fn get(db: &impl HirDatabase, file_id: FileId, fn_def: ast::FnDef) -> FnId { | 23 | pub struct FnId(pub(crate) DefId); |
22 | let file_items = db.file_items(file_id); | ||
23 | let item_id = file_items.id_of(fn_def.syntax()); | ||
24 | let item_id = SourceItemId { file_id, item_id }; | ||
25 | FnId::from_loc(db, &item_id) | ||
26 | } | ||
27 | } | ||
28 | 24 | ||
29 | pub struct Function { | 25 | pub struct Function { |
30 | fn_id: FnId, | 26 | fn_id: FnId, |
31 | } | 27 | } |
32 | 28 | ||
33 | impl Function { | 29 | impl Function { |
30 | pub(crate) fn new(def_id: DefId) -> Function { | ||
31 | let fn_id = FnId(def_id); | ||
32 | Function { fn_id } | ||
33 | } | ||
34 | |||
34 | pub fn guess_from_source( | 35 | pub fn guess_from_source( |
35 | db: &impl HirDatabase, | 36 | db: &impl HirDatabase, |
36 | file_id: FileId, | 37 | file_id: FileId, |
37 | fn_def: ast::FnDef, | 38 | fn_def: ast::FnDef, |
38 | ) -> Function { | 39 | ) -> Cancelable<Option<Function>> { |
39 | let fn_id = FnId::get(db, file_id, fn_def); | 40 | let module = ctry!(Module::guess_from_child_node(db, file_id, fn_def.syntax())?); |
40 | Function { fn_id } | 41 | let file_items = db.file_items(file_id); |
42 | let item_id = file_items.id_of(fn_def.syntax()); | ||
43 | let source_item_id = SourceItemId { file_id, item_id }; | ||
44 | let def_loc = DefLoc { | ||
45 | kind: DefKind::Function, | ||
46 | source_root_id: module.source_root_id, | ||
47 | module_id: module.module_id, | ||
48 | source_item_id, | ||
49 | }; | ||
50 | Ok(Some(Function::new(def_loc.id(db)))) | ||
41 | } | 51 | } |
42 | 52 | ||
43 | pub fn guess_for_name_ref( | 53 | pub fn guess_for_name_ref( |
44 | db: &impl HirDatabase, | 54 | db: &impl HirDatabase, |
45 | file_id: FileId, | 55 | file_id: FileId, |
46 | name_ref: ast::NameRef, | 56 | name_ref: ast::NameRef, |
47 | ) -> Option<Function> { | 57 | ) -> Cancelable<Option<Function>> { |
48 | Function::guess_for_node(db, file_id, name_ref.syntax()) | 58 | Function::guess_for_node(db, file_id, name_ref.syntax()) |
49 | } | 59 | } |
50 | 60 | ||
@@ -52,7 +62,7 @@ impl Function { | |||
52 | db: &impl HirDatabase, | 62 | db: &impl HirDatabase, |
53 | file_id: FileId, | 63 | file_id: FileId, |
54 | bind_pat: ast::BindPat, | 64 | bind_pat: ast::BindPat, |
55 | ) -> Option<Function> { | 65 | ) -> Cancelable<Option<Function>> { |
56 | Function::guess_for_node(db, file_id, bind_pat.syntax()) | 66 | Function::guess_for_node(db, file_id, bind_pat.syntax()) |
57 | } | 67 | } |
58 | 68 | ||
@@ -60,10 +70,9 @@ impl Function { | |||
60 | db: &impl HirDatabase, | 70 | db: &impl HirDatabase, |
61 | file_id: FileId, | 71 | file_id: FileId, |
62 | node: SyntaxNodeRef, | 72 | node: SyntaxNodeRef, |
63 | ) -> Option<Function> { | 73 | ) -> Cancelable<Option<Function>> { |
64 | let fn_def = node.ancestors().find_map(ast::FnDef::cast)?; | 74 | let fn_def = ctry!(node.ancestors().find_map(ast::FnDef::cast)); |
65 | let res = Function::guess_from_source(db, file_id, fn_def); | 75 | Function::guess_from_source(db, file_id, fn_def) |
66 | Some(res) | ||
67 | } | 76 | } |
68 | 77 | ||
69 | pub fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> { | 78 | pub fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> { |