aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir/src/code_model_impl
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_hir/src/code_model_impl')
-rw-r--r--crates/ra_hir/src/code_model_impl/krate.rs8
-rw-r--r--crates/ra_hir/src/code_model_impl/module.rs49
2 files changed, 25 insertions, 32 deletions
diff --git a/crates/ra_hir/src/code_model_impl/krate.rs b/crates/ra_hir/src/code_model_impl/krate.rs
index 712c6c86a..8c6e34873 100644
--- a/crates/ra_hir/src/code_model_impl/krate.rs
+++ b/crates/ra_hir/src/code_model_impl/krate.rs
@@ -1,4 +1,4 @@
1use ra_db::{CrateId, Cancelable}; 1use ra_db::CrateId;
2 2
3use crate::{ 3use crate::{
4 HirFileId, Crate, CrateDependency, AsName, DefLoc, DefKind, Module, SourceItemId, 4 HirFileId, Crate, CrateDependency, AsName, DefLoc, DefKind, Module, SourceItemId,
@@ -20,7 +20,7 @@ impl Crate {
20 }) 20 })
21 .collect() 21 .collect()
22 } 22 }
23 pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { 23 pub(crate) fn root_module_impl(&self, db: &impl HirDatabase) -> Option<Module> {
24 let crate_graph = db.crate_graph(); 24 let crate_graph = db.crate_graph();
25 let file_id = crate_graph.crate_root(self.crate_id); 25 let file_id = crate_graph.crate_root(self.crate_id);
26 let source_root_id = db.file_source_root(file_id); 26 let source_root_id = db.file_source_root(file_id);
@@ -31,7 +31,7 @@ impl Crate {
31 file_id, 31 file_id,
32 item_id: None, 32 item_id: None,
33 }; 33 };
34 let module_id = ctry!(module_tree.find_module_by_source(source)); 34 let module_id = module_tree.find_module_by_source(source)?;
35 35
36 let def_loc = DefLoc { 36 let def_loc = DefLoc {
37 kind: DefKind::Module, 37 kind: DefKind::Module,
@@ -42,6 +42,6 @@ impl Crate {
42 let def_id = def_loc.id(db); 42 let def_id = def_loc.id(db);
43 43
44 let module = Module::new(def_id); 44 let module = Module::new(def_id);
45 Ok(Some(module)) 45 Some(module)
46 } 46 }
47} 47}
diff --git a/crates/ra_hir/src/code_model_impl/module.rs b/crates/ra_hir/src/code_model_impl/module.rs
index 67808d282..8668d6c8a 100644
--- a/crates/ra_hir/src/code_model_impl/module.rs
+++ b/crates/ra_hir/src/code_model_impl/module.rs
@@ -30,17 +30,14 @@ impl Module {
30 Module::new(def_id) 30 Module::new(def_id)
31 } 31 }
32 32
33 pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> { 33 pub(crate) fn name_impl(&self, db: &impl HirDatabase) -> Option<Name> {
34 let loc = self.def_id.loc(db); 34 let loc = self.def_id.loc(db);
35 let module_tree = db.module_tree(loc.source_root_id); 35 let module_tree = db.module_tree(loc.source_root_id);
36 let link = ctry!(loc.module_id.parent_link(&module_tree)); 36 let link = loc.module_id.parent_link(&module_tree)?;
37 Ok(Some(link.name(&module_tree).clone())) 37 Some(link.name(&module_tree).clone())
38 } 38 }
39 39
40 pub fn definition_source_impl( 40 pub fn definition_source_impl(&self, db: &impl HirDatabase) -> (FileId, ModuleSource) {
41 &self,
42 db: &impl HirDatabase,
43 ) -> Cancelable<(FileId, ModuleSource)> {
44 let loc = self.def_id.loc(db); 41 let loc = self.def_id.loc(db);
45 let file_id = loc.source_item_id.file_id.as_original_file(); 42 let file_id = loc.source_item_id.file_id.as_original_file();
46 let syntax_node = db.file_item(loc.source_item_id); 43 let syntax_node = db.file_item(loc.source_item_id);
@@ -50,40 +47,40 @@ impl Module {
50 let module = ast::Module::cast(&syntax_node).unwrap(); 47 let module = ast::Module::cast(&syntax_node).unwrap();
51 ModuleSource::Module(module.to_owned()) 48 ModuleSource::Module(module.to_owned())
52 }; 49 };
53 Ok((file_id, module_source)) 50 (file_id, module_source)
54 } 51 }
55 52
56 pub fn declaration_source_impl( 53 pub fn declaration_source_impl(
57 &self, 54 &self,
58 db: &impl HirDatabase, 55 db: &impl HirDatabase,
59 ) -> Cancelable<Option<(FileId, TreeArc<ast::Module>)>> { 56 ) -> Option<(FileId, TreeArc<ast::Module>)> {
60 let loc = self.def_id.loc(db); 57 let loc = self.def_id.loc(db);
61 let module_tree = db.module_tree(loc.source_root_id); 58 let module_tree = db.module_tree(loc.source_root_id);
62 let link = ctry!(loc.module_id.parent_link(&module_tree)); 59 let link = loc.module_id.parent_link(&module_tree)?;
63 let file_id = link 60 let file_id = link
64 .owner(&module_tree) 61 .owner(&module_tree)
65 .source(&module_tree) 62 .source(&module_tree)
66 .file_id 63 .file_id
67 .as_original_file(); 64 .as_original_file();
68 let src = link.source(&module_tree, db); 65 let src = link.source(&module_tree, db);
69 Ok(Some((file_id, src))) 66 Some((file_id, src))
70 } 67 }
71 68
72 pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Crate>> { 69 pub(crate) fn krate_impl(&self, db: &impl HirDatabase) -> Option<Crate> {
73 let root = self.crate_root(db)?; 70 let root = self.crate_root(db);
74 let loc = root.def_id.loc(db); 71 let loc = root.def_id.loc(db);
75 let file_id = loc.source_item_id.file_id.as_original_file(); 72 let file_id = loc.source_item_id.file_id.as_original_file();
76 73
77 let crate_graph = db.crate_graph(); 74 let crate_graph = db.crate_graph();
78 let crate_id = ctry!(crate_graph.crate_id_for_crate_root(file_id)); 75 let crate_id = crate_graph.crate_id_for_crate_root(file_id)?;
79 Ok(Some(Crate::new(crate_id))) 76 Some(Crate::new(crate_id))
80 } 77 }
81 78
82 pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Cancelable<Module> { 79 pub(crate) fn crate_root_impl(&self, db: &impl HirDatabase) -> Module {
83 let loc = self.def_id.loc(db); 80 let loc = self.def_id.loc(db);
84 let module_tree = db.module_tree(loc.source_root_id); 81 let module_tree = db.module_tree(loc.source_root_id);
85 let module_id = loc.module_id.crate_root(&module_tree); 82 let module_id = loc.module_id.crate_root(&module_tree);
86 Ok(Module::from_module_id(db, loc.source_root_id, module_id)) 83 Module::from_module_id(db, loc.source_root_id, module_id)
87 } 84 }
88 85
89 /// Finds a child module with the specified name. 86 /// Finds a child module with the specified name.
@@ -95,7 +92,7 @@ impl Module {
95 } 92 }
96 93
97 /// Iterates over all child modules. 94 /// Iterates over all child modules.
98 pub fn children_impl(&self, db: &impl HirDatabase) -> Cancelable<impl Iterator<Item = Module>> { 95 pub fn children_impl(&self, db: &impl HirDatabase) -> impl Iterator<Item = Module> {
99 // FIXME this should be implementable without collecting into a vec, but 96 // FIXME this should be implementable without collecting into a vec, but
100 // it's kind of hard since the iterator needs to keep a reference to the 97 // it's kind of hard since the iterator needs to keep a reference to the
101 // module tree. 98 // module tree.
@@ -106,18 +103,14 @@ impl Module {
106 .children(&module_tree) 103 .children(&module_tree)
107 .map(|(_, module_id)| Module::from_module_id(db, loc.source_root_id, module_id)) 104 .map(|(_, module_id)| Module::from_module_id(db, loc.source_root_id, module_id))
108 .collect::<Vec<_>>(); 105 .collect::<Vec<_>>();
109 Ok(children.into_iter()) 106 children.into_iter()
110 } 107 }
111 108
112 pub fn parent_impl(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { 109 pub fn parent_impl(&self, db: &impl HirDatabase) -> Option<Module> {
113 let loc = self.def_id.loc(db); 110 let loc = self.def_id.loc(db);
114 let module_tree = db.module_tree(loc.source_root_id); 111 let module_tree = db.module_tree(loc.source_root_id);
115 let parent_id = ctry!(loc.module_id.parent(&module_tree)); 112 let parent_id = loc.module_id.parent(&module_tree)?;
116 Ok(Some(Module::from_module_id( 113 Some(Module::from_module_id(db, loc.source_root_id, parent_id))
117 db,
118 loc.source_root_id,
119 parent_id,
120 )))
121 } 114 }
122 115
123 /// Returns a `ModuleScope`: a set of items, visible in this module. 116 /// Returns a `ModuleScope`: a set of items, visible in this module.
@@ -135,10 +128,10 @@ impl Module {
135 ) -> Cancelable<PerNs<DefId>> { 128 ) -> Cancelable<PerNs<DefId>> {
136 let mut curr_per_ns = PerNs::types( 129 let mut curr_per_ns = PerNs::types(
137 match path.kind { 130 match path.kind {
138 PathKind::Crate => self.crate_root(db)?, 131 PathKind::Crate => self.crate_root(db),
139 PathKind::Self_ | PathKind::Plain => self.clone(), 132 PathKind::Self_ | PathKind::Plain => self.clone(),
140 PathKind::Super => { 133 PathKind::Super => {
141 if let Some(p) = self.parent(db)? { 134 if let Some(p) = self.parent(db) {
142 p 135 p
143 } else { 136 } else {
144 return Ok(PerNs::none()); 137 return Ok(PerNs::none());