aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_vfs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_vfs')
-rw-r--r--crates/ra_vfs/src/io/mod.rs (renamed from crates/ra_vfs/src/io.rs)10
-rw-r--r--crates/ra_vfs/src/io/watcher.rs (renamed from crates/ra_vfs/src/watcher.rs)14
-rw-r--r--crates/ra_vfs/src/lib.rs36
3 files changed, 29 insertions, 31 deletions
diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io/mod.rs
index a74222c02..6d5af7690 100644
--- a/crates/ra_vfs/src/io.rs
+++ b/crates/ra_vfs/src/io/mod.rs
@@ -11,10 +11,11 @@ use relative_path::RelativePathBuf;
11use thread_worker::WorkerHandle; 11use thread_worker::WorkerHandle;
12use walkdir::{DirEntry, WalkDir}; 12use walkdir::{DirEntry, WalkDir};
13 13
14use crate::{ 14mod watcher;
15 watcher::{Watcher, WatcherChange}, 15use watcher::Watcher;
16 VfsRoot, 16pub use watcher::WatcherChange;
17}; 17
18use crate::VfsRoot;
18 19
19pub(crate) enum Task { 20pub(crate) enum Task {
20 AddRoot { 21 AddRoot {
@@ -22,6 +23,7 @@ pub(crate) enum Task {
22 path: PathBuf, 23 path: PathBuf,
23 filter: Box<Fn(&DirEntry) -> bool + Send>, 24 filter: Box<Fn(&DirEntry) -> bool + Send>,
24 }, 25 },
26 /// this variant should only be created by the watcher
25 HandleChange(WatcherChange), 27 HandleChange(WatcherChange),
26 LoadChange(WatcherChange), 28 LoadChange(WatcherChange),
27 Watch { 29 Watch {
diff --git a/crates/ra_vfs/src/watcher.rs b/crates/ra_vfs/src/io/watcher.rs
index 606935891..e33298477 100644
--- a/crates/ra_vfs/src/watcher.rs
+++ b/crates/ra_vfs/src/io/watcher.rs
@@ -10,13 +10,6 @@ use std::{
10}; 10};
11use walkdir::{DirEntry, WalkDir}; 11use walkdir::{DirEntry, WalkDir};
12 12
13pub(crate) struct Watcher {
14 watcher: RecommendedWatcher,
15 thread: thread::JoinHandle<()>,
16 bomb: DropBomb,
17 sender: Sender<io::Task>,
18}
19
20#[derive(Debug)] 13#[derive(Debug)]
21pub enum WatcherChange { 14pub enum WatcherChange {
22 Create(PathBuf), 15 Create(PathBuf),
@@ -61,6 +54,13 @@ fn handle_change_event(
61 54
62const WATCHER_DELAY: Duration = Duration::from_millis(250); 55const WATCHER_DELAY: Duration = Duration::from_millis(250);
63 56
57pub(crate) struct Watcher {
58 watcher: RecommendedWatcher,
59 thread: thread::JoinHandle<()>,
60 bomb: DropBomb,
61 sender: Sender<io::Task>,
62}
63
64impl Watcher { 64impl Watcher {
65 pub(crate) fn start( 65 pub(crate) fn start(
66 output_sender: Sender<io::Task>, 66 output_sender: Sender<io::Task>,
diff --git a/crates/ra_vfs/src/lib.rs b/crates/ra_vfs/src/lib.rs
index 196180890..5db0d8646 100644
--- a/crates/ra_vfs/src/lib.rs
+++ b/crates/ra_vfs/src/lib.rs
@@ -14,7 +14,6 @@
14//! which are watched for changes. Typically, there will be a root for each 14//! which are watched for changes. Typically, there will be a root for each
15//! Cargo package. 15//! Cargo package.
16mod io; 16mod io;
17mod watcher;
18 17
19use std::{ 18use std::{
20 cmp::Reverse, 19 cmp::Reverse,
@@ -32,7 +31,7 @@ use rustc_hash::{FxHashMap, FxHashSet};
32use walkdir::DirEntry; 31use walkdir::DirEntry;
33 32
34pub use crate::io::TaskResult as VfsTask; 33pub use crate::io::TaskResult as VfsTask;
35pub use crate::watcher::WatcherChange; 34use io::{Task, TaskResult, WatcherChange, WatcherChangeData, Worker};
36 35
37/// `RootFilter` is a predicate that checks if a file can belong to a root. If 36/// `RootFilter` is a predicate that checks if a file can belong to a root. If
38/// several filters match a file (nested dirs), the most nested one wins. 37/// several filters match a file (nested dirs), the most nested one wins.
@@ -100,7 +99,7 @@ pub struct Vfs {
100 files: Arena<VfsFile, VfsFileData>, 99 files: Arena<VfsFile, VfsFileData>,
101 root2files: FxHashMap<VfsRoot, FxHashSet<VfsFile>>, 100 root2files: FxHashMap<VfsRoot, FxHashSet<VfsFile>>,
102 pending_changes: Vec<VfsChange>, 101 pending_changes: Vec<VfsChange>,
103 worker: io::Worker, 102 worker: Worker,
104} 103}
105 104
106impl fmt::Debug for Vfs { 105impl fmt::Debug for Vfs {
@@ -204,7 +203,7 @@ impl Vfs {
204 203
205 pub fn handle_task(&mut self, task: io::TaskResult) { 204 pub fn handle_task(&mut self, task: io::TaskResult) {
206 match task { 205 match task {
207 io::TaskResult::AddRoot(task) => { 206 TaskResult::AddRoot(task) => {
208 let mut files = Vec::new(); 207 let mut files = Vec::new();
209 // While we were scanning the root in the backgound, a file might have 208 // While we were scanning the root in the backgound, a file might have
210 // been open in the editor, so we need to account for that. 209 // been open in the editor, so we need to account for that.
@@ -229,38 +228,35 @@ impl Vfs {
229 }; 228 };
230 self.pending_changes.push(change); 229 self.pending_changes.push(change);
231 } 230 }
232 io::TaskResult::HandleChange(change) => match &change { 231 TaskResult::HandleChange(change) => match &change {
233 watcher::WatcherChange::Create(path) if path.is_dir() => { 232 WatcherChange::Create(path) if path.is_dir() => {
234 if let Some((root, _path, _file)) = self.find_root(&path) { 233 if let Some((root, _path, _file)) = self.find_root(&path) {
235 let root_filter = self.roots[root].clone(); 234 let root_filter = self.roots[root].clone();
236 let filter = 235 let filter =
237 move |entry: &DirEntry| root_filter.can_contain(entry.path()).is_some(); 236 move |entry: &DirEntry| root_filter.can_contain(entry.path()).is_some();
238 self.worker 237 self.worker
239 .sender() 238 .sender()
240 .send(io::Task::Watch { 239 .send(Task::Watch {
241 dir: path.to_path_buf(), 240 dir: path.to_path_buf(),
242 filter: Box::new(filter), 241 filter: Box::new(filter),
243 }) 242 })
244 .unwrap() 243 .unwrap()
245 } 244 }
246 } 245 }
247 watcher::WatcherChange::Create(path) 246 WatcherChange::Create(path)
248 | watcher::WatcherChange::Remove(path) 247 | WatcherChange::Remove(path)
249 | watcher::WatcherChange::Write(path) => { 248 | WatcherChange::Write(path) => {
250 if self.should_handle_change(&path) { 249 if self.should_handle_change(&path) {
251 self.worker 250 self.worker.sender().send(Task::LoadChange(change)).unwrap()
252 .sender()
253 .send(io::Task::LoadChange(change))
254 .unwrap()
255 } 251 }
256 } 252 }
257 watcher::WatcherChange::Rescan => { 253 WatcherChange::Rescan => {
258 // TODO we should reload all files 254 // TODO we should reload all files
259 } 255 }
260 }, 256 },
261 io::TaskResult::LoadChange(change) => match change { 257 TaskResult::LoadChange(change) => match change {
262 io::WatcherChangeData::Create { path, text } 258 WatcherChangeData::Create { path, text }
263 | io::WatcherChangeData::Write { path, text } => { 259 | WatcherChangeData::Write { path, text } => {
264 if let Some((root, path, file)) = self.find_root(&path) { 260 if let Some((root, path, file)) = self.find_root(&path) {
265 if let Some(file) = file { 261 if let Some(file) = file {
266 self.do_change_file(file, text, false); 262 self.do_change_file(file, text, false);
@@ -269,7 +265,7 @@ impl Vfs {
269 } 265 }
270 } 266 }
271 } 267 }
272 io::WatcherChangeData::Remove { path } => { 268 WatcherChangeData::Remove { path } => {
273 if let Some((root, path, file)) = self.find_root(&path) { 269 if let Some((root, path, file)) = self.find_root(&path) {
274 if let Some(file) = file { 270 if let Some(file) = file {
275 self.do_remove_file(root, path, file, false); 271 self.do_remove_file(root, path, file, false);
@@ -277,7 +273,7 @@ impl Vfs {
277 } 273 }
278 } 274 }
279 }, 275 },
280 io::TaskResult::NoOp => {} 276 TaskResult::NoOp => {}
281 } 277 }
282 } 278 }
283 279