diff options
author | Aleksey Kladov <[email protected]> | 2018-12-04 20:44:00 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-04 20:44:00 +0000 |
commit | d8b0379e1063941331253905795699a918233ef9 (patch) | |
tree | 18a7ef94cdf6575ed1954648287b284c3ace6451 /crates/ra_hir/src/function | |
parent | 947e3350e045aab1db9b232542425a3faa856907 (diff) |
Add functions to DefId
Diffstat (limited to 'crates/ra_hir/src/function')
-rw-r--r-- | crates/ra_hir/src/function/mod.rs | 41 |
1 files changed, 23 insertions, 18 deletions
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::{ | |||
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, |
@@ -35,16 +31,26 @@ impl Function { | |||
35 | db: &impl HirDatabase, | 31 | db: &impl HirDatabase, |
36 | file_id: FileId, | 32 | file_id: FileId, |
37 | fn_def: ast::FnDef, | 33 | fn_def: ast::FnDef, |
38 | ) -> Function { | 34 | ) -> Cancelable<Option<Function>> { |
39 | let fn_id = FnId::get(db, file_id, fn_def); | 35 | let module = ctry!(Module::guess_from_child_node(db, file_id, fn_def.syntax())?); |
40 | Function { fn_id } | 36 | let file_items = db.file_items(file_id); |
37 | let item_id = file_items.id_of(fn_def.syntax()); | ||
38 | let source_item_id = SourceItemId { file_id, item_id }; | ||
39 | let def_loc = DefLoc { | ||
40 | kind: DefKind::Function, | ||
41 | source_root_id: module.source_root_id, | ||
42 | module_id: module.module_id, | ||
43 | source_item_id, | ||
44 | }; | ||
45 | let fn_id = FnId(def_loc.id(db)); | ||
46 | Ok(Some(Function { fn_id })) | ||
41 | } | 47 | } |
42 | 48 | ||
43 | pub fn guess_for_name_ref( | 49 | pub fn guess_for_name_ref( |
44 | db: &impl HirDatabase, | 50 | db: &impl HirDatabase, |
45 | file_id: FileId, | 51 | file_id: FileId, |
46 | name_ref: ast::NameRef, | 52 | name_ref: ast::NameRef, |
47 | ) -> Option<Function> { | 53 | ) -> Cancelable<Option<Function>> { |
48 | Function::guess_for_node(db, file_id, name_ref.syntax()) | 54 | Function::guess_for_node(db, file_id, name_ref.syntax()) |
49 | } | 55 | } |
50 | 56 | ||
@@ -52,7 +58,7 @@ impl Function { | |||
52 | db: &impl HirDatabase, | 58 | db: &impl HirDatabase, |
53 | file_id: FileId, | 59 | file_id: FileId, |
54 | bind_pat: ast::BindPat, | 60 | bind_pat: ast::BindPat, |
55 | ) -> Option<Function> { | 61 | ) -> Cancelable<Option<Function>> { |
56 | Function::guess_for_node(db, file_id, bind_pat.syntax()) | 62 | Function::guess_for_node(db, file_id, bind_pat.syntax()) |
57 | } | 63 | } |
58 | 64 | ||
@@ -60,10 +66,9 @@ impl Function { | |||
60 | db: &impl HirDatabase, | 66 | db: &impl HirDatabase, |
61 | file_id: FileId, | 67 | file_id: FileId, |
62 | node: SyntaxNodeRef, | 68 | node: SyntaxNodeRef, |
63 | ) -> Option<Function> { | 69 | ) -> Cancelable<Option<Function>> { |
64 | let fn_def = node.ancestors().find_map(ast::FnDef::cast)?; | 70 | let fn_def = ctry!(node.ancestors().find_map(ast::FnDef::cast)); |
65 | let res = Function::guess_from_source(db, file_id, fn_def); | 71 | Function::guess_from_source(db, file_id, fn_def) |
66 | Some(res) | ||
67 | } | 72 | } |
68 | 73 | ||
69 | pub fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> { | 74 | pub fn scope(&self, db: &impl HirDatabase) -> Arc<FnScopes> { |