aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_hir
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-12-08 21:51:06 +0000
committerAleksey Kladov <[email protected]>2018-12-09 10:33:16 +0000
commitca7e5905c1daffbed6a08fe674ae821f99bd274d (patch)
tree754bc504f4a2d459e5e90dfbe312d23554fbb1bb /crates/ra_hir
parent9c6c7ec2daacdbcaae8fe697b30d4c99aae69090 (diff)
more crate boilerplate
Diffstat (limited to 'crates/ra_hir')
-rw-r--r--crates/ra_hir/src/krate.rs34
-rw-r--r--crates/ra_hir/src/lib.rs1
-rw-r--r--crates/ra_hir/src/module.rs11
3 files changed, 38 insertions, 8 deletions
diff --git a/crates/ra_hir/src/krate.rs b/crates/ra_hir/src/krate.rs
index 367ddbd21..61007cc29 100644
--- a/crates/ra_hir/src/krate.rs
+++ b/crates/ra_hir/src/krate.rs
@@ -1,15 +1,37 @@
1use crate::FileId; 1use crate::{HirDatabase, Module, Cancelable};
2 2
3#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] 3pub use ra_db::CrateId;
4pub struct CrateId(u32);
5 4
5/// 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/// root module.
6#[derive(Debug)] 8#[derive(Debug)]
7pub struct Crate { 9pub struct Crate {
8 root: FileId, 10 crate_id: CrateId,
9} 11}
10 12
11impl Crate { 13impl Crate {
12 pub fn dependencies(&self) -> Vec<CrateId> { 14 pub(crate) fn new(crate_id: CrateId) -> Crate {
13 Vec::new() 15 Crate { crate_id }
16 }
17 pub fn dependencies(&self, db: &impl HirDatabase) -> Vec<Crate> {
18 let crate_graph = db.crate_graph();
19 crate_graph
20 .dependencies(self.crate_id)
21 .map(|dep| Crate::new(dep.crate_id()))
22 .collect()
23 }
24 pub fn root_module(&self, db: &impl HirDatabase) -> Cancelable<Option<Module>> {
25 let crate_graph = db.crate_graph();
26 let file_id = crate_graph.crate_root(self.crate_id);
27 let source_root_id = db.file_source_root(file_id);
28 let module_tree = db.module_tree(source_root_id)?;
29 // FIXME: teach module tree about crate roots instead of guessing
30 let (module_id, _) = ctry!(module_tree
31 .modules_with_sources()
32 .find(|(_, src)| src.file_id() == file_id));
33
34 let module = Module::new(db, source_root_id, module_id)?;
35 Ok(Some(module))
14 } 36 }
15} 37}
diff --git a/crates/ra_hir/src/lib.rs b/crates/ra_hir/src/lib.rs
index 0fa2ec50f..578fde259 100644
--- a/crates/ra_hir/src/lib.rs
+++ b/crates/ra_hir/src/lib.rs
@@ -26,7 +26,6 @@ mod krate;
26mod module; 26mod module;
27mod function; 27mod function;
28 28
29
30use std::ops::Index; 29use std::ops::Index;
31 30
32use ra_syntax::{SyntaxNodeRef, SyntaxNode}; 31use ra_syntax::{SyntaxNodeRef, SyntaxNode};
diff --git a/crates/ra_hir/src/module.rs b/crates/ra_hir/src/module.rs
index e7a49f83a..c6bb76d56 100644
--- a/crates/ra_hir/src/module.rs
+++ b/crates/ra_hir/src/module.rs
@@ -12,7 +12,7 @@ use ra_db::{SourceRootId, FileId, Cancelable};
12use relative_path::RelativePathBuf; 12use relative_path::RelativePathBuf;
13 13
14use crate::{ 14use crate::{
15 DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, 15 DefKind, DefLoc, DefId, Path, PathKind, HirDatabase, SourceItemId, SourceFileItemId, Crate,
16 arena::{Arena, Id}, 16 arena::{Arena, Id},
17}; 17};
18 18
@@ -64,6 +64,15 @@ impl Module {
64 }) 64 })
65 } 65 }
66 66
67 /// Returns the crate this module is part of.
68 pub fn krate(&self, db: &impl HirDatabase) -> Option<Crate> {
69 let root_id = self.module_id.crate_root(&self.tree);
70 let file_id = root_id.source(&self.tree).file_id();
71 let crate_graph = db.crate_graph();
72 let crate_id = crate_graph.crate_id_for_crate_root(file_id)?;
73 Some(Crate::new(crate_id))
74 }
75
67 /// The root of the tree this module is part of 76 /// The root of the tree this module is part of
68 pub fn crate_root(&self) -> Module { 77 pub fn crate_root(&self) -> Module {
69 let root_id = self.module_id.crate_root(&self.tree); 78 let root_id = self.module_id.crate_root(&self.tree);