diff options
Diffstat (limited to 'crates/ra_db')
-rw-r--r-- | crates/ra_db/src/fixture.rs | 11 | ||||
-rw-r--r-- | crates/ra_db/src/input.rs | 129 |
2 files changed, 99 insertions, 41 deletions
diff --git a/crates/ra_db/src/fixture.rs b/crates/ra_db/src/fixture.rs index da7af110c..947d6ad56 100644 --- a/crates/ra_db/src/fixture.rs +++ b/crates/ra_db/src/fixture.rs | |||
@@ -56,6 +56,7 @@ fn with_single_file(db: &mut dyn SourceDatabaseExt, text: &str) -> FileId { | |||
56 | crate_graph.add_crate_root( | 56 | crate_graph.add_crate_root( |
57 | file_id, | 57 | file_id, |
58 | Edition::Edition2018, | 58 | Edition::Edition2018, |
59 | None, | ||
59 | CfgOptions::default(), | 60 | CfgOptions::default(), |
60 | Env::default(), | 61 | Env::default(), |
61 | ); | 62 | ); |
@@ -98,8 +99,13 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
98 | assert!(meta.path.starts_with(&source_root_prefix)); | 99 | assert!(meta.path.starts_with(&source_root_prefix)); |
99 | 100 | ||
100 | if let Some(krate) = meta.krate { | 101 | if let Some(krate) = meta.krate { |
101 | let crate_id = | 102 | let crate_id = crate_graph.add_crate_root( |
102 | crate_graph.add_crate_root(file_id, meta.edition, meta.cfg, Env::default()); | 103 | file_id, |
104 | meta.edition, | ||
105 | Some(krate.clone()), | ||
106 | meta.cfg, | ||
107 | Env::default(), | ||
108 | ); | ||
103 | let prev = crates.insert(krate.clone(), crate_id); | 109 | let prev = crates.insert(krate.clone(), crate_id); |
104 | assert!(prev.is_none()); | 110 | assert!(prev.is_none()); |
105 | for dep in meta.deps { | 111 | for dep in meta.deps { |
@@ -132,6 +138,7 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option<FilePosit | |||
132 | crate_graph.add_crate_root( | 138 | crate_graph.add_crate_root( |
133 | crate_root, | 139 | crate_root, |
134 | Edition::Edition2018, | 140 | Edition::Edition2018, |
141 | None, | ||
135 | CfgOptions::default(), | 142 | CfgOptions::default(), |
136 | Env::default(), | 143 | Env::default(), |
137 | ); | 144 | ); |
diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index eaff99fd3..4069c0fed 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs | |||
@@ -86,7 +86,7 @@ pub struct CrateId(pub u32); | |||
86 | pub struct CrateName(SmolStr); | 86 | pub struct CrateName(SmolStr); |
87 | 87 | ||
88 | impl CrateName { | 88 | impl CrateName { |
89 | /// Crates a crate name, checking for dashes in the string provided. | 89 | /// Creates a crate name, checking for dashes in the string provided. |
90 | /// Dashes are not allowed in the crate names, | 90 | /// Dashes are not allowed in the crate names, |
91 | /// hence the input string is returned as `Err` for those cases. | 91 | /// hence the input string is returned as `Err` for those cases. |
92 | pub fn new(name: &str) -> Result<CrateName, &str> { | 92 | pub fn new(name: &str) -> Result<CrateName, &str> { |
@@ -97,19 +97,23 @@ impl CrateName { | |||
97 | } | 97 | } |
98 | } | 98 | } |
99 | 99 | ||
100 | /// Crates a crate name, unconditionally replacing the dashes with underscores. | 100 | /// Creates a crate name, unconditionally replacing the dashes with underscores. |
101 | pub fn normalize_dashes(name: &str) -> CrateName { | 101 | pub fn normalize_dashes(name: &str) -> CrateName { |
102 | Self(SmolStr::new(name.replace('-', "_"))) | 102 | Self(SmolStr::new(name.replace('-', "_"))) |
103 | } | 103 | } |
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 | /// 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>, | ||
110 | cfg_options: CfgOptions, | 114 | cfg_options: CfgOptions, |
111 | env: Env, | 115 | env: Env, |
112 | dependencies: Vec<Dependency>, | 116 | pub dependencies: Vec<Dependency>, |
113 | } | 117 | } |
114 | 118 | ||
115 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | 119 | #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
@@ -134,10 +138,11 @@ impl CrateGraph { | |||
134 | &mut self, | 138 | &mut self, |
135 | file_id: FileId, | 139 | file_id: FileId, |
136 | edition: Edition, | 140 | edition: Edition, |
141 | display_name: Option<String>, | ||
137 | cfg_options: CfgOptions, | 142 | cfg_options: CfgOptions, |
138 | env: Env, | 143 | env: Env, |
139 | ) -> CrateId { | 144 | ) -> CrateId { |
140 | let data = CrateData::new(file_id, edition, cfg_options, env); | 145 | let data = CrateData::new(file_id, edition, display_name, cfg_options, env); |
141 | let crate_id = CrateId(self.arena.len() as u32); | 146 | let crate_id = CrateId(self.arena.len() as u32); |
142 | let prev = self.arena.insert(crate_id, data); | 147 | let prev = self.arena.insert(crate_id, data); |
143 | assert!(prev.is_none()); | 148 | assert!(prev.is_none()); |
@@ -169,24 +174,17 @@ impl CrateGraph { | |||
169 | self.arena.keys().copied() | 174 | self.arena.keys().copied() |
170 | } | 175 | } |
171 | 176 | ||
172 | pub fn crate_root(&self, crate_id: CrateId) -> FileId { | 177 | pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData { |
173 | self.arena[&crate_id].file_id | 178 | &self.arena[crate_id] |
174 | } | ||
175 | |||
176 | pub fn edition(&self, crate_id: CrateId) -> Edition { | ||
177 | self.arena[&crate_id].edition | ||
178 | } | 179 | } |
179 | 180 | ||
180 | // 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 |
181 | 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> { |
182 | 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)?; | ||
183 | Some(crate_id) | 185 | Some(crate_id) |
184 | } | 186 | } |
185 | 187 | ||
186 | pub fn dependencies(&self, crate_id: CrateId) -> impl Iterator<Item = &Dependency> { | ||
187 | self.arena[&crate_id].dependencies.iter() | ||
188 | } | ||
189 | |||
190 | /// Extends this crate graph by adding a complete disjoint second crate | 188 | /// Extends this crate graph by adding a complete disjoint second crate |
191 | /// graph. | 189 | /// graph. |
192 | /// | 190 | /// |
@@ -209,7 +207,7 @@ impl CrateGraph { | |||
209 | return false; | 207 | return false; |
210 | } | 208 | } |
211 | 209 | ||
212 | for dep in self.dependencies(from) { | 210 | for dep in &self.crate_data(&from).dependencies { |
213 | let crate_id = dep.crate_id(); | 211 | let crate_id = dep.crate_id(); |
214 | if crate_id == target { | 212 | if crate_id == target { |
215 | return true; | 213 | return true; |
@@ -230,8 +228,21 @@ impl CrateId { | |||
230 | } | 228 | } |
231 | 229 | ||
232 | impl CrateData { | 230 | impl CrateData { |
233 | fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData { | 231 | fn new( |
234 | CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env } | 232 | root_file_id: FileId, |
233 | edition: Edition, | ||
234 | display_name: Option<String>, | ||
235 | cfg_options: CfgOptions, | ||
236 | env: Env, | ||
237 | ) -> CrateData { | ||
238 | CrateData { | ||
239 | root_file_id, | ||
240 | edition, | ||
241 | display_name, | ||
242 | dependencies: Vec::new(), | ||
243 | cfg_options, | ||
244 | env, | ||
245 | } | ||
235 | } | 246 | } |
236 | 247 | ||
237 | fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { | 248 | fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { |
@@ -290,12 +301,27 @@ mod tests { | |||
290 | #[test] | 301 | #[test] |
291 | fn it_should_panic_because_of_cycle_dependencies() { | 302 | fn it_should_panic_because_of_cycle_dependencies() { |
292 | let mut graph = CrateGraph::default(); | 303 | let mut graph = CrateGraph::default(); |
293 | let crate1 = | 304 | let crate1 = graph.add_crate_root( |
294 | graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); | 305 | FileId(1u32), |
295 | let crate2 = | 306 | Edition2018, |
296 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | 307 | None, |
297 | let crate3 = | 308 | CfgOptions::default(), |
298 | graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); | 309 | Env::default(), |
310 | ); | ||
311 | let crate2 = graph.add_crate_root( | ||
312 | FileId(2u32), | ||
313 | Edition2018, | ||
314 | None, | ||
315 | CfgOptions::default(), | ||
316 | Env::default(), | ||
317 | ); | ||
318 | let crate3 = graph.add_crate_root( | ||
319 | FileId(3u32), | ||
320 | Edition2018, | ||
321 | None, | ||
322 | CfgOptions::default(), | ||
323 | Env::default(), | ||
324 | ); | ||
299 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); | 325 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); |
300 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); | 326 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); |
301 | assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err()); | 327 | assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err()); |
@@ -304,12 +330,27 @@ mod tests { | |||
304 | #[test] | 330 | #[test] |
305 | fn it_works() { | 331 | fn it_works() { |
306 | let mut graph = CrateGraph::default(); | 332 | let mut graph = CrateGraph::default(); |
307 | let crate1 = | 333 | let crate1 = graph.add_crate_root( |
308 | graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); | 334 | FileId(1u32), |
309 | let crate2 = | 335 | Edition2018, |
310 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | 336 | None, |
311 | let crate3 = | 337 | CfgOptions::default(), |
312 | graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); | 338 | Env::default(), |
339 | ); | ||
340 | let crate2 = graph.add_crate_root( | ||
341 | FileId(2u32), | ||
342 | Edition2018, | ||
343 | None, | ||
344 | CfgOptions::default(), | ||
345 | Env::default(), | ||
346 | ); | ||
347 | let crate3 = graph.add_crate_root( | ||
348 | FileId(3u32), | ||
349 | Edition2018, | ||
350 | None, | ||
351 | CfgOptions::default(), | ||
352 | Env::default(), | ||
353 | ); | ||
313 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); | 354 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); |
314 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); | 355 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); |
315 | } | 356 | } |
@@ -317,16 +358,26 @@ mod tests { | |||
317 | #[test] | 358 | #[test] |
318 | fn dashes_are_normalized() { | 359 | fn dashes_are_normalized() { |
319 | let mut graph = CrateGraph::default(); | 360 | let mut graph = CrateGraph::default(); |
320 | let crate1 = | 361 | let crate1 = graph.add_crate_root( |
321 | graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); | 362 | FileId(1u32), |
322 | let crate2 = | 363 | Edition2018, |
323 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | 364 | None, |
365 | CfgOptions::default(), | ||
366 | Env::default(), | ||
367 | ); | ||
368 | let crate2 = graph.add_crate_root( | ||
369 | FileId(2u32), | ||
370 | Edition2018, | ||
371 | None, | ||
372 | CfgOptions::default(), | ||
373 | Env::default(), | ||
374 | ); | ||
324 | assert!(graph | 375 | assert!(graph |
325 | .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) | 376 | .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) |
326 | .is_ok()); | 377 | .is_ok()); |
327 | assert_eq!( | 378 | assert_eq!( |
328 | graph.dependencies(crate1).collect::<Vec<_>>(), | 379 | graph.crate_data(&crate1).dependencies, |
329 | vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] | 380 | vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] |
330 | ); | 381 | ); |
331 | } | 382 | } |
332 | } | 383 | } |