From 5cffef56e2c373f6d67b0f7b70d7ade995795c04 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 8 Mar 2020 15:26:57 +0200 Subject: Consider crate declaration names --- crates/ra_db/src/fixture.rs | 11 ++++- crates/ra_db/src/input.rs | 99 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 87 insertions(+), 23 deletions(-) (limited to 'crates/ra_db') 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 { crate_graph.add_crate_root( file_id, Edition::Edition2018, + None, CfgOptions::default(), Env::default(), ); @@ -98,8 +99,13 @@ fn with_files(db: &mut dyn SourceDatabaseExt, fixture: &str) -> Option Option Result { @@ -97,7 +97,7 @@ impl CrateName { } } - /// Crates a crate name, unconditionally replacing the dashes with underscores. + /// Creates a crate name, unconditionally replacing the dashes with underscores. pub fn normalize_dashes(name: &str) -> CrateName { Self(SmolStr::new(name.replace('-', "_"))) } @@ -107,6 +107,7 @@ impl CrateName { struct CrateData { file_id: FileId, edition: Edition, + declaration_name: Option, cfg_options: CfgOptions, env: Env, dependencies: Vec, @@ -134,10 +135,11 @@ impl CrateGraph { &mut self, file_id: FileId, edition: Edition, + declaration_name: Option, cfg_options: CfgOptions, env: Env, ) -> CrateId { - let data = CrateData::new(file_id, edition, cfg_options, env); + let data = CrateData::new(file_id, edition, declaration_name, cfg_options, env); let crate_id = CrateId(self.arena.len() as u32); let prev = self.arena.insert(crate_id, data); assert!(prev.is_none()); @@ -177,6 +179,15 @@ impl CrateGraph { self.arena[&crate_id].edition } + /// Returns a name of a crate, declared in the root project. + /// May be missing for some cases, such as when the crate definition was created for a code snippet. + /// + /// This should not be considered as a normal crate name, since the actual name can be different in + /// a particular dependent crate, where it is specified. + pub fn declaration_name(&self, crate_id: &CrateId) -> Option<&String> { + self.arena[crate_id].declaration_name.as_ref() + } + // FIXME: this only finds one crate with the given root; we could have multiple pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option { let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?; @@ -230,8 +241,14 @@ impl CrateId { } impl CrateData { - fn new(file_id: FileId, edition: Edition, cfg_options: CfgOptions, env: Env) -> CrateData { - CrateData { file_id, edition, dependencies: Vec::new(), cfg_options, env } + fn new( + file_id: FileId, + edition: Edition, + declaration_name: Option, + cfg_options: CfgOptions, + env: Env, + ) -> CrateData { + CrateData { file_id, edition, declaration_name, dependencies: Vec::new(), cfg_options, env } } fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { @@ -290,12 +307,27 @@ mod tests { #[test] fn it_should_panic_because_of_cycle_dependencies() { let mut graph = CrateGraph::default(); - let crate1 = - graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); - let crate2 = - graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); - let crate3 = - graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); + let crate1 = graph.add_crate_root( + FileId(1u32), + Edition2018, + None, + CfgOptions::default(), + Env::default(), + ); + let crate2 = graph.add_crate_root( + FileId(2u32), + Edition2018, + None, + CfgOptions::default(), + Env::default(), + ); + let crate3 = graph.add_crate_root( + FileId(3u32), + Edition2018, + None, + CfgOptions::default(), + Env::default(), + ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); assert!(graph.add_dep(crate3, CrateName::new("crate1").unwrap(), crate1).is_err()); @@ -304,12 +336,27 @@ mod tests { #[test] fn it_works() { let mut graph = CrateGraph::default(); - let crate1 = - graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); - let crate2 = - graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); - let crate3 = - graph.add_crate_root(FileId(3u32), Edition2018, CfgOptions::default(), Env::default()); + let crate1 = graph.add_crate_root( + FileId(1u32), + Edition2018, + None, + CfgOptions::default(), + Env::default(), + ); + let crate2 = graph.add_crate_root( + FileId(2u32), + Edition2018, + None, + CfgOptions::default(), + Env::default(), + ); + let crate3 = graph.add_crate_root( + FileId(3u32), + Edition2018, + None, + CfgOptions::default(), + Env::default(), + ); assert!(graph.add_dep(crate1, CrateName::new("crate2").unwrap(), crate2).is_ok()); assert!(graph.add_dep(crate2, CrateName::new("crate3").unwrap(), crate3).is_ok()); } @@ -317,10 +364,20 @@ mod tests { #[test] fn dashes_are_normalized() { let mut graph = CrateGraph::default(); - let crate1 = - graph.add_crate_root(FileId(1u32), Edition2018, CfgOptions::default(), Env::default()); - let crate2 = - graph.add_crate_root(FileId(2u32), Edition2018, CfgOptions::default(), Env::default()); + let crate1 = graph.add_crate_root( + FileId(1u32), + Edition2018, + None, + CfgOptions::default(), + Env::default(), + ); + let crate2 = graph.add_crate_root( + FileId(2u32), + Edition2018, + None, + CfgOptions::default(), + Env::default(), + ); assert!(graph .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) .is_ok()); -- cgit v1.2.3 From e1aa96f2c5b6cdbf0fb7f49b47209055b7a937f2 Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Mon, 9 Mar 2020 11:26:46 +0200 Subject: Less abstract CrateData api --- crates/ra_db/src/input.rs | 60 +++++++++++++++++++++-------------------------- 1 file changed, 27 insertions(+), 33 deletions(-) (limited to 'crates/ra_db') diff --git a/crates/ra_db/src/input.rs b/crates/ra_db/src/input.rs index 7b9f4efe4..4069c0fed 100644 --- a/crates/ra_db/src/input.rs +++ b/crates/ra_db/src/input.rs @@ -104,13 +104,16 @@ impl CrateName { } #[derive(Debug, Clone, PartialEq, Eq)] -struct CrateData { - file_id: FileId, - edition: Edition, - declaration_name: Option, +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, cfg_options: CfgOptions, env: Env, - dependencies: Vec, + pub dependencies: Vec, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -135,11 +138,11 @@ impl CrateGraph { &mut self, file_id: FileId, edition: Edition, - declaration_name: Option, + display_name: Option, cfg_options: CfgOptions, env: Env, ) -> CrateId { - let data = CrateData::new(file_id, edition, declaration_name, cfg_options, env); + let data = CrateData::new(file_id, edition, display_name, cfg_options, env); let crate_id = CrateId(self.arena.len() as u32); let prev = self.arena.insert(crate_id, data); assert!(prev.is_none()); @@ -171,33 +174,17 @@ impl CrateGraph { self.arena.keys().copied() } - pub fn crate_root(&self, crate_id: CrateId) -> FileId { - self.arena[&crate_id].file_id - } - - pub fn edition(&self, crate_id: CrateId) -> Edition { - self.arena[&crate_id].edition - } - - /// Returns a name of a crate, declared in the root project. - /// May be missing for some cases, such as when the crate definition was created for a code snippet. - /// - /// This should not be considered as a normal crate name, since the actual name can be different in - /// a particular dependent crate, where it is specified. - pub fn declaration_name(&self, crate_id: &CrateId) -> Option<&String> { - self.arena[crate_id].declaration_name.as_ref() + pub fn crate_data(&self, crate_id: &CrateId) -> &CrateData { + &self.arena[crate_id] } // FIXME: this only finds one crate with the given root; we could have multiple pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option { - let (&crate_id, _) = self.arena.iter().find(|(_crate_id, data)| data.file_id == file_id)?; + let (&crate_id, _) = + self.arena.iter().find(|(_crate_id, data)| data.root_file_id == file_id)?; Some(crate_id) } - pub fn dependencies(&self, crate_id: CrateId) -> impl Iterator { - self.arena[&crate_id].dependencies.iter() - } - /// Extends this crate graph by adding a complete disjoint second crate /// graph. /// @@ -220,7 +207,7 @@ impl CrateGraph { return false; } - for dep in self.dependencies(from) { + for dep in &self.crate_data(&from).dependencies { let crate_id = dep.crate_id(); if crate_id == target { return true; @@ -242,13 +229,20 @@ impl CrateId { impl CrateData { fn new( - file_id: FileId, + root_file_id: FileId, edition: Edition, - declaration_name: Option, + display_name: Option, cfg_options: CfgOptions, env: Env, ) -> CrateData { - CrateData { file_id, edition, declaration_name, dependencies: Vec::new(), cfg_options, env } + CrateData { + root_file_id, + edition, + display_name, + dependencies: Vec::new(), + cfg_options, + env, + } } fn add_dep(&mut self, name: SmolStr, crate_id: CrateId) { @@ -382,8 +376,8 @@ mod tests { .add_dep(crate1, CrateName::normalize_dashes("crate-name-with-dashes"), crate2) .is_ok()); assert_eq!( - graph.dependencies(crate1).collect::>(), - vec![&Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] + graph.crate_data(&crate1).dependencies, + vec![Dependency { crate_id: crate2, name: "crate_name_with_dashes".into() }] ); } } -- cgit v1.2.3