diff options
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r-- | crates/ra_db/src/input.rs | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 614325a0f..8decc65c5 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -91,6 +91,7 @@ impl CrateGraph { | |||
91 | assert!(prev.is_none()); | 91 | assert!(prev.is_none()); |
92 | crate_id | 92 | crate_id |
93 | } | 93 | } |
94 | |||
94 | pub fn add_dep( | 95 | pub fn add_dep( |
95 | &mut self, | 96 | &mut self, |
96 | from: CrateId, | 97 | from: CrateId, |
@@ -102,22 +103,41 @@ impl CrateGraph { | |||
102 | } | 103 | } |
103 | Ok(self.arena.get_mut(&from).unwrap().add_dep(name, to)) | 104 | Ok(self.arena.get_mut(&from).unwrap().add_dep(name, to)) |
104 | } | 105 | } |
106 | |||
105 | pub fn is_empty(&self) -> bool { | 107 | pub fn is_empty(&self) -> bool { |
106 | self.arena.is_empty() | 108 | self.arena.is_empty() |
107 | } | 109 | } |
110 | |||
108 | pub fn crate_root(&self, crate_id: CrateId) -> FileId { | 111 | pub fn crate_root(&self, crate_id: CrateId) -> FileId { |
109 | self.arena[&crate_id].file_id | 112 | self.arena[&crate_id].file_id |
110 | } | 113 | } |
114 | |||
115 | // TODO: this only finds one crate with the given root; we could have multiple | ||
111 | pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { | 116 | pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { |
112 | let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?; | 117 | let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?; |
113 | Some(crate_id) | 118 | Some(crate_id) |
114 | } | 119 | } |
120 | |||
115 | pub fn dependencies<'a>( | 121 | pub fn dependencies<'a>( |
116 | &'a self, | 122 | &'a self, |
117 | crate_id: CrateId, | 123 | crate_id: CrateId, |
118 | ) -> impl Iterator<Item = &'a Dependency> + 'a { | 124 | ) -> impl Iterator<Item = &'a Dependency> + 'a { |
119 | self.arena[&crate_id].dependencies.iter() | 125 | self.arena[&crate_id].dependencies.iter() |
120 | } | 126 | } |
127 | |||
128 | /// Extends this crate graph by adding a complete disjoint second crate | ||
129 | /// graph. | ||
130 | pub fn extend(&mut self, other: CrateGraph) { | ||
131 | let start = self.arena.len() as u32; | ||
132 | self.arena.extend(other.arena.into_iter().map(|(id, mut data)| { | ||
133 | let new_id = CrateId(id.0 + start); | ||
134 | for dep in &mut data.dependencies { | ||
135 | dep.crate_id = CrateId(dep.crate_id.0 + start); | ||
136 | } | ||
137 | (new_id, data) | ||
138 | })); | ||
139 | } | ||
140 | |||
121 | fn dfs_find(&self, target: CrateId, from: CrateId, visited: &mut FxHashSet<CrateId>) -> bool { | 141 | fn dfs_find(&self, target: CrateId, from: CrateId, visited: &mut FxHashSet<CrateId>) -> bool { |
122 | if !visited.insert(from) { | 142 | if !visited.insert(from) { |
123 | return false; | 143 | return false; |