diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-21 10:56:05 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-03-21 10:56:05 +0000 |
commit | b03883279528986bd829e14f967db4b4c980049d (patch) | |
tree | b891f591e240e84d14dc8060e3706ee025cc0906 /crates/ra_batch | |
parent | 1eb3bf41d75a8c744a0ecb586d4a2e92a1175c08 (diff) | |
parent | c6d6e6c6259709ec30eadb79a1908dca707a6499 (diff) |
Merge #997
997: Improve filtering of file roots r=matklad a=vipentti
`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.
Relates to discussion in #869
Co-authored-by: Ville Penttinen <[email protected]>
Diffstat (limited to 'crates/ra_batch')
-rw-r--r-- | crates/ra_batch/src/lib.rs | 36 | ||||
-rw-r--r-- | crates/ra_batch/src/vfs_filter.rs | 54 |
2 files changed, 61 insertions, 29 deletions
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 @@ | |||
1 | mod vfs_filter; | ||
2 | |||
1 | use std::sync::Arc; | 3 | use std::sync::Arc; |
2 | use std::path::{Path, PathBuf}; | 4 | use std::path::Path; |
3 | use std::collections::HashSet; | 5 | use std::collections::HashSet; |
4 | 6 | ||
5 | use rustc_hash::FxHashMap; | 7 | use rustc_hash::FxHashMap; |
@@ -9,7 +11,8 @@ use ra_db::{ | |||
9 | }; | 11 | }; |
10 | use ra_hir::{db, HirInterner}; | 12 | use ra_hir::{db, HirInterner}; |
11 | use ra_project_model::ProjectWorkspace; | 13 | use ra_project_model::ProjectWorkspace; |
12 | use ra_vfs::{Vfs, VfsChange, RootEntry, Filter, RelativePath}; | 14 | use ra_vfs::{Vfs, VfsChange}; |
15 | use vfs_filter::IncludeRustFiles; | ||
13 | 16 | ||
14 | type Result<T> = std::result::Result<T, failure::Error>; | 17 | type Result<T> = std::result::Result<T, failure::Error>; |
15 | 18 | ||
@@ -43,30 +46,6 @@ fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId { | |||
43 | SourceRootId(r.0.into()) | 46 | SourceRootId(r.0.into()) |
44 | } | 47 | } |
45 | 48 | ||
46 | struct IncludeRustFiles; | ||
47 | |||
48 | impl IncludeRustFiles { | ||
49 | fn to_entry(path: PathBuf) -> RootEntry { | ||
50 | RootEntry::new(path, Box::new(Self {})) | ||
51 | } | ||
52 | } | ||
53 | |||
54 | impl Filter for IncludeRustFiles { | ||
55 | fn include_dir(&self, dir_path: &RelativePath) -> bool { | ||
56 | const IGNORED_FOLDERS: &[&str] = &["node_modules", "target", ".git"]; | ||
57 | |||
58 | let is_ignored = dir_path.components().any(|c| IGNORED_FOLDERS.contains(&c.as_str())); | ||
59 | |||
60 | let hidden = dir_path.components().any(|c| c.as_str().starts_with(".")); | ||
61 | |||
62 | !is_ignored && !hidden | ||
63 | } | ||
64 | |||
65 | fn include_file(&self, file_path: &RelativePath) -> bool { | ||
66 | file_path.extension() == Some("rs") | ||
67 | } | ||
68 | } | ||
69 | |||
70 | impl BatchDatabase { | 49 | impl BatchDatabase { |
71 | pub fn load(crate_graph: CrateGraph, vfs: &mut Vfs) -> BatchDatabase { | 50 | pub fn load(crate_graph: CrateGraph, vfs: &mut Vfs) -> BatchDatabase { |
72 | let mut db = | 51 | let mut db = |
@@ -122,9 +101,8 @@ impl BatchDatabase { | |||
122 | let root = std::env::current_dir()?.join(root); | 101 | let root = std::env::current_dir()?.join(root); |
123 | let ws = ProjectWorkspace::discover(root.as_ref())?; | 102 | let ws = ProjectWorkspace::discover(root.as_ref())?; |
124 | let mut roots = Vec::new(); | 103 | let mut roots = Vec::new(); |
125 | roots.push(root.clone()); | 104 | roots.push(IncludeRustFiles::member(root.clone())); |
126 | roots.extend(ws.to_roots()); | 105 | roots.extend(IncludeRustFiles::from_roots(ws.to_roots())); |
127 | let roots = roots.into_iter().map(IncludeRustFiles::to_entry).collect::<Vec<_>>(); | ||
128 | let (mut vfs, roots) = Vfs::new(roots); | 106 | let (mut vfs, roots) = Vfs::new(roots); |
129 | let mut load = |path: &Path| { | 107 | let mut load = |path: &Path| { |
130 | let vfs_file = vfs.load(path); | 108 | 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..dd20c1203 --- /dev/null +++ b/crates/ra_batch/src/vfs_filter.rs | |||
@@ -0,0 +1,54 @@ | |||
1 | use std::path::PathBuf; | ||
2 | use ra_project_model::ProjectRoot; | ||
3 | use ra_vfs::{RootEntry, Filter, RelativePath}; | ||
4 | |||
5 | /// `IncludeRustFiles` is used to convert | ||
6 | /// from `ProjectRoot` to `RootEntry` for VFS | ||
7 | pub struct IncludeRustFiles { | ||
8 | root: ProjectRoot, | ||
9 | } | ||
10 | |||
11 | impl IncludeRustFiles { | ||
12 | pub fn from_roots<R>(roots: R) -> impl Iterator<Item = RootEntry> | ||
13 | where | ||
14 | R: IntoIterator<Item = ProjectRoot>, | ||
15 | { | ||
16 | roots.into_iter().map(IncludeRustFiles::from_root) | ||
17 | } | ||
18 | |||
19 | pub fn from_root(root: ProjectRoot) -> RootEntry { | ||
20 | IncludeRustFiles::from(root).into() | ||
21 | } | ||
22 | |||
23 | #[allow(unused)] | ||
24 | pub fn external(path: PathBuf) -> RootEntry { | ||
25 | IncludeRustFiles::from_root(ProjectRoot::new(path, false)) | ||
26 | } | ||
27 | |||
28 | pub fn member(path: PathBuf) -> RootEntry { | ||
29 | IncludeRustFiles::from_root(ProjectRoot::new(path, true)) | ||
30 | } | ||
31 | } | ||
32 | |||
33 | impl Filter for IncludeRustFiles { | ||
34 | fn include_dir(&self, dir_path: &RelativePath) -> bool { | ||
35 | self.root.include_dir(dir_path) | ||
36 | } | ||
37 | |||
38 | fn include_file(&self, file_path: &RelativePath) -> bool { | ||
39 | self.root.include_file(file_path) | ||
40 | } | ||
41 | } | ||
42 | |||
43 | impl std::convert::From<ProjectRoot> for IncludeRustFiles { | ||
44 | fn from(v: ProjectRoot) -> IncludeRustFiles { | ||
45 | IncludeRustFiles { root: v } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | impl std::convert::From<IncludeRustFiles> for RootEntry { | ||
50 | fn from(v: IncludeRustFiles) -> RootEntry { | ||
51 | let path = v.root.path().clone(); | ||
52 | RootEntry::new(path, Box::new(v)) | ||
53 | } | ||
54 | } | ||