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 | 99 |
2 files changed, 87 insertions, 23 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..7b9f4efe4 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,7 +97,7 @@ 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 | } |
@@ -107,6 +107,7 @@ impl CrateName { | |||
107 | struct CrateData { | 107 | struct CrateData { |
108 | file_id: FileId, | 108 | file_id: FileId, |
109 | edition: Edition, | 109 | edition: Edition, |
110 | declaration_name: Option<String>, | ||
110 | cfg_options: CfgOptions, | 111 | cfg_options: CfgOptions, |
111 | env: Env, | 112 | env: Env, |
112 | dependencies: Vec<Dependency>, | 113 | dependencies: Vec<Dependency>, |
@@ -134,10 +135,11 @@ impl CrateGraph { | |||
134 | &mut self, | 135 | &mut self, |
135 | file_id: FileId, | 136 | file_id: FileId, |
136 | edition: Edition, | 137 | edition: Edition, |
138 | declaration_name: Option<String>, | ||
137 | cfg_options: CfgOptions, | 139 | cfg_options: CfgOptions, |
138 | env: Env, | 140 | env: Env, |
139 | ) -> CrateId { | 141 | ) -> CrateId { |
140 | let data = CrateData::new(file_id, edition, cfg_options, env); | 142 | let data = CrateData::new(file_id, edition, declaration_name, cfg_options, env); |
141 | let crate_id = CrateId(self.arena.len() as u32); | 143 | let crate_id = CrateId(self.arena.len() as u32); |
142 | let prev = self.arena.insert(crate_id, data); | 144 | let prev = self.arena.insert(crate_id, data); |
143 | assert!(prev.is_none()); | 145 | assert!(prev.is_none()); |
@@ -177,6 +179,15 @@ impl CrateGraph { | |||
177 | self.arena[&crate_id].edition | 179 | self.arena[&crate_id].edition |
178 | } | 180 | } |
179 | 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 | } | ||
190 | |||
180 | // FIXME: this only finds one crate with the given root; we could have multiple | 191 | // 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> { | 192 | 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)?; | 193 | let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?; |
@@ -230,8 +241,14 @@ impl CrateId { | |||
230 | } | 241 | } |
231 | 242 | ||
232 | impl CrateData { | 243 | impl CrateData { |
233 | fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData { | 244 | fn new( |
234 | CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env } | 245 | file_id: FileId, |
246 | edition: Edition, | ||
247 | declaration_name: Option<String>, | ||
248 | cfg_options: CfgOptions, | ||
249 | env: Env, | ||
250 | ) -> CrateData { | ||
251 | CrateData { file_id, edition, declaration_name, dependencies: Vec::new(), cfg_options, env } | ||
235 | } | 252 | } |
236 | 253 | ||
237 | fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { | 254 | fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { |
@@ -290,12 +307,27 @@ mod tests { | |||
290 | #[test] | 307 | #[test] |
291 | fn it_should_panic_because_of_cycle_dependencies() { | 308 | fn it_should_panic_because_of_cycle_dependencies() { |
292 | let mut graph = CrateGraph::default(); | 309 | let mut graph = CrateGraph::default(); |
293 | let crate1 = | 310 | let crate1 = graph.add_crate_root( |
294 | graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); | 311 | FileId(1u32), |
295 | let crate2 = | 312 | Edition2018, |
296 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | 313 | None, |
297 | let crate3 = | 314 | CfgOptions::default(), |
298 | graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); | 315 | Env::default(), |
316 | ); | ||
317 | let crate2 = graph.add_crate_root( | ||
318 | FileId(2u32), | ||
319 | Edition2018, | ||
320 | None, | ||
321 | CfgOptions::default(), | ||
322 | Env::default(), | ||
323 | ); | ||
324 | let crate3 = graph.add_crate_root( | ||
325 | FileId(3u32), | ||
326 | Edition2018, | ||
327 | None, | ||
328 | CfgOptions::default(), | ||
329 | Env::default(), | ||
330 | ); | ||
299 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); | 331 | 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()); | 332 | 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()); | 333 | assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err()); |
@@ -304,12 +336,27 @@ mod tests { | |||
304 | #[test] | 336 | #[test] |
305 | fn it_works() { | 337 | fn it_works() { |
306 | let mut graph = CrateGraph::default(); | 338 | let mut graph = CrateGraph::default(); |
307 | let crate1 = | 339 | let crate1 = graph.add_crate_root( |
308 | graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); | 340 | FileId(1u32), |
309 | let crate2 = | 341 | Edition2018, |
310 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | 342 | None, |
311 | let crate3 = | 343 | CfgOptions::default(), |
312 | graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); | 344 | Env::default(), |
345 | ); | ||
346 | let crate2 = graph.add_crate_root( | ||
347 | FileId(2u32), | ||
348 | Edition2018, | ||
349 | None, | ||
350 | CfgOptions::default(), | ||
351 | Env::default(), | ||
352 | ); | ||
353 | let crate3 = graph.add_crate_root( | ||
354 | FileId(3u32), | ||
355 | Edition2018, | ||
356 | None, | ||
357 | CfgOptions::default(), | ||
358 | Env::default(), | ||
359 | ); | ||
313 | assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); | 360 | 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()); | 361 | assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); |
315 | } | 362 | } |
@@ -317,10 +364,20 @@ mod tests { | |||
317 | #[test] | 364 | #[test] |
318 | fn dashes_are_normalized() { | 365 | fn dashes_are_normalized() { |
319 | let mut graph = CrateGraph::default(); | 366 | let mut graph = CrateGraph::default(); |
320 | let crate1 = | 367 | let crate1 = graph.add_crate_root( |
321 | graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); | 368 | FileId(1u32), |
322 | let crate2 = | 369 | Edition2018, |
323 | graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); | 370 | None, |
371 | CfgOptions::default(), | ||
372 | Env::default(), | ||
373 | ); | ||
374 | let crate2 = graph.add_crate_root( | ||
375 | FileId(2u32), | ||
376 | Edition2018, | ||
377 | None, | ||
378 | CfgOptions::default(), | ||
379 | Env::default(), | ||
380 | ); | ||
324 | assert!(graph | 381 | assert!(graph |
325 | .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) | 382 | .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) |
326 | .is_ok()); | 383 | .is_ok()); |