From af4e75533f2c071330e740e2fa94b131e3a2b538 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 20 Oct 2020 15:38:11 +0200 Subject: Rename declaration_name -> display_name Declaration names sounds like a name of declaration -- something you can use for analysis. It empathically isn't, and is just a label displayed in various UI. It's important not to confuse the two, least we accidentally mix semantics with UI (I believe, there's already a case of this in the FamousDefs at least). --- crates/base_db/src/input.rs | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) (limited to 'crates/base_db/src') diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs index b870e2cee..eb3aac88d 100644 --- a/crates/base_db/src/input.rs +++ b/crates/base_db/src/input.rs @@ -127,11 +127,13 @@ impl PartialEq for ProcMacro { pub struct CrateData { pub root_file_id: FileId, pub edition: Edition, - /// 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, + /// 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). + /// + /// For purposes of analysis, crates are anonymous (only names in + /// `Dependency` matters), this name should only be used for UI. + pub display_name: Option, pub cfg_options: CfgOptions, pub env: Env, pub dependencies: Vec, @@ -160,7 +162,7 @@ impl CrateGraph { &mut self, file_id: FileId, edition: Edition, - declaration_name: Option, + display_name: Option, cfg_options: CfgOptions, env: Env, proc_macro: Vec<(SmolStr, Arc)>, @@ -171,7 +173,7 @@ impl CrateGraph { let data = CrateData { root_file_id: file_id, edition, - declaration_name, + display_name, cfg_options, env, proc_macro, @@ -310,8 +312,8 @@ impl CrateGraph { } } - fn hacky_find_crate(&self, declaration_name: &str) -> Option { - self.iter().find(|it| self[*it].declaration_name.as_deref() == Some(declaration_name)) + fn hacky_find_crate(&self, display_name: &str) -> Option { + self.iter().find(|it| self[*it].display_name.as_deref() == Some(display_name)) } } -- cgit v1.2.3 From a85c4280bf7af3ea25c34c0cd72d05c8de17454d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 20 Oct 2020 16:00:51 +0200 Subject: Introduce CrateDisplayName --- crates/base_db/src/input.rs | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'crates/base_db/src') diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs index eb3aac88d..02c7348ff 100644 --- a/crates/base_db/src/input.rs +++ b/crates/base_db/src/input.rs @@ -102,7 +102,29 @@ impl fmt::Display for CrateName { impl ops::Deref for CrateName { type Target = str; - fn deref(&self) -> &Self::Target { + fn deref(&self) -> &str { + &*self.0 + } +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct CrateDisplayName(CrateName); + +impl From for CrateDisplayName { + fn from(inner: CrateName) -> CrateDisplayName { + CrateDisplayName(inner) + } +} + +impl fmt::Display for CrateDisplayName { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "{}", self.0) + } +} + +impl ops::Deref for CrateDisplayName { + type Target = str; + fn deref(&self) -> &str { &*self.0 } } -- cgit v1.2.3 From 3b1a648539487c08bc613b6fd6e573b0e0e38948 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 20 Oct 2020 17:04:38 +0200 Subject: More type safety around names --- crates/base_db/src/fixture.rs | 4 ++-- crates/base_db/src/input.rs | 27 ++++++++++++++++++++------- crates/base_db/src/lib.rs | 4 ++-- 3 files changed, 24 insertions(+), 11 deletions(-) (limited to 'crates/base_db/src') 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 { let crate_id = crate_graph.add_crate_root( file_id, meta.edition, - Some(crate_name.clone()), + Some(crate_name.clone().into()), meta.cfg, meta.env, Default::default(), @@ -187,7 +187,7 @@ impl ChangeFixture { crate_graph.add_crate_root( crate_root, Edition::Edition2018, - Some(CrateName::new("test").unwrap()), + Some(CrateName::new("test").unwrap().into()), default_cfg, Env::default(), Default::default(), diff --git a/crates/base_db/src/input.rs b/crates/base_db/src/input.rs index 02c7348ff..87f0a0ce5 100644 --- a/crates/base_db/src/input.rs +++ b/crates/base_db/src/input.rs @@ -108,24 +108,37 @@ impl ops::Deref for CrateName { } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct CrateDisplayName(CrateName); +pub struct CrateDisplayName { + // The name we use to display various paths (with `_`). + crate_name: CrateName, + // The name as specified in Cargo.toml (with `-`). + canonical_name: String, +} impl From for CrateDisplayName { - fn from(inner: CrateName) -> CrateDisplayName { - CrateDisplayName(inner) + fn from(crate_name: CrateName) -> CrateDisplayName { + let canonical_name = crate_name.to_string(); + CrateDisplayName { crate_name, canonical_name } } } impl fmt::Display for CrateDisplayName { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.0) + write!(f, "{}", self.crate_name) } } impl ops::Deref for CrateDisplayName { type Target = str; fn deref(&self) -> &str { - &*self.0 + &*self.crate_name + } +} + +impl CrateDisplayName { + pub fn from_canonical_name(canonical_name: String) -> CrateDisplayName { + let crate_name = CrateName::normalize_dashes(&canonical_name); + CrateDisplayName { crate_name, canonical_name } } } @@ -155,7 +168,7 @@ pub struct CrateData { /// /// For purposes of analysis, crates are anonymous (only names in /// `Dependency` matters), this name should only be used for UI. - pub display_name: Option, + pub display_name: Option, pub cfg_options: CfgOptions, pub env: Env, pub dependencies: Vec, @@ -184,7 +197,7 @@ impl CrateGraph { &mut self, file_id: FileId, edition: Edition, - display_name: Option, + display_name: Option, cfg_options: CfgOptions, env: Env, proc_macro: Vec<(SmolStr, Arc)>, 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::{ cancellation::Canceled, change::Change, input::{ - CrateData, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, FileId, ProcMacroId, - SourceRoot, SourceRootId, + CrateData, CrateDisplayName, CrateGraph, CrateId, CrateName, Dependency, Edition, Env, + FileId, ProcMacroId, SourceRoot, SourceRootId, }, }; pub use salsa; -- cgit v1.2.3