diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-10-06 12:51:15 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-10-06 12:51:15 +0100 |
commit | 87cb840a4e140a49946235823384694da58c2a5a (patch) | |
tree | 4e6ce0749103bdf8e373b3d12472866bdc10d4c8 /crates | |
parent | af0e54a566ab8c8be9b39a628aaa4992f161695c (diff) | |
parent | 9d19e5b962f77259dd1334b9edb4da4de54f0987 (diff) |
Merge #6124
6124: Better normalized crate name usage r=jonas-schievink a=SomeoneToIgnore
Closes https://github.com/rust-analyzer/rust-analyzer/issues/5343
Closes https://github.com/rust-analyzer/rust-analyzer/issues/5932
Uses normalized name for code snippets (to be able to test the fix), hover messages and documentation rewrite links (are there any tests for those?).
Also renamed the field to better resemble the semantics.
Co-authored-by: Kirill Bulatov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r-- | crates/base_db/src/fixture.rs | 8 | ||||
-rw-r--r-- | crates/base_db/src/input.rs | 13 | ||||
-rw-r--r-- | crates/hir/src/code_model.rs | 6 | ||||
-rw-r--r-- | crates/hir_def/src/import_map.rs | 10 | ||||
-rw-r--r-- | crates/hir_def/src/nameres.rs | 2 | ||||
-rw-r--r-- | crates/ide/src/hover.rs | 32 | ||||
-rw-r--r-- | crates/ide/src/link_rewrite.rs | 6 | ||||
-rw-r--r-- | crates/ide/src/status.rs | 2 | ||||
-rw-r--r-- | crates/project_model/src/lib.rs | 5 | ||||
-rw-r--r-- | crates/rust-analyzer/src/cli/diagnostics.rs | 11 |
10 files changed, 64 insertions, 31 deletions
diff --git a/crates/base_db/src/fixture.rs b/crates/base_db/src/fixture.rs index b7286fc7d..72f1fd667 100644 --- a/crates/base_db/src/fixture.rs +++ b/crates/base_db/src/fixture.rs | |||
@@ -154,19 +154,19 @@ impl ChangeFixture { | |||
154 | assert!(meta.path.starts_with(&source_root_prefix)); | 154 | assert!(meta.path.starts_with(&source_root_prefix)); |
155 | 155 | ||
156 | if let Some(krate) = meta.krate { | 156 | if let Some(krate) = meta.krate { |
157 | let crate_name = CrateName::normalize_dashes(&krate); | ||
157 | let crate_id = crate_graph.add_crate_root( | 158 | let crate_id = crate_graph.add_crate_root( |
158 | file_id, | 159 | file_id, |
159 | meta.edition, | 160 | meta.edition, |
160 | Some(krate.clone()), | 161 | Some(crate_name.clone()), |
161 | meta.cfg, | 162 | meta.cfg, |
162 | meta.env, | 163 | meta.env, |
163 | Default::default(), | 164 | Default::default(), |
164 | ); | 165 | ); |
165 | let crate_name = CrateName::new(&krate).unwrap(); | ||
166 | let prev = crates.insert(crate_name.clone(), crate_id); | 166 | let prev = crates.insert(crate_name.clone(), crate_id); |
167 | assert!(prev.is_none()); | 167 | assert!(prev.is_none()); |
168 | for dep in meta.deps { | 168 | for dep in meta.deps { |
169 | let dep = CrateName::new(&dep).unwrap(); | 169 | let dep = CrateName::normalize_dashes(&dep); |
170 | crate_deps.push((crate_name.clone(), dep)) | 170 | crate_deps.push((crate_name.clone(), dep)) |
171 | } | 171 | } |
172 | } else if meta.path == "/main.rs" || meta.path == "/lib.rs" { | 172 | } else if meta.path == "/main.rs" || meta.path == "/lib.rs" { |
@@ -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("test".to_string()), | 190 | Some(CrateName::new("test").unwrap()), |
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 9a61f1d56..c330314d4 100644 --- a/crates/base_db/src/input.rs +++ b/crates/base_db/src/input.rs | |||
@@ -127,10 +127,11 @@ impl PartialEq for ProcMacro { | |||
127 | pub struct CrateData { | 127 | pub struct CrateData { |
128 | pub root_file_id: FileId, | 128 | pub root_file_id: FileId, |
129 | pub edition: Edition, | 129 | pub edition: Edition, |
130 | /// The name to display to the end user. | 130 | /// A name used in the package's project declaration: for Cargo projects, it's [package].name, |
131 | /// This actual crate name can be different in a particular dependent crate | 131 | /// can be different for other project types or even absent (a dummy crate for the code snippet, for example). |
132 | /// or may even be missing for some cases, such as a dummy crate for the code snippet. | 132 | /// NOTE: The crate can be referenced as a dependency under a different name, |
133 | pub display_name: Option<String>, | 133 | /// this one should be used when working with crate hierarchies. |
134 | pub declaration_name: Option<CrateName>, | ||
134 | pub cfg_options: CfgOptions, | 135 | pub cfg_options: CfgOptions, |
135 | pub env: Env, | 136 | pub env: Env, |
136 | pub dependencies: Vec<Dependency>, | 137 | pub dependencies: Vec<Dependency>, |
@@ -159,7 +160,7 @@ impl CrateGraph { | |||
159 | &mut self, | 160 | &mut self, |
160 | file_id: FileId, | 161 | file_id: FileId, |
161 | edition: Edition, | 162 | edition: Edition, |
162 | display_name: Option<String>, | 163 | declaration_name: Option<CrateName>, |
163 | cfg_options: CfgOptions, | 164 | cfg_options: CfgOptions, |
164 | env: Env, | 165 | env: Env, |
165 | proc_macro: Vec<(SmolStr, Arc<dyn tt::TokenExpander>)>, | 166 | proc_macro: Vec<(SmolStr, Arc<dyn tt::TokenExpander>)>, |
@@ -170,7 +171,7 @@ impl CrateGraph { | |||
170 | let data = CrateData { | 171 | let data = CrateData { |
171 | root_file_id: file_id, | 172 | root_file_id: file_id, |
172 | edition, | 173 | edition, |
173 | display_name, | 174 | declaration_name, |
174 | cfg_options, | 175 | cfg_options, |
175 | env, | 176 | env, |
176 | proc_macro, | 177 | proc_macro, |
diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index a445a97b3..c75d46bff 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs | |||
@@ -2,7 +2,7 @@ | |||
2 | use std::{iter, sync::Arc}; | 2 | use std::{iter, sync::Arc}; |
3 | 3 | ||
4 | use arrayvec::ArrayVec; | 4 | use arrayvec::ArrayVec; |
5 | use base_db::{CrateId, Edition, FileId}; | 5 | use base_db::{CrateId, CrateName, Edition, FileId}; |
6 | use either::Either; | 6 | use either::Either; |
7 | use hir_def::find_path::PrefixKind; | 7 | use hir_def::find_path::PrefixKind; |
8 | use hir_def::{ | 8 | use hir_def::{ |
@@ -99,8 +99,8 @@ impl Crate { | |||
99 | db.crate_graph()[self.id].edition | 99 | db.crate_graph()[self.id].edition |
100 | } | 100 | } |
101 | 101 | ||
102 | pub fn display_name(self, db: &dyn HirDatabase) -> Option<String> { | 102 | pub fn declaration_name(self, db: &dyn HirDatabase) -> Option<CrateName> { |
103 | db.crate_graph()[self.id].display_name.clone() | 103 | db.crate_graph()[self.id].declaration_name.clone() |
104 | } | 104 | } |
105 | 105 | ||
106 | pub fn query_external_importables( | 106 | pub fn query_external_importables( |
diff --git a/crates/hir_def/src/import_map.rs b/crates/hir_def/src/import_map.rs index a442fb63a..44bfe1593 100644 --- a/crates/hir_def/src/import_map.rs +++ b/crates/hir_def/src/import_map.rs | |||
@@ -334,14 +334,14 @@ mod tests { | |||
334 | 334 | ||
335 | use super::*; | 335 | use super::*; |
336 | 336 | ||
337 | fn check_search(ra_fixture: &str, krate_name: &str, query: Query, expect: Expect) { | 337 | fn check_search(ra_fixture: &str, crate_name: &str, query: Query, expect: Expect) { |
338 | let db = TestDB::with_files(ra_fixture); | 338 | let db = TestDB::with_files(ra_fixture); |
339 | let crate_graph = db.crate_graph(); | 339 | let crate_graph = db.crate_graph(); |
340 | let krate = crate_graph | 340 | let krate = crate_graph |
341 | .iter() | 341 | .iter() |
342 | .find(|krate| { | 342 | .find(|krate| { |
343 | crate_graph[*krate].display_name.as_ref().map(|n| n.to_string()) | 343 | crate_graph[*krate].declaration_name.as_ref().map(|n| n.to_string()) |
344 | == Some(krate_name.to_string()) | 344 | == Some(crate_name.to_string()) |
345 | }) | 345 | }) |
346 | .unwrap(); | 346 | .unwrap(); |
347 | 347 | ||
@@ -359,7 +359,7 @@ mod tests { | |||
359 | let path = map.path_of(item).unwrap(); | 359 | let path = map.path_of(item).unwrap(); |
360 | format!( | 360 | format!( |
361 | "{}::{} ({})\n", | 361 | "{}::{} ({})\n", |
362 | crate_graph[krate].display_name.as_ref().unwrap(), | 362 | crate_graph[krate].declaration_name.as_ref().unwrap(), |
363 | path, | 363 | path, |
364 | mark | 364 | mark |
365 | ) | 365 | ) |
@@ -400,7 +400,7 @@ mod tests { | |||
400 | .iter() | 400 | .iter() |
401 | .filter_map(|krate| { | 401 | .filter_map(|krate| { |
402 | let cdata = &crate_graph[krate]; | 402 | let cdata = &crate_graph[krate]; |
403 | let name = cdata.display_name.as_ref()?; | 403 | let name = cdata.declaration_name.as_ref()?; |
404 | 404 | ||
405 | let map = db.import_map(krate); | 405 | let map = db.import_map(krate); |
406 | 406 | ||
diff --git a/crates/hir_def/src/nameres.rs b/crates/hir_def/src/nameres.rs index 5e4d73c1f..464ffef21 100644 --- a/crates/hir_def/src/nameres.rs +++ b/crates/hir_def/src/nameres.rs | |||
@@ -173,7 +173,7 @@ impl CrateDefMap { | |||
173 | pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { | 173 | pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<CrateDefMap> { |
174 | let _p = profile::span("crate_def_map_query").detail(|| { | 174 | let _p = profile::span("crate_def_map_query").detail(|| { |
175 | db.crate_graph()[krate] | 175 | db.crate_graph()[krate] |
176 | .display_name | 176 | .declaration_name |
177 | .as_ref() | 177 | .as_ref() |
178 | .map(ToString::to_string) | 178 | .map(ToString::to_string) |
179 | .unwrap_or_default() | 179 | .unwrap_or_default() |
diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index 9cf02f0a3..4521d72cc 100644 --- a/crates/ide/src/hover.rs +++ b/crates/ide/src/hover.rs | |||
@@ -289,7 +289,7 @@ fn definition_owner_name(db: &RootDatabase, def: &Definition) -> Option<String> | |||
289 | 289 | ||
290 | fn render_path(db: &RootDatabase, module: Module, item_name: Option<String>) -> String { | 290 | fn render_path(db: &RootDatabase, module: Module, item_name: Option<String>) -> String { |
291 | let crate_name = | 291 | let crate_name = |
292 | db.crate_graph()[module.krate().into()].display_name.as_ref().map(ToString::to_string); | 292 | db.crate_graph()[module.krate().into()].declaration_name.as_ref().map(ToString::to_string); |
293 | let module_path = module | 293 | let module_path = module |
294 | .path_to_root(db) | 294 | .path_to_root(db) |
295 | .into_iter() | 295 | .into_iter() |
@@ -3163,4 +3163,34 @@ fn main() { let s<|>t = test().get(); } | |||
3163 | "#]], | 3163 | "#]], |
3164 | ); | 3164 | ); |
3165 | } | 3165 | } |
3166 | |||
3167 | #[test] | ||
3168 | fn hover_displays_normalized_crate_names() { | ||
3169 | check( | ||
3170 | r#" | ||
3171 | //- /lib.rs crate:name-with-dashes | ||
3172 | pub mod wrapper { | ||
3173 | pub struct Thing { x: u32 } | ||
3174 | |||
3175 | impl Thing { | ||
3176 | pub fn new() -> Thing { Thing { x: 0 } } | ||
3177 | } | ||
3178 | } | ||
3179 | |||
3180 | //- /main.rs crate:main deps:name-with-dashes | ||
3181 | fn main() { let foo_test = name_with_dashes::wrapper::Thing::new<|>(); } | ||
3182 | "#, | ||
3183 | expect![[r#" | ||
3184 | *new* | ||
3185 | |||
3186 | ```rust | ||
3187 | name_with_dashes::wrapper::Thing | ||
3188 | ``` | ||
3189 | |||
3190 | ```rust | ||
3191 | pub fn new() -> Thing | ||
3192 | ``` | ||
3193 | "#]], | ||
3194 | ) | ||
3195 | } | ||
3166 | } | 3196 | } |
diff --git a/crates/ide/src/link_rewrite.rs b/crates/ide/src/link_rewrite.rs index a16f90e17..c317a2379 100644 --- a/crates/ide/src/link_rewrite.rs +++ b/crates/ide/src/link_rewrite.rs | |||
@@ -107,7 +107,7 @@ fn rewrite_intra_doc_link( | |||
107 | let krate = resolved.module(db)?.krate(); | 107 | let krate = resolved.module(db)?.krate(); |
108 | let canonical_path = resolved.canonical_path(db)?; | 108 | let canonical_path = resolved.canonical_path(db)?; |
109 | let new_target = get_doc_url(db, &krate)? | 109 | let new_target = get_doc_url(db, &krate)? |
110 | .join(&format!("{}/", krate.display_name(db)?)) | 110 | .join(&format!("{}/", krate.declaration_name(db)?)) |
111 | .ok()? | 111 | .ok()? |
112 | .join(&canonical_path.replace("::", "/")) | 112 | .join(&canonical_path.replace("::", "/")) |
113 | .ok()? | 113 | .ok()? |
@@ -127,7 +127,7 @@ fn rewrite_url_link(db: &RootDatabase, def: ModuleDef, target: &str) -> Option<S | |||
127 | let module = def.module(db)?; | 127 | let module = def.module(db)?; |
128 | let krate = module.krate(); | 128 | let krate = module.krate(); |
129 | let canonical_path = def.canonical_path(db)?; | 129 | let canonical_path = def.canonical_path(db)?; |
130 | let base = format!("{}/{}", krate.display_name(db)?, canonical_path.replace("::", "/")); | 130 | let base = format!("{}/{}", krate.declaration_name(db)?, canonical_path.replace("::", "/")); |
131 | 131 | ||
132 | get_doc_url(db, &krate) | 132 | get_doc_url(db, &krate) |
133 | .and_then(|url| url.join(&base).ok()) | 133 | .and_then(|url| url.join(&base).ok()) |
@@ -248,7 +248,7 @@ fn get_doc_url(db: &RootDatabase, krate: &Crate) -> Option<Url> { | |||
248 | // | 248 | // |
249 | // FIXME: clicking on the link should just open the file in the editor, | 249 | // FIXME: clicking on the link should just open the file in the editor, |
250 | // instead of falling back to external urls. | 250 | // instead of falling back to external urls. |
251 | Some(format!("https://docs.rs/{}/*/", krate.display_name(db)?)) | 251 | Some(format!("https://docs.rs/{}/*/", krate.declaration_name(db)?)) |
252 | }) | 252 | }) |
253 | .and_then(|s| Url::parse(&s).ok()) | 253 | .and_then(|s| Url::parse(&s).ok()) |
254 | } | 254 | } |
diff --git a/crates/ide/src/status.rs b/crates/ide/src/status.rs index 0af84daa0..f67f10491 100644 --- a/crates/ide/src/status.rs +++ b/crates/ide/src/status.rs | |||
@@ -45,7 +45,7 @@ pub(crate) fn status(db: &RootDatabase, file_id: Option<FileId>) -> String { | |||
45 | match krate { | 45 | match krate { |
46 | Some(krate) => { | 46 | Some(krate) => { |
47 | let crate_graph = db.crate_graph(); | 47 | let crate_graph = db.crate_graph(); |
48 | let display_crate = |krate: CrateId| match &crate_graph[krate].display_name { | 48 | let display_crate = |krate: CrateId| match &crate_graph[krate].declaration_name { |
49 | Some(it) => format!("{}({:?})", it, krate), | 49 | Some(it) => format!("{}({:?})", it, krate), |
50 | None => format!("{:?}", krate), | 50 | None => format!("{:?}", krate), |
51 | }; | 51 | }; |
diff --git a/crates/project_model/src/lib.rs b/crates/project_model/src/lib.rs index 258f60e28..d1e7602fc 100644 --- a/crates/project_model/src/lib.rs +++ b/crates/project_model/src/lib.rs | |||
@@ -411,7 +411,7 @@ impl ProjectWorkspace { | |||
411 | let crate_id = crate_graph.add_crate_root( | 411 | let crate_id = crate_graph.add_crate_root( |
412 | file_id, | 412 | file_id, |
413 | edition, | 413 | edition, |
414 | Some(cargo[pkg].name.clone()), | 414 | Some(CrateName::normalize_dashes(&cargo[pkg].name)), |
415 | cfg_options, | 415 | cfg_options, |
416 | env, | 416 | env, |
417 | proc_macro.clone(), | 417 | proc_macro.clone(), |
@@ -546,7 +546,8 @@ fn sysroot_to_crate_graph( | |||
546 | 546 | ||
547 | let env = Env::default(); | 547 | let env = Env::default(); |
548 | let proc_macro = vec![]; | 548 | let proc_macro = vec![]; |
549 | let name = sysroot[krate].name.clone(); | 549 | let name = CrateName::new(&sysroot[krate].name) |
550 | .expect("Sysroot crates' names do not contain dashes"); | ||
550 | let crate_id = crate_graph.add_crate_root( | 551 | let crate_id = crate_graph.add_crate_root( |
551 | file_id, | 552 | file_id, |
552 | Edition::Edition2018, | 553 | Edition::Edition2018, |
diff --git a/crates/rust-analyzer/src/cli/diagnostics.rs b/crates/rust-analyzer/src/cli/diagnostics.rs index f3b6c900e..d1d3b12f8 100644 --- a/crates/rust-analyzer/src/cli/diagnostics.rs +++ b/crates/rust-analyzer/src/cli/diagnostics.rs | |||
@@ -36,11 +36,12 @@ pub fn diagnostics(path: &Path, load_output_dirs: bool, with_proc_macro: bool) - | |||
36 | for module in work { | 36 | for module in work { |
37 | let file_id = module.definition_source(db).file_id.original_file(db); | 37 | let file_id = module.definition_source(db).file_id.original_file(db); |
38 | if !visited_files.contains(&file_id) { | 38 | if !visited_files.contains(&file_id) { |
39 | let crate_name = if let Some(name) = module.krate().display_name(db) { | 39 | let crate_name = module |
40 | format!("{}", name) | 40 | .krate() |
41 | } else { | 41 | .declaration_name(db) |
42 | String::from("unknown") | 42 | .as_ref() |
43 | }; | 43 | .map(ToString::to_string) |
44 | .unwrap_or_else(|| "unknown".to_string()); | ||
44 | println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id)); | 45 | println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id)); |
45 | for diagnostic in analysis.diagnostics(&DiagnosticsConfig::default(), file_id).unwrap() | 46 | for diagnostic in analysis.diagnostics(&DiagnosticsConfig::default(), file_id).unwrap() |
46 | { | 47 | { |