aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_vfs/src/io.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2018-12-18 13:38:05 +0000
committerAleksey Kladov <[email protected]>2018-12-20 09:15:38 +0000
commita422d480a188a28c6b5e7862fbf07817eb2c7447 (patch)
treed2a1945e49d1728f210c29ae8e88bffef19d22b7 /crates/ra_vfs/src/io.rs
parente69b05781f7fb0f0dfdcd4acb433dbcde9cbb7b7 (diff)
implement vfs events handling
Diffstat (limited to 'crates/ra_vfs/src/io.rs')
-rw-r--r--crates/ra_vfs/src/io.rs43
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 @@
1use std::{ 1use std::{
2 fs, 2 fs,
3 path::{Path, PathBuf}, 3 path::{Path, PathBuf},
4 thread::JoinHandle,
5}; 4};
6 5
7use walkdir::{DirEntry, WalkDir}; 6use walkdir::{DirEntry, WalkDir};
8use crossbeam_channel::{Sender, Receiver};
9use thread_worker::{WorkerHandle}; 7use thread_worker::{WorkerHandle};
8use relative_path::RelativePathBuf;
10 9
11use crate::VfsRoot; 10use crate::VfsRoot;
12 11
13pub(crate) enum Task { 12pub(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)]
22pub(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)] 18pub struct TaskResult {
28pub(crate) enum FileEventKind { 19 pub(crate) root: VfsRoot,
29 Add(String), 20 pub(crate) files: Vec<(RelativePathBuf, String)>,
30} 21}
31 22
32pub(crate) type Worker = thread_worker::Worker<Task, (PathBuf, Vec<FileEvent>)>; 23pub(crate) type Worker = thread_worker::Worker<Task, TaskResult>;
33 24
34pub(crate) fn start() -> (Worker, WorkerHandle) { 25pub(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
42fn handle_task(task: Task) -> (PathBuf, Vec<FileEvent>) { 33fn 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
50fn load_root(path: &Path) -> Vec<FileEvent> { 41fn 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}