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