From 9d19e5b962f77259dd1334b9edb4da4de54f0987 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Fri, 2 Oct 2020 21:38:22 +0300 Subject: Properly name the field --- crates/base_db/src/input.rs | 13 +++++++------ crates/hir/src/code_model.rs | 4 ++-- crates/hir_def/src/import_map.rs | 10 +++++----- crates/hir_def/src/nameres.rs | 2 +- crates/ide/src/hover.rs | 2 +- crates/ide/src/link_rewrite.rs | 6 +++--- crates/ide/src/status.rs | 2 +- crates/rust-analyzer/src/cli/diagnostics.rs | 11 ++++++----- 8 files changed, 26 insertions(+), 24 deletions(-) (limited to 'crates') diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs index 251a7b245..c330314d4 100644 --- a/crates/base_db/src/input.rs +++ b/crates/base_db/src/input.rs @@ -127,10 +127,11 @@ impl PartialEq for ProcMacro { pub struct CrateData { pub root_file_id: FileId, pub edition: Edition, - /// The name to display to the end user. - /// This actual crate name can be different in a particular dependent crate - /// or may even be missing for some cases, such as a dummy crate for the code snippet. - pub display_name: Option, + /// A name used in the package's project declaration: for Cargo projects, it's [package].name, + /// can be different for other project types or even absent (a dummy crate for the code snippet, for example). + /// NOTE: The crate can be referenced as a dependency under a different name, + /// this one should be used when working with crate hierarchies. + pub declaration_name: Option, pub cfg_options: CfgOptions, pub env: Env, pub dependencies: Vec, @@ -159,7 +160,7 @@ impl CrateGraph { &mut self, file_id: FileId, edition: Edition, - display_name: Option, + declaration_name: Option, cfg_options: CfgOptions, env: Env, proc_macro: Vec<(SmolStr, Arc)>, @@ -170,7 +171,7 @@ impl CrateGraph { let data = CrateData { root_file_id: file_id, edition, - display_name, + declaration_name, cfg_options, env, proc_macro, diff --git a/crates/hir/src/code_model.rs b/crates/hir/src/code_model.rs index f3388da32..a19b0fad7 100644 --- a/crates/hir/src/code_model.rs +++ b/crates/hir/src/code_model.rs @@ -98,8 +98,8 @@ impl Crate { db.crate_graph()[self.id].edition } - pub fn display_name(self, db: &dyn HirDatabase) -> Option { - db.crate_graph()[self.id].display_name.clone() + pub fn declaration_name(self, db: &dyn HirDatabase) -> Option { + db.crate_graph()[self.id].declaration_name.clone() } 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 { use super::*; - fn check_search(ra_fixture: &str, krate_name: &str, query: Query, expect: Expect) { + fn check_search(ra_fixture: &str, crate_name: &str, query: Query, expect: Expect) { let db = TestDB::with_files(ra_fixture); let crate_graph = db.crate_graph(); let krate = crate_graph .iter() .find(|krate| { - crate_graph[*krate].display_name.as_ref().map(|n| n.to_string()) - == Some(krate_name.to_string()) + crate_graph[*krate].declaration_name.as_ref().map(|n| n.to_string()) + == Some(crate_name.to_string()) }) .unwrap(); @@ -359,7 +359,7 @@ mod tests { let path = map.path_of(item).unwrap(); format!( "{}::{} ({})\n", - crate_graph[krate].display_name.as_ref().unwrap(), + crate_graph[krate].declaration_name.as_ref().unwrap(), path, mark ) @@ -400,7 +400,7 @@ mod tests { .iter() .filter_map(|krate| { let cdata = &crate_graph[krate]; - let name = cdata.display_name.as_ref()?; + let name = cdata.declaration_name.as_ref()?; let map = db.import_map(krate); 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 { pub(crate) fn crate_def_map_query(db: &dyn DefDatabase, krate: CrateId) -> Arc { let _p = profile::span("crate_def_map_query").detail(|| { db.crate_graph()[krate] - .display_name + .declaration_name .as_ref() .map(ToString::to_string) .unwrap_or_default() diff --git a/crates/ide/src/hover.rs b/crates/ide/src/hover.rs index acdb51f26..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 fn render_path(db: &RootDatabase, module: Module, item_name: Option) -> String { let crate_name = - db.crate_graph()[module.krate().into()].display_name.as_ref().map(ToString::to_string); + db.crate_graph()[module.krate().into()].declaration_name.as_ref().map(ToString::to_string); let module_path = module .path_to_root(db) .into_iter() 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( let krate = resolved.module(db)?.krate(); let canonical_path = resolved.canonical_path(db)?; let new_target = get_doc_url(db, &krate)? - .join(&format!("{}/", krate.display_name(db)?)) + .join(&format!("{}/", krate.declaration_name(db)?)) .ok()? .join(&canonical_path.replace("::", "/")) .ok()? @@ -127,7 +127,7 @@ fn rewrite_url_link(db: &RootDatabase, def: ModuleDef, target: &str) -> Option Option { // // FIXME: clicking on the link should just open the file in the editor, // instead of falling back to external urls. - Some(format!("https://docs.rs/{}/*/", krate.display_name(db)?)) + Some(format!("https://docs.rs/{}/*/", krate.declaration_name(db)?)) }) .and_then(|s| Url::parse(&s).ok()) } 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) -> String { match krate { Some(krate) => { let crate_graph = db.crate_graph(); - let display_crate = |krate: CrateId| match &crate_graph[krate].display_name { + let display_crate = |krate: CrateId| match &crate_graph[krate].declaration_name { Some(it) => format!("{}({:?})", it, krate), None => format!("{:?}", krate), }; 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) - for module in work { let file_id = module.definition_source(db).file_id.original_file(db); if !visited_files.contains(&file_id) { - let crate_name = if let Some(name) = module.krate().display_name(db) { - format!("{}", name) - } else { - String::from("unknown") - }; + let crate_name = module + .krate() + .declaration_name(db) + .as_ref() + .map(ToString::to_string) + .unwrap_or_else(|| "unknown".to_string()); println!("processing crate: {}, module: {}", crate_name, _vfs.file_path(file_id)); for diagnostic in analysis.diagnostics(&DiagnosticsConfig::default(), file_id).unwrap() { -- cgit v1.2.3