aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/hir/mod.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-11-27 22:45:36 +0000
committerAleksey Kladov <[email protected]>2018-11-27 22:45:36 +0000
commit5e7f4202cf4d64f565d6d035cd2e854acfc336ab (patch)
treebc76e79dd732fecc217dbb1a2109e5ae1a1803fc /crates/ra_analysis/src/hir/mod.rs
parentb9100d769a043c55f83b709de2714dac935e333f (diff)
Move FileItems up
Diffstat (limited to 'crates/ra_analysis/src/hir/mod.rs')
-rw-r--r--crates/ra_analysis/src/hir/mod.rs38
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;
11mod module; 11mod module;
12mod path; 12mod path;
13 13
14use std::ops::Index;
15
16use ra_syntax::{SyntaxNodeRef, SyntaxNode};
17
14use crate::{ 18use 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
20pub(crate) use self::{ 25pub(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.
54pub(crate) type FileItemId = Id<SyntaxNode>;
55
56/// Maps item's `SyntaxNode`s to `FileItemId` and back.
57#[derive(Debug, PartialEq, Eq, Default)]
58pub(crate) struct FileItems {
59 arena: Arena<SyntaxNode>,
60}
61
62impl 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
76impl Index<FileItemId> for FileItems {
77 type Output = SyntaxNode;
78 fn index(&self, idx: FileItemId) -> &SyntaxNode {
79 &self.arena[idx]
80 }
81}