aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_vfs/src/roots.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_vfs/src/roots.rs')
-rw-r--r--crates/ra_vfs/src/roots.rs102
1 files changed, 102 insertions, 0 deletions
diff --git a/crates/ra_vfs/src/roots.rs b/crates/ra_vfs/src/roots.rs
new file mode 100644
index 000000000..564e12239
--- /dev/null
+++ b/crates/ra_vfs/src/roots.rs
@@ -0,0 +1,102 @@
1use std::{
2 sync::Arc,
3 path::{Path, PathBuf},
4};
5
6use relative_path::RelativePathBuf;
7use ra_arena::{impl_arena_id, Arena, RawId};
8
9#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct VfsRoot(pub RawId);
11impl_arena_id!(VfsRoot);
12
13/// Describes the contents of a single source root.
14///
15/// `RootConfig` can be thought of as a glob pattern like `src/**.rs` which
16/// specifies the source root or as a function which takes a `PathBuf` and
17/// returns `true` iff path belongs to the source root
18pub(crate) struct RootConfig {
19 pub(crate) root: PathBuf,
20 // result of `root.canonicalize()` if that differs from `root`; `None` otherwise.
21 canonical_root: Option<PathBuf>,
22 excluded_dirs: Vec<PathBuf>,
23}
24
25pub(crate) struct Roots {
26 roots: Arena<VfsRoot, Arc<RootConfig>>,
27}
28
29impl std::ops::Deref for Roots {
30 type Target = Arena<VfsRoot, Arc<RootConfig>>;
31 fn deref(&self) -> &Self::Target {
32 &self.roots
33 }
34}
35
36impl RootConfig {
37 fn new(root: PathBuf, excluded_dirs: Vec<PathBuf>) -> RootConfig {
38 let mut canonical_root = root.canonicalize().ok();
39 if Some(&root) == canonical_root.as_ref() {
40 canonical_root = None;
41 }
42 RootConfig { root, canonical_root, excluded_dirs }
43 }
44 /// Checks if root contains a path and returns a root-relative path.
45 pub(crate) fn contains(&self, path: &Path) -> Option<RelativePathBuf> {
46 // First, check excluded dirs
47 if self.excluded_dirs.iter().any(|it| path.starts_with(it)) {
48 return None;
49 }
50 let rel_path = path
51 .strip_prefix(&self.root)
52 .or_else(|err_payload| {
53 self.canonical_root
54 .as_ref()
55 .map_or(Err(err_payload), |canonical_root| path.strip_prefix(canonical_root))
56 })
57 .ok()?;
58 let rel_path = RelativePathBuf::from_path(rel_path).ok()?;
59
60 // Ignore some common directories.
61 //
62 // FIXME: don't hard-code, specify at source-root creation time using
63 // gitignore
64 for (i, c) in rel_path.components().enumerate() {
65 if let relative_path::Component::Normal(c) = c {
66 if (i == 0 && c == "target") || c == ".git" || c == "node_modules" {
67 return None;
68 }
69 }
70 }
71
72 if path.is_file() && rel_path.extension() != Some("rs") {
73 return None;
74 }
75
76 Some(rel_path)
77 }
78}
79
80impl Roots {
81 pub(crate) fn new(mut paths: Vec<PathBuf>) -> Roots {
82 let mut roots = Arena::default();
83 // A hack to make nesting work.
84 paths.sort_by_key(|it| std::cmp::Reverse(it.as_os_str().len()));
85 paths.dedup();
86 for (i, path) in paths.iter().enumerate() {
87 let nested_roots = paths[..i]
88 .iter()
89 .filter(|it| it.starts_with(path))
90 .map(|it| it.clone())
91 .collect::<Vec<_>>();
92
93 let config = Arc::new(RootConfig::new(path.clone(), nested_roots));
94
95 roots.alloc(config.clone());
96 }
97 Roots { roots }
98 }
99 pub(crate) fn find(&self, path: &Path) -> Option<(VfsRoot, RelativePathBuf)> {
100 self.roots.iter().find_map(|(root, data)| data.contains(path).map(|it| (root, it)))
101 }
102}