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.rs97
1 files changed, 74 insertions, 23 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index 098c7f40b..d4244f70c 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -2,7 +2,7 @@ use std::sync::Arc;
2 2
3use relative_path::RelativePathBuf; 3use relative_path::RelativePathBuf;
4use ra_db::{CrateId, Cancelable, FileId}; 4use ra_db::{CrateId, Cancelable, FileId};
5use ra_syntax::{ast, TreeArc, SyntaxNode, AstNode}; 5use ra_syntax::{ast, TreeArc, SyntaxNode};
6 6
7use crate::{ 7use crate::{
8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId, 8 Name, DefId, Path, PerNs, ScopesWithSyntaxMapping, Ty, HirFileId,
@@ -12,6 +12,7 @@ use crate::{
12 expr::BodySyntaxMapping, 12 expr::BodySyntaxMapping,
13 ty::InferenceResult, 13 ty::InferenceResult,
14 adt::VariantData, 14 adt::VariantData,
15 code_model_impl::def_id_to_ast,
15}; 16};
16 17
17/// hir::Crate describes a single crate. It's the main interface with which 18/// hir::Crate describes a single crate. It's the main interface with which
@@ -40,12 +41,17 @@ impl Crate {
40 } 41 }
41} 42}
42 43
44#[derive(Debug)]
43pub enum Def { 45pub enum Def {
44 Module(Module), 46 Module(Module),
45 Struct(Struct), 47 Struct(Struct),
46 Enum(Enum), 48 Enum(Enum),
47 EnumVariant(EnumVariant), 49 EnumVariant(EnumVariant),
48 Function(Function), 50 Function(Function),
51 Const(Const),
52 Static(Static),
53 Trait(Trait),
54 Type(Type),
49 Item, 55 Item,
50} 56}
51 57
@@ -186,13 +192,7 @@ impl Struct {
186 &self, 192 &self,
187 db: &impl HirDatabase, 193 db: &impl HirDatabase,
188 ) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> { 194 ) -> Cancelable<(HirFileId, TreeArc<ast::StructDef>)> {
189 let (file_id, syntax) = self.def_id.source(db); 195 Ok(def_id_to_ast(db, self.def_id))
190 Ok((
191 file_id,
192 ast::StructDef::cast(&syntax)
193 .expect("struct def should point to StructDef node")
194 .to_owned(),
195 ))
196 } 196 }
197} 197}
198 198
@@ -219,13 +219,7 @@ impl Enum {
219 } 219 }
220 220
221 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> { 221 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::EnumDef>)> {
222 let (file_id, syntax) = self.def_id.source(db); 222 Ok(def_id_to_ast(db, self.def_id))
223 Ok((
224 file_id,
225 ast::EnumDef::cast(&syntax)
226 .expect("enum def should point to EnumDef node")
227 .to_owned(),
228 ))
229 } 223 }
230} 224}
231 225
@@ -259,13 +253,7 @@ impl EnumVariant {
259 &self, 253 &self,
260 db: &impl HirDatabase, 254 db: &impl HirDatabase,
261 ) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> { 255 ) -> Cancelable<(HirFileId, TreeArc<ast::EnumVariant>)> {
262 let (file_id, syntax) = self.def_id.source(db); 256 Ok(def_id_to_ast(db, self.def_id))
263 Ok((
264 file_id,
265 ast::EnumVariant::cast(&syntax)
266 .expect("variant def should point to EnumVariant node")
267 .to_owned(),
268 ))
269 } 257 }
270} 258}
271 259
@@ -304,7 +292,7 @@ impl Function {
304 } 292 }
305 293
306 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> { 294 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::FnDef>)> {
307 Ok(self.source_impl(db)) 295 Ok(def_id_to_ast(db, self.def_id))
308 } 296 }
309 297
310 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> { 298 pub fn body_syntax_mapping(&self, db: &impl HirDatabase) -> Cancelable<Arc<BodySyntaxMapping>> {
@@ -328,3 +316,66 @@ impl Function {
328 db.infer(self.def_id) 316 db.infer(self.def_id)
329 } 317 }
330} 318}
319
320#[derive(Debug, Clone, PartialEq, Eq, Hash)]
321pub struct Const {
322 pub(crate) def_id: DefId,
323}
324
325impl Const {
326 pub(crate) fn new(def_id: DefId) -> Const {
327 Const { def_id }
328 }
329
330 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::ConstDef>)> {
331 Ok(def_id_to_ast(db, self.def_id))
332 }
333}
334
335#[derive(Debug, Clone, PartialEq, Eq, Hash)]
336pub struct Static {
337 pub(crate) def_id: DefId,
338}
339
340impl Static {
341 pub(crate) fn new(def_id: DefId) -> Static {
342 Static { def_id }
343 }
344
345 pub fn source(
346 &self,
347 db: &impl HirDatabase,
348 ) -> Cancelable<(HirFileId, TreeArc<ast::StaticDef>)> {
349 Ok(def_id_to_ast(db, self.def_id))
350 }
351}
352
353#[derive(Debug, Clone, PartialEq, Eq, Hash)]
354pub struct Trait {
355 pub(crate) def_id: DefId,
356}
357
358impl Trait {
359 pub(crate) fn new(def_id: DefId) -> Trait {
360 Trait { def_id }
361 }
362
363 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::TraitDef>)> {
364 Ok(def_id_to_ast(db, self.def_id))
365 }
366}
367
368#[derive(Debug, Clone, PartialEq, Eq, Hash)]
369pub struct Type {
370 pub(crate) def_id: DefId,
371}
372
373impl Type {
374 pub(crate) fn new(def_id: DefId) -> Type {
375 Type { def_id }
376 }
377
378 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<(HirFileId, TreeArc<ast::TypeDef>)> {
379 Ok(def_id_to_ast(db, self.def_id))
380 }
381}