aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/hir/module/nameres.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_analysis/src/hir/module/nameres.rs')
-rw-r--r--crates/ra_analysis/src/hir/module/nameres.rs100
1 files changed, 12 insertions, 88 deletions
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.
17use std::{ 17use 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;
25use ra_syntax::{ 24use 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
31use crate::{ 30use 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
53impl FileItems { 52impl 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
74pub(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
89pub(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
170pub(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
195pub(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
244impl InputModuleItems { 168impl 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
309struct Resolver<'a, DB> { 233pub(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
317impl<'a, DB> Resolver<'a, DB> 241impl<'a, DB> Resolver<'a, DB>
318where 242where
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) {