From e32462c6d56592acd22f1aab64f627636a476d6c Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Tue, 19 Mar 2019 15:14:16 +0200 Subject: Improve filtering of file roots `ProjectWorkspace::to_roots` now returns a new `ProjectRoot` which contains information regarding whether or not the given path is part of the current workspace or an external dependency. This information can then be used in `ra_batch` and `ra_lsp_server` to implement more advanced filtering. This allows us to filter some unnecessary folders from external dependencies such as tests, examples and benches. --- crates/ra_project_model/src/lib.rs | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 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 c566ec0fb..b27cb55ef 100644 --- a/crates/ra_project_model/src/lib.rs +++ b/crates/ra_project_model/src/lib.rs @@ -32,6 +32,30 @@ pub enum ProjectWorkspace { Json { project: JsonProject }, } +/// `ProjectRoot` describes a workspace root folder. +/// Which may be an external dependency, or a member of +/// the current workspace. +pub struct ProjectRoot { + /// Path to the root folder + path: PathBuf, + /// Is a member of the current workspace + is_member: bool, +} + +impl ProjectRoot { + fn new(path: PathBuf, is_member: bool) -> ProjectRoot { + ProjectRoot { path, is_member } + } + + pub fn into_path(self) -> PathBuf { + self.path + } + + pub fn is_member(&self) -> bool { + self.is_member + } +} + impl ProjectWorkspace { pub fn discover(path: &Path) -> Result { match find_rust_project_json(path) { @@ -50,12 +74,15 @@ impl ProjectWorkspace { } } - pub fn to_roots(&self) -> Vec { + /// Returns the roots for the current ProjectWorkspace + /// The return type contains the path and whether or not + /// the root is a member of the current workspace + 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(root.path.clone()); + roots.push(ProjectRoot::new(root.path.clone(), true)); } roots } @@ -63,10 +90,12 @@ impl ProjectWorkspace { let mut roots = Vec::with_capacity(cargo.packages().count() + sysroot.crates().count()); for pkg in cargo.packages() { - roots.push(pkg.root(&cargo).to_path_buf()); + let root = pkg.root(&cargo).to_path_buf(); + let member = pkg.is_member(&cargo); + roots.push(ProjectRoot::new(root, member)); } for krate in sysroot.crates() { - roots.push(krate.root_dir(&sysroot).to_path_buf()) + roots.push(ProjectRoot::new(krate.root_dir(&sysroot).to_path_buf(), false)) } roots } -- cgit v1.2.3