aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_db')
-rw-r--r--crates/ra_db/src/input.rs34
-rw-r--r--crates/ra_db/src/mock.rs7
2 files changed, 31 insertions, 10 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs
index 7d9faa43c..ac144b991 100644
--- a/crates/ra_db/src/input.rs
+++ b/crates/ra_db/src/input.rs
@@ -1,7 +1,7 @@
1use std::sync::Arc; 1use std::sync::Arc;
2 2
3use rustc_hash::FxHashMap; 3use rustc_hash::{FxHashSet, FxHashMap};
4use rustc_hash::FxHashSet; 4use ra_syntax::SmolStr;
5use salsa; 5use salsa;
6 6
7use crate::file_resolver::FileResolverImp; 7use crate::file_resolver::FileResolverImp;
@@ -20,25 +20,32 @@ 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, name: SmolStr, crate_id: CrateId) {
35 self.deps.push(Dependency { crate_: dep }) 35 self.dependencies.push(Dependency { name, crate_id })
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 pub crate_id: CrateId,
42 pub name: SmolStr,
43}
44
45impl Dependency {
46 pub fn crate_id(&self) -> CrateId {
47 self.crate_id
48 }
42} 49}
43 50
44impl CrateGraph { 51impl CrateGraph {
@@ -48,8 +55,11 @@ impl CrateGraph {
48 assert!(prev.is_none()); 55 assert!(prev.is_none());
49 crate_id 56 crate_id
50 } 57 }
51 pub fn add_dep(&mut self, from: CrateId, to: CrateId) { 58 //FIXME: check that we don't have cycles here.
52 self.arena.get_mut(&from).unwrap().add_dep(to) 59 // Just a simple depth first search from `to` should work,
60 // the graph is small.
61 pub fn add_dep(&mut self, from: CrateId, name: SmolStr, to: CrateId) {
62 self.arena.get_mut(&from).unwrap().add_dep(name, to)
53 } 63 }
54 pub fn crate_root(&self, crate_id: CrateId) -> FileId { 64 pub fn crate_root(&self, crate_id: CrateId) -> FileId {
55 self.arena[&crate_id].file_id 65 self.arena[&crate_id].file_id
@@ -61,6 +71,12 @@ impl CrateGraph {
61 .find(|(_crate_id, data)| data.file_id == file_id)?; 71 .find(|(_crate_id, data)| data.file_id == file_id)?;
62 Some(crate_id) 72 Some(crate_id)
63 } 73 }
74 pub fn dependencies<'a>(
75 &'a self,
76 crate_id: CrateId,
77 ) -> impl Iterator<Item = &'a Dependency> + 'a {
78 self.arena[&crate_id].dependencies.iter()
79 }
64} 80}
65 81
66salsa::query_group! { 82salsa::query_group! {
diff --git a/crates/ra_db/src/mock.rs b/crates/ra_db/src/mock.rs
index 2840f9655..2f7551597 100644
--- a/crates/ra_db/src/mock.rs
+++ b/crates/ra_db/src/mock.rs
@@ -5,7 +5,7 @@ use relative_path::{RelativePath, RelativePathBuf};
5 5
6use crate::{FileId, FileResolver, SourceRoot, FileResolverImp}; 6use crate::{FileId, FileResolver, SourceRoot, FileResolverImp};
7 7
8#[derive(Default, Debug)] 8#[derive(Default, Debug, Clone)]
9pub struct FileMap(Vec<(FileId, RelativePathBuf)>); 9pub struct FileMap(Vec<(FileId, RelativePathBuf)>);
10 10
11impl FileMap { 11impl FileMap {
@@ -28,6 +28,11 @@ impl FileMap {
28 self.iter().map(|(id, _)| id).collect() 28 self.iter().map(|(id, _)| id).collect()
29 } 29 }
30 30
31 pub fn file_id(&self, path: &str) -> FileId {
32 assert!(path.starts_with('/'));
33 self.iter().find(|(_, p)| p == &path[1..]).unwrap().0
34 }
35
31 fn iter<'a>(&'a self) -> impl Iterator<Item = (FileId, &'a RelativePath)> + 'a { 36 fn iter<'a>(&'a self) -> impl Iterator<Item = (FileId, &'a RelativePath)> + 'a {
32 self.0 37 self.0
33 .iter() 38 .iter()