diff options
author | Aleksey Kladov <[email protected]> | 2018-11-27 20:33:36 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-11-27 20:33:36 +0000 |
commit | b7049ea543d5ea9a965dfa51d9da923739f2420d (patch) | |
tree | 4cfda71ca655fc13479413825b452419fdc0e281 /crates/ra_analysis/src/hir/module | |
parent | 90bc832b22aefb2a382b3465793df7e528351aa1 (diff) |
move query definitions
Diffstat (limited to 'crates/ra_analysis/src/hir/module')
-rw-r--r-- | crates/ra_analysis/src/hir/module/imp.rs | 35 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/module/mod.rs | 10 | ||||
-rw-r--r-- | crates/ra_analysis/src/hir/module/nameres.rs | 100 |
3 files changed, 18 insertions, 127 deletions
diff --git a/crates/ra_analysis/src/hir/module/imp.rs b/crates/ra_analysis/src/hir/module/imp.rs index f9853584e..3b1baff76 100644 --- a/crates/ra_analysis/src/hir/module/imp.rs +++ b/crates/ra_analysis/src/hir/module/imp.rs | |||
@@ -15,7 +15,7 @@ use crate::{ | |||
15 | }; | 15 | }; |
16 | 16 | ||
17 | use super::{ | 17 | use super::{ |
18 | LinkData, LinkId, ModuleData, ModuleId, ModuleSource, ModuleSourceNode, | 18 | LinkData, LinkId, ModuleData, ModuleId, ModuleSource, |
19 | ModuleTree, Problem, | 19 | ModuleTree, Problem, |
20 | }; | 20 | }; |
21 | 21 | ||
@@ -34,39 +34,6 @@ impl Submodule { | |||
34 | } | 34 | } |
35 | } | 35 | } |
36 | 36 | ||
37 | pub(crate) fn submodules( | ||
38 | db: &impl HirDatabase, | ||
39 | source: ModuleSource, | ||
40 | ) -> Cancelable<Arc<Vec<Submodule>>> { | ||
41 | db::check_canceled(db)?; | ||
42 | let file_id = source.file_id(); | ||
43 | let submodules = match source.resolve(db) { | ||
44 | ModuleSourceNode::SourceFile(it) => collect_submodules(file_id, it.borrowed()), | ||
45 | ModuleSourceNode::Module(it) => it | ||
46 | .borrowed() | ||
47 | .item_list() | ||
48 | .map(|it| collect_submodules(file_id, it)) | ||
49 | .unwrap_or_else(Vec::new), | ||
50 | }; | ||
51 | return Ok(Arc::new(submodules)); | ||
52 | |||
53 | fn collect_submodules<'a>( | ||
54 | file_id: FileId, | ||
55 | root: impl ast::ModuleItemOwner<'a>, | ||
56 | ) -> Vec<Submodule> { | ||
57 | modules(root) | ||
58 | .map(|(name, m)| { | ||
59 | if m.has_semi() { | ||
60 | Submodule::Declaration(name) | ||
61 | } else { | ||
62 | let src = ModuleSource::new_inline(file_id, m); | ||
63 | Submodule::Definition(name, src) | ||
64 | } | ||
65 | }) | ||
66 | .collect() | ||
67 | } | ||
68 | } | ||
69 | |||
70 | pub(crate) fn modules<'a>( | 37 | pub(crate) fn modules<'a>( |
71 | root: impl ast::ModuleItemOwner<'a>, | 38 | root: impl ast::ModuleItemOwner<'a>, |
72 | ) -> impl Iterator<Item = (SmolStr, ast::Module<'a>)> { | 39 | ) -> impl Iterator<Item = (SmolStr, ast::Module<'a>)> { |
diff --git a/crates/ra_analysis/src/hir/module/mod.rs b/crates/ra_analysis/src/hir/module/mod.rs index 55b6639be..4d5945b1a 100644 --- a/crates/ra_analysis/src/hir/module/mod.rs +++ b/crates/ra_analysis/src/hir/module/mod.rs | |||
@@ -196,7 +196,7 @@ pub(crate) struct ModuleTree { | |||
196 | } | 196 | } |
197 | 197 | ||
198 | impl ModuleTree { | 198 | impl ModuleTree { |
199 | fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a { | 199 | pub(in crate::hir) fn modules<'a>(&'a self) -> impl Iterator<Item = ModuleId> + 'a { |
200 | self.mods.iter().map(|(id, _)| id) | 200 | self.mods.iter().map(|(id, _)| id) |
201 | } | 201 | } |
202 | 202 | ||
@@ -224,7 +224,7 @@ pub(crate) enum ModuleSource { | |||
224 | 224 | ||
225 | /// An owned syntax node for a module. Unlike `ModuleSource`, | 225 | /// An owned syntax node for a module. Unlike `ModuleSource`, |
226 | /// this holds onto the AST for the whole file. | 226 | /// this holds onto the AST for the whole file. |
227 | enum ModuleSourceNode { | 227 | pub(crate) enum ModuleSourceNode { |
228 | SourceFile(ast::SourceFileNode), | 228 | SourceFile(ast::SourceFileNode), |
229 | Module(ast::ModuleNode), | 229 | Module(ast::ModuleNode), |
230 | } | 230 | } |
@@ -244,7 +244,7 @@ pub enum Problem { | |||
244 | } | 244 | } |
245 | 245 | ||
246 | impl ModuleId { | 246 | impl ModuleId { |
247 | fn source(self, tree: &ModuleTree) -> ModuleSource { | 247 | pub(in crate::hir) fn source(self, tree: &ModuleTree) -> ModuleSource { |
248 | tree.mods[self].source | 248 | tree.mods[self].source |
249 | } | 249 | } |
250 | fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> { | 250 | fn parent_link(self, tree: &ModuleTree) -> Option<LinkId> { |
@@ -318,7 +318,7 @@ pub(crate) struct ModuleData { | |||
318 | } | 318 | } |
319 | 319 | ||
320 | impl ModuleSource { | 320 | impl ModuleSource { |
321 | fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource { | 321 | pub(crate) fn new_inline(file_id: FileId, module: ast::Module) -> ModuleSource { |
322 | assert!(!module.has_semi()); | 322 | assert!(!module.has_semi()); |
323 | let ptr = SyntaxPtr::new(file_id, module.syntax()); | 323 | let ptr = SyntaxPtr::new(file_id, module.syntax()); |
324 | ModuleSource::Module(ptr) | 324 | ModuleSource::Module(ptr) |
@@ -338,7 +338,7 @@ impl ModuleSource { | |||
338 | } | 338 | } |
339 | } | 339 | } |
340 | 340 | ||
341 | fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode { | 341 | pub(crate) fn resolve(self, db: &impl SyntaxDatabase) -> ModuleSourceNode { |
342 | match self { | 342 | match self { |
343 | ModuleSource::SourceFile(file_id) => { | 343 | ModuleSource::SourceFile(file_id) => { |
344 | let syntax = db.file_syntax(file_id); | 344 | let syntax = db.file_syntax(file_id); |
diff --git a/crates/ra_analysis/src/hir/module/nameres.rs b/crates/ra_analysis/src/hir/module/nameres.rs index eaf9f9373..db5d6d9c0 100644 --- a/crates/ra_analysis/src/hir/module/nameres.rs +++ b/crates/ra_analysis/src/hir/module/nameres.rs | |||
@@ -16,7 +16,6 @@ | |||
16 | //! structure itself is modified. | 16 | //! structure itself is modified. |
17 | use std::{ | 17 | use std::{ |
18 | sync::Arc, | 18 | sync::Arc, |
19 | time::Instant, | ||
20 | ops::Index, | 19 | ops::Index, |
21 | }; | 20 | }; |
22 | 21 | ||
@@ -25,7 +24,7 @@ use rustc_hash::FxHashMap; | |||
25 | use ra_syntax::{ | 24 | use ra_syntax::{ |
26 | SyntaxNode, SyntaxNodeRef, TextRange, | 25 | SyntaxNode, SyntaxNodeRef, TextRange, |
27 | SmolStr, SyntaxKind::{self, *}, | 26 | SmolStr, SyntaxKind::{self, *}, |
28 | ast::{self, ModuleItemOwner, AstNode} | 27 | ast::{self, AstNode} |
29 | }; | 28 | }; |
30 | 29 | ||
31 | use crate::{ | 30 | use crate::{ |
@@ -34,7 +33,7 @@ use crate::{ | |||
34 | hir::{ | 33 | hir::{ |
35 | Path, PathKind, | 34 | Path, PathKind, |
36 | HirDatabase, | 35 | HirDatabase, |
37 | module::{ModuleId, ModuleTree, ModuleSourceNode}, | 36 | module::{ModuleId, ModuleTree}, |
38 | }, | 37 | }, |
39 | input::SourceRootId, | 38 | input::SourceRootId, |
40 | arena::{Arena, Id} | 39 | arena::{Arena, Id} |
@@ -51,7 +50,7 @@ pub(crate) struct FileItems { | |||
51 | } | 50 | } |
52 | 51 | ||
53 | impl FileItems { | 52 | impl FileItems { |
54 | fn alloc(&mut self, item: SyntaxNode) -> FileItemId { | 53 | pub(crate) fn alloc(&mut self, item: SyntaxNode) -> FileItemId { |
55 | self.arena.alloc(item) | 54 | self.arena.alloc(item) |
56 | } | 55 | } |
57 | fn id_of(&self, item: SyntaxNodeRef) -> FileItemId { | 56 | fn id_of(&self, item: SyntaxNodeRef) -> FileItemId { |
@@ -71,29 +70,6 @@ impl Index<FileItemId> for FileItems { | |||
71 | } | 70 | } |
72 | } | 71 | } |
73 | 72 | ||
74 | pub(crate) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<FileItems> { | ||
75 | let source_file = db.file_syntax(file_id); | ||
76 | let source_file = source_file.borrowed(); | ||
77 | let mut res = FileItems::default(); | ||
78 | source_file | ||
79 | .syntax() | ||
80 | .descendants() | ||
81 | .filter_map(ast::ModuleItem::cast) | ||
82 | .map(|it| it.syntax().owned()) | ||
83 | .for_each(|it| { | ||
84 | res.alloc(it); | ||
85 | }); | ||
86 | Arc::new(res) | ||
87 | } | ||
88 | |||
89 | pub(crate) fn file_item( | ||
90 | db: &impl HirDatabase, | ||
91 | file_id: FileId, | ||
92 | file_item_id: FileItemId, | ||
93 | ) -> SyntaxNode { | ||
94 | db.file_items(file_id)[file_item_id].clone() | ||
95 | } | ||
96 | |||
97 | /// Item map is the result of the name resolution. Item map contains, for each | 73 | /// Item map is the result of the name resolution. Item map contains, for each |
98 | /// module, the set of visible items. | 74 | /// module, the set of visible items. |
99 | #[derive(Default, Debug, PartialEq, Eq)] | 75 | #[derive(Default, Debug, PartialEq, Eq)] |
@@ -167,58 +143,6 @@ enum ImportKind { | |||
167 | Named(NamedImport), | 143 | Named(NamedImport), |
168 | } | 144 | } |
169 | 145 | ||
170 | pub(crate) fn input_module_items( | ||
171 | db: &impl HirDatabase, | ||
172 | source_root: SourceRootId, | ||
173 | module_id: ModuleId, | ||
174 | ) -> Cancelable<Arc<InputModuleItems>> { | ||
175 | let module_tree = db.module_tree(source_root)?; | ||
176 | let source = module_id.source(&module_tree); | ||
177 | let file_items = db.file_items(source.file_id()); | ||
178 | let res = match source.resolve(db) { | ||
179 | ModuleSourceNode::SourceFile(it) => { | ||
180 | let items = it.borrowed().items(); | ||
181 | InputModuleItems::new(&file_items, items) | ||
182 | } | ||
183 | ModuleSourceNode::Module(it) => { | ||
184 | let items = it | ||
185 | .borrowed() | ||
186 | .item_list() | ||
187 | .into_iter() | ||
188 | .flat_map(|it| it.items()); | ||
189 | InputModuleItems::new(&file_items, items) | ||
190 | } | ||
191 | }; | ||
192 | Ok(Arc::new(res)) | ||
193 | } | ||
194 | |||
195 | pub(crate) fn item_map( | ||
196 | db: &impl HirDatabase, | ||
197 | source_root: SourceRootId, | ||
198 | ) -> Cancelable<Arc<ItemMap>> { | ||
199 | let start = Instant::now(); | ||
200 | let module_tree = db.module_tree(source_root)?; | ||
201 | let input = module_tree | ||
202 | .modules() | ||
203 | .map(|id| { | ||
204 | let items = db.input_module_items(source_root, id)?; | ||
205 | Ok((id, items)) | ||
206 | }) | ||
207 | .collect::<Cancelable<FxHashMap<_, _>>>()?; | ||
208 | let mut resolver = Resolver { | ||
209 | db: db, | ||
210 | input: &input, | ||
211 | source_root, | ||
212 | module_tree, | ||
213 | result: ItemMap::default(), | ||
214 | }; | ||
215 | resolver.resolve()?; | ||
216 | let res = resolver.result; | ||
217 | let elapsed = start.elapsed(); | ||
218 | log::info!("item_map: {:?}", elapsed); | ||
219 | Ok(Arc::new(res)) | ||
220 | } | ||
221 | |||
222 | /// Resolution is basically `DefId` atm, but it should account for stuff like | 146 | /// Resolution is basically `DefId` atm, but it should account for stuff like |
223 | /// multiple namespaces, ambiguity and errors. | 147 | /// multiple namespaces, ambiguity and errors. |
224 | #[derive(Debug, Clone, PartialEq, Eq)] | 148 | #[derive(Debug, Clone, PartialEq, Eq)] |
@@ -242,7 +166,7 @@ pub(crate) struct Resolution { | |||
242 | // } | 166 | // } |
243 | 167 | ||
244 | impl InputModuleItems { | 168 | impl InputModuleItems { |
245 | fn new<'a>( | 169 | pub(in crate::hir) fn new<'a>( |
246 | file_items: &FileItems, | 170 | file_items: &FileItems, |
247 | items: impl Iterator<Item = ast::ModuleItem<'a>>, | 171 | items: impl Iterator<Item = ast::ModuleItem<'a>>, |
248 | ) -> InputModuleItems { | 172 | ) -> InputModuleItems { |
@@ -306,19 +230,19 @@ impl ModuleItem { | |||
306 | } | 230 | } |
307 | } | 231 | } |
308 | 232 | ||
309 | struct Resolver<'a, DB> { | 233 | pub(in crate::hir) struct Resolver<'a, DB> { |
310 | db: &'a DB, | 234 | pub db: &'a DB, |
311 | input: &'a FxHashMap<ModuleId, Arc<InputModuleItems>>, | 235 | pub input: &'a FxHashMap<ModuleId, Arc<InputModuleItems>>, |
312 | source_root: SourceRootId, | 236 | pub source_root: SourceRootId, |
313 | module_tree: Arc<ModuleTree>, | 237 | pub module_tree: Arc<ModuleTree>, |
314 | result: ItemMap, | 238 | pub result: ItemMap, |
315 | } | 239 | } |
316 | 240 | ||
317 | impl<'a, DB> Resolver<'a, DB> | 241 | impl<'a, DB> Resolver<'a, DB> |
318 | where | 242 | where |
319 | DB: HirDatabase, | 243 | DB: HirDatabase, |
320 | { | 244 | { |
321 | fn resolve(&mut self) -> Cancelable<()> { | 245 | pub(in crate::hir) fn resolve(mut self) -> Cancelable<ItemMap> { |
322 | for (&module_id, items) in self.input.iter() { | 246 | for (&module_id, items) in self.input.iter() { |
323 | self.populate_module(module_id, items) | 247 | self.populate_module(module_id, items) |
324 | } | 248 | } |
@@ -327,7 +251,7 @@ where | |||
327 | crate::db::check_canceled(self.db)?; | 251 | crate::db::check_canceled(self.db)?; |
328 | self.resolve_imports(module_id); | 252 | self.resolve_imports(module_id); |
329 | } | 253 | } |
330 | Ok(()) | 254 | Ok(self.result) |
331 | } | 255 | } |
332 | 256 | ||
333 | fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) { | 257 | fn populate_module(&mut self, module_id: ModuleId, input: &InputModuleItems) { |