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

69 lines
1.7 KiB
Rust
Raw Normal View History

2018-08-13 10:46:05 +00:00
use std::{
fs,
path::{Path, PathBuf},
2018-08-13 10:46:05 +00:00
};
use walkdir::WalkDir;
use crate::thread_watcher::{ThreadWatcher, Worker};
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),
}
2018-09-08 10:15:01 +00:00
pub fn roots_loader() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, ThreadWatcher) {
Worker::<PathBuf, (PathBuf, Vec<FileEvent>)>::spawn(
"roots loader",
128,
|input_receiver, output_sender| {
2018-09-08 10:15:01 +00:00
input_receiver
.map(|path| {
2018-12-06 18:03:39 +00:00
log::debug!("loading {} ...", path.as_path().display());
2018-09-08 10:15:01 +00:00
let events = load_root(path.as_path());
2018-12-06 18:03:39 +00:00
log::debug!("... loaded {}", path.as_path().display());
2018-09-08 10:15:01 +00:00
(path, events)
})
.for_each(|it| output_sender.send(it))
},
2018-09-08 10:15:01 +00:00
)
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) => {
2018-12-06 18:03:39 +00:00
log::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) => {
2018-12-06 18:03:39 +00:00
log::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
}