aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_vfs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_vfs')
-rw-r--r--crates/ra_vfs/Cargo.toml3
-rw-r--r--crates/ra_vfs/src/io.rs117
-rw-r--r--crates/ra_vfs/src/lib.rs25
3 files changed, 82 insertions, 63 deletions
diff --git a/crates/ra_vfs/Cargo.toml b/crates/ra_vfs/Cargo.toml
index f8d49b3f5..9ce619a77 100644
--- a/crates/ra_vfs/Cargo.toml
+++ b/crates/ra_vfs/Cargo.toml
@@ -9,3 +9,6 @@ walkdir = "2.2.7"
9relative-path = "0.4.0" 9relative-path = "0.4.0"
10rustc-hash = "1.0" 10rustc-hash = "1.0"
11crossbeam-channel = "0.2.4" 11crossbeam-channel = "0.2.4"
12log = "0.4.6"
13
14thread_worker = { path = "../thread_worker" }
diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io.rs
index f90fe0e84..ce3271d48 100644
--- a/crates/ra_vfs/src/io.rs
+++ b/crates/ra_vfs/src/io.rs
@@ -6,67 +6,64 @@ use std::{
6 6
7use walkdir::WalkDir; 7use walkdir::WalkDir;
8use crossbeam_channel::{Sender, Receiver}; 8use crossbeam_channel::{Sender, Receiver};
9use thread_worker::{WorkerHandle, Worker};
9 10
10pub(crate) fn start_io() -> (JoinHandle<(), Sender<()>, Receiver()>) {} 11#[derive(Debug)]
12pub struct FileEvent {
13 pub path: PathBuf,
14 pub kind: FileEventKind,
15}
11 16
12// use crate::thread_watcher::{ThreadWatcher, Worker}; 17#[derive(Debug)]
18pub enum FileEventKind {
19 Add(String),
20}
13 21
14// #[derive(Debug)] 22pub fn start() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, WorkerHandle) {
15// pub struct FileEvent { 23 thread_worker::spawn::<PathBuf, (PathBuf, Vec<FileEvent>), _>(
16// pub path: PathBuf, 24 "vfs",
17// pub kind: FileEventKind, 25 128,
18// } 26 |input_receiver, output_sender| {
27 input_receiver
28 .map(|path| {
29 log::debug!("loading {} ...", path.as_path().display());
30 let events = load_root(path.as_path());
31 log::debug!("... loaded {}", path.as_path().display());
32 (path, events)
33 })
34 .for_each(|it| output_sender.send(it))
35 },
36 )
37}
19 38
20// #[derive(Debug)] 39fn load_root(path: &Path) -> Vec<FileEvent> {
21// pub enum FileEventKind { 40 let mut res = Vec::new();
22// Add(String), 41 for entry in WalkDir::new(path) {
23// } 42 let entry = match entry {
24 43 Ok(entry) => entry,
25// pub fn roots_loader() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, ThreadWatcher) { 44 Err(e) => {
26// Worker::<PathBuf, (PathBuf, Vec<FileEvent>)>::spawn( 45 log::warn!("watcher error: {}", e);
27// "roots loader", 46 continue;
28// 128, 47 }
29// |input_receiver, output_sender| { 48 };
30// input_receiver 49 if !entry.file_type().is_file() {
31// .map(|path| { 50 continue;
32// log::debug!("loading {} ...", path.as_path().display()); 51 }
33// let events = load_root(path.as_path()); 52 let path = entry.path();
34// log::debug!("... loaded {}", path.as_path().display()); 53 if path.extension().and_then(|os| os.to_str()) != Some("rs") {
35// (path, events) 54 continue;
36// }) 55 }
37// .for_each(|it| output_sender.send(it)) 56 let text = match fs::read_to_string(path) {
38// }, 57 Ok(text) => text,
39// ) 58 Err(e) => {
40// } 59 log::warn!("watcher error: {}", e);
41 60 continue;
42// fn load_root(path: &Path) -> Vec<FileEvent> { 61 }
43// let mut res = Vec::new(); 62 };
44// for entry in WalkDir::new(path) { 63 res.push(FileEvent {
45// let entry = match entry { 64 path: path.to_owned(),
46// Ok(entry) => entry, 65 kind: FileEventKind::Add(text),
47// Err(e) => { 66 })
48// log::warn!("watcher error: {}", e); 67 }
49// continue; 68 res
50// } 69}
51// };
52// if !entry.file_type().is_file() {
53// continue;
54// }
55// let path = entry.path();
56// if path.extension().and_then(|os| os.to_str()) != Some("rs") {
57// continue;
58// }
59// let text = match fs::read_to_string(path) {
60// Ok(text) => text,
61// Err(e) => {
62// log::warn!("watcher error: {}", e);
63// continue;
64// }
65// };
66// res.push(FileEvent {
67// path: path.to_owned(),
68// kind: FileEventKind::Add(text),
69// })
70// }
71// res
72// }
diff --git a/crates/ra_vfs/src/lib.rs b/crates/ra_vfs/src/lib.rs
index 8f6abadb7..b80c12058 100644
--- a/crates/ra_vfs/src/lib.rs
+++ b/crates/ra_vfs/src/lib.rs
@@ -15,6 +15,7 @@ mod arena;
15mod io; 15mod io;
16 16
17use std::{ 17use std::{
18 thread,
18 cmp::Reverse, 19 cmp::Reverse,
19 path::{Path, PathBuf}, 20 path::{Path, PathBuf},
20 ffi::OsStr, 21 ffi::OsStr,
@@ -22,7 +23,12 @@ use std::{
22}; 23};
23 24
24use relative_path::RelativePathBuf; 25use relative_path::RelativePathBuf;
25use crate::arena::{ArenaId, Arena}; 26use thread_worker::{WorkerHandle, Worker};
27
28use crate::{
29 arena::{ArenaId, Arena},
30 io::FileEvent,
31};
26 32
27/// `RootFilter` is a predicate that checks if a file can belong to a root 33/// `RootFilter` is a predicate that checks if a file can belong to a root
28struct RootFilter { 34struct RootFilter {
@@ -76,16 +82,24 @@ struct VfsFileData {
76 text: Arc<String>, 82 text: Arc<String>,
77} 83}
78 84
79#[derive(Default)]
80struct Vfs { 85struct Vfs {
81 roots: Arena<VfsRoot, RootFilter>, 86 roots: Arena<VfsRoot, RootFilter>,
82 files: Arena<VfsFile, VfsFileData>, 87 files: Arena<VfsFile, VfsFileData>,
83 // pending_changes: Vec<PendingChange>, 88 // pending_changes: Vec<PendingChange>,
89 worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>,
90 worker_handle: WorkerHandle,
84} 91}
85 92
86impl Vfs { 93impl Vfs {
87 pub fn new(mut roots: Vec<PathBuf>) -> Vfs { 94 pub fn new(mut roots: Vec<PathBuf>) -> Vfs {
88 let mut res = Vfs::default(); 95 let (worker, worker_handle) = io::start();
96
97 let mut res = Vfs {
98 roots: Arena::default(),
99 files: Arena::default(),
100 worker,
101 worker_handle,
102 };
89 103
90 roots.sort_by_key(|it| Reverse(it.as_os_str().len())); 104 roots.sort_by_key(|it| Reverse(it.as_os_str().len()));
91 105
@@ -104,6 +118,11 @@ impl Vfs {
104 pub fn commit_changes(&mut self) -> Vec<VfsChange> { 118 pub fn commit_changes(&mut self) -> Vec<VfsChange> {
105 unimplemented!() 119 unimplemented!()
106 } 120 }
121
122 pub fn shutdown(self) -> thread::Result<()> {
123 let _ = self.worker.shutdown();
124 self.worker_handle.shutdown()
125 }
107} 126}
108 127
109#[derive(Debug, Clone)] 128#[derive(Debug, Clone)]