aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_vfs/src
diff options
context:
space:
mode:
authorBernardo <[email protected]>2019-01-21 21:12:34 +0000
committerAleksey Kladov <[email protected]>2019-01-26 08:46:27 +0000
commit10a24cf649b4e136bb4f25cd295c2fb15125d71a (patch)
tree3f953a565964882f114436e5be87451d3f452ff5 /crates/ra_vfs/src
parenteeed6cf53b9f6112329cc8a274dcf63bce887c50 (diff)
simplify and optimize `RootFilter`by determining if is contained first
Diffstat (limited to 'crates/ra_vfs/src')
-rw-r--r--crates/ra_vfs/src/lib.rs35
1 files changed, 11 insertions, 24 deletions
diff --git a/crates/ra_vfs/src/lib.rs b/crates/ra_vfs/src/lib.rs
index 1d0af6a09..f4447be43 100644
--- a/crates/ra_vfs/src/lib.rs
+++ b/crates/ra_vfs/src/lib.rs
@@ -17,16 +17,15 @@ mod io;
17 17
18use std::{ 18use std::{
19 cmp::Reverse, 19 cmp::Reverse,
20 ffi::OsStr,
21 fmt, fs, mem, 20 fmt, fs, mem,
22 path::{Component, Path, PathBuf}, 21 path::{Path, PathBuf},
23 sync::Arc, 22 sync::Arc,
24 thread, 23 thread,
25}; 24};
26 25
27use crossbeam_channel::Receiver; 26use crossbeam_channel::Receiver;
28use ra_arena::{impl_arena_id, Arena, RawId}; 27use ra_arena::{impl_arena_id, Arena, RawId};
29use relative_path::RelativePathBuf; 28use relative_path::{Component, RelativePath, RelativePathBuf};
30use rustc_hash::{FxHashMap, FxHashSet}; 29use rustc_hash::{FxHashMap, FxHashSet};
31 30
32pub use crate::io::TaskResult as VfsTask; 31pub use crate::io::TaskResult as VfsTask;
@@ -36,12 +35,7 @@ use io::{Task, TaskResult, WatcherChange, WatcherChangeData, Worker};
36/// several filters match a file (nested dirs), the most nested one wins. 35/// several filters match a file (nested dirs), the most nested one wins.
37pub(crate) struct RootFilter { 36pub(crate) struct RootFilter {
38 root: PathBuf, 37 root: PathBuf,
39 filter: fn(RootEntry) -> bool, 38 filter: fn(&Path, &RelativePath) -> bool,
40}
41
42pub(crate) struct RootEntry<'a, 'b> {
43 root: &'a Path,
44 path: &'b Path,
45} 39}
46 40
47impl RootFilter { 41impl RootFilter {
@@ -54,27 +48,20 @@ impl RootFilter {
54 /// Check if this root can contain `path`. NB: even if this returns 48 /// Check if this root can contain `path`. NB: even if this returns
55 /// true, the `path` might actually be conained in some nested root. 49 /// true, the `path` might actually be conained in some nested root.
56 pub(crate) fn can_contain(&self, path: &Path) -> Option<RelativePathBuf> { 50 pub(crate) fn can_contain(&self, path: &Path) -> Option<RelativePathBuf> {
57 if !(self.filter)(RootEntry { 51 let rel_path = path.strip_prefix(&self.root).ok()?;
58 root: &self.root, 52 let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
59 path, 53 if !(self.filter)(path, rel_path.as_relative_path()) {
60 }) {
61 return None; 54 return None;
62 } 55 }
63 let path = path.strip_prefix(&self.root).ok()?; 56 Some(rel_path)
64 RelativePathBuf::from_path(path).ok()
65 } 57 }
66} 58}
67 59
68pub(crate) fn default_filter(entry: RootEntry) -> bool { 60pub(crate) fn default_filter(path: &Path, rel_path: &RelativePath) -> bool {
69 if entry.path.is_dir() { 61 if path.is_dir() {
70 // first component relative to root is "target" 62 rel_path.components().next() != Some(Component::Normal("target"))
71 entry
72 .path
73 .strip_prefix(entry.root)
74 .map(|p| p.components().next() != Some(Component::Normal(OsStr::new("target"))))
75 .unwrap_or(false)
76 } else { 63 } else {
77 entry.path.extension() == Some(OsStr::new("rs")) 64 rel_path.extension() == Some("rs")
78 } 65 }
79} 66}
80 67