diff options
author | Aleksey Kladov <[email protected]> | 2018-11-05 11:10:20 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-11-05 11:10:20 +0000 |
commit | 6bbcfca7aec6408cf27415ae6f965adc472d6f18 (patch) | |
tree | 7b332c6db6e662ca31aafded143476d81a60453f /crates/ra_analysis/src/descriptors/module/imp.rs | |
parent | 44d891938493cc32efd2e44d81bc76cc3bc391c0 (diff) |
Fully add inline modules to module tree
Diffstat (limited to 'crates/ra_analysis/src/descriptors/module/imp.rs')
-rw-r--r-- | crates/ra_analysis/src/descriptors/module/imp.rs | 49 |
1 files changed, 34 insertions, 15 deletions
diff --git a/crates/ra_analysis/src/descriptors/module/imp.rs b/crates/ra_analysis/src/descriptors/module/imp.rs index 257a323ed..b3b1f1f21 100644 --- a/crates/ra_analysis/src/descriptors/module/imp.rs +++ b/crates/ra_analysis/src/descriptors/module/imp.rs | |||
@@ -1,7 +1,7 @@ | |||
1 | use std::sync::Arc; | 1 | use std::sync::Arc; |
2 | 2 | ||
3 | use ra_syntax::{ | 3 | use ra_syntax::{ |
4 | ast::{self, ModuleItemOwner, NameOwner, AstNode}, | 4 | ast::{self, ModuleItemOwner, NameOwner}, |
5 | SmolStr, | 5 | SmolStr, |
6 | }; | 6 | }; |
7 | use relative_path::RelativePathBuf; | 7 | use relative_path::RelativePathBuf; |
@@ -12,7 +12,6 @@ use crate::{ | |||
12 | descriptors::DescriptorDatabase, | 12 | descriptors::DescriptorDatabase, |
13 | input::{SourceRoot, SourceRootId}, | 13 | input::{SourceRoot, SourceRootId}, |
14 | Cancelable, FileId, FileResolverImp, | 14 | Cancelable, FileId, FileResolverImp, |
15 | syntax_ptr::SyntaxPtr, | ||
16 | }; | 15 | }; |
17 | 16 | ||
18 | use super::{ | 17 | use super::{ |
@@ -23,7 +22,7 @@ use super::{ | |||
23 | #[derive(Clone, Hash, PartialEq, Eq, Debug)] | 22 | #[derive(Clone, Hash, PartialEq, Eq, Debug)] |
24 | pub(crate) enum Submodule { | 23 | pub(crate) enum Submodule { |
25 | Declaration(SmolStr), | 24 | Declaration(SmolStr), |
26 | Definition(SmolStr, SyntaxPtr), | 25 | Definition(SmolStr, ModuleSource), |
27 | } | 26 | } |
28 | 27 | ||
29 | impl Submodule { | 28 | impl Submodule { |
@@ -60,7 +59,8 @@ pub(crate) fn submodules( | |||
60 | if m.has_semi() { | 59 | if m.has_semi() { |
61 | Submodule::Declaration(name) | 60 | Submodule::Declaration(name) |
62 | } else { | 61 | } else { |
63 | Submodule::Definition(name, SyntaxPtr::new(file_id, m.syntax())) | 62 | let src = ModuleSource::new_inline(file_id, m); |
63 | Submodule::Definition(name, src) | ||
64 | } | 64 | } |
65 | }) | 65 | }) |
66 | .collect() | 66 | .collect() |
@@ -121,7 +121,8 @@ fn create_module_tree<'a>( | |||
121 | 121 | ||
122 | let source_root = db.source_root(source_root); | 122 | let source_root = db.source_root(source_root); |
123 | for &file_id in source_root.files.iter() { | 123 | for &file_id in source_root.files.iter() { |
124 | if visited.contains(&file_id) { | 124 | let source = ModuleSource::File(file_id); |
125 | if visited.contains(&source) { | ||
125 | continue; // TODO: use explicit crate_roots here | 126 | continue; // TODO: use explicit crate_roots here |
126 | } | 127 | } |
127 | assert!(!roots.contains_key(&file_id)); | 128 | assert!(!roots.contains_key(&file_id)); |
@@ -132,7 +133,7 @@ fn create_module_tree<'a>( | |||
132 | &mut visited, | 133 | &mut visited, |
133 | &mut roots, | 134 | &mut roots, |
134 | None, | 135 | None, |
135 | file_id, | 136 | source, |
136 | )?; | 137 | )?; |
137 | roots.insert(file_id, module_id); | 138 | roots.insert(file_id, module_id); |
138 | } | 139 | } |
@@ -143,18 +144,18 @@ fn build_subtree( | |||
143 | db: &impl DescriptorDatabase, | 144 | db: &impl DescriptorDatabase, |
144 | source_root: &SourceRoot, | 145 | source_root: &SourceRoot, |
145 | tree: &mut ModuleTree, | 146 | tree: &mut ModuleTree, |
146 | visited: &mut FxHashSet<FileId>, | 147 | visited: &mut FxHashSet<ModuleSource>, |
147 | roots: &mut FxHashMap<FileId, ModuleId>, | 148 | roots: &mut FxHashMap<FileId, ModuleId>, |
148 | parent: Option<LinkId>, | 149 | parent: Option<LinkId>, |
149 | file_id: FileId, | 150 | source: ModuleSource, |
150 | ) -> Cancelable<ModuleId> { | 151 | ) -> Cancelable<ModuleId> { |
151 | visited.insert(file_id); | 152 | visited.insert(source); |
152 | let id = tree.push_mod(ModuleData { | 153 | let id = tree.push_mod(ModuleData { |
153 | source: ModuleSource::File(file_id), | 154 | source, |
154 | parent, | 155 | parent, |
155 | children: Vec::new(), | 156 | children: Vec::new(), |
156 | }); | 157 | }); |
157 | for sub in db.submodules(ModuleSource::File(file_id))?.iter() { | 158 | for sub in db.submodules(source)?.iter() { |
158 | let link = tree.push_link(LinkData { | 159 | let link = tree.push_link(LinkData { |
159 | name: sub.name().clone(), | 160 | name: sub.name().clone(), |
160 | owner: id, | 161 | owner: id, |
@@ -165,7 +166,7 @@ fn build_subtree( | |||
165 | let (points_to, problem) = match sub { | 166 | let (points_to, problem) = match sub { |
166 | Submodule::Declaration(name) => { | 167 | Submodule::Declaration(name) => { |
167 | let (points_to, problem) = | 168 | let (points_to, problem) = |
168 | resolve_submodule(file_id, &name, &source_root.file_resolver); | 169 | resolve_submodule(source, &name, &source_root.file_resolver); |
169 | let points_to = points_to | 170 | let points_to = points_to |
170 | .into_iter() | 171 | .into_iter() |
171 | .map(|file_id| match roots.remove(&file_id) { | 172 | .map(|file_id| match roots.remove(&file_id) { |
@@ -180,13 +181,24 @@ fn build_subtree( | |||
180 | visited, | 181 | visited, |
181 | roots, | 182 | roots, |
182 | Some(link), | 183 | Some(link), |
183 | file_id, | 184 | ModuleSource::File(file_id), |
184 | ), | 185 | ), |
185 | }) | 186 | }) |
186 | .collect::<Cancelable<Vec<_>>>()?; | 187 | .collect::<Cancelable<Vec<_>>>()?; |
187 | (points_to, problem) | 188 | (points_to, problem) |
188 | } | 189 | } |
189 | Submodule::Definition(..) => continue, | 190 | Submodule::Definition(_name, submodule_source) => { |
191 | let points_to = build_subtree( | ||
192 | db, | ||
193 | source_root, | ||
194 | tree, | ||
195 | visited, | ||
196 | roots, | ||
197 | Some(link), | ||
198 | *submodule_source, | ||
199 | )?; | ||
200 | (vec![points_to], None) | ||
201 | } | ||
190 | }; | 202 | }; |
191 | 203 | ||
192 | tree.link_mut(link).points_to = points_to; | 204 | tree.link_mut(link).points_to = points_to; |
@@ -196,10 +208,17 @@ fn build_subtree( | |||
196 | } | 208 | } |
197 | 209 | ||
198 | fn resolve_submodule( | 210 | fn resolve_submodule( |
199 | file_id: FileId, | 211 | source: ModuleSource, |
200 | name: &SmolStr, | 212 | name: &SmolStr, |
201 | file_resolver: &FileResolverImp, | 213 | file_resolver: &FileResolverImp, |
202 | ) -> (Vec<FileId>, Option<Problem>) { | 214 | ) -> (Vec<FileId>, Option<Problem>) { |
215 | let file_id = match source { | ||
216 | ModuleSource::File(it) => it, | ||
217 | ModuleSource::Inline(..) => { | ||
218 | // TODO | ||
219 | return (Vec::new(), None); | ||
220 | } | ||
221 | }; | ||
203 | let mod_name = file_resolver.file_stem(file_id); | 222 | let mod_name = file_resolver.file_stem(file_id); |
204 | let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; | 223 | let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; |
205 | 224 | ||