From e70f7dc10c622e0ffec4b264235ad203b4047171 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 26 Jun 2020 00:27:39 +0200 Subject: Minor --- crates/rust-analyzer/src/dispatch.rs | 2 +- crates/rust-analyzer/src/global_state.rs | 38 ++++++++++++++++++-------------- crates/rust-analyzer/src/main_loop.rs | 20 ++++++++--------- crates/rust-analyzer/src/reload.rs | 10 ++++----- 4 files changed, 38 insertions(+), 32 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/dispatch.rs b/crates/rust-analyzer/src/dispatch.rs index 03b373dee..891fdb96d 100644 --- a/crates/rust-analyzer/src/dispatch.rs +++ b/crates/rust-analyzer/src/dispatch.rs @@ -59,7 +59,7 @@ impl<'a> RequestDispatcher<'a> { } }; - self.global_state.task_pool.0.spawn({ + self.global_state.task_pool.handle.spawn({ let world = self.global_state.snapshot(); move || { let result = f(world, params); diff --git a/crates/rust-analyzer/src/global_state.rs b/crates/rust-analyzer/src/global_state.rs index 8486da627..17de2a075 100644 --- a/crates/rust-analyzer/src/global_state.rs +++ b/crates/rust-analyzer/src/global_state.rs @@ -13,7 +13,7 @@ use ra_db::{CrateId, VfsPath}; use ra_ide::{Analysis, AnalysisChange, AnalysisHost, FileId}; use ra_project_model::{CargoWorkspace, ProcMacroClient, ProjectWorkspace, Target}; use stdx::format_to; -use vfs::loader::Handle; +use vfs::loader::Handle as _; use crate::{ config::Config, @@ -42,19 +42,26 @@ impl Default for Status { } } +// Enforces drop order +pub(crate) struct Handle { + pub(crate) handle: H, + pub(crate) receiver: C, +} + /// `GlobalState` is the primary mutable state of the language server /// /// The most interesting components are `vfs`, which stores a consistent /// snapshot of the file systems, and `analysis_host`, which stores our /// incremental salsa database. +/// +/// Note that this struct has more than on impl in various modules! pub(crate) struct GlobalState { sender: Sender, + pub(crate) task_pool: Handle, Receiver>, + pub(crate) loader: Handle, Receiver>, + pub(crate) flycheck: Option>>, pub(crate) config: Config, - pub(crate) task_pool: (TaskPool, Receiver), pub(crate) analysis_host: AnalysisHost, - pub(crate) loader: Box, - pub(crate) task_receiver: Receiver, - pub(crate) flycheck: Option<(FlycheckHandle, Receiver)>, pub(crate) diagnostics: DiagnosticCollection, pub(crate) mem_docs: FxHashSet, pub(crate) vfs: Arc)>>, @@ -82,37 +89,36 @@ impl GlobalState { lru_capacity: Option, config: Config, ) -> GlobalState { - let (task_sender, task_receiver) = unbounded::(); - let loader = { - let loader = vfs_notify::NotifyHandle::spawn(Box::new(move |msg| { - task_sender.send(msg).unwrap() - })); - Box::new(loader) + let (sender, receiver) = unbounded::(); + let handle = + vfs_notify::NotifyHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap())); + let handle = Box::new(handle) as Box; + Handle { handle, receiver } }; let task_pool = { let (sender, receiver) = unbounded(); - (TaskPool::new(sender), receiver) + let handle = TaskPool::new(sender); + Handle { handle, receiver } }; GlobalState { sender, - config, task_pool, - analysis_host: AnalysisHost::new(lru_capacity), loader, - task_receiver, + config, + analysis_host: AnalysisHost::new(lru_capacity), flycheck: None, diagnostics: Default::default(), mem_docs: FxHashSet::default(), vfs: Arc::new(RwLock::new((vfs::Vfs::default(), FxHashMap::default()))), status: Status::default(), req_queue: ReqQueue::default(), - latest_requests: Default::default(), source_root_config: SourceRootConfig::default(), proc_macro_client: ProcMacroClient::dummy(), workspaces: Arc::new(Vec::new()), + latest_requests: Default::default(), } } diff --git a/crates/rust-analyzer/src/main_loop.rs b/crates/rust-analyzer/src/main_loop.rs index 6ac50745a..d4879283d 100644 --- a/crates/rust-analyzer/src/main_loop.rs +++ b/crates/rust-analyzer/src/main_loop.rs @@ -100,13 +100,13 @@ impl GlobalState { recv(inbox) -> msg => msg.ok().map(Event::Lsp), - recv(self.task_pool.1) -> task => + recv(self.task_pool.receiver) -> task => Some(Event::Task(task.unwrap())), - recv(self.task_receiver) -> task => + recv(self.loader.receiver) -> task => Some(Event::Vfs(task.unwrap())), - recv(self.flycheck.as_ref().map_or(&never(), |it| &it.1)) -> task => + recv(self.flycheck.as_ref().map_or(&never(), |it| &it.receiver)) -> task => Some(Event::Flycheck(task.unwrap())), } } @@ -132,7 +132,7 @@ impl GlobalState { let _p = profile("GlobalState::handle_event"); log::info!("handle_event({:?})", event); - let queue_count = self.task_pool.0.len(); + let queue_count = self.task_pool.handle.len(); if queue_count > 0 { log::info!("queued count = {}", queue_count); } @@ -233,7 +233,7 @@ impl GlobalState { let state_changed = self.process_changes(); if became_ready { if let Some(flycheck) = &self.flycheck { - flycheck.0.update(); + flycheck.handle.update(); } } @@ -370,7 +370,7 @@ impl GlobalState { log::error!("orphan DidCloseTextDocument: {}", path) } if let Some(path) = path.as_path() { - this.loader.invalidate(path.to_path_buf()); + this.loader.handle.invalidate(path.to_path_buf()); } } let params = lsp_types::PublishDiagnosticsParams { @@ -384,7 +384,7 @@ impl GlobalState { })? .on::(|this, _params| { if let Some(flycheck) = &this.flycheck { - flycheck.0.update(); + flycheck.handle.update(); } Ok(()) })? @@ -427,7 +427,7 @@ impl GlobalState { .on::(|this, params| { for change in params.changes { if let Ok(path) = from_proto::abs_path(&change.uri) { - this.loader.invalidate(path); + this.loader.handle.invalidate(path); } } Ok(()) @@ -440,7 +440,7 @@ impl GlobalState { if self.config.publish_diagnostics { let snapshot = self.snapshot(); let subscriptions = subscriptions.clone(); - self.task_pool.0.spawn(move || { + self.task_pool.handle.spawn(move || { let diagnostics = subscriptions .into_iter() .filter_map(|file_id| { @@ -458,7 +458,7 @@ impl GlobalState { Task::Diagnostics(diagnostics) }) } - self.task_pool.0.spawn({ + self.task_pool.handle.spawn({ let subs = subscriptions; let snap = self.snapshot(); move || { diff --git a/crates/rust-analyzer/src/reload.rs b/crates/rust-analyzer/src/reload.rs index 1981a97da..a22d3e262 100644 --- a/crates/rust-analyzer/src/reload.rs +++ b/crates/rust-analyzer/src/reload.rs @@ -11,7 +11,7 @@ use vfs::{file_set::FileSetConfig, AbsPath}; use crate::{ config::{Config, FilesWatcher, LinkedProject}, - global_state::GlobalState, + global_state::{GlobalState, Handle}, }; impl GlobalState { @@ -105,7 +105,7 @@ impl GlobalState { FilesWatcher::Client => vec![], FilesWatcher::Notify => project_folders.watch, }; - self.loader.set_config(vfs::loader::Config { load: project_folders.load, watch }); + self.loader.handle.set_config(vfs::loader::Config { load: project_folders.load, watch }); // Create crate graph from all the workspaces let crate_graph = { @@ -113,7 +113,7 @@ impl GlobalState { let vfs = &mut self.vfs.write().0; let loader = &mut self.loader; let mut load = |path: &AbsPath| { - let contents = loader.load_sync(path); + let contents = loader.handle.load_sync(path); let path = vfs::VfsPath::from(path.to_path_buf()); vfs.set_file_contents(path.clone(), contents); vfs.file_id(&path) @@ -153,9 +153,9 @@ impl GlobalState { 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 = + let handle = FlycheckHandle::spawn(sender, config.clone(), cargo_project_root.into()); - Some((flycheck, receiver)) + Some(Handle { handle, receiver }) } ProjectWorkspace::Json { .. } => { log::warn!("Cargo check watching only supported for cargo workspaces, disabling"); -- cgit v1.2.3