From 0a086508524bed87bb15113437e9c2b1e1be4c42 Mon Sep 17 00:00:00 2001 From: Bernardo Date: Tue, 22 Jan 2019 18:14:31 +0100 Subject: hardcode ".git" and "node_modules" also --- crates/ra_vfs/src/io.rs | 233 ++++++++++++++++++++++++++++++++++++++++++++ crates/ra_vfs/src/io/mod.rs | 233 -------------------------------------------- crates/ra_vfs/src/lib.rs | 10 +- 3 files changed, 242 insertions(+), 234 deletions(-) create mode 100644 crates/ra_vfs/src/io.rs delete mode 100644 crates/ra_vfs/src/io/mod.rs (limited to 'crates/ra_vfs') diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io.rs new file mode 100644 index 000000000..3ab52ac3d --- /dev/null +++ b/crates/ra_vfs/src/io.rs @@ -0,0 +1,233 @@ +use std::{ + fmt, fs, + path::{Path, PathBuf}, + sync::Arc, + thread, +}; + +use crossbeam_channel::{Receiver, Sender}; +use parking_lot::Mutex; +use relative_path::RelativePathBuf; +use thread_worker::WorkerHandle; +use walkdir::WalkDir; + +mod watcher; +use watcher::Watcher; +pub use watcher::WatcherChange; + +use crate::{RootFilter, VfsRoot}; + +pub(crate) enum Task { + AddRoot { + root: VfsRoot, + path: PathBuf, + root_filter: Arc, + nested_roots: Vec, + }, + /// this variant should only be created by the watcher + HandleChange(WatcherChange), + LoadChange(WatcherChange), + Watch { + dir: PathBuf, + root_filter: Arc, + }, +} + +#[derive(Debug)] +pub struct AddRootResult { + pub(crate) root: VfsRoot, + pub(crate) files: Vec<(RelativePathBuf, String)>, +} + +#[derive(Debug)] +pub enum WatcherChangeData { + Create { path: PathBuf, text: String }, + Write { path: PathBuf, text: String }, + Remove { path: PathBuf }, +} + +pub enum TaskResult { + AddRoot(AddRootResult), + HandleChange(WatcherChange), + LoadChange(WatcherChangeData), + NoOp, +} + +impl fmt::Debug for TaskResult { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("TaskResult { ... }") + } +} + +pub(crate) struct Worker { + worker: thread_worker::Worker, + worker_handle: WorkerHandle, + watcher: Arc>>, +} + +impl Worker { + pub(crate) fn start() -> Worker { + let watcher = Arc::new(Mutex::new(None)); + let watcher_clone = watcher.clone(); + let (worker, worker_handle) = + thread_worker::spawn("vfs", 128, move |input_receiver, output_sender| { + input_receiver + .into_iter() + .map(|t| handle_task(t, &watcher_clone)) + .try_for_each(|it| output_sender.send(it)) + .unwrap() + }); + match Watcher::start(worker.inp.clone()) { + Ok(w) => { + watcher.lock().replace(w); + } + Err(e) => log::error!("could not start watcher: {}", e), + }; + Worker { + worker, + worker_handle, + watcher, + } + } + + pub(crate) fn sender(&self) -> &Sender { + &self.worker.inp + } + + pub(crate) fn receiver(&self) -> &Receiver { + &self.worker.out + } + + pub(crate) fn shutdown(self) -> thread::Result<()> { + if let Some(watcher) = self.watcher.lock().take() { + let _ = watcher.shutdown(); + } + let _ = self.worker.shutdown(); + self.worker_handle.shutdown() + } +} + +fn watch( + watcher: &Arc>>, + dir: &Path, + filter_entry: &RootFilter, + emit_for_existing: bool, +) { + let mut watcher = watcher.lock(); + let watcher = match *watcher { + Some(ref mut w) => w, + None => { + // watcher dropped or couldn't start + return; + } + }; + watcher.watch_recursive(dir, filter_entry, emit_for_existing) +} + +fn handle_task(task: Task, watcher: &Arc>>) -> TaskResult { + match task { + Task::AddRoot { + root, + path, + root_filter, + nested_roots, + } => { + watch(watcher, &path, root_filter.as_ref(), false); + log::debug!("loading {} ...", path.as_path().display()); + let files = load_root( + path.as_path(), + root_filter.as_ref(), + nested_roots.as_slice(), + ); + log::debug!("... loaded {}", path.as_path().display()); + TaskResult::AddRoot(AddRootResult { root, files }) + } + Task::HandleChange(change) => { + // forward as is because Vfs has to decide if we should load it + TaskResult::HandleChange(change) + } + Task::LoadChange(change) => { + log::debug!("loading {:?} ...", change); + match load_change(change) { + Some(data) => TaskResult::LoadChange(data), + None => TaskResult::NoOp, + } + } + Task::Watch { dir, root_filter } => { + watch(watcher, &dir, root_filter.as_ref(), true); + TaskResult::NoOp + } + } +} + +fn load_root( + root: &Path, + root_filter: &RootFilter, + nested_roots: &[PathBuf], +) -> Vec<(RelativePathBuf, String)> { + let mut res = Vec::new(); + for entry in WalkDir::new(root).into_iter().filter_entry(|entry| { + if entry.file_type().is_dir() && nested_roots.iter().any(|it| it == entry.path()) { + // do not load files of a nested root + false + } else { + root_filter.can_contain(entry.path()).is_some() + } + }) { + let entry = match entry { + Ok(entry) => entry, + Err(e) => { + log::warn!("watcher error: {}", e); + continue; + } + }; + if !entry.file_type().is_file() { + continue; + } + let path = entry.path(); + let text = match fs::read_to_string(path) { + Ok(text) => text, + Err(e) => { + log::warn!("watcher error: {}", e); + continue; + } + }; + let path = RelativePathBuf::from_path(path.strip_prefix(root).unwrap()).unwrap(); + res.push((path.to_owned(), text)) + } + res +} + +fn load_change(change: WatcherChange) -> Option { + let data = match change { + WatcherChange::Create(path) => { + if path.is_dir() { + return None; + } + let text = match fs::read_to_string(&path) { + Ok(text) => text, + Err(e) => { + log::warn!("watcher error \"{}\": {}", path.display(), e); + return None; + } + }; + WatcherChangeData::Create { path, text } + } + WatcherChange::Write(path) => { + let text = match fs::read_to_string(&path) { + Ok(text) => text, + Err(e) => { + log::warn!("watcher error \"{}\": {}", path.display(), e); + return None; + } + }; + WatcherChangeData::Write { path, text } + } + WatcherChange::Remove(path) => WatcherChangeData::Remove { path }, + WatcherChange::Rescan => { + // this should be handled by Vfs::handle_task + return None; + } + }; + Some(data) +} diff --git a/crates/ra_vfs/src/io/mod.rs b/crates/ra_vfs/src/io/mod.rs deleted file mode 100644 index daac6c6f2..000000000 --- a/crates/ra_vfs/src/io/mod.rs +++ /dev/null @@ -1,233 +0,0 @@ -use std::{ - fmt, fs, - path::{Path, PathBuf}, - sync::Arc, - thread, -}; - -use crossbeam_channel::{Receiver, Sender}; -use parking_lot::Mutex; -use relative_path::RelativePathBuf; -use thread_worker::WorkerHandle; -use walkdir::WalkDir; - -mod watcher; -use watcher::Watcher; -pub use watcher::WatcherChange; - -use crate::{RootFilter, VfsRoot}; - -pub(crate) enum Task { - AddRoot { - root: VfsRoot, - path: PathBuf, - root_filter: Arc, - nested_roots: Vec, - }, - /// this variant should only be created by the watcher - HandleChange(WatcherChange), - LoadChange(WatcherChange), - Watch { - dir: PathBuf, - root_filter: Arc, - }, -} - -#[derive(Debug)] -pub struct AddRootResult { - pub(crate) root: VfsRoot, - pub(crate) files: Vec<(RelativePathBuf, String)>, -} - -#[derive(Debug)] -pub enum WatcherChangeData { - Create { path: PathBuf, text: String }, - Write { path: PathBuf, text: String }, - Remove { path: PathBuf }, -} - -pub enum TaskResult { - AddRoot(AddRootResult), - HandleChange(WatcherChange), - LoadChange(WatcherChangeData), - NoOp, -} - -impl fmt::Debug for TaskResult { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str("TaskResult { ... }") - } -} - -pub(crate) struct Worker { - worker: thread_worker::Worker, - worker_handle: WorkerHandle, - watcher: Arc>>, -} - -impl Worker { - pub(crate) fn start() -> Worker { - let watcher = Arc::new(Mutex::new(None)); - let watcher_clone = watcher.clone(); - let (worker, worker_handle) = - thread_worker::spawn("vfs", 128, move |input_receiver, output_sender| { - input_receiver - .into_iter() - .map(|t| handle_task(t, &watcher_clone)) - .try_for_each(|it| output_sender.send(it)) - .unwrap() - }); - match Watcher::start(worker.inp.clone()) { - Ok(w) => { - watcher.lock().replace(w); - } - Err(e) => log::error!("could not start watcher: {}", e), - }; - Worker { - worker, - worker_handle, - watcher, - } - } - - pub(crate) fn sender(&self) -> &Sender { - &self.worker.inp - } - - pub(crate) fn receiver(&self) -> &Receiver { - &self.worker.out - } - - pub(crate) fn shutdown(self) -> thread::Result<()> { - if let Some(watcher) = self.watcher.lock().take() { - let _ = watcher.shutdown(); - } - let _ = self.worker.shutdown(); - self.worker_handle.shutdown() - } -} - -fn watch( - watcher: &Arc>>, - dir: &Path, - filter_entry: &RootFilter, - emit_for_existing: bool, -) { - let mut watcher = watcher.lock(); - let watcher = match *watcher { - Some(ref mut w) => w, - None => { - // watcher dropped or couldn't start - return; - } - }; - watcher.watch_recursive(dir, filter_entry, emit_for_existing) -} - -fn handle_task(task: Task, watcher: &Arc>>) -> TaskResult { - match task { - Task::AddRoot { - root, - path, - root_filter, - nested_roots, - } => { - watch(watcher, &path, &*root_filter, false); - log::debug!("loading {} ...", path.as_path().display()); - let files = load_root( - path.as_path(), - root_filter.as_ref(), - nested_roots.as_slice(), - ); - log::debug!("... loaded {}", path.as_path().display()); - TaskResult::AddRoot(AddRootResult { root, files }) - } - Task::HandleChange(change) => { - // forward as is because Vfs has to decide if we should load it - TaskResult::HandleChange(change) - } - Task::LoadChange(change) => { - log::debug!("loading {:?} ...", change); - match load_change(change) { - Some(data) => TaskResult::LoadChange(data), - None => TaskResult::NoOp, - } - } - Task::Watch { dir, root_filter } => { - watch(watcher, &dir, root_filter.as_ref(), true); - TaskResult::NoOp - } - } -} - -fn load_root( - root: &Path, - root_filter: &RootFilter, - nested_roots: &[PathBuf], -) -> Vec<(RelativePathBuf, String)> { - let mut res = Vec::new(); - for entry in WalkDir::new(root).into_iter().filter_entry(|entry| { - if entry.file_type().is_dir() && nested_roots.iter().any(|it| it == entry.path()) { - // do not load files of a nested root - false - } else { - root_filter.can_contain(entry.path()).is_some() - } - }) { - let entry = match entry { - Ok(entry) => entry, - Err(e) => { - log::warn!("watcher error: {}", e); - continue; - } - }; - if !entry.file_type().is_file() { - continue; - } - let path = entry.path(); - let text = match fs::read_to_string(path) { - Ok(text) => text, - Err(e) => { - log::warn!("watcher error: {}", e); - continue; - } - }; - let path = RelativePathBuf::from_path(path.strip_prefix(root).unwrap()).unwrap(); - res.push((path.to_owned(), text)) - } - res -} - -fn load_change(change: WatcherChange) -> Option { - let data = match change { - WatcherChange::Create(path) => { - if path.is_dir() { - return None; - } - let text = match fs::read_to_string(&path) { - Ok(text) => text, - Err(e) => { - log::warn!("watcher error \"{}\": {}", path.display(), e); - return None; - } - }; - WatcherChangeData::Create { path, text } - } - WatcherChange::Write(path) => { - let text = match fs::read_to_string(&path) { - Ok(text) => text, - Err(e) => { - log::warn!("watcher error \"{}\": {}", path.display(), e); - return None; - } - }; - WatcherChangeData::Write { path, text } - } - WatcherChange::Remove(path) => WatcherChangeData::Remove { path }, - WatcherChange::Rescan => { - // this should be handled by Vfs::handle_task - return None; - } - }; - Some(data) -} diff --git a/crates/ra_vfs/src/lib.rs b/crates/ra_vfs/src/lib.rs index f4447be43..4f3896a82 100644 --- a/crates/ra_vfs/src/lib.rs +++ b/crates/ra_vfs/src/lib.rs @@ -59,7 +59,15 @@ impl RootFilter { pub(crate) fn default_filter(path: &Path, rel_path: &RelativePath) -> bool { if path.is_dir() { - rel_path.components().next() != Some(Component::Normal("target")) + for (i, c) in rel_path.components().enumerate() { + if let Component::Normal(c) = c { + // hardcoded for now + if (i == 0 && c == "target") || c == ".git" || c == "node_modules" { + return false; + } + } + } + true } else { rel_path.extension() == Some("rs") } -- cgit v1.2.3