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.rs40
1 files changed, 35 insertions, 5 deletions
diff --git a/crates/ra_hir/src/code_model_api.rs b/crates/ra_hir/src/code_model_api.rs
index eca7d0225..0c7f743d4 100644
--- a/crates/ra_hir/src/code_model_api.rs
+++ b/crates/ra_hir/src/code_model_api.rs
@@ -1,6 +1,7 @@
1use ra_db::{CrateId, Cancelable}; 1use ra_db::{CrateId, Cancelable, FileId};
2use ra_syntax::{ast, SyntaxNode};
2 3
3use crate::{Name, db::HirDatabase, DefId, Path, PerNs, module::{ModuleSource, ModuleScope}}; 4use crate::{Name, db::HirDatabase, DefId, Path, PerNs, module::{Problem, ModuleScope}};
4 5
5/// hir::Crate describes a single crate. It's the main inteface with which 6/// hir::Crate describes a single crate. It's the main inteface with which
6/// crate's dependencies interact. Mostly, it should be just a proxy for the 7/// crate's dependencies interact. Mostly, it should be just a proxy for the
@@ -33,10 +34,27 @@ pub struct Module {
33 pub(crate) def_id: DefId, 34 pub(crate) def_id: DefId,
34} 35}
35 36
37/// An owned syntax node for a module. Unlike `ModuleSource`,
38/// this holds onto the AST for the whole file.
39pub enum ModuleSource {
40 SourceFile(ast::SourceFileNode),
41 Module(ast::ModuleNode),
42}
43
36impl Module { 44impl Module {
37 // FIXME: what is a module source exactly? It should contain two nodes 45 pub fn name(&self, db: &impl HirDatabase) -> Cancelable<Option<Name>> {
38 pub fn source(&self, db: &impl HirDatabase) -> Cancelable<ModuleSource> { 46 self.name_impl(db)
39 Ok(self.source_impl(db)) 47 }
48
49 pub fn defenition_source(&self, db: &impl HirDatabase) -> Cancelable<(FileId, ModuleSource)> {
50 self.defenition_source_impl(db)
51 }
52
53 pub fn declaration_source(
54 &self,
55 db: &impl HirDatabase,
56 ) -> Cancelable<Option<(FileId, ast::ModuleNode)>> {
57 self.declaration_source_impl(db)
40 } 58 }
41 59
42 /// Returns the crate this module is part of. 60 /// Returns the crate this module is part of.
@@ -56,6 +74,15 @@ impl Module {
56 pub fn parent(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> { 74 pub fn parent(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
57 self.parent_impl(db) 75 self.parent_impl(db)
58 } 76 }
77 pub fn path_to_root(&self, db: &impl HirDatabase) -> Cancelable<Vec<Module>> {
78 let mut res = vec![self.clone()];
79 let mut curr = self.clone();
80 while let Some(next) = curr.parent(db)? {
81 res.push(next.clone());
82 curr = next
83 }
84 Ok(res)
85 }
59 /// Returns a `ModuleScope`: a set of items, visible in this module. 86 /// Returns a `ModuleScope`: a set of items, visible in this module.
60 pub fn scope(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> { 87 pub fn scope(&self, db: &impl HirDatabase) -> Cancelable<ModuleScope> {
61 self.scope_impl(db) 88 self.scope_impl(db)
@@ -63,4 +90,7 @@ impl Module {
63 pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> Cancelable<PerNs<DefId>> { 90 pub fn resolve_path(&self, db: &impl HirDatabase, path: &Path) -> Cancelable<PerNs<DefId>> {
64 self.resolve_path_impl(db, path) 91 self.resolve_path_impl(db, path)
65 } 92 }
93 pub fn problems(&self, db: &impl HirDatabase) -> Cancelable<Vec<(SyntaxNode, Problem)>> {
94 self.problems_impl(db)
95 }
66} 96}