diff options
Diffstat (limited to 'crates/ra_analysis/src/hir/query_definitions.rs')
-rw-r--r-- | crates/ra_analysis/src/hir/query_definitions.rs | 157 |
1 files changed, 0 insertions, 157 deletions
diff --git a/crates/ra_analysis/src/hir/query_definitions.rs b/crates/ra_analysis/src/hir/query_definitions.rs deleted file mode 100644 index 00237b633..000000000 --- a/crates/ra_analysis/src/hir/query_definitions.rs +++ /dev/null | |||
@@ -1,157 +0,0 @@ | |||
1 | use std::{ | ||
2 | sync::Arc, | ||
3 | time::Instant, | ||
4 | }; | ||
5 | |||
6 | use rustc_hash::FxHashMap; | ||
7 | use ra_syntax::{ | ||
8 | AstNode, SyntaxNode, SmolStr, | ||
9 | ast::{self, FnDef, FnDefNode, NameOwner, ModuleItemOwner} | ||
10 | }; | ||
11 | use ra_db::SourceRootId; | ||
12 | |||
13 | use crate::{ | ||
14 | FileId, Cancelable, | ||
15 | hir::{ | ||
16 | FnId, | ||
17 | SourceFileItems, SourceItemId, | ||
18 | db::HirDatabase, | ||
19 | function::FnScopes, | ||
20 | module::{ | ||
21 | ModuleSource, ModuleSourceNode, ModuleId, | ||
22 | imp::Submodule, | ||
23 | nameres::{InputModuleItems, ItemMap, Resolver}, | ||
24 | }, | ||
25 | }, | ||
26 | }; | ||
27 | |||
28 | /// Resolve `FnId` to the corresponding `SyntaxNode` | ||
29 | pub(super) fn fn_syntax(db: &impl HirDatabase, fn_id: FnId) -> FnDefNode { | ||
30 | let item_id = fn_id.loc(db); | ||
31 | let syntax = db.file_item(item_id); | ||
32 | FnDef::cast(syntax.borrowed()).unwrap().owned() | ||
33 | } | ||
34 | |||
35 | pub(super) fn fn_scopes(db: &impl HirDatabase, fn_id: FnId) -> Arc<FnScopes> { | ||
36 | let syntax = db.fn_syntax(fn_id); | ||
37 | let res = FnScopes::new(syntax.borrowed()); | ||
38 | Arc::new(res) | ||
39 | } | ||
40 | |||
41 | pub(super) fn file_items(db: &impl HirDatabase, file_id: FileId) -> Arc<SourceFileItems> { | ||
42 | let source_file = db.source_file(file_id); | ||
43 | let source_file = source_file.borrowed(); | ||
44 | let mut res = SourceFileItems::default(); | ||
45 | source_file | ||
46 | .syntax() | ||
47 | .descendants() | ||
48 | .filter_map(ast::ModuleItem::cast) | ||
49 | .map(|it| it.syntax().owned()) | ||
50 | .for_each(|it| { | ||
51 | res.alloc(it); | ||
52 | }); | ||
53 | Arc::new(res) | ||
54 | } | ||
55 | |||
56 | pub(super) fn file_item(db: &impl HirDatabase, source_item_id: SourceItemId) -> SyntaxNode { | ||
57 | db.file_items(source_item_id.file_id)[source_item_id.item_id].clone() | ||
58 | } | ||
59 | |||
60 | pub(crate) fn submodules( | ||
61 | db: &impl HirDatabase, | ||
62 | source: ModuleSource, | ||
63 | ) -> Cancelable<Arc<Vec<Submodule>>> { | ||
64 | db.check_canceled()?; | ||
65 | let file_id = source.file_id(); | ||
66 | let submodules = match source.resolve(db) { | ||
67 | ModuleSourceNode::SourceFile(it) => collect_submodules(db, file_id, it.borrowed()), | ||
68 | ModuleSourceNode::Module(it) => it | ||
69 | .borrowed() | ||
70 | .item_list() | ||
71 | .map(|it| collect_submodules(db, file_id, it)) | ||
72 | .unwrap_or_else(Vec::new), | ||
73 | }; | ||
74 | return Ok(Arc::new(submodules)); | ||
75 | |||
76 | fn collect_submodules<'a>( | ||
77 | db: &impl HirDatabase, | ||
78 | file_id: FileId, | ||
79 | root: impl ast::ModuleItemOwner<'a>, | ||
80 | ) -> Vec<Submodule> { | ||
81 | modules(root) | ||
82 | .map(|(name, m)| { | ||
83 | if m.has_semi() { | ||
84 | Submodule::Declaration(name) | ||
85 | } else { | ||
86 | let src = ModuleSource::new_inline(db, file_id, m); | ||
87 | Submodule::Definition(name, src) | ||
88 | } | ||
89 | }) | ||
90 | .collect() | ||
91 | } | ||
92 | } | ||
93 | |||
94 | pub(crate) fn modules<'a>( | ||
95 | root: impl ast::ModuleItemOwner<'a>, | ||
96 | ) -> impl Iterator<Item = (SmolStr, ast::Module<'a>)> { | ||
97 | root.items() | ||
98 | .filter_map(|item| match item { | ||
99 | ast::ModuleItem::Module(m) => Some(m), | ||
100 | _ => None, | ||
101 | }) | ||
102 | .filter_map(|module| { | ||
103 | let name = module.name()?.text(); | ||
104 | Some((name, module)) | ||
105 | }) | ||
106 | } | ||
107 | |||
108 | pub(super) fn input_module_items( | ||
109 | db: &impl HirDatabase, | ||
110 | source_root: SourceRootId, | ||
111 | module_id: ModuleId, | ||
112 | ) -> Cancelable<Arc<InputModuleItems>> { | ||
113 | let module_tree = db.module_tree(source_root)?; | ||
114 | let source = module_id.source(&module_tree); | ||
115 | let file_items = db.file_items(source.file_id()); | ||
116 | let res = match source.resolve(db) { | ||
117 | ModuleSourceNode::SourceFile(it) => { | ||
118 | let items = it.borrowed().items(); | ||
119 | InputModuleItems::new(&file_items, items) | ||
120 | } | ||
121 | ModuleSourceNode::Module(it) => { | ||
122 | let items = it | ||
123 | .borrowed() | ||
124 | .item_list() | ||
125 | .into_iter() | ||
126 | .flat_map(|it| it.items()); | ||
127 | InputModuleItems::new(&file_items, items) | ||
128 | } | ||
129 | }; | ||
130 | Ok(Arc::new(res)) | ||
131 | } | ||
132 | |||
133 | pub(super) fn item_map( | ||
134 | db: &impl HirDatabase, | ||
135 | source_root: SourceRootId, | ||
136 | ) -> Cancelable<Arc<ItemMap>> { | ||
137 | let start = Instant::now(); | ||
138 | let module_tree = db.module_tree(source_root)?; | ||
139 | let input = module_tree | ||
140 | .modules() | ||
141 | .map(|id| { | ||
142 | let items = db.input_module_items(source_root, id)?; | ||
143 | Ok((id, items)) | ||
144 | }) | ||
145 | .collect::<Cancelable<FxHashMap<_, _>>>()?; | ||
146 | let resolver = Resolver { | ||
147 | db: db, | ||
148 | input: &input, | ||
149 | source_root, | ||
150 | module_tree, | ||
151 | result: ItemMap::default(), | ||
152 | }; | ||
153 | let res = resolver.resolve()?; | ||
154 | let elapsed = start.elapsed(); | ||
155 | log::info!("item_map: {:?}", elapsed); | ||
156 | Ok(Arc::new(res)) | ||
157 | } | ||