From b7d5172f69204b6d097d80a39dacad3494a26f5e Mon Sep 17 00:00:00 2001 From: veetaha Date: Wed, 1 Apr 2020 02:15:20 +0300 Subject: Simpify workspace handling --- crates/ra_project_model/src/lib.rs | 46 ++++++++++++++------------------------ 1 file changed, 17 insertions(+), 29 deletions(-) (limited to 'crates/ra_project_model') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index dd9c80691..a133243b4 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -58,22 +58,16 @@ pub enum ProjectWorkspace { #[derive(Clone)] pub struct PackageRoot { /// Path to the root folder - path: PathBuf, + pub path: PathBuf, /// Is a member of the current workspace - is_member: bool, + pub is_member: bool, } - impl PackageRoot { - pub fn new(path: PathBuf, is_member: bool) -> PackageRoot { - PackageRoot { path, is_member } - } - - pub fn path(&self) -> &PathBuf { - &self.path + pub fn new_member(path: PathBuf) -> PackageRoot { + Self { path, is_member: true } } - - pub fn is_member(&self) -> bool { - self.is_member + pub fn new_non_member(path: PathBuf) -> PackageRoot { + Self { path, is_member: false } } } @@ -130,24 +124,18 @@ impl ProjectWorkspace { pub fn to_roots(&self) -> Vec { match self { ProjectWorkspace::Json { project } => { - let mut roots = Vec::with_capacity(project.roots.len()); - for root in &project.roots { - roots.push(PackageRoot::new(root.path.clone(), true)); - } - roots - } - ProjectWorkspace::Cargo { cargo, sysroot } => { - let mut roots = Vec::with_capacity(cargo.packages().len() + sysroot.crates().len()); - for pkg in cargo.packages() { - let root = cargo[pkg].root().to_path_buf(); - let member = cargo[pkg].is_member; - roots.push(PackageRoot::new(root, member)); - } - for krate in sysroot.crates() { - roots.push(PackageRoot::new(sysroot[krate].root_dir().to_path_buf(), false)) - } - roots + project.roots.iter().map(|r| PackageRoot::new_member(r.path.clone())).collect() } + ProjectWorkspace::Cargo { cargo, sysroot } => cargo + .packages() + .map(|pkg| PackageRoot { + path: cargo[pkg].root().to_path_buf(), + is_member: cargo[pkg].is_member, + }) + .chain(sysroot.crates().map(|krate| { + PackageRoot::new_non_member(sysroot[krate].root_dir().to_path_buf()) + })) + .collect(), } } -- cgit v1.2.3 From 987fb26a5b9bb2c4352b688cfa20e0fcc8506216 Mon Sep 17 00:00:00 2001 From: veetaha Date: Wed, 1 Apr 2020 02:52:44 +0300 Subject: Migrate to iterators --- crates/ra_project_model/src/lib.rs | 18 +++--------------- 1 file changed, 3 insertions(+), 15 deletions(-) (limited to 'crates/ra_project_model') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index a133243b4..950db71ec 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -142,22 +142,10 @@ impl ProjectWorkspace { pub fn out_dirs(&self) -> Vec { match self { ProjectWorkspace::Json { project } => { - let mut out_dirs = Vec::with_capacity(project.crates.len()); - for krate in &project.crates { - if let Some(out_dir) = &krate.out_dir { - out_dirs.push(out_dir.to_path_buf()); - } - } - out_dirs + project.crates.iter().filter_map(|krate| krate.out_dir.as_ref()).cloned().collect() } - ProjectWorkspace::Cargo { cargo, sysroot: _sysroot } => { - let mut out_dirs = Vec::with_capacity(cargo.packages().len()); - for pkg in cargo.packages() { - if let Some(out_dir) = &cargo[pkg].out_dir { - out_dirs.push(out_dir.to_path_buf()); - } - } - out_dirs + ProjectWorkspace::Cargo { cargo, sysroot: _ } => { + cargo.packages().filter_map(|pkg| cargo[pkg].out_dir.as_ref()).cloned().collect() } } } -- cgit v1.2.3 From a90401aeed24203b28012c040f19cda66e0589f3 Mon Sep 17 00:00:00 2001 From: veetaha Date: Wed, 1 Apr 2020 03:01:30 +0300 Subject: Migrate to iters some more --- crates/ra_project_model/src/lib.rs | 29 +++++++++++------------------ 1 file changed, 11 insertions(+), 18 deletions(-) (limited to 'crates/ra_project_model') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index 950db71ec..e9c0c83db 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -152,24 +152,17 @@ impl ProjectWorkspace { pub fn proc_macro_dylib_paths(&self) -> Vec { match self { - ProjectWorkspace::Json { project } => { - let mut proc_macro_dylib_paths = Vec::with_capacity(project.crates.len()); - for krate in &project.crates { - if let Some(out_dir) = &krate.proc_macro_dylib_path { - proc_macro_dylib_paths.push(out_dir.to_path_buf()); - } - } - proc_macro_dylib_paths - } - ProjectWorkspace::Cargo { cargo, sysroot: _sysroot } => { - let mut proc_macro_dylib_paths = Vec::with_capacity(cargo.packages().len()); - for pkg in cargo.packages() { - if let Some(dylib_path) = &cargo[pkg].proc_macro_dylib_path { - proc_macro_dylib_paths.push(dylib_path.to_path_buf()); - } - } - proc_macro_dylib_paths - } + ProjectWorkspace::Json { project } => project + .crates + .iter() + .filter_map(|krate| krate.proc_macro_dylib_path.as_ref()) + .cloned() + .collect(), + ProjectWorkspace::Cargo { cargo, sysroot: _sysroot } => cargo + .packages() + .filter_map(|pkg| cargo[pkg].proc_macro_dylib_path.as_ref()) + .cloned() + .collect(), } } -- cgit v1.2.3 From bef899aa78f65dbf4ee0d304c171ba02e101a685 Mon Sep 17 00:00:00 2001 From: veetaha Date: Wed, 1 Apr 2020 03:13:39 +0300 Subject: Less mutability --- crates/ra_project_model/src/lib.rs | 42 +++++++++++++++++++++----------------- 1 file changed, 23 insertions(+), 19 deletions(-) (limited to 'crates/ra_project_model') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index e9c0c83db..ec8574952 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -185,10 +185,12 @@ impl ProjectWorkspace { let mut crate_graph = CrateGraph::default(); match self { ProjectWorkspace::Json { project } => { - let mut crates = FxHashMap::default(); - for (id, krate) in project.crates.iter().enumerate() { - let crate_id = json_project::CrateId(id); - if let Some(file_id) = load(&krate.root_module) { + let crates: FxHashMap<_, _> = project + .crates + .iter() + .enumerate() + .filter_map(|(seq_index, krate)| { + let file_id = load(&krate.root_module)?; let edition = match krate.edition { json_project::Edition::Edition2015 => Edition::Edition2015, json_project::Edition::Edition2018 => Edition::Edition2018, @@ -218,8 +220,8 @@ impl ProjectWorkspace { .clone() .map(|it| proc_macro_client.by_dylib_path(&it)); // FIXME: No crate name in json definition such that we cannot add OUT_DIR to env - crates.insert( - crate_id, + Some(( + json_project::CrateId(seq_index), crate_graph.add_crate_root( file_id, edition, @@ -230,9 +232,9 @@ impl ProjectWorkspace { extern_source, proc_macro.unwrap_or_default(), ), - ); - } - } + )) + }) + .collect(); for (id, krate) in project.crates.iter().enumerate() { for dep in &krate.deps { @@ -256,9 +258,11 @@ impl ProjectWorkspace { } } ProjectWorkspace::Cargo { cargo, sysroot } => { - let mut sysroot_crates = FxHashMap::default(); - for krate in sysroot.crates() { - if let Some(file_id) = load(&sysroot[krate].root) { + let sysroot_crates: FxHashMap<_, _> = sysroot + .crates() + .filter_map(|krate| { + let file_id = load(&sysroot[krate].root)?; + // Crates from sysroot have `cfg(test)` disabled let cfg_options = { let mut opts = default_cfg_options.clone(); @@ -269,22 +273,22 @@ impl ProjectWorkspace { let env = Env::default(); let extern_source = ExternSource::default(); let proc_macro = vec![]; + let crate_name = CrateName::new(&sysroot[krate].name) + .expect("Sysroot crate names should not contain dashes"); let crate_id = crate_graph.add_crate_root( file_id, Edition::Edition2018, - Some( - CrateName::new(&sysroot[krate].name) - .expect("Sysroot crate names should not contain dashes"), - ), + Some(crate_name), cfg_options, env, extern_source, proc_macro, ); - sysroot_crates.insert(krate, crate_id); - } - } + Some((krate, crate_id)) + }) + .collect(); + for from in sysroot.crates() { for &to in sysroot[from].deps.iter() { let name = &sysroot[to].name; -- cgit v1.2.3 From 6190caeeaedd026e02640fb30691d835d72a8899 Mon Sep 17 00:00:00 2001 From: veetaha Date: Wed, 1 Apr 2020 13:40:40 +0300 Subject: Migrate to privacy as per review commets --- crates/ra_project_model/src/lib.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'crates/ra_project_model') diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs index ec8574952..0ab64a1e0 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -58,9 +58,9 @@ pub enum ProjectWorkspace { #[derive(Clone)] pub struct PackageRoot { /// Path to the root folder - pub path: PathBuf, + path: PathBuf, /// Is a member of the current workspace - pub is_member: bool, + is_member: bool, } impl PackageRoot { pub fn new_member(path: PathBuf) -> PackageRoot { @@ -69,6 +69,12 @@ impl PackageRoot { pub fn new_non_member(path: PathBuf) -> PackageRoot { Self { path, is_member: false } } + pub fn path(&self) -> &Path { + &self.path + } + pub fn is_member(&self) -> bool { + self.is_member + } } impl ProjectWorkspace { -- cgit v1.2.3