From 9fcebbc51284408203c05219a0ee92519f51ea74 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 30 Aug 2018 16:27:09 +0300 Subject: subscriptions --- crates/server/src/main_loop/handlers.rs | 10 ++-- crates/server/src/main_loop/mod.rs | 80 ++++++++++++++-------------- crates/server/src/main_loop/subscriptions.rs | 21 ++++++++ crates/server/src/server_world.rs | 7 +-- 4 files changed, 71 insertions(+), 47 deletions(-) create mode 100644 crates/server/src/main_loop/subscriptions.rs (limited to 'crates/server/src') diff --git a/crates/server/src/main_loop/handlers.rs b/crates/server/src/main_loop/handlers.rs index 45083b084..6b70399b0 100644 --- a/crates/server/src/main_loop/handlers.rs +++ b/crates/server/src/main_loop/handlers.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use languageserver_types::{ - Diagnostic, DiagnosticSeverity, Url, DocumentSymbol, + Diagnostic, DiagnosticSeverity, DocumentSymbol, Command, TextDocumentIdentifier, SymbolInformation, Position, Location, TextEdit, CompletionItem, InsertTextFormat, CompletionItemKind, @@ -325,9 +325,9 @@ pub fn handle_code_action( pub fn publish_diagnostics( world: ServerWorld, - uri: Url + file_id: FileId, ) -> Result { - let file_id = world.uri_to_file_id(&uri)?; + let uri = world.file_id_to_uri(file_id)?; let line_index = world.analysis().file_line_index(file_id); let diagnostics = world.analysis().diagnostics(file_id) .into_iter() @@ -344,9 +344,9 @@ pub fn publish_diagnostics( pub fn publish_decorations( world: ServerWorld, - uri: Url + file_id: FileId, ) -> Result { - let file_id = world.uri_to_file_id(&uri)?; + let uri = world.file_id_to_uri(file_id)?; Ok(req::PublishDecorationsParams { uri, decorations: highlight(&world, file_id), diff --git a/crates/server/src/main_loop/mod.rs b/crates/server/src/main_loop/mod.rs index 0f66248a5..cd17cab56 100644 --- a/crates/server/src/main_loop/mod.rs +++ b/crates/server/src/main_loop/mod.rs @@ -1,4 +1,5 @@ mod handlers; +mod subscriptions; use std::{ collections::{HashSet}, @@ -6,7 +7,7 @@ use std::{ use threadpool::ThreadPool; use crossbeam_channel::{Sender, Receiver}; -use languageserver_types::Url; +use libanalysis::FileId; use { req, dispatch, @@ -14,6 +15,7 @@ use { io::{Io, RawMsg, RawRequest, RawNotification}, vfs::FileEvent, server_world::{ServerWorldState, ServerWorld}, + main_loop::subscriptions::{Subscriptions}, }; pub(super) fn main_loop( @@ -28,6 +30,7 @@ pub(super) fn main_loop( let mut pending_requests: HashSet = HashSet::new(); let mut fs_events_receiver = Some(&fs_events_receiver); + let mut subs = Subscriptions::new(); loop { enum Event { Msg(RawMsg), @@ -47,7 +50,7 @@ pub(super) fn main_loop( None => Event::FsWatcherDead, } }; - + let mut state_changed = false; match event { Event::ReceiverDead => { io.cleanup_receiver()?; @@ -70,6 +73,7 @@ pub(super) fn main_loop( Event::Fs(events) => { trace!("fs change, {} events", events.len()); state.apply_fs_changes(events); + state_changed = true; } Event::Msg(msg) => { match msg { @@ -79,7 +83,8 @@ pub(super) fn main_loop( } } RawMsg::Notification(not) => { - on_notification(io, &mut state, pool, &task_sender, not)? + on_notification(io, &mut state, &mut subs, not)?; + state_changed = true; } RawMsg::Response(resp) => { if !pending_requests.remove(&resp.id) { @@ -89,6 +94,15 @@ pub(super) fn main_loop( } } }; + + if state_changed { + update_file_notifications_on_threadpool( + pool, + state.snapshot(), + task_sender.clone(), + subs.subscriptions(), + ) + } } } @@ -140,8 +154,7 @@ fn on_request( fn on_notification( io: &mut Io, state: &mut ServerWorldState, - pool: &ThreadPool, - sender: &Sender, + subs: &mut Subscriptions, not: RawNotification, ) -> Result<()> { let mut not = Some(not); @@ -149,13 +162,8 @@ fn on_notification( let uri = params.text_document.uri; let path = uri.to_file_path() .map_err(|()| format_err!("invalid uri: {}", uri))?; - state.add_mem_file(path, params.text_document.text); - update_file_notifications_on_threadpool( - pool, - state.snapshot(), - sender.clone(), - uri, - ); + let file_id = state.add_mem_file(path, params.text_document.text); + subs.add_sub(file_id); Ok(()) })?; dispatch::handle_notification::(&mut not, |mut params| { @@ -166,23 +174,15 @@ fn on_notification( .ok_or_else(|| format_err!("empty changes"))? .text; state.change_mem_file(path.as_path(), text)?; - update_file_notifications_on_threadpool( - pool, - state.snapshot(), - sender.clone(), - uri, - ); Ok(()) })?; dispatch::handle_notification::(&mut not, |params| { let uri = params.text_document.uri; let path = uri.to_file_path() .map_err(|()| format_err!("invalid uri: {}", uri))?; - state.remove_mem_file(path.as_path())?; - let not = req::PublishDiagnosticsParams { - uri, - diagnostics: Vec::new(), - }; + let file_id = state.remove_mem_file(path.as_path())?; + subs.remove_sub(file_id); + let not = req::PublishDiagnosticsParams { uri, diagnostics: Vec::new() }; let not = dispatch::send_notification::(not); io.send(RawMsg::Notification(not)); Ok(()) @@ -227,25 +227,27 @@ fn update_file_notifications_on_threadpool( pool: &ThreadPool, world: ServerWorld, sender: Sender, - uri: Url, + subscriptions: Vec, ) { pool.execute(move || { - match handlers::publish_diagnostics(world.clone(), uri.clone()) { - Err(e) => { - error!("failed to compute diagnostics: {:?}", e) - } - Ok(params) => { - let not = dispatch::send_notification::(params); - sender.send(Task::Notify(not)); - } - } - match handlers::publish_decorations(world, uri) { - Err(e) => { - error!("failed to compute decorations: {:?}", e) + for file_id in subscriptions { + match handlers::publish_diagnostics(world.clone(), file_id) { + Err(e) => { + error!("failed to compute diagnostics: {:?}", e) + } + Ok(params) => { + let not = dispatch::send_notification::(params); + sender.send(Task::Notify(not)); + } } - Ok(params) => { - let not = dispatch::send_notification::(params); - sender.send(Task::Notify(not)) + match handlers::publish_decorations(world.clone(), file_id) { + Err(e) => { + error!("failed to compute decorations: {:?}", e) + } + Ok(params) => { + let not = dispatch::send_notification::(params); + sender.send(Task::Notify(not)) + } } } }); diff --git a/crates/server/src/main_loop/subscriptions.rs b/crates/server/src/main_loop/subscriptions.rs new file mode 100644 index 000000000..963096aef --- /dev/null +++ b/crates/server/src/main_loop/subscriptions.rs @@ -0,0 +1,21 @@ +use std::collections::HashSet; +use libanalysis::FileId; + +pub struct Subscriptions { + subs: HashSet, +} + +impl Subscriptions { + pub fn new() -> Subscriptions { + Subscriptions { subs: HashSet::new() } + } + pub fn add_sub(&mut self, file_id: FileId) { + self.subs.insert(file_id); + } + pub fn remove_sub(&mut self, file_id: FileId) { + self.subs.remove(&file_id); + } + pub fn subscriptions(&self) -> Vec { + self.subs.iter().cloned().collect() + } +} diff --git a/crates/server/src/server_world.rs b/crates/server/src/server_world.rs index 9ba7df0b8..d99ef661e 100644 --- a/crates/server/src/server_world.rs +++ b/crates/server/src/server_world.rs @@ -61,10 +61,11 @@ impl ServerWorldState { self.analysis_host.change_files(changes); } - pub fn add_mem_file(&mut self, path: PathBuf, text: String) { + pub fn add_mem_file(&mut self, path: PathBuf, text: String) -> FileId { let file_id = self.path_map.get_or_insert(path); self.mem_map.insert(file_id, None); self.analysis_host.change_file(file_id, Some(text)); + file_id } pub fn change_mem_file(&mut self, path: &Path, text: String) -> Result<()> { @@ -75,7 +76,7 @@ impl ServerWorldState { Ok(()) } - pub fn remove_mem_file(&mut self, path: &Path) -> Result<()> { + pub fn remove_mem_file(&mut self, path: &Path) -> Result { let file_id = self.path_map.get_id(path).ok_or_else(|| { format_err!("change to unknown file: {}", path.display()) })?; @@ -86,7 +87,7 @@ impl ServerWorldState { // Do this via file watcher ideally. let text = fs::read_to_string(path).ok(); self.analysis_host.change_file(file_id, text); - Ok(()) + Ok(file_id) } pub fn snapshot(&self) -> ServerWorld { -- cgit v1.2.3