aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_api.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model_api.rs')
-rw-r--r--crates/ra_hir/src/code_model_api.rs115
1 files changed, 52 insertions, 63 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 5db53a34f..7ccd29e2f 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -33,10 +33,10 @@ impl Crate {
33 pub fn crate_id(&self) -> CrateId { 33 pub fn crate_id(&self) -> CrateId {
34 self.crate_id 34 self.crate_id
35 } 35 }
36 pub fn dependencies(&self, db: &impl HirDatabase) -> Cancelable<Vec<CrateDependency>> { 36 pub fn dependencies(&self, db: &impl HirDatabase) -> Vec<CrateDependency> {
37 Ok(self.dependencies_impl(db)) 37 self.dependencies_impl(db)
38 } 38 }
39 pub fn root_module(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { 39 pub fn root_module(&self, db: &impl HirDatabase) -> Option<Module> {
40 self.root_module_impl(db) 40 self.root_module_impl(db)
41 } 41 }
42} 42}
@@ -78,12 +78,12 @@ pub enum Problem {
78 78
79impl Module { 79impl Module {
80 /// Name of this module. 80 /// Name of this module.
81 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { 81 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
82 self.name_impl(db) 82 self.name_impl(db)
83 } 83 }
84 84
85 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items. 85 /// Returns a node which defines this module. That is, a file or a `mod foo {}` with items.
86 pub fn definition_source(&self, db: &impl HirDatabase) -> Cancelable<(FileId, ModuleSource)> { 86 pub fn definition_source(&self, db: &impl HirDatabase) -> (FileId, ModuleSource) {
87 self.definition_source_impl(db) 87 self.definition_source_impl(db)
88 } 88 }
89 89
@@ -92,19 +92,19 @@ impl Module {
92 pub fn declaration_source( 92 pub fn declaration_source(
93 &self, 93 &self,
94 db: &impl HirDatabase, 94 db: &impl HirDatabase,
95 ) -> Cancelable<Option<(FileId, TreeArc<ast::Module>)>> { 95 ) -> Option<(FileId, TreeArc<ast::Module>)> {
96 self.declaration_source_impl(db) 96 self.declaration_source_impl(db)
97 } 97 }
98 98
99 /// Returns the crate this module is part of. 99 /// Returns the crate this module is part of.
100 pub fn krate(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { 100 pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
101 self.krate_impl(db) 101 self.krate_impl(db)
102 } 102 }
103 103
104 /// Topmost parent of this module. Every module has a `crate_root`, but some 104 /// Topmost parent of this module. Every module has a `crate_root`, but some
105 /// might be missing `krate`. This can happen if a module's file is not included 105 /// might be missing `krate`. This can happen if a module's file is not included
106 /// in the module tree of any target in Cargo.toml. 106 /// in the module tree of any target in Cargo.toml.
107 pub fn crate_root(&self, db: &impl HirDatabase) -> Cancelable<Module> { 107 pub fn crate_root(&self, db: &impl HirDatabase) -> Module {
108 self.crate_root_impl(db) 108 self.crate_root_impl(db)
109 } 109 }
110 110
@@ -114,31 +114,31 @@ impl Module {
114 } 114 }
115 115
116 /// Iterates over all child modules. 116 /// Iterates over all child modules.
117 pub fn children(&self, db: &impl HirDatabase) -> Cancelable<impl Iterator<Item = Module>> { 117 pub fn children(&self, db: &impl HirDatabase) -> impl Iterator<Item = Module> {
118 self.children_impl(db) 118 self.children_impl(db)
119 } 119 }
120 120
121 /// Finds a parent module. 121 /// Finds a parent module.
122 pub fn parent(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { 122 pub fn parent(&self, db: &impl HirDatabase) -> Option<Module> {
123 self.parent_impl(db) 123 self.parent_impl(db)
124 } 124 }
125 125
126 pub fn path_to_root(&self, db: &impl HirDatabase) -> Cancelable<Vec<Module>> { 126 pub fn path_to_root(&self, db: &impl HirDatabase) -> Vec<Module> {
127 let mut res = vec![self.clone()]; 127 let mut res = vec![self.clone()];
128 let mut curr = self.clone(); 128 let mut curr = self.clone();
129 while let Some(next) = curr.parent(db)? { 129 while let Some(next) = curr.parent(db) {
130 res.push(next.clone()); 130 res.push(next.clone());
131 curr = next 131 curr = next
132 } 132 }
133 Ok(res) 133 res
134 } 134 }
135 135
136 /// Returns a `ModuleScope`: a set of items, visible in this module. 136 /// Returns a `ModuleScope`: a set of items, visible in this module.
137 pub fn scope(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> { 137 pub fn scope(&self, db: &impl HirDatabase) -> ModuleScope {
138 self.scope_impl(db) 138 self.scope_impl(db)
139 } 139 }
140 140
141 pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> Cancelable<PerNs<DefId>> { 141 pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> PerNs<DefId> {
142 self.resolve_path_impl(db, path) 142 self.resolve_path_impl(db, path)
143 } 143 }
144 144
@@ -175,13 +175,12 @@ impl Struct {
175 self.def_id 175 self.def_id
176 } 176 }
177 177
178 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { 178 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
179 Ok(db.struct_data(self.def_id)?.name.clone()) 179 db.struct_data(self.def_id).name.clone()
180 } 180 }
181 181
182 pub fn fields(&self, db: &impl HirDatabase) -> Cancelable<Vec<StructField>> { 182 pub fn fields(&self, db: &impl HirDatabase) -> Vec<StructField> {
183 let res = db 183 db.struct_data(self.def_id)
184 .struct_data(self.def_id)?
185 .variant_data 184 .variant_data
186 .fields() 185 .fields()
187 .iter() 186 .iter()
@@ -189,15 +188,11 @@ impl Struct {
189 struct_: self.clone(), 188 struct_: self.clone(),
190 name: it.name.clone(), 189 name: it.name.clone(),
191 }) 190 })
192 .collect(); 191 .collect()
193 Ok(res)
194 } 192 }
195 193
196 pub fn source( 194 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StructDef>) {
197 &self, 195 def_id_to_ast(db, self.def_id)
198 db: &impl HirDatabase,
199 ) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> {
200 Ok(def_id_to_ast(db, self.def_id))
201 } 196 }
202} 197}
203 198
@@ -215,16 +210,16 @@ impl Enum {
215 self.def_id 210 self.def_id
216 } 211 }
217 212
218 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { 213 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
219 Ok(db.enum_data(self.def_id)?.name.clone()) 214 db.enum_data(self.def_id).name.clone()
220 } 215 }
221 216
222 pub fn variants(&self, db: &impl HirDatabase) -> Cancelable<Vec<(Name, EnumVariant)>> { 217 pub fn variants(&self, db: &impl HirDatabase) -> Vec<(Name, EnumVariant)> {
223 Ok(db.enum_data(self.def_id)?.variants.clone()) 218 db.enum_data(self.def_id).variants.clone()
224 } 219 }
225 220
226 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> { 221 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumDef>) {
227 Ok(def_id_to_ast(db, self.def_id)) 222 def_id_to_ast(db, self.def_id)
228 } 223 }
229} 224}
230 225
@@ -242,23 +237,20 @@ impl EnumVariant {
242 self.def_id 237 self.def_id
243 } 238 }
244 239
245 pub fn parent_enum(&self, db: &impl HirDatabase) -> Cancelable<Enum> { 240 pub fn parent_enum(&self, db: &impl HirDatabase) -> Enum {
246 Ok(db.enum_variant_data(self.def_id)?.parent_enum.clone()) 241 db.enum_variant_data(self.def_id).parent_enum.clone()
247 } 242 }
248 243
249 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { 244 pub fn name(&self, db: &impl HirDatabase) -> Option<Name> {
250 Ok(db.enum_variant_data(self.def_id)?.name.clone()) 245 db.enum_variant_data(self.def_id).name.clone()
251 } 246 }
252 247
253 pub fn variant_data(&self, db: &impl HirDatabase) -> Cancelable<Arc<VariantData>> { 248 pub fn variant_data(&self, db: &impl HirDatabase) -> Arc<VariantData> {
254 Ok(db.enum_variant_data(self.def_id)?.variant_data.clone()) 249 db.enum_variant_data(self.def_id).variant_data.clone()
255 } 250 }
256 251
257 pub fn source( 252 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::EnumVariant>) {
258 &self, 253 def_id_to_ast(db, self.def_id)
259 db: &impl HirDatabase,
260 ) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> {
261 Ok(def_id_to_ast(db, self.def_id))
262 } 254 }
263} 255}
264 256
@@ -305,21 +297,21 @@ impl Function {
305 self.def_id 297 self.def_id
306 } 298 }
307 299
308 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> { 300 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::FnDef>) {
309 Ok(def_id_to_ast(db, self.def_id)) 301 def_id_to_ast(db, self.def_id)
310 } 302 }
311 303
312 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> { 304 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Arc<BodySyntaxMapping> {
313 db.body_syntax_mapping(self.def_id) 305 db.body_syntax_mapping(self.def_id)
314 } 306 }
315 307
316 pub fn scopes(&self, db: &impl HirDatabase) -> Cancelable<ScopesWithSyntaxMapping> { 308 pub fn scopes(&self, db: &impl HirDatabase) -> ScopesWithSyntaxMapping {
317 let scopes = db.fn_scopes(self.def_id)?; 309 let scopes = db.fn_scopes(self.def_id);
318 let syntax_mapping = db.body_syntax_mapping(self.def_id)?; 310 let syntax_mapping = db.body_syntax_mapping(self.def_id);
319 Ok(ScopesWithSyntaxMapping { 311 ScopesWithSyntaxMapping {
320 scopes, 312 scopes,
321 syntax_mapping, 313 syntax_mapping,
322 }) 314 }
323 } 315 }
324 316
325 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> { 317 pub fn signature(&self, db: &impl HirDatabase) -> Arc<FnSignature> {
@@ -341,8 +333,8 @@ impl Const {
341 Const { def_id } 333 Const { def_id }
342 } 334 }
343 335
344 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::ConstDef>)> { 336 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::ConstDef>) {
345 Ok(def_id_to_ast(db, self.def_id)) 337 def_id_to_ast(db, self.def_id)
346 } 338 }
347} 339}
348 340
@@ -356,11 +348,8 @@ impl Static {
356 Static { def_id } 348 Static { def_id }
357 } 349 }
358 350
359 pub fn source( 351 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::StaticDef>) {
360 &self, 352 def_id_to_ast(db, self.def_id)
361 db: &impl HirDatabase,
362 ) -> Cancelable<(HirFileId, TreeArc<ast::StaticDef>)> {
363 Ok(def_id_to_ast(db, self.def_id))
364 } 353 }
365} 354}
366 355
@@ -374,8 +363,8 @@ impl Trait {
374 Trait { def_id } 363 Trait { def_id }
375 } 364 }
376 365
377 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::TraitDef>)> { 366 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::TraitDef>) {
378 Ok(def_id_to_ast(db, self.def_id)) 367 def_id_to_ast(db, self.def_id)
379 } 368 }
380} 369}
381 370
@@ -389,7 +378,7 @@ impl Type {
389 Type { def_id } 378 Type { def_id }
390 } 379 }
391 380
392 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::TypeDef>)> { 381 pub fn source(&self, db: &impl HirDatabase) -> (HirFileId, TreeArc<ast::TypeDef>) {
393 Ok(def_id_to_ast(db, self.def_id)) 382 def_id_to_ast(db, self.def_id)
394 } 383 }
395} 384}