diff options
Diffstat (limited to 'crates/base_db/src')
-rw-r--r-- | crates/base_db/src/fixture.rs | 4 | ||||
-rw-r--r-- | crates/base_db/src/input.rs | 76 | ||||
-rw-r--r-- | crates/base_db/src/lib.rs | 4 |
3 files changed, 72 insertions, 12 deletions
diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index 72f1fd667..66e6443cb 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs | |||
@@ -158,7 +158,7 @@ impl ChangeFixture { | |||
158 | let crate_id = crate_graph.add_crate_root( | 158 | let crate_id = crate_graph.add_crate_root( |
159 | file_id, | 159 | file_id, |
160 | meta.edition, | 160 | meta.edition, |
161 | Some(crate_name.clone()), | 161 | Some(crate_name.clone().into()), |
162 | meta.cfg, | 162 | meta.cfg, |
163 | meta.env, | 163 | meta.env, |
164 | Default::default(), | 164 | Default::default(), |
@@ -187,7 +187,7 @@ impl ChangeFixture { | |||
187 | crate_graph.add_crate_root( | 187 | crate_graph.add_crate_root( |
188 | crate_root, | 188 | crate_root, |
189 | Edition::Edition2018, | 189 | Edition::Edition2018, |
190 | Some(CrateName::new("test").unwrap()), | 190 | Some(CrateName::new("test").unwrap().into()), |
191 | default_cfg, | 191 | default_cfg, |
192 | Env::default(), | 192 | Env::default(), |
193 | Default::default(), | 193 | Default::default(), |
diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs index 215ac4b41..87f0a0ce5 100644 --- a/crates/base_db/src/input.rs +++ b/crates/base_db/src/input.rs | |||
@@ -102,11 +102,46 @@ impl fmt::Display for CrateName { | |||
102 | 102 | ||
103 | impl ops::Deref for CrateName { | 103 | impl ops::Deref for CrateName { |
104 | type Target = str; | 104 | type Target = str; |
105 | fn deref(&self) -> &Self::Target { | 105 | fn deref(&self) -> &str { |
106 | &*self.0 | 106 | &*self.0 |
107 | } | 107 | } |
108 | } | 108 | } |
109 | 109 | ||
110 | #[derive(Debug, Clone, PartialEq, Eq, Hash)] | ||
111 | pub struct CrateDisplayName { | ||
112 | // The name we use to display various paths (with `_`). | ||
113 | crate_name: CrateName, | ||
114 | // The name as specified in Cargo.toml (with `-`). | ||
115 | canonical_name: String, | ||
116 | } | ||
117 | |||
118 | impl From<CrateName> for CrateDisplayName { | ||
119 | fn from(crate_name: CrateName) -> CrateDisplayName { | ||
120 | let canonical_name = crate_name.to_string(); | ||
121 | CrateDisplayName { crate_name, canonical_name } | ||
122 | } | ||
123 | } | ||
124 | |||
125 | impl fmt::Display for CrateDisplayName { | ||
126 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
127 | write!(f, "{}", self.crate_name) | ||
128 | } | ||
129 | } | ||
130 | |||
131 | impl ops::Deref for CrateDisplayName { | ||
132 | type Target = str; | ||
133 | fn deref(&self) -> &str { | ||
134 | &*self.crate_name | ||
135 | } | ||
136 | } | ||
137 | |||
138 | impl CrateDisplayName { | ||
139 | pub fn from_canonical_name(canonical_name: String) -> CrateDisplayName { | ||
140 | let crate_name = CrateName::normalize_dashes(&canonical_name); | ||
141 | CrateDisplayName { crate_name, canonical_name } | ||
142 | } | ||
143 | } | ||
144 | |||
110 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] | 145 | #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] |
111 | pub struct ProcMacroId(pub u32); | 146 | pub struct ProcMacroId(pub u32); |
112 | 147 | ||
@@ -127,11 +162,13 @@ impl PartialEq for ProcMacro { | |||
127 | pub struct CrateData { | 162 | pub struct CrateData { |
128 | pub root_file_id: FileId, | 163 | pub root_file_id: FileId, |
129 | pub edition: Edition, | 164 | pub edition: Edition, |
130 | /// A name used in the package's project declaration: for Cargo projects, it's [package].name, | 165 | /// A name used in the package's project declaration: for Cargo projects, |
131 | /// can be different for other project types or even absent (a dummy crate for the code snippet, for example). | 166 | /// it's [package].name, can be different for other project types or even |
132 | /// NOTE: The crate can be referenced as a dependency under a different name, | 167 | /// absent (a dummy crate for the code snippet, for example). |
133 | /// this one should be used when working with crate hierarchies. | 168 | /// |
134 | pub declaration_name: Option<CrateName>, | 169 | /// For purposes of analysis, crates are anonymous (only names in |
170 | /// `Dependency` matters), this name should only be used for UI. | ||
171 | pub display_name: Option<CrateDisplayName>, | ||
135 | pub cfg_options: CfgOptions, | 172 | pub cfg_options: CfgOptions, |
136 | pub env: Env, | 173 | pub env: Env, |
137 | pub dependencies: Vec<Dependency>, | 174 | pub dependencies: Vec<Dependency>, |
@@ -160,7 +197,7 @@ impl CrateGraph { | |||
160 | &mut self, | 197 | &mut self, |
161 | file_id: FileId, | 198 | file_id: FileId, |
162 | edition: Edition, | 199 | edition: Edition, |
163 | declaration_name: Option<CrateName>, | 200 | display_name: Option<CrateDisplayName>, |
164 | cfg_options: CfgOptions, | 201 | cfg_options: CfgOptions, |
165 | env: Env, | 202 | env: Env, |
166 | proc_macro: Vec<(SmolStr, Arc<dyn tt::TokenExpander>)>, | 203 | proc_macro: Vec<(SmolStr, Arc<dyn tt::TokenExpander>)>, |
@@ -171,7 +208,7 @@ impl CrateGraph { | |||
171 | let data = CrateData { | 208 | let data = CrateData { |
172 | root_file_id: file_id, | 209 | root_file_id: file_id, |
173 | edition, | 210 | edition, |
174 | declaration_name, | 211 | display_name, |
175 | cfg_options, | 212 | cfg_options, |
176 | env, | 213 | env, |
177 | proc_macro, | 214 | proc_macro, |
@@ -290,6 +327,29 @@ impl CrateGraph { | |||
290 | } | 327 | } |
291 | false | 328 | false |
292 | } | 329 | } |
330 | |||
331 | // Work around for https://github.com/rust-analyzer/rust-analyzer/issues/6038. | ||
332 | // As hacky as it gets. | ||
333 | pub fn patch_cfg_if(&mut self) -> bool { | ||
334 | let cfg_if = self.hacky_find_crate("cfg_if"); | ||
335 | let std = self.hacky_find_crate("std"); | ||
336 | match (cfg_if, std) { | ||
337 | (Some(cfg_if), Some(std)) => { | ||
338 | self.arena.get_mut(&cfg_if).unwrap().dependencies.clear(); | ||
339 | self.arena | ||
340 | .get_mut(&std) | ||
341 | .unwrap() | ||
342 | .dependencies | ||
343 | .push(Dependency { crate_id: cfg_if, name: CrateName::new("cfg_if").unwrap() }); | ||
344 | true | ||
345 | } | ||
346 | _ => false, | ||
347 | } | ||
348 | } | ||
349 | |||
350 | fn hacky_find_crate(&self, display_name: &str) -> Option<CrateId> { | ||
351 | self.iter().find(|it| self[*it].display_name.as_deref() == Some(display_name)) | ||
352 | } | ||
293 | } | 353 | } |
294 | 354 | ||
295 | impl ops::Index<CrateId> for CrateGraph { | 355 | impl ops::Index<CrateId> for CrateGraph { |
diff --git a/crates/base_db/src/lib.rs b/crates/base_db/src/lib.rs index e38aa7257..0804202d6 100644 --- a/crates/base_db/src/lib.rs +++ b/crates/base_db/src/lib.rs | |||
@@ -13,8 +13,8 @@ pub use crate::{ | |||
13 | cancellation::Canceled, | 13 | cancellation::Canceled, |
14 | change::Change, | 14 | change::Change, |
15 | input::{ | 15 | input::{ |
16 | CrateData, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, ProcMacroId, | 16 | CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, |
17 | SourceRoot, SourceRootId, | 17 | FileId, ProcMacroId, SourceRoot, SourceRootId, |
18 | }, | 18 | }, |
19 | }; | 19 | }; |
20 | pub use salsa; | 20 | pub use salsa; |