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.
This commit is contained in:
Ville Penttinen 2019-03-19 15:14:16 +02:00
parent 91576afc7e
commit e32462c6d5
6 changed files with 164 additions and 62 deletions

View file

@ -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<T> = std::result::Result<T, failure::Error>;
@ -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::<Vec<_>>();
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);