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/src') 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