diff options
author | Aleksey Kladov <[email protected]> | 2019-10-30 13:12:55 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-10-30 14:43:11 +0000 |
commit | 16e620c052016010b2f17070a98bdc1e7e849ab3 (patch) | |
tree | a00ec9181595cea9e340da3ead490348ff7b1666 /crates/ra_hir_def/src/lib.rs | |
parent | f996b6019bd2f388bd9994ea83f25487eb111560 (diff) |
move raw_items to hir_def
Diffstat (limited to 'crates/ra_hir_def/src/lib.rs')
-rw-r--r-- | crates/ra_hir_def/src/lib.rs | 95 |
1 files changed, 94 insertions, 1 deletions
diff --git a/crates/ra_hir_def/src/lib.rs b/crates/ra_hir_def/src/lib.rs index 4d6b9db03..95d503325 100644 --- a/crates/ra_hir_def/src/lib.rs +++ b/crates/ra_hir_def/src/lib.rs | |||
@@ -8,12 +8,20 @@ | |||
8 | //! actually true. | 8 | //! actually true. |
9 | 9 | ||
10 | pub mod db; | 10 | pub mod db; |
11 | pub mod either; | ||
12 | pub mod attr; | ||
13 | pub mod name; | ||
14 | pub mod path; | ||
15 | pub mod type_ref; | ||
16 | |||
17 | // FIXME: this should be private | ||
18 | pub mod nameres; | ||
11 | 19 | ||
12 | use std::hash::{Hash, Hasher}; | 20 | use std::hash::{Hash, Hasher}; |
13 | 21 | ||
14 | use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId}; | 22 | use hir_expand::{ast_id_map::FileAstId, db::AstDatabase, AstId, HirFileId}; |
15 | use ra_arena::{impl_arena_id, RawId}; | 23 | use ra_arena::{impl_arena_id, RawId}; |
16 | use ra_db::{salsa, CrateId}; | 24 | use ra_db::{salsa, CrateId, FileId}; |
17 | use ra_syntax::{ast, AstNode, SyntaxNode}; | 25 | use ra_syntax::{ast, AstNode, SyntaxNode}; |
18 | 26 | ||
19 | use crate::db::InternDatabase; | 27 | use crate::db::InternDatabase; |
@@ -24,6 +32,68 @@ pub struct Source<T> { | |||
24 | pub ast: T, | 32 | pub ast: T, |
25 | } | 33 | } |
26 | 34 | ||
35 | pub enum ModuleSource { | ||
36 | SourceFile(ast::SourceFile), | ||
37 | Module(ast::Module), | ||
38 | } | ||
39 | |||
40 | impl ModuleSource { | ||
41 | pub fn new( | ||
42 | db: &impl db::DefDatabase2, | ||
43 | file_id: Option<FileId>, | ||
44 | decl_id: Option<AstId<ast::Module>>, | ||
45 | ) -> ModuleSource { | ||
46 | match (file_id, decl_id) { | ||
47 | (Some(file_id), _) => { | ||
48 | let source_file = db.parse(file_id).tree(); | ||
49 | ModuleSource::SourceFile(source_file) | ||
50 | } | ||
51 | (None, Some(item_id)) => { | ||
52 | let module = item_id.to_node(db); | ||
53 | assert!(module.item_list().is_some(), "expected inline module"); | ||
54 | ModuleSource::Module(module) | ||
55 | } | ||
56 | (None, None) => panic!(), | ||
57 | } | ||
58 | } | ||
59 | |||
60 | // FIXME: this methods do not belong here | ||
61 | pub fn from_position( | ||
62 | db: &impl db::DefDatabase2, | ||
63 | position: ra_db::FilePosition, | ||
64 | ) -> ModuleSource { | ||
65 | let parse = db.parse(position.file_id); | ||
66 | match &ra_syntax::algo::find_node_at_offset::<ast::Module>( | ||
67 | parse.tree().syntax(), | ||
68 | position.offset, | ||
69 | ) { | ||
70 | Some(m) if !m.has_semi() => ModuleSource::Module(m.clone()), | ||
71 | _ => { | ||
72 | let source_file = parse.tree(); | ||
73 | ModuleSource::SourceFile(source_file) | ||
74 | } | ||
75 | } | ||
76 | } | ||
77 | |||
78 | pub fn from_child_node( | ||
79 | db: &impl db::DefDatabase2, | ||
80 | file_id: FileId, | ||
81 | child: &SyntaxNode, | ||
82 | ) -> ModuleSource { | ||
83 | if let Some(m) = child.ancestors().filter_map(ast::Module::cast).find(|it| !it.has_semi()) { | ||
84 | ModuleSource::Module(m) | ||
85 | } else { | ||
86 | let source_file = db.parse(file_id).tree(); | ||
87 | ModuleSource::SourceFile(source_file) | ||
88 | } | ||
89 | } | ||
90 | |||
91 | pub fn from_file_id(db: &impl db::DefDatabase2, file_id: FileId) -> ModuleSource { | ||
92 | let source_file = db.parse(file_id).tree(); | ||
93 | ModuleSource::SourceFile(source_file) | ||
94 | } | ||
95 | } | ||
96 | |||
27 | impl<T> Source<T> { | 97 | impl<T> Source<T> { |
28 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { | 98 | pub fn map<F: FnOnce(T) -> U, U>(self, f: F) -> Source<U> { |
29 | Source { file_id: self.file_id, ast: f(self.ast) } | 99 | Source { file_id: self.file_id, ast: f(self.ast) } |
@@ -156,6 +226,18 @@ impl AstItemDef<ast::StructDef> for StructId { | |||
156 | } | 226 | } |
157 | 227 | ||
158 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 228 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
229 | pub struct UnionId(salsa::InternId); | ||
230 | impl_intern_key!(UnionId); | ||
231 | impl AstItemDef<ast::StructDef> for UnionId { | ||
232 | fn intern(db: &impl InternDatabase, loc: ItemLoc<ast::StructDef>) -> Self { | ||
233 | db.intern_union(loc) | ||
234 | } | ||
235 | fn lookup_intern(self, db: &impl InternDatabase) -> ItemLoc<ast::StructDef> { | ||
236 | db.lookup_intern_union(self) | ||
237 | } | ||
238 | } | ||
239 | |||
240 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
159 | pub struct EnumId(salsa::InternId); | 241 | pub struct EnumId(salsa::InternId); |
160 | impl_intern_key!(EnumId); | 242 | impl_intern_key!(EnumId); |
161 | impl AstItemDef<ast::EnumDef> for EnumId { | 243 | impl AstItemDef<ast::EnumDef> for EnumId { |
@@ -167,6 +249,17 @@ impl AstItemDef<ast::EnumDef> for EnumId { | |||
167 | } | 249 | } |
168 | } | 250 | } |
169 | 251 | ||
252 | // FIXME: rename to `VariantId`, only enums can ave variants | ||
253 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
254 | pub struct EnumVariantId { | ||
255 | parent: EnumId, | ||
256 | local_id: LocalEnumVariantId, | ||
257 | } | ||
258 | |||
259 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
260 | pub(crate) struct LocalEnumVariantId(RawId); | ||
261 | impl_arena_id!(LocalEnumVariantId); | ||
262 | |||
170 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 263 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
171 | pub struct ConstId(salsa::InternId); | 264 | pub struct ConstId(salsa::InternId); |
172 | impl_intern_key!(ConstId); | 265 | impl_intern_key!(ConstId); |