diff options
Diffstat (limited to 'crates/ra_hir/src/module/mod.rs')
-rw-r--r-- | crates/ra_hir/src/module/mod.rs | 71 |
1 files changed, 4 insertions, 67 deletions
diff --git a/crates/ra_hir/src/module/mod.rs b/crates/ra_hir/src/module/mod.rs index 11e6e8e75..9ab7dbff5 100644 --- a/crates/ra_hir/src/module/mod.rs +++ b/crates/ra_hir/src/module/mod.rs | |||
@@ -3,14 +3,12 @@ pub(super) mod nameres; | |||
3 | 3 | ||
4 | use std::sync::Arc; | 4 | use std::sync::Arc; |
5 | 5 | ||
6 | use ra_editor::find_node_at_offset; | ||
7 | |||
8 | use ra_syntax::{ | 6 | use ra_syntax::{ |
9 | algo::generate, | 7 | algo::generate, |
10 | ast::{self, AstNode, NameOwner}, | 8 | ast::{self, AstNode, NameOwner}, |
11 | SmolStr, SyntaxNode, SyntaxNodeRef, | 9 | SmolStr, SyntaxNode, |
12 | }; | 10 | }; |
13 | use ra_db::{SourceRootId, FileId, FilePosition, Cancelable}; | 11 | use ra_db::{SourceRootId, FileId, Cancelable}; |
14 | use relative_path::RelativePathBuf; | 12 | use relative_path::RelativePathBuf; |
15 | 13 | ||
16 | use crate::{ | 14 | use crate::{ |
@@ -30,68 +28,6 @@ pub struct Module { | |||
30 | } | 28 | } |
31 | 29 | ||
32 | impl Module { | 30 | impl Module { |
33 | /// Lookup `Module` by `FileId`. Note that this is inherently | ||
34 | /// lossy transformation: in general, a single source might correspond to | ||
35 | /// several modules. | ||
36 | pub fn guess_from_file_id( | ||
37 | db: &impl HirDatabase, | ||
38 | file_id: FileId, | ||
39 | ) -> Cancelable<Option<Module>> { | ||
40 | let module_source = ModuleSource::new_file(db, file_id); | ||
41 | Module::guess_from_source(db, module_source) | ||
42 | } | ||
43 | |||
44 | /// Lookup `Module` by position in the source code. Note that this | ||
45 | /// is inherently lossy transformation: in general, a single source might | ||
46 | /// correspond to several modules. | ||
47 | pub fn guess_from_position( | ||
48 | db: &impl HirDatabase, | ||
49 | position: FilePosition, | ||
50 | ) -> Cancelable<Option<Module>> { | ||
51 | let file = db.source_file(position.file_id); | ||
52 | let module_source = match find_node_at_offset::<ast::Module>(file.syntax(), position.offset) | ||
53 | { | ||
54 | Some(m) if !m.has_semi() => ModuleSource::new_inline(db, position.file_id, m), | ||
55 | _ => ModuleSource::new_file(db, position.file_id), | ||
56 | }; | ||
57 | Module::guess_from_source(db, module_source) | ||
58 | } | ||
59 | |||
60 | pub fn guess_from_child_node( | ||
61 | db: &impl HirDatabase, | ||
62 | file_id: FileId, | ||
63 | node: SyntaxNodeRef, | ||
64 | ) -> Cancelable<Option<Module>> { | ||
65 | let module_source = if let Some(m) = node | ||
66 | .ancestors() | ||
67 | .filter_map(ast::Module::cast) | ||
68 | .find(|it| !it.has_semi()) | ||
69 | { | ||
70 | ModuleSource::new_inline(db, file_id, m) | ||
71 | } else { | ||
72 | ModuleSource::new_file(db, file_id) | ||
73 | }; | ||
74 | Module::guess_from_source(db, module_source) | ||
75 | } | ||
76 | |||
77 | fn guess_from_source( | ||
78 | db: &impl HirDatabase, | ||
79 | module_source: ModuleSource, | ||
80 | ) -> Cancelable<Option<Module>> { | ||
81 | let source_root_id = db.file_source_root(module_source.file_id()); | ||
82 | let module_tree = db.module_tree(source_root_id)?; | ||
83 | |||
84 | let res = match module_tree.any_module_for_source(module_source) { | ||
85 | None => None, | ||
86 | Some(module_id) => Some(Module { | ||
87 | tree: module_tree, | ||
88 | source_root_id, | ||
89 | module_id, | ||
90 | }), | ||
91 | }; | ||
92 | Ok(res) | ||
93 | } | ||
94 | |||
95 | pub(super) fn new( | 31 | pub(super) fn new( |
96 | db: &impl HirDatabase, | 32 | db: &impl HirDatabase, |
97 | source_root_id: SourceRootId, | 33 | source_root_id: SourceRootId, |
@@ -225,7 +161,8 @@ impl ModuleTree { | |||
225 | .collect() | 161 | .collect() |
226 | } | 162 | } |
227 | 163 | ||
228 | fn any_module_for_source(&self, source: ModuleSource) -> Option<ModuleId> { | 164 | //TODO: move to source binders? |
165 | pub(crate) fn any_module_for_source(&self, source: ModuleSource) -> Option<ModuleId> { | ||
229 | self.modules_for_source(source).pop() | 166 | self.modules_for_source(source).pop() |
230 | } | 167 | } |
231 | } | 168 | } |