diff options
author | Aleksey Kladov <[email protected]> | 2019-01-06 14:33:27 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-06 14:36:10 +0000 |
commit | fd4456d0ec88e3433a7a8be6f27d8af9afedefe5 (patch) | |
tree | 37f1225b085e22756cbfd65875595c60205cc93a /crates/ra_hir/src/code_model_impl | |
parent | 5a505189a8f7ed274893a45aed0d0249083d1277 (diff) |
flatten module structure
Diffstat (limited to 'crates/ra_hir/src/code_model_impl')
-rw-r--r-- | crates/ra_hir/src/code_model_impl/krate.rs | 45 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model_impl/module.rs | 154 |
2 files changed, 199 insertions, 0 deletions
diff --git a/crates/ra_hir/src/code_model_impl/krate.rs b/crates/ra_hir/src/code_model_impl/krate.rs new file mode 100644 index 000000000..591a81597 --- /dev/null +++ b/crates/ra_hir/src/code_model_impl/krate.rs | |||
@@ -0,0 +1,45 @@ | |||
1 | use ra_db::{CrateId, Cancelable}; | ||
2 | |||
3 | use crate::{ | ||
4 | HirFileId, Crate, CrateDependency, AsName, DefLoc, DefKind, Module, | ||
5 | db::HirDatabase, | ||
6 | }; | ||
7 | |||
8 | impl Crate { | ||
9 | pub(crate) fn new(crate_id: CrateId) -> Crate { | ||
10 | Crate { crate_id } | ||
11 | } | ||
12 | pub(crate) fn dependencies_impl(&self, db: &impl HirDatabase) -> Vec<CrateDependency> { | ||
13 | let crate_graph = db.crate_graph(); | ||
14 | crate_graph | ||
15 | .dependencies(self.crate_id) | ||
16 | .map(|dep| { | ||
17 | let krate = Crate::new(dep.crate_id()); | ||
18 | let name = dep.as_name(); | ||
19 | CrateDependency { krate, name } | ||
20 | }) | ||
21 | .collect() | ||
22 | } | ||
23 | pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { | ||
24 | let crate_graph = db.crate_graph(); | ||
25 | let file_id = crate_graph.crate_root(self.crate_id); | ||
26 | let source_root_id = db.file_source_root(file_id); | ||
27 | let file_id = HirFileId::from(file_id); | ||
28 | let module_tree = db.module_tree(source_root_id)?; | ||
29 | // FIXME: teach module tree about crate roots instead of guessing | ||
30 | let (module_id, _) = ctry!(module_tree | ||
31 | .modules_with_sources() | ||
32 | .find(|(_, src)| src.file_id() == file_id)); | ||
33 | |||
34 | let def_loc = DefLoc { | ||
35 | kind: DefKind::Module, | ||
36 | source_root_id, | ||
37 | module_id, | ||
38 | source_item_id: module_id.source(&module_tree).0, | ||
39 | }; | ||
40 | let def_id = def_loc.id(db); | ||
41 | |||
42 | let module = Module::new(def_id); | ||
43 | Ok(Some(module)) | ||
44 | } | ||
45 | } | ||
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs new file mode 100644 index 000000000..02078f188 --- /dev/null +++ b/crates/ra_hir/src/code_model_impl/module.rs | |||
@@ -0,0 +1,154 @@ | |||
1 | use ra_db::{Cancelable, SourceRootId, FileId}; | ||
2 | use ra_syntax::{ast, SyntaxNode, AstNode}; | ||
3 | |||
4 | use crate::{ | ||
5 | Module, ModuleSource, Problem, | ||
6 | Crate, DefId, DefLoc, DefKind, Name, Path, PathKind, PerNs, Def, ModuleId, | ||
7 | nameres::ModuleScope, | ||
8 | db::HirDatabase, | ||
9 | }; | ||
10 | |||
11 | impl Module { | ||
12 | pub(crate) fn new(def_id: DefId) -> Self { | ||
13 | crate::code_model_api::Module { def_id } | ||
14 | } | ||
15 | pub(crate) fn from_module_id( | ||
16 | db: &impl HirDatabase, | ||
17 | source_root_id: SourceRootId, | ||
18 | module_id: ModuleId, | ||
19 | ) -> Cancelable<Self> { | ||
20 | let module_tree = db.module_tree(source_root_id)?; | ||
21 | let def_loc = DefLoc { | ||
22 | kind: DefKind::Module, | ||
23 | source_root_id, | ||
24 | module_id, | ||
25 | source_item_id: module_id.source(&module_tree).0, | ||
26 | }; | ||
27 | let def_id = def_loc.id(db); | ||
28 | let module = Module::new(def_id); | ||
29 | Ok(module) | ||
30 | } | ||
31 | |||
32 | pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { | ||
33 | let loc = self.def_id.loc(db); | ||
34 | let module_tree = db.module_tree(loc.source_root_id)?; | ||
35 | let link = ctry!(loc.module_id.parent_link(&module_tree)); | ||
36 | Ok(Some(link.name(&module_tree).clone())) | ||
37 | } | ||
38 | |||
39 | pub fn defenition_source_impl( | ||
40 | &self, | ||
41 | db: &impl HirDatabase, | ||
42 | ) -> Cancelable<(FileId, ModuleSource)> { | ||
43 | let loc = self.def_id.loc(db); | ||
44 | let file_id = loc.source_item_id.file_id.as_original_file(); | ||
45 | let syntax_node = db.file_item(loc.source_item_id); | ||
46 | let syntax_node = syntax_node.borrowed(); | ||
47 | let module_source = if let Some(source_file) = ast::SourceFile::cast(syntax_node) { | ||
48 | ModuleSource::SourceFile(source_file.owned()) | ||
49 | } else { | ||
50 | let module = ast::Module::cast(syntax_node).unwrap(); | ||
51 | ModuleSource::Module(module.owned()) | ||
52 | }; | ||
53 | Ok((file_id, module_source)) | ||
54 | } | ||
55 | |||
56 | pub fn declaration_source_impl( | ||
57 | &self, | ||
58 | db: &impl HirDatabase, | ||
59 | ) -> Cancelable<Option<(FileId, ast::ModuleNode)>> { | ||
60 | let loc = self.def_id.loc(db); | ||
61 | let module_tree = db.module_tree(loc.source_root_id)?; | ||
62 | let link = ctry!(loc.module_id.parent_link(&module_tree)); | ||
63 | let file_id = link | ||
64 | .owner(&module_tree) | ||
65 | .source(&module_tree) | ||
66 | .file_id() | ||
67 | .as_original_file(); | ||
68 | let src = link.bind_source(&module_tree, db); | ||
69 | Ok(Some((file_id, src))) | ||
70 | } | ||
71 | |||
72 | pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { | ||
73 | let root = self.crate_root(db)?; | ||
74 | let loc = root.def_id.loc(db); | ||
75 | let file_id = loc.source_item_id.file_id.as_original_file(); | ||
76 | |||
77 | let crate_graph = db.crate_graph(); | ||
78 | let crate_id = ctry!(crate_graph.crate_id_for_crate_root(file_id)); | ||
79 | Ok(Some(Crate::new(crate_id))) | ||
80 | } | ||
81 | |||
82 | pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable<Module> { | ||
83 | let loc = self.def_id.loc(db); | ||
84 | let module_tree = db.module_tree(loc.source_root_id)?; | ||
85 | let module_id = loc.module_id.crate_root(&module_tree); | ||
86 | Module::from_module_id(db, loc.source_root_id, module_id) | ||
87 | } | ||
88 | /// Finds a child module with the specified name. | ||
89 | pub fn child_impl(&self, db: &impl HirDatabase, name: &Name) -> Cancelable<Option<Module>> { | ||
90 | let loc = self.def_id.loc(db); | ||
91 | let module_tree = db.module_tree(loc.source_root_id)?; | ||
92 | let child_id = ctry!(loc.module_id.child(&module_tree, name)); | ||
93 | Module::from_module_id(db, loc.source_root_id, child_id).map(Some) | ||
94 | } | ||
95 | pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { | ||
96 | let loc = self.def_id.loc(db); | ||
97 | let module_tree = db.module_tree(loc.source_root_id)?; | ||
98 | let parent_id = ctry!(loc.module_id.parent(&module_tree)); | ||
99 | Module::from_module_id(db, loc.source_root_id, parent_id).map(Some) | ||
100 | } | ||
101 | /// Returns a `ModuleScope`: a set of items, visible in this module. | ||
102 | pub fn scope_impl(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> { | ||
103 | let loc = self.def_id.loc(db); | ||
104 | let item_map = db.item_map(loc.source_root_id)?; | ||
105 | let res = item_map.per_module[&loc.module_id].clone(); | ||
106 | Ok(res) | ||
107 | } | ||
108 | pub fn resolve_path_impl( | ||
109 | &self, | ||
110 | db: &impl HirDatabase, | ||
111 | path: &Path, | ||
112 | ) -> Cancelable<PerNs<DefId>> { | ||
113 | let mut curr_per_ns = PerNs::types( | ||
114 | match path.kind { | ||
115 | PathKind::Crate => self.crate_root(db)?, | ||
116 | PathKind::Self_ | PathKind::Plain => self.clone(), | ||
117 | PathKind::Super => { | ||
118 | if let Some(p) = self.parent(db)? { | ||
119 | p | ||
120 | } else { | ||
121 | return Ok(PerNs::none()); | ||
122 | } | ||
123 | } | ||
124 | } | ||
125 | .def_id, | ||
126 | ); | ||
127 | |||
128 | let segments = &path.segments; | ||
129 | for name in segments.iter() { | ||
130 | let curr = if let Some(r) = curr_per_ns.as_ref().take_types() { | ||
131 | r | ||
132 | } else { | ||
133 | return Ok(PerNs::none()); | ||
134 | }; | ||
135 | let module = match curr.resolve(db)? { | ||
136 | Def::Module(it) => it, | ||
137 | // TODO here would be the place to handle enum variants... | ||
138 | _ => return Ok(PerNs::none()), | ||
139 | }; | ||
140 | let scope = module.scope(db)?; | ||
141 | curr_per_ns = if let Some(r) = scope.get(&name) { | ||
142 | r.def_id | ||
143 | } else { | ||
144 | return Ok(PerNs::none()); | ||
145 | }; | ||
146 | } | ||
147 | Ok(curr_per_ns) | ||
148 | } | ||
149 | pub fn problems_impl(&self, db: &impl HirDatabase) -> Cancelable<Vec<(SyntaxNode, Problem)>> { | ||
150 | let loc = self.def_id.loc(db); | ||
151 | let module_tree = db.module_tree(loc.source_root_id)?; | ||
152 | Ok(loc.module_id.problems(&module_tree, db)) | ||
153 | } | ||
154 | } | ||