aboutsummaryrefslogtreecommitdiff
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
parent9c6c7ec2daacdbcaae8fe697b30d4c99aae69090 (diff)
more crate boilerplate
-rw-r--r--Cargo.toml1
-rw-r--r--crates/ra_db/src/input.rs20
-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
5 files changed, 54 insertions, 13 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 8e4e84729..5cfc064b5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,5 @@
1[workspace] 1[workspace]
2members = [ "crates/*" ] 2members = [ "crates/*" ]
3exclude = [ "crates/rowan"]
4 3
5[profile.release] 4[profile.release]
6debug = true 5debug = true
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index a48d05b98..37da8c549 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -20,25 +20,31 @@ pub struct CrateGraph {
20#[derive(Debug, Clone, PartialEq, Eq)] 20#[derive(Debug, Clone, PartialEq, Eq)]
21struct CrateData { 21struct CrateData {
22 file_id: FileId, 22 file_id: FileId,
23 deps: Vec<Dependency>, 23 dependencies: Vec<Dependency>,
24} 24}
25 25
26impl CrateData { 26impl CrateData {
27 fn new(file_id: FileId) -> CrateData { 27 fn new(file_id: FileId) -> CrateData {
28 CrateData { 28 CrateData {
29 file_id, 29 file_id,
30 deps: Vec::new(), 30 dependencies: Vec::new(),
31 } 31 }
32 } 32 }
33 33
34 fn add_dep(&mut self, dep: CrateId) { 34 fn add_dep(&mut self, dep: CrateId) {
35 self.deps.push(Dependency { crate_: dep }) 35 self.dependencies.push(Dependency { crate_id: dep })
36 } 36 }
37} 37}
38 38
39#[derive(Debug, Clone, PartialEq, Eq)] 39#[derive(Debug, Clone, PartialEq, Eq)]
40pub struct Dependency { 40pub struct Dependency {
41 crate_: CrateId, 41 crate_id: CrateId,
42}
43
44impl Dependency {
45 pub fn crate_id(&self) -> CrateId {
46 self.crate_id
47 }
42} 48}
43 49
44impl CrateGraph { 50impl CrateGraph {
@@ -64,6 +70,12 @@ impl CrateGraph {
64 .find(|(_crate_id, data)| data.file_id == file_id)?; 70 .find(|(_crate_id, data)| data.file_id == file_id)?;
65 Some(crate_id) 71 Some(crate_id)
66 } 72 }
73 pub fn dependencies<'a>(
74 &'a self,
75 crate_id: CrateId,
76 ) -> impl Iterator<Item = &'a Dependency> + 'a {
77 self.arena[&crate_id].dependencies.iter()
78 }
67} 79}
68 80
69salsa::query_group! { 81salsa::query_group! {
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);