diff options
Diffstat (limited to 'crates/ra_vfs/src/io.rs')
-rw-r--r-- | crates/ra_vfs/src/io.rs | 43 |
1 files changed, 16 insertions, 27 deletions
diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io.rs index c46760583..178c9beff 100644 --- a/crates/ra_vfs/src/io.rs +++ b/crates/ra_vfs/src/io.rs | |||
@@ -1,35 +1,26 @@ | |||
1 | use std::{ | 1 | use std::{ |
2 | fs, | 2 | fs, |
3 | path::{Path, PathBuf}, | 3 | path::{Path, PathBuf}, |
4 | thread::JoinHandle, | ||
5 | }; | 4 | }; |
6 | 5 | ||
7 | use walkdir::{DirEntry, WalkDir}; | 6 | use walkdir::{DirEntry, WalkDir}; |
8 | use crossbeam_channel::{Sender, Receiver}; | ||
9 | use thread_worker::{WorkerHandle}; | 7 | use thread_worker::{WorkerHandle}; |
8 | use relative_path::RelativePathBuf; | ||
10 | 9 | ||
11 | use crate::VfsRoot; | 10 | use crate::VfsRoot; |
12 | 11 | ||
13 | pub(crate) enum Task { | 12 | pub(crate) struct Task { |
14 | ScanRoot { | 13 | pub(crate) root: VfsRoot, |
15 | root: VfsRoot, | ||
16 | path: PathBuf, | ||
17 | filter: Box<FnMut(&DirEntry) -> bool + Send>, | ||
18 | }, | ||
19 | } | ||
20 | |||
21 | #[derive(Debug)] | ||
22 | pub(crate) struct FileEvent { | ||
23 | pub(crate) path: PathBuf, | 14 | pub(crate) path: PathBuf, |
24 | pub(crate) kind: FileEventKind, | 15 | pub(crate) filter: Box<Fn(&DirEntry) -> bool + Send>, |
25 | } | 16 | } |
26 | 17 | ||
27 | #[derive(Debug)] | 18 | pub struct TaskResult { |
28 | pub(crate) enum FileEventKind { | 19 | pub(crate) root: VfsRoot, |
29 | Add(String), | 20 | pub(crate) files: Vec<(RelativePathBuf, String)>, |
30 | } | 21 | } |
31 | 22 | ||
32 | pub(crate) type Worker = thread_worker::Worker<Task, (PathBuf, Vec<FileEvent>)>; | 23 | pub(crate) type Worker = thread_worker::Worker<Task, TaskResult>; |
33 | 24 | ||
34 | pub(crate) fn start() -> (Worker, WorkerHandle) { | 25 | pub(crate) fn start() -> (Worker, WorkerHandle) { |
35 | thread_worker::spawn("vfs", 128, |input_receiver, output_sender| { | 26 | thread_worker::spawn("vfs", 128, |input_receiver, output_sender| { |
@@ -39,17 +30,17 @@ pub(crate) fn start() -> (Worker, WorkerHandle) { | |||
39 | }) | 30 | }) |
40 | } | 31 | } |
41 | 32 | ||
42 | fn handle_task(task: Task) -> (PathBuf, Vec<FileEvent>) { | 33 | fn handle_task(task: Task) -> TaskResult { |
43 | let Task::ScanRoot { path, .. } = task; | 34 | let Task { root, path, filter } = task; |
44 | log::debug!("loading {} ...", path.as_path().display()); | 35 | log::debug!("loading {} ...", path.as_path().display()); |
45 | let events = load_root(path.as_path()); | 36 | let files = load_root(path.as_path(), &*filter); |
46 | log::debug!("... loaded {}", path.as_path().display()); | 37 | log::debug!("... loaded {}", path.as_path().display()); |
47 | (path, events) | 38 | TaskResult { root, files } |
48 | } | 39 | } |
49 | 40 | ||
50 | fn load_root(path: &Path) -> Vec<FileEvent> { | 41 | fn load_root(root: &Path, filter: &dyn Fn(&DirEntry) -> bool) -> Vec<(RelativePathBuf, String)> { |
51 | let mut res = Vec::new(); | 42 | let mut res = Vec::new(); |
52 | for entry in WalkDir::new(path) { | 43 | for entry in WalkDir::new(root).into_iter().filter_entry(filter) { |
53 | let entry = match entry { | 44 | let entry = match entry { |
54 | Ok(entry) => entry, | 45 | Ok(entry) => entry, |
55 | Err(e) => { | 46 | Err(e) => { |
@@ -71,10 +62,8 @@ fn load_root(path: &Path) -> Vec<FileEvent> { | |||
71 | continue; | 62 | continue; |
72 | } | 63 | } |
73 | }; | 64 | }; |
74 | res.push(FileEvent { | 65 | let path = RelativePathBuf::from_path(path.strip_prefix(root).unwrap()).unwrap(); |
75 | path: path.to_owned(), | 66 | res.push((path.to_owned(), text)) |
76 | kind: FileEventKind::Add(text), | ||
77 | }) | ||
78 | } | 67 | } |
79 | res | 68 | res |
80 | } | 69 | } |