rust-analyzer/crates/server/src/vfs.rs

74 lines
1.8 KiB
Rust
Raw Normal View History

2018-08-13 10:46:05 +00:00
use std::{
2018-09-03 18:03:37 +00:00
path::{PathBuf, Path},
2018-08-13 10:46:05 +00:00
fs,
};
use crossbeam_channel::{Sender, Receiver};
2018-08-13 10:46:05 +00:00
use walkdir::WalkDir;
2018-09-02 11:46:15 +00:00
use {
thread_watcher::{ThreadWatcher, worker_chan},
2018-09-02 11:46:15 +00:00
};
2018-08-13 10:46:05 +00:00
2018-09-01 17:21:11 +00:00
#[derive(Debug)]
2018-08-13 10:46:05 +00:00
pub struct FileEvent {
pub path: PathBuf,
pub kind: FileEventKind,
}
2018-09-01 17:21:11 +00:00
#[derive(Debug)]
2018-08-13 10:46:05 +00:00
pub enum FileEventKind {
Add(String),
}
pub fn roots_loader() -> ((Sender<PathBuf>, Receiver<(PathBuf, Vec<FileEvent>)>), ThreadWatcher) {
let (interface, input_receiver, output_sender) =
worker_chan::<PathBuf, (PathBuf, Vec<FileEvent>)>(128);
2018-09-03 18:03:37 +00:00
let thread = ThreadWatcher::spawn("roots loader", move || {
input_receiver
2018-09-03 18:03:37 +00:00
.into_iter()
.map(|path| {
debug!("loading {} ...", path.as_path().display());
let events = load_root(path.as_path());
debug!("... loaded {}", path.as_path().display());
(path, events)
})
.for_each(|it| output_sender.send(it))
2018-09-03 18:03:37 +00:00
});
(interface, thread)
2018-08-13 10:46:05 +00:00
}
2018-09-03 18:03:37 +00:00
fn load_root(path: &Path) -> Vec<FileEvent> {
let mut res = Vec::new();
for entry in WalkDir::new(path) {
let entry = match entry {
Ok(entry) => entry,
Err(e) => {
warn!("watcher error: {}", e);
2018-08-13 10:46:05 +00:00
continue;
}
2018-09-03 18:03:37 +00:00
};
if !entry.file_type().is_file() {
continue;
}
let path = entry.path();
if path.extension().and_then(|os| os.to_str()) != Some("rs") {
continue;
}
let text = match fs::read_to_string(path) {
Ok(text) => text,
Err(e) => {
warn!("watcher error: {}", e);
2018-08-13 10:46:05 +00:00
continue;
}
2018-09-03 18:03:37 +00:00
};
res.push(FileEvent {
path: path.to_owned(),
kind: FileEventKind::Add(text),
})
2018-08-13 10:46:05 +00:00
}
2018-09-03 18:03:37 +00:00
res
2018-08-13 10:46:05 +00:00
}