diff options
author | Aleksey Kladov <[email protected]> | 2020-06-11 10:04:09 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-06-23 16:51:06 +0100 |
commit | dad1333b48c38bc7a5628fc0ff5304d003776a85 (patch) | |
tree | 29be52a980b4cae72f46a48c48135a15e31641e0 /crates/rust-analyzer/src/vfs_glob.rs | |
parent | 7aa66371ee3e8b31217513204c8b4f683584419d (diff) |
New VFS
Diffstat (limited to 'crates/rust-analyzer/src/vfs_glob.rs')
-rw-r--r-- | crates/rust-analyzer/src/vfs_glob.rs | 98 |
1 files changed, 0 insertions, 98 deletions
diff --git a/crates/rust-analyzer/src/vfs_glob.rs b/crates/rust-analyzer/src/vfs_glob.rs deleted file mode 100644 index ff37a7008..000000000 --- a/crates/rust-analyzer/src/vfs_glob.rs +++ /dev/null | |||
@@ -1,98 +0,0 @@ | |||
1 | //! Exclusion rules for vfs. | ||
2 | //! | ||
3 | //! By default, we include only `.rs` files, and skip some know offenders like | ||
4 | //! `/target` or `/node_modules` altogether. | ||
5 | //! | ||
6 | //! It's also possible to add custom exclusion globs. | ||
7 | |||
8 | use globset::{GlobSet, GlobSetBuilder}; | ||
9 | use ra_vfs::{Filter, RelativePath}; | ||
10 | |||
11 | pub use globset::{Glob, GlobBuilder}; | ||
12 | |||
13 | const ALWAYS_IGNORED: &[&str] = &["target/**", "**/node_modules/**", "**/.git/**"]; | ||
14 | const IGNORED_FOR_NON_MEMBERS: &[&str] = &["examples/**", "tests/**", "benches/**"]; | ||
15 | |||
16 | pub struct RustPackageFilterBuilder { | ||
17 | is_member: bool, | ||
18 | exclude: GlobSetBuilder, | ||
19 | } | ||
20 | |||
21 | impl Default for RustPackageFilterBuilder { | ||
22 | fn default() -> RustPackageFilterBuilder { | ||
23 | RustPackageFilterBuilder { is_member: false, exclude: GlobSetBuilder::new() } | ||
24 | } | ||
25 | } | ||
26 | |||
27 | impl RustPackageFilterBuilder { | ||
28 | pub fn set_member(mut self, is_member: bool) -> RustPackageFilterBuilder { | ||
29 | self.is_member = is_member; | ||
30 | self | ||
31 | } | ||
32 | |||
33 | pub fn exclude(mut self, globs: impl IntoIterator<Item = Glob>) -> RustPackageFilterBuilder { | ||
34 | for glob in globs.into_iter() { | ||
35 | self.exclude.add(glob); | ||
36 | } | ||
37 | self | ||
38 | } | ||
39 | |||
40 | pub fn into_vfs_filter(self) -> Box<dyn Filter> { | ||
41 | let RustPackageFilterBuilder { is_member, mut exclude } = self; | ||
42 | for &glob in ALWAYS_IGNORED { | ||
43 | exclude.add(Glob::new(glob).unwrap()); | ||
44 | } | ||
45 | if !is_member { | ||
46 | for &glob in IGNORED_FOR_NON_MEMBERS { | ||
47 | exclude.add(Glob::new(glob).unwrap()); | ||
48 | } | ||
49 | } | ||
50 | Box::new(RustPackageFilter { exclude: exclude.build().unwrap() }) | ||
51 | } | ||
52 | } | ||
53 | |||
54 | struct RustPackageFilter { | ||
55 | exclude: GlobSet, | ||
56 | } | ||
57 | |||
58 | impl Filter for RustPackageFilter { | ||
59 | fn include_dir(&self, dir_path: &RelativePath) -> bool { | ||
60 | !self.exclude.is_match(dir_path.as_str()) | ||
61 | } | ||
62 | |||
63 | fn include_file(&self, file_path: &RelativePath) -> bool { | ||
64 | file_path.extension() == Some("rs") | ||
65 | } | ||
66 | } | ||
67 | |||
68 | #[test] | ||
69 | fn test_globs() { | ||
70 | let filter = RustPackageFilterBuilder::default().set_member(true).into_vfs_filter(); | ||
71 | |||
72 | assert!(filter.include_dir(RelativePath::new("src/tests"))); | ||
73 | assert!(filter.include_dir(RelativePath::new("src/target"))); | ||
74 | assert!(filter.include_dir(RelativePath::new("tests"))); | ||
75 | assert!(filter.include_dir(RelativePath::new("benches"))); | ||
76 | |||
77 | assert!(!filter.include_dir(RelativePath::new("target"))); | ||
78 | assert!(!filter.include_dir(RelativePath::new("src/foo/.git"))); | ||
79 | assert!(!filter.include_dir(RelativePath::new("foo/node_modules"))); | ||
80 | |||
81 | let filter = RustPackageFilterBuilder::default().set_member(false).into_vfs_filter(); | ||
82 | |||
83 | assert!(filter.include_dir(RelativePath::new("src/tests"))); | ||
84 | assert!(filter.include_dir(RelativePath::new("src/target"))); | ||
85 | |||
86 | assert!(!filter.include_dir(RelativePath::new("target"))); | ||
87 | assert!(!filter.include_dir(RelativePath::new("src/foo/.git"))); | ||
88 | assert!(!filter.include_dir(RelativePath::new("foo/node_modules"))); | ||
89 | assert!(!filter.include_dir(RelativePath::new("tests"))); | ||
90 | assert!(!filter.include_dir(RelativePath::new("benches"))); | ||
91 | |||
92 | let filter = RustPackageFilterBuilder::default() | ||
93 | .set_member(true) | ||
94 | .exclude(std::iter::once(Glob::new("src/llvm-project/**").unwrap())) | ||
95 | .into_vfs_filter(); | ||
96 | |||
97 | assert!(!filter.include_dir(RelativePath::new("src/llvm-project/clang"))); | ||
98 | } | ||