diff options
author | Aleksey Kladov <[email protected]> | 2018-11-27 22:45:36 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-11-27 22:45:36 +0000 |
commit | 5e7f4202cf4d64f565d6d035cd2e854acfc336ab (patch) | |
tree | bc76e79dd732fecc217dbb1a2109e5ae1a1803fc /crates/ra_analysis/src/hir/mod.rs | |
parent | b9100d769a043c55f83b709de2714dac935e333f (diff) |
Move FileItems up
Diffstat (limited to 'crates/ra_analysis/src/hir/mod.rs')
-rw-r--r-- | crates/ra_analysis/src/hir/mod.rs | 38 |
1 files changed, 37 insertions, 1 deletions
diff --git a/crates/ra_analysis/src/hir/mod.rs b/crates/ra_analysis/src/hir/mod.rs index 863ffd919..3d4a55ca4 100644 --- a/crates/ra_analysis/src/hir/mod.rs +++ b/crates/ra_analysis/src/hir/mod.rs | |||
@@ -11,15 +11,20 @@ mod function; | |||
11 | mod module; | 11 | mod module; |
12 | mod path; | 12 | mod path; |
13 | 13 | ||
14 | use std::ops::Index; | ||
15 | |||
16 | use ra_syntax::{SyntaxNodeRef, SyntaxNode}; | ||
17 | |||
14 | use crate::{ | 18 | use crate::{ |
15 | hir::db::HirDatabase, | 19 | hir::db::HirDatabase, |
16 | loc2id::{DefId, DefLoc}, | 20 | loc2id::{DefId, DefLoc}, |
17 | Cancelable, | 21 | Cancelable, |
22 | arena::{Arena, Id}, | ||
18 | }; | 23 | }; |
19 | 24 | ||
20 | pub(crate) use self::{ | 25 | pub(crate) use self::{ |
21 | path::{Path, PathKind}, | 26 | path::{Path, PathKind}, |
22 | module::{Module, ModuleId, Problem, nameres::FileItemId}, | 27 | module::{Module, ModuleId, Problem}, |
23 | function::{Function, FnScopes}, | 28 | function::{Function, FnScopes}, |
24 | }; | 29 | }; |
25 | 30 | ||
@@ -43,3 +48,34 @@ impl DefId { | |||
43 | Ok(res) | 48 | Ok(res) |
44 | } | 49 | } |
45 | } | 50 | } |
51 | |||
52 | /// Identifier of item within a specific file. This is stable over reparses, so | ||
53 | /// it's OK to use it as a salsa key/value. | ||
54 | pub(crate) type FileItemId = Id<SyntaxNode>; | ||
55 | |||
56 | /// Maps item's `SyntaxNode`s to `FileItemId` and back. | ||
57 | #[derive(Debug, PartialEq, Eq, Default)] | ||
58 | pub(crate) struct FileItems { | ||
59 | arena: Arena<SyntaxNode>, | ||
60 | } | ||
61 | |||
62 | impl FileItems { | ||
63 | fn alloc(&mut self, item: SyntaxNode) -> FileItemId { | ||
64 | self.arena.alloc(item) | ||
65 | } | ||
66 | fn id_of(&self, item: SyntaxNodeRef) -> FileItemId { | ||
67 | let (id, _item) = self | ||
68 | .arena | ||
69 | .iter() | ||
70 | .find(|(_id, i)| i.borrowed() == item) | ||
71 | .unwrap(); | ||
72 | id | ||
73 | } | ||
74 | } | ||
75 | |||
76 | impl Index<FileItemId> for FileItems { | ||
77 | type Output = SyntaxNode; | ||
78 | fn index(&self, idx: FileItemId) -> &SyntaxNode { | ||
79 | &self.arena[idx] | ||
80 | } | ||
81 | } | ||