aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_batch/src/vfs_filter.rs
blob: 63bf77704ad9aa689862d748185c09c5bed3afa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use ra_project_model::PackageRoot;
use ra_vfs::{Filter, RelativePath, RootEntry};
use std::path::PathBuf;

/// `IncludeRustFiles` is used to convert
/// from `PackageRoot` to `RootEntry` for VFS
pub struct IncludeRustFiles {
    root: PackageRoot,
}

impl IncludeRustFiles {
    pub fn from_roots<R>(roots: R) -> impl Iterator<Item = RootEntry>
    where
        R: IntoIterator<Item = PackageRoot>,
    {
        roots.into_iter().map(IncludeRustFiles::from_root)
    }

    pub fn from_root(root: PackageRoot) -> RootEntry {
        IncludeRustFiles::from(root).into()
    }

    #[allow(unused)]
    pub fn external(path: PathBuf) -> RootEntry {
        IncludeRustFiles::from_root(PackageRoot::new(path, false))
    }

    pub fn member(path: PathBuf) -> RootEntry {
        IncludeRustFiles::from_root(PackageRoot::new(path, true))
    }
}

impl Filter for IncludeRustFiles {
    fn include_dir(&self, dir_path: &RelativePath) -> bool {
        self.root.include_dir(dir_path)
    }

    fn include_file(&self, file_path: &RelativePath) -> bool {
        self.root.include_file(file_path)
    }
}

impl From<PackageRoot> for IncludeRustFiles {
    fn from(v: PackageRoot) -> IncludeRustFiles {
        IncludeRustFiles { root: v }
    }
}

impl From<IncludeRustFiles> for RootEntry {
    fn from(v: IncludeRustFiles) -> RootEntry {
        let path = v.root.path().clone();
        RootEntry::new(path, Box::new(v))
    }
}