From 7509901fa0985f8fc4893a83e0275a063f072dda Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Dec 2018 12:29:14 +0300 Subject: wip --- crates/ra_vfs/src/io.rs | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 crates/ra_vfs/src/io.rs (limited to 'crates/ra_vfs/src/io.rs') diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io.rs new file mode 100644 index 000000000..f90fe0e84 --- /dev/null +++ b/crates/ra_vfs/src/io.rs @@ -0,0 +1,72 @@ +use std::{ + fs, + path::{Path, PathBuf}, + thread::JoinHandle, +}; + +use walkdir::WalkDir; +use crossbeam_channel::{Sender, Receiver}; + +pub(crate) fn start_io() -> (JoinHandle<(), Sender<()>, Receiver()>) {} + +// use crate::thread_watcher::{ThreadWatcher, Worker}; + +// #[derive(Debug)] +// pub struct FileEvent { +// pub path: PathBuf, +// pub kind: FileEventKind, +// } + +// #[derive(Debug)] +// pub enum FileEventKind { +// Add(String), +// } + +// pub fn roots_loader() -> (Worker)>, ThreadWatcher) { +// Worker::)>::spawn( +// "roots loader", +// 128, +// |input_receiver, output_sender| { +// input_receiver +// .map(|path| { +// log::debug!("loading {} ...", path.as_path().display()); +// let events = load_root(path.as_path()); +// log::debug!("... loaded {}", path.as_path().display()); +// (path, events) +// }) +// .for_each(|it| output_sender.send(it)) +// }, +// ) +// } + +// fn load_root(path: &Path) -> Vec { +// let mut res = Vec::new(); +// for entry in WalkDir::new(path) { +// 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(); +// 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) => { +// log::warn!("watcher error: {}", e); +// continue; +// } +// }; +// res.push(FileEvent { +// path: path.to_owned(), +// kind: FileEventKind::Add(text), +// }) +// } +// res +// } -- cgit v1.2.3 From 2ae05a6163d8b15f3d8a18a2ab713d1fbd83c505 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Dec 2018 13:18:55 +0300 Subject: vfs crate scaffold --- crates/ra_vfs/src/io.rs | 117 +++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 60 deletions(-) (limited to 'crates/ra_vfs/src/io.rs') 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::{ use walkdir::WalkDir; use crossbeam_channel::{Sender, Receiver}; +use thread_worker::{WorkerHandle, Worker}; -pub(crate) fn start_io() -> (JoinHandle<(), Sender<()>, Receiver()>) {} +#[derive(Debug)] +pub struct FileEvent { + pub path: PathBuf, + pub kind: FileEventKind, +} -// use crate::thread_watcher::{ThreadWatcher, Worker}; +#[derive(Debug)] +pub enum FileEventKind { + Add(String), +} -// #[derive(Debug)] -// pub struct FileEvent { -// pub path: PathBuf, -// pub kind: FileEventKind, -// } +pub fn start() -> (Worker)>, WorkerHandle) { + thread_worker::spawn::), _>( + "vfs", + 128, + |input_receiver, output_sender| { + input_receiver + .map(|path| { + log::debug!("loading {} ...", path.as_path().display()); + let events = load_root(path.as_path()); + log::debug!("... loaded {}", path.as_path().display()); + (path, events) + }) + .for_each(|it| output_sender.send(it)) + }, + ) +} -// #[derive(Debug)] -// pub enum FileEventKind { -// Add(String), -// } - -// pub fn roots_loader() -> (Worker)>, ThreadWatcher) { -// Worker::)>::spawn( -// "roots loader", -// 128, -// |input_receiver, output_sender| { -// input_receiver -// .map(|path| { -// log::debug!("loading {} ...", path.as_path().display()); -// let events = load_root(path.as_path()); -// log::debug!("... loaded {}", path.as_path().display()); -// (path, events) -// }) -// .for_each(|it| output_sender.send(it)) -// }, -// ) -// } - -// fn load_root(path: &Path) -> Vec { -// let mut res = Vec::new(); -// for entry in WalkDir::new(path) { -// 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(); -// 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) => { -// log::warn!("watcher error: {}", e); -// continue; -// } -// }; -// res.push(FileEvent { -// path: path.to_owned(), -// kind: FileEventKind::Add(text), -// }) -// } -// res -// } +fn load_root(path: &Path) -> Vec { + let mut res = Vec::new(); + for entry in WalkDir::new(path) { + 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(); + 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) => { + log::warn!("watcher error: {}", e); + continue; + } + }; + res.push(FileEvent { + path: path.to_owned(), + kind: FileEventKind::Add(text), + }) + } + res +} -- cgit v1.2.3 From 99561cf2f20ad572c7149644a70cd7740c0f8b86 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Dec 2018 13:23:23 +0300 Subject: Add type alias --- crates/ra_vfs/src/io.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'crates/ra_vfs/src/io.rs') diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io.rs index ce3271d48..257f1bf53 100644 --- a/crates/ra_vfs/src/io.rs +++ b/crates/ra_vfs/src/io.rs @@ -19,7 +19,9 @@ pub enum FileEventKind { Add(String), } -pub fn start() -> (Worker)>, WorkerHandle) { +pub(crate) type FsWorker = Worker)>; + +pub(crate) fn start() -> (FsWorker, WorkerHandle) { thread_worker::spawn::), _>( "vfs", 128, -- cgit v1.2.3 From e69b05781f7fb0f0dfdcd4acb433dbcde9cbb7b7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Dec 2018 13:35:05 +0300 Subject: add io::Task --- crates/ra_vfs/src/io.rs | 53 +++++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 22 deletions(-) (limited to 'crates/ra_vfs/src/io.rs') diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io.rs index 257f1bf53..c46760583 100644 --- a/crates/ra_vfs/src/io.rs +++ b/crates/ra_vfs/src/io.rs @@ -4,38 +4,47 @@ use std::{ thread::JoinHandle, }; -use walkdir::WalkDir; +use walkdir::{DirEntry, WalkDir}; use crossbeam_channel::{Sender, Receiver}; -use thread_worker::{WorkerHandle, Worker}; +use thread_worker::{WorkerHandle}; + +use crate::VfsRoot; + +pub(crate) enum Task { + ScanRoot { + root: VfsRoot, + path: PathBuf, + filter: Box bool + Send>, + }, +} #[derive(Debug)] -pub struct FileEvent { - pub path: PathBuf, - pub kind: FileEventKind, +pub(crate) struct FileEvent { + pub(crate) path: PathBuf, + pub(crate) kind: FileEventKind, } #[derive(Debug)] -pub enum FileEventKind { +pub(crate) enum FileEventKind { Add(String), } -pub(crate) type FsWorker = Worker)>; +pub(crate) type Worker = thread_worker::Worker)>; + +pub(crate) fn start() -> (Worker, WorkerHandle) { + thread_worker::spawn("vfs", 128, |input_receiver, output_sender| { + input_receiver + .map(handle_task) + .for_each(|it| output_sender.send(it)) + }) +} -pub(crate) fn start() -> (FsWorker, WorkerHandle) { - thread_worker::spawn::), _>( - "vfs", - 128, - |input_receiver, output_sender| { - input_receiver - .map(|path| { - log::debug!("loading {} ...", path.as_path().display()); - let events = load_root(path.as_path()); - log::debug!("... loaded {}", path.as_path().display()); - (path, events) - }) - .for_each(|it| output_sender.send(it)) - }, - ) +fn handle_task(task: Task) -> (PathBuf, Vec) { + let Task::ScanRoot { path, .. } = task; + log::debug!("loading {} ...", path.as_path().display()); + let events = load_root(path.as_path()); + log::debug!("... loaded {}", path.as_path().display()); + (path, events) } fn load_root(path: &Path) -> Vec { -- cgit v1.2.3 From a422d480a188a28c6b5e7862fbf07817eb2c7447 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 18 Dec 2018 16:38:05 +0300 Subject: implement vfs events handling --- crates/ra_vfs/src/io.rs | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) (limited to 'crates/ra_vfs/src/io.rs') diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io.rs index c46760583..178c9beff 100644 --- a/crates/ra_vfs/src/io.rs +++ b/crates/ra_vfs/src/io.rs @@ -1,35 +1,26 @@ use std::{ fs, path::{Path, PathBuf}, - thread::JoinHandle, }; use walkdir::{DirEntry, WalkDir}; -use crossbeam_channel::{Sender, Receiver}; use thread_worker::{WorkerHandle}; +use relative_path::RelativePathBuf; use crate::VfsRoot; -pub(crate) enum Task { - ScanRoot { - root: VfsRoot, - path: PathBuf, - filter: Box bool + Send>, - }, -} - -#[derive(Debug)] -pub(crate) struct FileEvent { +pub(crate) struct Task { + pub(crate) root: VfsRoot, pub(crate) path: PathBuf, - pub(crate) kind: FileEventKind, + pub(crate) filter: Box bool + Send>, } -#[derive(Debug)] -pub(crate) enum FileEventKind { - Add(String), +pub struct TaskResult { + pub(crate) root: VfsRoot, + pub(crate) files: Vec<(RelativePathBuf, String)>, } -pub(crate) type Worker = thread_worker::Worker)>; +pub(crate) type Worker = thread_worker::Worker; pub(crate) fn start() -> (Worker, WorkerHandle) { thread_worker::spawn("vfs", 128, |input_receiver, output_sender| { @@ -39,17 +30,17 @@ pub(crate) fn start() -> (Worker, WorkerHandle) { }) } -fn handle_task(task: Task) -> (PathBuf, Vec) { - let Task::ScanRoot { path, .. } = task; +fn handle_task(task: Task) -> TaskResult { + let Task { root, path, filter } = task; log::debug!("loading {} ...", path.as_path().display()); - let events = load_root(path.as_path()); + let files = load_root(path.as_path(), &*filter); log::debug!("... loaded {}", path.as_path().display()); - (path, events) + TaskResult { root, files } } -fn load_root(path: &Path) -> Vec { +fn load_root(root: &Path, filter: &dyn Fn(&DirEntry) -> bool) -> Vec<(RelativePathBuf, String)> { let mut res = Vec::new(); - for entry in WalkDir::new(path) { + for entry in WalkDir::new(root).into_iter().filter_entry(filter) { let entry = match entry { Ok(entry) => entry, Err(e) => { @@ -71,10 +62,8 @@ fn load_root(path: &Path) -> Vec { continue; } }; - res.push(FileEvent { - path: path.to_owned(), - kind: FileEventKind::Add(text), - }) + let path = RelativePathBuf::from_path(path.strip_prefix(root).unwrap()).unwrap(); + res.push((path.to_owned(), text)) } res } -- cgit v1.2.3 From a5ef8ad05b7c1f7148c59814b55d641fd75aff75 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 19 Dec 2018 15:04:15 +0300 Subject: swtich lsp server to vfs --- crates/ra_vfs/src/io.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'crates/ra_vfs/src/io.rs') diff --git a/crates/ra_vfs/src/io.rs b/crates/ra_vfs/src/io.rs index 178c9beff..be400bae9 100644 --- a/crates/ra_vfs/src/io.rs +++ b/crates/ra_vfs/src/io.rs @@ -1,4 +1,5 @@ use std::{ + fmt, fs, path::{Path, PathBuf}, }; @@ -20,6 +21,12 @@ pub struct TaskResult { pub(crate) files: Vec<(RelativePathBuf, String)>, } +impl fmt::Debug for TaskResult { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str("TaskResult { ... }") + } +} + pub(crate) type Worker = thread_worker::Worker; pub(crate) fn start() -> (Worker, WorkerHandle) { -- cgit v1.2.3