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_batch/src/lib.rs | 36 ++++--------------- crates/ra_batch/src/vfs_filter.rs | 59 ++++++++++++++++++++++++++++++++ crates/ra_lsp_server/src/lib.rs | 1 + crates/ra_lsp_server/src/server_world.rs | 34 +++--------------- crates/ra_lsp_server/src/vfs_filter.rs | 59 ++++++++++++++++++++++++++++++++ crates/ra_project_model/src/lib.rs | 37 +++++++++++++++++--- 6 files changed, 164 insertions(+), 62 deletions(-) create mode 100644 crates/ra_batch/src/vfs_filter.rs create mode 100644 crates/ra_lsp_server/src/vfs_filter.rs (limited to 'crates') diff --git a/crates/ra_batch/src/lib.rs b/crates/ra_batch/src/lib.rs index a054d0da3..deb9f95d7 100644 --- a/crates/ra_batch/src/lib.rs +++ b/crates/ra_batch/src/lib.rs @@ -1,5 +1,7 @@ +mod vfs_filter; + use std::sync::Arc; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::collections::HashSet; use rustc_hash::FxHashMap; @@ -9,7 +11,8 @@ use ra_db::{ }; use ra_hir::{db, HirInterner}; use ra_project_model::ProjectWorkspace; -use ra_vfs::{Vfs, VfsChange, RootEntry, Filter, RelativePath}; +use ra_vfs::{Vfs, VfsChange}; +use vfs_filter::IncludeRustFiles; type Result = std::result::Result; @@ -43,30 +46,6 @@ fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId { SourceRootId(r.0.into()) } -struct IncludeRustFiles; - -impl IncludeRustFiles { - fn to_entry(path: PathBuf) -> RootEntry { - RootEntry::new(path, Box::new(Self {})) - } -} - -impl Filter for IncludeRustFiles { - fn include_dir(&self, dir_path: &RelativePath) -> bool { - const IGNORED_FOLDERS: &[&str] = &["node_modules", "target", ".git"]; - - let is_ignored = dir_path.components().any(|c| IGNORED_FOLDERS.contains(&c.as_str())); - - let hidden = dir_path.components().any(|c| c.as_str().starts_with(".")); - - !is_ignored && !hidden - } - - fn include_file(&self, file_path: &RelativePath) -> bool { - file_path.extension() == Some("rs") - } -} - impl BatchDatabase { pub fn load(crate_graph: CrateGraph, vfs: &mut Vfs) -> BatchDatabase { let mut db = @@ -122,9 +101,8 @@ impl BatchDatabase { let root = std::env::current_dir()?.join(root); let ws = ProjectWorkspace::discover(root.as_ref())?; let mut roots = Vec::new(); - roots.push(root.clone()); - roots.extend(ws.to_roots()); - let roots = roots.into_iter().map(IncludeRustFiles::to_entry).collect::>(); + roots.push(IncludeRustFiles::member(root.clone())); + roots.extend(IncludeRustFiles::from_roots(ws.to_roots())); let (mut vfs, roots) = Vfs::new(roots); let mut load = |path: &Path| { let vfs_file = vfs.load(path); diff --git a/crates/ra_batch/src/vfs_filter.rs b/crates/ra_batch/src/vfs_filter.rs new file mode 100644 index 000000000..290aec172 --- /dev/null +++ b/crates/ra_batch/src/vfs_filter.rs @@ -0,0 +1,59 @@ +use std::path::PathBuf; +use ra_project_model::ProjectRoot; +use ra_vfs::{RootEntry, Filter, RelativePath}; + +pub struct IncludeRustFiles { + /// Is a member of the current workspace + is_member: bool, +} + +impl IncludeRustFiles { + pub fn from_roots(roots: R) -> impl Iterator + where + R: IntoIterator, + { + roots.into_iter().map(IncludeRustFiles::from_root) + } + + pub fn from_root(root: ProjectRoot) -> RootEntry { + let is_member = root.is_member(); + IncludeRustFiles::into_entry(root.into_path(), is_member) + } + + #[allow(unused)] + pub fn external(path: PathBuf) -> RootEntry { + IncludeRustFiles::into_entry(path, false) + } + + pub fn member(path: PathBuf) -> RootEntry { + IncludeRustFiles::into_entry(path, true) + } + + fn into_entry(path: PathBuf, is_member: bool) -> RootEntry { + RootEntry::new(path, Box::new(Self { is_member })) + } +} + +impl Filter for IncludeRustFiles { + fn include_dir(&self, dir_path: &RelativePath) -> bool { + const COMMON_IGNORED_DIRS: &[&str] = &["node_modules", "target", ".git"]; + const EXTERNAL_IGNORED_DIRS: &[&str] = &["examples", "tests", "benches"]; + + let is_ignored = if self.is_member { + dir_path.components().any(|c| COMMON_IGNORED_DIRS.contains(&c.as_str())) + } else { + dir_path.components().any(|c| { + let path = c.as_str(); + COMMON_IGNORED_DIRS.contains(&path) || EXTERNAL_IGNORED_DIRS.contains(&path) + }) + }; + + let hidden = dir_path.components().any(|c| c.as_str().starts_with(".")); + + !is_ignored && !hidden + } + + fn include_file(&self, file_path: &RelativePath) -> bool { + file_path.extension() == Some("rs") + } +} diff --git a/crates/ra_lsp_server/src/lib.rs b/crates/ra_lsp_server/src/lib.rs index 59e16a47c..113883bdd 100644 --- a/crates/ra_lsp_server/src/lib.rs +++ b/crates/ra_lsp_server/src/lib.rs @@ -4,6 +4,7 @@ mod conv; mod main_loop; mod markdown; mod project_model; +mod vfs_filter; pub mod req; pub mod init; mod server_world; diff --git a/crates/ra_lsp_server/src/server_world.rs b/crates/ra_lsp_server/src/server_world.rs index cf7c17c5c..af4798494 100644 --- a/crates/ra_lsp_server/src/server_world.rs +++ b/crates/ra_lsp_server/src/server_world.rs @@ -8,13 +8,14 @@ use ra_ide_api::{ Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId, LibraryData, SourceRootId }; -use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot, RootEntry, Filter}; -use relative_path::{RelativePath, RelativePathBuf}; +use ra_vfs::{Vfs, VfsChange, VfsFile, VfsRoot}; +use relative_path::RelativePathBuf; use parking_lot::RwLock; use failure::format_err; use crate::{ project_model::ProjectWorkspace, + vfs_filter::IncludeRustFiles, Result, }; @@ -33,40 +34,15 @@ pub struct ServerWorld { pub vfs: Arc>, } -struct IncludeRustFiles; - -impl IncludeRustFiles { - fn to_entry(path: PathBuf) -> RootEntry { - RootEntry::new(path, Box::new(Self {})) - } -} - -impl Filter for IncludeRustFiles { - fn include_dir(&self, dir_path: &RelativePath) -> bool { - const IGNORED_FOLDERS: &[&str] = &["node_modules", "target", ".git"]; - - let is_ignored = dir_path.components().any(|c| IGNORED_FOLDERS.contains(&c.as_str())); - - let hidden = dir_path.components().any(|c| c.as_str().starts_with(".")); - - !is_ignored && !hidden - } - - fn include_file(&self, file_path: &RelativePath) -> bool { - file_path.extension() == Some("rs") - } -} - impl ServerWorldState { pub fn new(root: PathBuf, workspaces: Vec) -> ServerWorldState { let mut change = AnalysisChange::new(); let mut roots = Vec::new(); - roots.push(root.clone()); + roots.push(IncludeRustFiles::member(root.clone())); for ws in workspaces.iter() { - roots.extend(ws.to_roots()); + roots.extend(IncludeRustFiles::from_roots(ws.to_roots())); } - let roots = roots.into_iter().map(IncludeRustFiles::to_entry).collect::>(); let (mut vfs, roots) = Vfs::new(roots); let roots_to_scan = roots.len(); diff --git a/crates/ra_lsp_server/src/vfs_filter.rs b/crates/ra_lsp_server/src/vfs_filter.rs new file mode 100644 index 000000000..290aec172 --- /dev/null +++ b/crates/ra_lsp_server/src/vfs_filter.rs @@ -0,0 +1,59 @@ +use std::path::PathBuf; +use ra_project_model::ProjectRoot; +use ra_vfs::{RootEntry, Filter, RelativePath}; + +pub struct IncludeRustFiles { + /// Is a member of the current workspace + is_member: bool, +} + +impl IncludeRustFiles { + pub fn from_roots(roots: R) -> impl Iterator + where + R: IntoIterator, + { + roots.into_iter().map(IncludeRustFiles::from_root) + } + + pub fn from_root(root: ProjectRoot) -> RootEntry { + let is_member = root.is_member(); + IncludeRustFiles::into_entry(root.into_path(), is_member) + } + + #[allow(unused)] + pub fn external(path: PathBuf) -> RootEntry { + IncludeRustFiles::into_entry(path, false) + } + + pub fn member(path: PathBuf) -> RootEntry { + IncludeRustFiles::into_entry(path, true) + } + + fn into_entry(path: PathBuf, is_member: bool) -> RootEntry { + RootEntry::new(path, Box::new(Self { is_member })) + } +} + +impl Filter for IncludeRustFiles { + fn include_dir(&self, dir_path: &RelativePath) -> bool { + const COMMON_IGNORED_DIRS: &[&str] = &["node_modules", "target", ".git"]; + const EXTERNAL_IGNORED_DIRS: &[&str] = &["examples", "tests", "benches"]; + + let is_ignored = if self.is_member { + dir_path.components().any(|c| COMMON_IGNORED_DIRS.contains(&c.as_str())) + } else { + dir_path.components().any(|c| { + let path = c.as_str(); + COMMON_IGNORED_DIRS.contains(&path) || EXTERNAL_IGNORED_DIRS.contains(&path) + }) + }; + + let hidden = dir_path.components().any(|c| c.as_str().starts_with(".")); + + !is_ignored && !hidden + } + + fn include_file(&self, file_path: &RelativePath) -> bool { + file_path.extension() == Some("rs") + } +} 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