Skip to content

Commit

Permalink
Add mechanism to deal with server events
Browse files Browse the repository at this point in the history
  • Loading branch information
pfoerster committed May 21, 2019
1 parent 9994a38 commit bb18080
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 13 deletions.
10 changes: 8 additions & 2 deletions jsonrpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod types;

pub use self::{
client::{Client, ResponseHandler},
server::{handle_notification, handle_request, Server},
server::{handle_notification, handle_request, Server, EventHandler},
types::*,
};

Expand All @@ -26,7 +26,7 @@ pub struct MessageHandler<S, H, I, O> {

impl<S, H, I, O> MessageHandler<S, H, I, O>
where
S: Server + Send + Sync + 'static,
S: Server + EventHandler + Send + Sync + 'static,
H: ResponseHandler + Send + Sync + 'static,
I: Stream<Item = std::io::Result<String>> + Unpin,
O: Sink<String> + Unpin + Send + 'static,
Expand Down Expand Up @@ -64,12 +64,18 @@ where
let json = serde_json::to_string(&response).unwrap();
let mut output = await!(output.lock());
await!(output.send(json));
await!(server.handle_events());
};

self.pool.spawn(handler).unwrap();
}
Ok(Message::Notification(notification)) => {
self.server.handle_notification(notification);

let server = Arc::clone(&self.server);
self.pool.spawn(async move {
await!(server.handle_events());
});
}
Ok(Message::Response(response)) => {
await!(self.response_handler.handle(response));
Expand Down
4 changes: 4 additions & 0 deletions jsonrpc/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ pub trait Server {
fn handle_notification(&self, notification: Notification);
}

pub trait EventHandler {
fn handle_events(&self) -> BoxFuture<'_, ()>;
}

const DESERIALIZE_OBJECT_ERROR: &str = "Could not deserialize parameter object";

pub async fn handle_request<'a, H, F, I, O>(request: Request, handler: H) -> Response
Expand Down
23 changes: 23 additions & 0 deletions src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::sync::Mutex;

#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Event {
WorkspaceChanged,
}

#[derive(Debug, Default)]
pub struct EventManager {
events: Mutex<Vec<Event>>,
}

impl EventManager {
pub fn push(&self, event: Event) {
let mut events = self.events.lock().unwrap();
events.push(event);
}

pub fn take(&self) -> Vec<Event> {
let mut events = self.events.lock().unwrap();
std::mem::replace(&mut *events, Vec::new())
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod codec;
pub mod completion;
pub mod data;
pub mod definition;
pub mod event;
pub mod feature;
pub mod folding;
pub mod formatting;
Expand Down
42 changes: 31 additions & 11 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ use serde::de::DeserializeOwned;
use std::borrow::Cow;
use std::sync::Arc;
use walkdir::WalkDir;
use futures::future::BoxFuture;
use futures::prelude::*;
use crate::event::{Event, EventManager};

pub struct LatexLspServer<C> {
client: Arc<C>,
workspace_manager: WorkspaceManager,
event_manager: EventManager,
}

#[jsonrpc_server]
Expand All @@ -36,6 +40,7 @@ impl<C: LspClient + Send + Sync> LatexLspServer<C> {
LatexLspServer {
client,
workspace_manager: WorkspaceManager::new(),
event_manager: EventManager::default(),
}
}

Expand All @@ -51,12 +56,6 @@ impl<C: LspClient + Send + Sync> LatexLspServer<C> {
}
}

let workspace = self.workspace_manager.get();
workspace
.documents
.iter()
.for_each(|x| info!("{}", x.uri.as_str()));

let capabilities = ServerCapabilities {
text_document_sync: Some(TextDocumentSyncCapability::Options(
TextDocumentSyncOptions {
Expand Down Expand Up @@ -99,22 +98,25 @@ impl<C: LspClient + Send + Sync> LatexLspServer<C> {
}

#[jsonrpc_method("initialized", kind = "notification")]
pub fn initialized(&self, params: InitializedParams) {}
pub fn initialized(&self, _params: InitializedParams) {
self.event_manager.push(Event::WorkspaceChanged);
}

#[jsonrpc_method("shutdown", kind = "request")]
pub async fn shutdown(&self, params: ()) -> Result<()> {
pub async fn shutdown(&self, _params: ()) -> Result<()> {
Ok(())
}

#[jsonrpc_method("exit", kind = "notification")]
pub fn exit(&self, params: ()) {}
pub fn exit(&self, _params: ()) {}

#[jsonrpc_method("workspace/didChangeWatchedFiles", kind = "notification")]
pub fn did_change_watched_files(&self, params: DidChangeWatchedFilesParams) {}

#[jsonrpc_method("textDocument/didOpen", kind = "notification")]
pub fn did_open(&self, params: DidOpenTextDocumentParams) {
self.workspace_manager.add(params.text_document);
self.event_manager.push(Event::WorkspaceChanged);
}

#[jsonrpc_method("textDocument/didChange", kind = "notification")]
Expand All @@ -123,13 +125,16 @@ impl<C: LspClient + Send + Sync> LatexLspServer<C> {
let uri = params.text_document.uri.clone();
self.workspace_manager.update(uri, change.text);
}
self.event_manager.push(Event::WorkspaceChanged);
}

#[jsonrpc_method("textDocument/didSave", kind = "notification")]
pub fn did_save(&self, params: DidSaveTextDocumentParams) {}
pub fn did_save(&self, _params: DidSaveTextDocumentParams) {
self.event_manager.push(Event::WorkspaceChanged);
}

#[jsonrpc_method("textDocument/didClose", kind = "notification")]
pub fn did_close(&self, params: DidCloseTextDocumentParams) {}
pub fn did_close(&self, _params: DidCloseTextDocumentParams) {}

#[jsonrpc_method("textDocument/completion", kind = "request")]
pub async fn completion(&self, params: CompletionParams) -> Result<CompletionList> {
Expand Down Expand Up @@ -266,6 +271,21 @@ impl<C: LspClient + Send + Sync> LatexLspServer<C> {
}
}

impl<C: LspClient + Send + Sync> jsonrpc::EventHandler for LatexLspServer<C> {
fn handle_events(&self) -> BoxFuture<'_, ()> {
let handler = async move {
for event in self.event_manager.take() {
match event {
Event::WorkspaceChanged => {
log::info!("TODO: Workspace Changed");
}
}
}
};
handler.boxed()
}
}

#[macro_export]
macro_rules! request {
($server:expr, $params:expr) => {{
Expand Down

0 comments on commit bb18080

Please sign in to comment.