From 69e6924dd596cab20333c81b4557008b7a67bad0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 25 Jun 2020 08:24:27 +0200 Subject: Use Handle/Actor terminology for flycheck --- crates/ra_flycheck/src/lib.rs | 18 +++++++++--------- crates/rust-analyzer/src/global_state.rs | 11 +++++++---- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/crates/ra_flycheck/src/lib.rs b/crates/ra_flycheck/src/lib.rs index 0e2ee8698..6751e5c38 100644 --- a/crates/ra_flycheck/src/lib.rs +++ b/crates/ra_flycheck/src/lib.rs @@ -48,21 +48,21 @@ impl fmt::Display for FlycheckConfig { /// diagnostics based on the output. /// The spawned thread is shut down when this struct is dropped. #[derive(Debug)] -pub struct Flycheck { +pub struct FlycheckHandle { // XXX: drop order is significant cmd_send: Sender, handle: jod_thread::JoinHandle<()>, pub task_recv: Receiver, } -impl Flycheck { - pub fn new(config: FlycheckConfig, workspace_root: PathBuf) -> Flycheck { +impl FlycheckHandle { + pub fn spawn(config: FlycheckConfig, workspace_root: PathBuf) -> FlycheckHandle { let (task_send, task_recv) = unbounded::(); let (cmd_send, cmd_recv) = unbounded::(); let handle = jod_thread::spawn(move || { - FlycheckThread::new(config, workspace_root).run(&task_send, &cmd_recv); + FlycheckActor::new(config, workspace_root).run(&task_send, &cmd_recv); }); - Flycheck { task_recv, cmd_send, handle } + FlycheckHandle { task_recv, cmd_send, handle } } /// Schedule a re-start of the cargo check worker. @@ -95,7 +95,7 @@ pub enum CheckCommand { Update, } -struct FlycheckThread { +struct FlycheckActor { config: FlycheckConfig, workspace_root: PathBuf, last_update_req: Option, @@ -109,9 +109,9 @@ struct FlycheckThread { check_process: Option>, } -impl FlycheckThread { - fn new(config: FlycheckConfig, workspace_root: PathBuf) -> FlycheckThread { - FlycheckThread { +impl FlycheckActor { + fn new(config: FlycheckConfig, workspace_root: PathBuf) -> FlycheckActor { + FlycheckActor { config, workspace_root, last_update_req: None, diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 64d4e2787..2a7111a88 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -9,7 +9,7 @@ use crossbeam_channel::{unbounded, Receiver}; use lsp_types::Url; use parking_lot::RwLock; use ra_db::{CrateId, SourceRoot, VfsPath}; -use ra_flycheck::{Flycheck, FlycheckConfig}; +use ra_flycheck::{FlycheckConfig, FlycheckHandle}; use ra_ide::{Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId}; use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target}; use stdx::format_to; @@ -27,12 +27,15 @@ use crate::{ }; use rustc_hash::{FxHashMap, FxHashSet}; -fn create_flycheck(workspaces: &[ProjectWorkspace], config: &FlycheckConfig) -> Option { +fn create_flycheck( + workspaces: &[ProjectWorkspace], + config: &FlycheckConfig, +) -> Option { // FIXME: Figure out the multi-workspace situation workspaces.iter().find_map(move |w| match w { ProjectWorkspace::Cargo { cargo, .. } => { let cargo_project_root = cargo.workspace_root().to_path_buf(); - Some(Flycheck::new(config.clone(), cargo_project_root.into())) + Some(FlycheckHandle::spawn(config.clone(), cargo_project_root.into())) } ProjectWorkspace::Json { .. } => { log::warn!("Cargo check watching only supported for cargo workspaces, disabling"); @@ -63,7 +66,7 @@ pub(crate) struct GlobalState { pub(crate) analysis_host: AnalysisHost, pub(crate) loader: Box, pub(crate) task_receiver: Receiver, - pub(crate) flycheck: Option, + pub(crate) flycheck: Option, pub(crate) diagnostics: DiagnosticCollection, pub(crate) mem_docs: FxHashSet, pub(crate) vfs: Arc)>>, -- cgit v1.2.3 From dab8808e82b26a45cff00d1f49863562cf4f5ce8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 25 Jun 2020 08:39:33 +0200 Subject: Abstract over channel --- crates/ra_flycheck/src/lib.rs | 56 +++++++++++++++++++------------- crates/rust-analyzer/src/global_state.rs | 11 ++++--- crates/rust-analyzer/src/main_loop.rs | 6 ++-- 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/crates/ra_flycheck/src/lib.rs b/crates/ra_flycheck/src/lib.rs index 6751e5c38..063603b45 100644 --- a/crates/ra_flycheck/src/lib.rs +++ b/crates/ra_flycheck/src/lib.rs @@ -52,17 +52,19 @@ pub struct FlycheckHandle { // XXX: drop order is significant cmd_send: Sender, handle: jod_thread::JoinHandle<()>, - pub task_recv: Receiver, } impl FlycheckHandle { - pub fn spawn(config: FlycheckConfig, workspace_root: PathBuf) -> FlycheckHandle { - let (task_send, task_recv) = unbounded::(); + pub fn spawn( + config: FlycheckConfig, + workspace_root: PathBuf, + sender: Box, + ) -> FlycheckHandle { let (cmd_send, cmd_recv) = unbounded::(); let handle = jod_thread::spawn(move || { - FlycheckActor::new(config, workspace_root).run(&task_send, &cmd_recv); + FlycheckActor::new(config, workspace_root, sender).run(&cmd_recv); }); - FlycheckHandle { task_recv, cmd_send, handle } + FlycheckHandle { cmd_send, handle } } /// Schedule a re-start of the cargo check worker. @@ -96,6 +98,7 @@ pub enum CheckCommand { } struct FlycheckActor { + sender: Box, config: FlycheckConfig, workspace_root: PathBuf, last_update_req: Option, @@ -110,8 +113,13 @@ struct FlycheckActor { } impl FlycheckActor { - fn new(config: FlycheckConfig, workspace_root: PathBuf) -> FlycheckActor { + fn new( + config: FlycheckConfig, + workspace_root: PathBuf, + sender: Box, + ) -> FlycheckActor { FlycheckActor { + sender, config, workspace_root, last_update_req: None, @@ -120,9 +128,9 @@ impl FlycheckActor { } } - fn run(&mut self, task_send: &Sender, cmd_recv: &Receiver) { + fn run(&mut self, cmd_recv: &Receiver) { // If we rerun the thread, we need to discard the previous check results first - self.clean_previous_results(task_send); + self.clean_previous_results(); loop { select! { @@ -134,7 +142,7 @@ impl FlycheckActor { }, }, recv(self.message_recv) -> msg => match msg { - Ok(msg) => self.handle_message(msg, task_send), + Ok(msg) => self.handle_message(msg), Err(RecvError) => { // Watcher finished, replace it with a never channel to // avoid busy-waiting. @@ -146,15 +154,15 @@ impl FlycheckActor { if self.should_recheck() { self.last_update_req = None; - task_send.send(CheckTask::ClearDiagnostics).unwrap(); + self.send(CheckTask::ClearDiagnostics); self.restart_check_process(); } } } - fn clean_previous_results(&self, task_send: &Sender) { - task_send.send(CheckTask::ClearDiagnostics).unwrap(); - task_send.send(CheckTask::Status(Status::End)).unwrap(); + fn clean_previous_results(&self) { + self.send(CheckTask::ClearDiagnostics); + self.send(CheckTask::Status(Status::End)); } fn should_recheck(&mut self) -> bool { @@ -173,27 +181,25 @@ impl FlycheckActor { } } - fn handle_message(&self, msg: CheckEvent, task_send: &Sender) { + fn handle_message(&self, msg: CheckEvent) { match msg { CheckEvent::Begin => { - task_send.send(CheckTask::Status(Status::Being)).unwrap(); + self.send(CheckTask::Status(Status::Being)); } CheckEvent::End => { - task_send.send(CheckTask::Status(Status::End)).unwrap(); + self.send(CheckTask::Status(Status::End)); } CheckEvent::Msg(Message::CompilerArtifact(msg)) => { - task_send.send(CheckTask::Status(Status::Progress(msg.target.name))).unwrap(); + self.send(CheckTask::Status(Status::Progress(msg.target.name))); } CheckEvent::Msg(Message::CompilerMessage(msg)) => { - task_send - .send(CheckTask::AddDiagnostic { - workspace_root: self.workspace_root.clone(), - diagnostic: msg.message, - }) - .unwrap(); + self.send(CheckTask::AddDiagnostic { + workspace_root: self.workspace_root.clone(), + diagnostic: msg.message, + }); } CheckEvent::Msg(Message::BuildScriptExecuted(_msg)) => {} @@ -271,6 +277,10 @@ impl FlycheckActor { let _ = message_send.send(CheckEvent::End); })) } + + fn send(&self, check_task: CheckTask) { + (self.sender)(check_task) + } } enum CheckEvent { diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 2a7111a88..42edadd70 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -9,7 +9,7 @@ use crossbeam_channel::{unbounded, Receiver}; use lsp_types::Url; use parking_lot::RwLock; use ra_db::{CrateId, SourceRoot, VfsPath}; -use ra_flycheck::{FlycheckConfig, FlycheckHandle}; +use ra_flycheck::{CheckTask, FlycheckConfig, FlycheckHandle}; use ra_ide::{Analysis, AnalysisChange, AnalysisHost, CrateGraph, FileId}; use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target}; use stdx::format_to; @@ -30,12 +30,15 @@ use rustc_hash::{FxHashMap, FxHashSet}; fn create_flycheck( workspaces: &[ProjectWorkspace], config: &FlycheckConfig, -) -> Option { +) -> Option<(FlycheckHandle, Receiver)> { // FIXME: Figure out the multi-workspace situation workspaces.iter().find_map(move |w| match w { ProjectWorkspace::Cargo { cargo, .. } => { + let (sender, receiver) = unbounded(); + let sender = Box::new(move |msg| sender.send(msg).unwrap()); let cargo_project_root = cargo.workspace_root().to_path_buf(); - Some(FlycheckHandle::spawn(config.clone(), cargo_project_root.into())) + let flycheck = FlycheckHandle::spawn(config.clone(), cargo_project_root.into(), sender); + Some((flycheck, receiver)) } ProjectWorkspace::Json { .. } => { log::warn!("Cargo check watching only supported for cargo workspaces, disabling"); @@ -66,7 +69,7 @@ pub(crate) struct GlobalState { pub(crate) analysis_host: AnalysisHost, pub(crate) loader: Box, pub(crate) task_receiver: Receiver, - pub(crate) flycheck: Option, + pub(crate) flycheck: Option<(FlycheckHandle, Receiver)>, pub(crate) diagnostics: DiagnosticCollection, pub(crate) mem_docs: FxHashSet, pub(crate) vfs: Arc)>>, diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 03569086a..e5f82de5e 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -136,7 +136,7 @@ pub fn main_loop(config: Config, connection: Connection) -> Result<()> { Ok(task) => Event::Vfs(task), Err(RecvError) => return Err("vfs died".into()), }, - recv(global_state.flycheck.as_ref().map_or(&never(), |it| &it.task_recv)) -> task => match task { + recv(global_state.flycheck.as_ref().map_or(&never(), |it| &it.1)) -> task => match task { Ok(task) => Event::CheckWatcher(task), Err(RecvError) => return Err("check watcher died".into()), }, @@ -290,7 +290,7 @@ fn loop_turn( if became_ready { if let Some(flycheck) = &global_state.flycheck { - flycheck.update(); + flycheck.0.update(); } } @@ -486,7 +486,7 @@ fn on_notification( let not = match notification_cast::(not) { Ok(_params) => { if let Some(flycheck) = &global_state.flycheck { - flycheck.update(); + flycheck.0.update(); } return Ok(()); } -- cgit v1.2.3 From 5a184fe85517507fd3b07c6fb36b017e558665f7 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 25 Jun 2020 08:59:55 +0200 Subject: Unify style --- crates/ra_flycheck/src/lib.rs | 6 +++--- crates/rust-analyzer/src/cli/load_cargo.rs | 2 +- crates/rust-analyzer/src/global_state.rs | 4 ++-- crates/vfs-notify/src/lib.rs | 26 +++++++++++++------------- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/crates/ra_flycheck/src/lib.rs b/crates/ra_flycheck/src/lib.rs index 063603b45..af75adbe2 100644 --- a/crates/ra_flycheck/src/lib.rs +++ b/crates/ra_flycheck/src/lib.rs @@ -56,13 +56,13 @@ pub struct FlycheckHandle { impl FlycheckHandle { pub fn spawn( + sender: Box, config: FlycheckConfig, workspace_root: PathBuf, - sender: Box, ) -> FlycheckHandle { let (cmd_send, cmd_recv) = unbounded::(); let handle = jod_thread::spawn(move || { - FlycheckActor::new(config, workspace_root, sender).run(&cmd_recv); + FlycheckActor::new(sender, config, workspace_root).run(&cmd_recv); }); FlycheckHandle { cmd_send, handle } } @@ -114,9 +114,9 @@ struct FlycheckActor { impl FlycheckActor { fn new( + sender: Box, config: FlycheckConfig, workspace_root: PathBuf, - sender: Box, ) -> FlycheckActor { FlycheckActor { sender, diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs index 92e9b60fc..c5cf5ff27 100644 --- a/crates/rust-analyzer/src/cli/load_cargo.rs +++ b/crates/rust-analyzer/src/cli/load_cargo.rs @@ -28,7 +28,7 @@ pub fn load_cargo( let mut vfs = vfs::Vfs::default(); let mut loader = { let loader = - vfs_notify::LoaderHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap())); + vfs_notify::NotifyHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap())); Box::new(loader) }; diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 42edadd70..c022ff705 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -37,7 +37,7 @@ fn create_flycheck( let (sender, receiver) = unbounded(); let sender = Box::new(move |msg| sender.send(msg).unwrap()); let cargo_project_root = cargo.workspace_root().to_path_buf(); - let flycheck = FlycheckHandle::spawn(config.clone(), cargo_project_root.into(), sender); + let flycheck = FlycheckHandle::spawn(sender, config.clone(), cargo_project_root.into()); Some((flycheck, receiver)) } ProjectWorkspace::Json { .. } => { @@ -121,7 +121,7 @@ impl GlobalState { }; let mut loader = { - let loader = vfs_notify::LoaderHandle::spawn(Box::new(move |msg| { + let loader = vfs_notify::NotifyHandle::spawn(Box::new(move |msg| { task_sender.send(msg).unwrap() })); Box::new(loader) diff --git a/crates/vfs-notify/src/lib.rs b/crates/vfs-notify/src/lib.rs index a2f4e0c5b..282cf0358 100644 --- a/crates/vfs-notify/src/lib.rs +++ b/crates/vfs-notify/src/lib.rs @@ -20,7 +20,7 @@ use walkdir::WalkDir; use crate::include::Include; #[derive(Debug)] -pub struct LoaderHandle { +pub struct NotifyHandle { // Relative order of fields below is significant. sender: crossbeam_channel::Sender, _thread: jod_thread::JoinHandle, @@ -32,12 +32,12 @@ enum Message { Invalidate(AbsPathBuf), } -impl loader::Handle for LoaderHandle { - fn spawn(sender: loader::Sender) -> LoaderHandle { - let actor = LoaderActor::new(sender); +impl loader::Handle for NotifyHandle { + fn spawn(sender: loader::Sender) -> NotifyHandle { + let actor = NotifyActor::new(sender); let (sender, receiver) = unbounded::(); let thread = jod_thread::spawn(move || actor.run(receiver)); - LoaderHandle { sender, _thread: thread } + NotifyHandle { sender, _thread: thread } } fn set_config(&mut self, config: loader::Config) { self.sender.send(Message::Config(config)).unwrap() @@ -52,10 +52,10 @@ impl loader::Handle for LoaderHandle { type NotifyEvent = notify::Result; -struct LoaderActor { +struct NotifyActor { + sender: loader::Sender, config: Vec<(AbsPathBuf, Include, bool)>, watched_paths: FxHashSet, - sender: loader::Sender, // Drop order of fields bellow is significant, watcher: Option, watcher_receiver: Receiver, @@ -67,19 +67,19 @@ enum Event { NotifyEvent(NotifyEvent), } -impl LoaderActor { - fn new(sender: loader::Sender) -> LoaderActor { +impl NotifyActor { + fn new(sender: loader::Sender) -> NotifyActor { let (watcher_sender, watcher_receiver) = unbounded(); let watcher = log_notify_error(Watcher::new_immediate(move |event| { watcher_sender.send(event).unwrap() })); - LoaderActor { - watcher, - watcher_receiver, - watched_paths: FxHashSet::default(), + NotifyActor { sender, config: Vec::new(), + watched_paths: FxHashSet::default(), + watcher, + watcher_receiver, } } -- cgit v1.2.3