diff options
author | Kirill Bulatov <[email protected]> | 2020-03-09 09:26:46 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-03-09 09:26:46 +0000 |
commit | e1aa96f2c5b6cdbf0fb7f49b47209055b7a937f2 (patch) | |
tree | 3bf11b84559f60cf1aa8cf4098f2b310da20aa91 /crates/ra_db | |
parent | 5cffef56e2c373f6d67b0f7b70d7ade995795c04 (diff) |
Less abstract CrateData api
Diffstat (limited to 'crates/ra_db')
-rw-r--r-- | crates/ra_db/src/input.rs | 60 |
1 files changed, 27 insertions, 33 deletions
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 7b9f4efe4..4069c0fed 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -104,13 +104,16 @@ impl CrateName { | |||
104 | } | 104 | } |
105 | 105 | ||
106 | #[derive(Debug, Clone, PartialEq, Eq)] | 106 | #[derive(Debug, Clone, PartialEq, Eq)] |
107 | struct CrateData { | 107 | pub struct CrateData { |
108 | file_id: FileId, | 108 | pub root_file_id: FileId, |
109 | edition: Edition, | 109 | pub edition: Edition, |
110 | declaration_name: Option<String>, | 110 | /// The name to display to the end user. |
111 | /// This actual crate name can be different in a particular dependent crate | ||
112 | /// or may even be missing for some cases, such as a dummy crate for the code snippet. | ||
113 | pub display_name: Option<String>, | ||
111 | cfg_options: CfgOptions, | 114 | cfg_options: CfgOptions, |
112 | env: Env, | 115 | env: Env, |
113 | dependencies: Vec<Dependency>, | 116 | pub dependencies: Vec<Dependency>, |
114 | } | 117 | } |
115 | 118 | ||
116 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 119 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -135,11 +138,11 @@ impl CrateGraph { | |||
135 | &mut self, | 138 | &mut self, |
136 | file_id: FileId, | 139 | file_id: FileId, |
137 | edition: Edition, | 140 | edition: Edition, |
138 | declaration_name: Option<String>, | 141 | display_name: Option<String>, |
139 | cfg_options: CfgOptions, | 142 | cfg_options: CfgOptions, |
140 | env: Env, | 143 | env: Env, |
141 | ) -> CrateId { | 144 | ) -> CrateId { |
142 | let data = CrateData::new(file_id, edition, declaration_name, cfg_options, env); | 145 | let data = CrateData::new(file_id, edition, display_name, cfg_options, env); |
143 | let crate_id = CrateId(self.arena.len() as u32); | 146 | let crate_id = CrateId(self.arena.len() as u32); |
144 | let prev = self.arena.insert(crate_id, data); | 147 | let prev = self.arena.insert(crate_id, data); |
145 | assert!(prev.is_none()); | 148 | assert!(prev.is_none()); |
@@ -171,33 +174,17 @@ impl CrateGraph { | |||
171 | self.arena.keys().copied() | 174 | self.arena.keys().copied() |
172 | } | 175 | } |
173 | 176 | ||
174 | pub fn crate_root(&self, crate_id: CrateId) -> FileId { | 177 | pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData { |
175 | self.arena[&crate_id].file_id | 178 | &self.arena[crate_id] |
176 | } | ||
177 | |||
178 | pub fn edition(&self, crate_id: CrateId) -> Edition { | ||
179 | self.arena[&crate_id].edition | ||
180 | } | ||
181 | |||
182 | /// Returns a name of a crate, declared in the root project. | ||
183 | /// May be missing for some cases, such as when the crate definition was created for a code snippet. | ||
184 | /// | ||
185 | /// This should not be considered as a normal crate name, since the actual name can be different in | ||
186 | /// a particular dependent crate, where it is specified. | ||
187 | pub fn declaration_name(&self, crate_id: &CrateId) -> Option<&String> { | ||
188 | self.arena[crate_id].declaration_name.as_ref() | ||
189 | } | 179 | } |
190 | 180 | ||
191 | // FIXME: this only finds one crate with the given root; we could have multiple | 181 | // FIXME: this only finds one crate with the given root; we could have multiple |
192 | pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { | 182 | pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> { |
193 | let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?; | 183 | let (&crate_id, _) = |
184 | self.arena.iter().find(|(_crate_id, data)| data.root_file_id == file_id)?; | ||
194 | Some(crate_id) | 185 | Some(crate_id) |
195 | } | 186 | } |
196 | 187 | ||
197 | pub fn dependencies(&self, crate_id: CrateId) -> impl Iterator<Item = &Dependency> { | ||
198 | self.arena[&crate_id].dependencies.iter() | ||
199 | } | ||
200 | |||
201 | /// Extends this crate graph by adding a complete disjoint second crate | 188 | /// Extends this crate graph by adding a complete disjoint second crate |
202 | /// graph. | 189 | /// graph. |
203 | /// | 190 | /// |
@@ -220,7 +207,7 @@ impl CrateGraph { | |||
220 | return false; | 207 | return false; |
221 | } | 208 | } |
222 | 209 | ||
223 | for dep in self.dependencies(from) { | 210 | for dep in &self.crate_data(&from).dependencies { |
224 | let crate_id = dep.crate_id(); | 211 | let crate_id = dep.crate_id(); |
225 | if crate_id == target { | 212 | if crate_id == target { |
226 | return true; | 213 | return true; |
@@ -242,13 +229,20 @@ impl CrateId { | |||
242 | 229 | ||
243 | impl CrateData { | 230 | impl CrateData { |
244 | fn new( | 231 | fn new( |
245 | file_id: FileId, | 232 | root_file_id: FileId, |
246 | edition: Edition, | 233 | edition: Edition, |
247 | declaration_name: Option<String>, | 234 | display_name: Option<String>, |
248 | cfg_options: CfgOptions, | 235 | cfg_options: CfgOptions, |
249 | env: Env, | 236 | env: Env, |
250 | ) -> CrateData { | 237 | ) -> CrateData { |
251 | CrateData { file_id, edition, declaration_name, dependencies: Vec::new(), cfg_options, env } | 238 | CrateData { |
239 | root_file_id, | ||
240 | edition, | ||
241 | display_name, | ||
242 | dependencies: Vec::new(), | ||
243 | cfg_options, | ||
244 | env, | ||
245 | } | ||
252 | } | 246 | } |
253 | 247 | ||
254 | fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { | 248 | fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { |
@@ -382,8 +376,8 @@ mod tests { | |||
382 | .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) | 376 | .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) |
383 | .is_ok()); | 377 | .is_ok()); |
384 | assert_eq!( | 378 | assert_eq!( |
385 | graph.dependencies(crate1).collect::<Vec<_>>(), | 379 | graph.crate_data(&crate1).dependencies, |
386 | vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] | 380 | vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] |
387 | ); | 381 | ); |
388 | } | 382 | } |
389 | } | 383 | } |