diff options
author | Aleksey Kladov <[email protected]> | 2018-12-04 19:46:23 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-04 19:46:23 +0000 |
commit | 45fce9034909f2f0c5f6ac70eca7cf87ec3db856 (patch) | |
tree | e8c664b75784b4395953ba6326514605e4ba40ab /crates/ra_hir/src/module | |
parent | a9e4142f434b1a2aa0729387534121fdf9c812a6 (diff) |
ModuleSource is ItemSource
Diffstat (limited to 'crates/ra_hir/src/module')
-rw-r--r-- | crates/ra_hir/src/module/imp.rs | 13 | ||||
-rw-r--r-- | crates/ra_hir/src/module/mod.rs | 69 |
2 files changed, 35 insertions, 47 deletions
diff --git a/crates/ra_hir/src/module/imp.rs b/crates/ra_hir/src/module/imp.rs index 76ea129a7..0eec38797 100644 --- a/crates/ra_hir/src/module/imp.rs +++ b/crates/ra_hir/src/module/imp.rs | |||
@@ -66,7 +66,7 @@ fn create_module_tree<'a>( | |||
66 | 66 | ||
67 | let source_root = db.source_root(source_root); | 67 | let source_root = db.source_root(source_root); |
68 | for &file_id in source_root.files.iter() { | 68 | for &file_id in source_root.files.iter() { |
69 | let source = ModuleSource::SourceFile(file_id); | 69 | let source = ModuleSource::new_file(db, file_id); |
70 | if visited.contains(&source) { | 70 | if visited.contains(&source) { |
71 | continue; // TODO: use explicit crate_roots here | 71 | continue; // TODO: use explicit crate_roots here |
72 | } | 72 | } |
@@ -126,7 +126,7 @@ fn build_subtree( | |||
126 | visited, | 126 | visited, |
127 | roots, | 127 | roots, |
128 | Some(link), | 128 | Some(link), |
129 | ModuleSource::SourceFile(file_id), | 129 | ModuleSource::new_file(db, file_id), |
130 | ), | 130 | ), |
131 | }) | 131 | }) |
132 | .collect::<Cancelable<Vec<_>>>()?; | 132 | .collect::<Cancelable<Vec<_>>>()?; |
@@ -157,13 +157,8 @@ fn resolve_submodule( | |||
157 | name: &SmolStr, | 157 | name: &SmolStr, |
158 | file_resolver: &FileResolverImp, | 158 | file_resolver: &FileResolverImp, |
159 | ) -> (Vec<FileId>, Option<Problem>) { | 159 | ) -> (Vec<FileId>, Option<Problem>) { |
160 | let file_id = match source { | 160 | // TODO: handle submodules of inline modules properly |
161 | ModuleSource::SourceFile(it) => it, | 161 | let file_id = source.file_id(); |
162 | ModuleSource::Module(..) => { | ||
163 | // TODO | ||
164 | return (Vec::new(), None); | ||
165 | } | ||
166 | }; | ||
167 | let mod_name = file_resolver.file_stem(file_id); | 162 | let mod_name = file_resolver.file_stem(file_id); |
168 | let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; | 163 | let is_dir_owner = mod_name == "mod" || mod_name == "lib" || mod_name == "main"; |
169 | 164 | ||
diff --git a/crates/ra_hir/src/module/mod.rs b/crates/ra_hir/src/module/mod.rs index 3ae83d8cb..fa9ee94eb 100644 --- a/crates/ra_hir/src/module/mod.rs +++ b/crates/ra_hir/src/module/mod.rs | |||
@@ -14,7 +14,7 @@ use ra_db::{SourceRootId, FileId, FilePosition, Cancelable}; | |||
14 | use relative_path::RelativePathBuf; | 14 | use relative_path::RelativePathBuf; |
15 | 15 | ||
16 | use crate::{ | 16 | use crate::{ |
17 | DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, | 17 | DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, |
18 | arena::{Arena, Id}, | 18 | arena::{Arena, Id}, |
19 | }; | 19 | }; |
20 | 20 | ||
@@ -37,7 +37,8 @@ impl Module { | |||
37 | db: &impl HirDatabase, | 37 | db: &impl HirDatabase, |
38 | file_id: FileId, | 38 | file_id: FileId, |
39 | ) -> Cancelable<Option<Module>> { | 39 | ) -> Cancelable<Option<Module>> { |
40 | Module::guess_from_source(db, file_id, ModuleSource::SourceFile(file_id)) | 40 | let module_source = ModuleSource::new_file(db, file_id); |
41 | Module::guess_from_source(db, module_source) | ||
41 | } | 42 | } |
42 | 43 | ||
43 | /// Lookup `Module` by position in the source code. Note that this | 44 | /// Lookup `Module` by position in the source code. Note that this |
@@ -51,17 +52,16 @@ impl Module { | |||
51 | let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) | 52 | let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) |
52 | { | 53 | { |
53 | Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), | 54 | Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), |
54 | _ => ModuleSource::SourceFile(position.file_id), | 55 | _ => ModuleSource::new_file(db, position.file_id), |
55 | }; | 56 | }; |
56 | Module::guess_from_source(db, position.file_id, module_source) | 57 | Module::guess_from_source(db, module_source) |
57 | } | 58 | } |
58 | 59 | ||
59 | fn guess_from_source( | 60 | fn guess_from_source( |
60 | db: &impl HirDatabase, | 61 | db: &impl HirDatabase, |
61 | file_id: FileId, | ||
62 | module_source: ModuleSource, | 62 | module_source: ModuleSource, |
63 | ) -> Cancelable<Option<Module>> { | 63 | ) -> Cancelable<Option<Module>> { |
64 | let source_root_id = db.file_source_root(file_id); | 64 | let source_root_id = db.file_source_root(module_source.file_id()); |
65 | let module_tree = db.module_tree(source_root_id)?; | 65 | let module_tree = db.module_tree(source_root_id)?; |
66 | 66 | ||
67 | let res = match module_tree.any_module_for_source(module_source) { | 67 | let res = match module_tree.any_module_for_source(module_source) { |
@@ -209,10 +209,7 @@ impl ModuleTree { | |||
209 | /// `ModuleSource` is the syntax tree element that produced this module: | 209 | /// `ModuleSource` is the syntax tree element that produced this module: |
210 | /// either a file, or an inlinde module. | 210 | /// either a file, or an inlinde module. |
211 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] | 211 | #[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] |
212 | pub enum ModuleSource { | 212 | pub struct ModuleSource(SourceItemId); |
213 | SourceFile(FileId), | ||
214 | Module(SourceItemId), | ||
215 | } | ||
216 | 213 | ||
217 | /// An owned syntax node for a module. Unlike `ModuleSource`, | 214 | /// An owned syntax node for a module. Unlike `ModuleSource`, |
218 | /// this holds onto the AST for the whole file. | 215 | /// this holds onto the AST for the whole file. |
@@ -310,45 +307,41 @@ pub struct ModuleData { | |||
310 | } | 307 | } |
311 | 308 | ||
312 | impl ModuleSource { | 309 | impl ModuleSource { |
310 | // precondition: item_id **must** point to module | ||
311 | fn new(file_id: FileId, item_id: SourceFileItemId) -> ModuleSource { | ||
312 | let source_item_id = SourceItemId { file_id, item_id }; | ||
313 | ModuleSource(source_item_id) | ||
314 | } | ||
315 | |||
316 | pub(crate) fn new_file(db: &impl HirDatabase, file_id: FileId) -> ModuleSource { | ||
317 | let file_items = db.file_items(file_id); | ||
318 | let item_id = file_items.id_of_source_file(); | ||
319 | ModuleSource::new(file_id, item_id) | ||
320 | } | ||
321 | |||
313 | pub(crate) fn new_inline( | 322 | pub(crate) fn new_inline( |
314 | db: &impl HirDatabase, | 323 | db: &impl HirDatabase, |
315 | file_id: FileId, | 324 | file_id: FileId, |
316 | module: ast::Module, | 325 | m: ast::Module, |
317 | ) -> ModuleSource { | 326 | ) -> ModuleSource { |
318 | assert!(!module.has_semi()); | 327 | assert!(!m.has_semi()); |
319 | let items = db.file_items(file_id); | 328 | let file_items = db.file_items(file_id); |
320 | let item_id = items.id_of(module.syntax()); | 329 | let item_id = file_items.id_of(m.syntax()); |
321 | let id = SourceItemId { file_id, item_id }; | 330 | ModuleSource::new(file_id, item_id) |
322 | ModuleSource::Module(id) | ||
323 | } | ||
324 | |||
325 | pub fn as_file(self) -> Option<FileId> { | ||
326 | match self { | ||
327 | ModuleSource::SourceFile(f) => Some(f), | ||
328 | ModuleSource::Module(..) => None, | ||
329 | } | ||
330 | } | 331 | } |
331 | 332 | ||
332 | pub fn file_id(self) -> FileId { | 333 | pub fn file_id(self) -> FileId { |
333 | match self { | 334 | self.0.file_id |
334 | ModuleSource::SourceFile(f) => f, | ||
335 | ModuleSource::Module(source_item_id) => source_item_id.file_id, | ||
336 | } | ||
337 | } | 335 | } |
338 | 336 | ||
339 | pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { | 337 | pub(crate) fn resolve(self, db: &impl HirDatabase) -> ModuleSourceNode { |
340 | match self { | 338 | let syntax_node = db.file_item(self.0); |
341 | ModuleSource::SourceFile(file_id) => { | 339 | let syntax_node = syntax_node.borrowed(); |
342 | let syntax = db.source_file(file_id); | 340 | if let Some(file) = ast::SourceFile::cast(syntax_node) { |
343 | ModuleSourceNode::SourceFile(syntax.ast().owned()) | 341 | return ModuleSourceNode::SourceFile(file.owned()); |
344 | } | ||
345 | ModuleSource::Module(item_id) => { | ||
346 | let syntax = db.file_item(item_id); | ||
347 | let syntax = syntax.borrowed(); | ||
348 | let module = ast::Module::cast(syntax).unwrap(); | ||
349 | ModuleSourceNode::Module(module.owned()) | ||
350 | } | ||
351 | } | 342 | } |
343 | let module = ast::Module::cast(syntax_node).unwrap(); | ||
344 | ModuleSourceNode::Module(module.owned()) | ||
352 | } | 345 | } |
353 | } | 346 | } |
354 | 347 | ||