diff options
Diffstat (limited to 'crates/ra_analysis/src/imp.rs')
-rw-r--r-- | crates/ra_analysis/src/imp.rs | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/crates/ra_analysis/src/imp.rs b/crates/ra_analysis/src/imp.rs index c0bed04bf..49b863756 100644 --- a/crates/ra_analysis/src/imp.rs +++ b/crates/ra_analysis/src/imp.rs | |||
@@ -220,6 +220,8 @@ impl AnalysisImpl { | |||
220 | let source_root = self.db.file_source_root(file_id); | 220 | let source_root = self.db.file_source_root(file_id); |
221 | self.db.module_tree(source_root) | 221 | self.db.module_tree(source_root) |
222 | } | 222 | } |
223 | /// This return `Vec`: a module may be inclucded from several places. | ||
224 | /// We don't handle this case yet though, so the Vec has length at most one. | ||
223 | pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> { | 225 | pub fn parent_module(&self, position: FilePosition) -> Cancelable<Vec<(FileId, FileSymbol)>> { |
224 | let descr = match ModuleDescriptor::guess_from_position(&*self.db, position)? { | 226 | let descr = match ModuleDescriptor::guess_from_position(&*self.db, position)? { |
225 | None => return Ok(Vec::new()), | 227 | None => return Ok(Vec::new()), |
@@ -238,18 +240,21 @@ impl AnalysisImpl { | |||
238 | }; | 240 | }; |
239 | Ok(vec![(file_id, sym)]) | 241 | Ok(vec![(file_id, sym)]) |
240 | } | 242 | } |
243 | /// Returns `Vec` for the same reason as `parent_module` | ||
241 | pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { | 244 | pub fn crate_for(&self, file_id: FileId) -> Cancelable<Vec<CrateId>> { |
242 | let module_tree = self.module_tree(file_id)?; | 245 | let descr = match ModuleDescriptor::guess_from_file_id(&*self.db, file_id)? { |
243 | let crate_graph = self.db.crate_graph(); | 246 | None => return Ok(Vec::new()), |
244 | let res = module_tree | 247 | Some(it) => it, |
245 | .modules_for_source(ModuleSource::SourceFile(file_id)) | 248 | }; |
246 | .into_iter() | 249 | let root = descr.crate_root(); |
247 | .map(|it| it.root(&module_tree)) | 250 | let file_id = root |
248 | .filter_map(|it| it.source(&module_tree).as_file()) | 251 | .source() |
249 | .filter_map(|it| crate_graph.crate_id_for_crate_root(it)) | 252 | .as_file() |
250 | .collect(); | 253 | .expect("root module always has a file as a source"); |
251 | 254 | ||
252 | Ok(res) | 255 | let crate_graph = self.db.crate_graph(); |
256 | let crate_id = crate_graph.crate_id_for_crate_root(file_id); | ||
257 | Ok(crate_id.into_iter().collect()) | ||
253 | } | 258 | } |
254 | pub fn crate_root(&self, crate_id: CrateId) -> FileId { | 259 | pub fn crate_root(&self, crate_id: CrateId) -> FileId { |
255 | self.db.crate_graph().crate_roots[&crate_id] | 260 | self.db.crate_graph().crate_roots[&crate_id] |