2021-08-30 02:53:59 -05:00
|
|
|
//! See [RequestDispatcher].
|
2021-08-22 09:41:09 -05:00
|
|
|
use std::{fmt, panic, thread};
|
2020-06-25 10:22:18 -05:00
|
|
|
|
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
global_state::{GlobalState, GlobalStateSnapshot},
|
2021-05-17 12:07:10 -05:00
|
|
|
lsp_utils::is_cancelled,
|
2020-06-25 10:22:18 -05:00
|
|
|
main_loop::Task,
|
|
|
|
LspError, Result,
|
|
|
|
};
|
|
|
|
|
2021-08-30 02:53:59 -05:00
|
|
|
/// A visitor for routing a raw JSON request to an appropriate handler function.
|
|
|
|
///
|
|
|
|
/// Most requests are read-only and async and are handled on the threadpool
|
|
|
|
/// (`on` method).
|
|
|
|
///
|
|
|
|
/// Some read-only requests are latency sensitive, and are immediately handled
|
|
|
|
/// on the main loop thread (`on_sync`). These are typically typing-related
|
|
|
|
/// requests.
|
|
|
|
///
|
|
|
|
/// Some requests modify the state, and are run on the main thread to get
|
|
|
|
/// `&mut` (`on_sync_mut`).
|
|
|
|
///
|
|
|
|
/// Read-only requests are wrapped into `catch_unwind` -- they don't modify the
|
|
|
|
/// state, so it's OK to recover from their failures.
|
2020-06-25 10:22:18 -05:00
|
|
|
pub(crate) struct RequestDispatcher<'a> {
|
|
|
|
pub(crate) req: Option<lsp_server::Request>,
|
|
|
|
pub(crate) global_state: &'a mut GlobalState,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> RequestDispatcher<'a> {
|
2021-08-30 02:53:59 -05:00
|
|
|
/// Dispatches the request onto the current thread, given full access to
|
|
|
|
/// mutable global state. Unlike all other methods here, this one isn't
|
|
|
|
/// guarded by `catch_unwind`, so, please, don't make bugs :-)
|
|
|
|
pub(crate) fn on_sync_mut<R>(
|
2020-06-25 10:22:18 -05:00
|
|
|
&mut self,
|
|
|
|
f: fn(&mut GlobalState, R::Params) -> Result<R::Result>,
|
|
|
|
) -> Result<&mut Self>
|
|
|
|
where
|
|
|
|
R: lsp_types::request::Request + 'static,
|
2020-09-29 13:46:36 -05:00
|
|
|
R::Params: DeserializeOwned + panic::UnwindSafe + fmt::Debug + 'static,
|
2020-06-25 10:22:18 -05:00
|
|
|
R::Result: Serialize + 'static,
|
|
|
|
{
|
2021-08-30 02:53:59 -05:00
|
|
|
let (id, params, panic_context) = match self.parse::<R>() {
|
2020-06-25 10:22:18 -05:00
|
|
|
Some(it) => it,
|
2020-10-30 13:38:29 -05:00
|
|
|
None => return Ok(self),
|
2020-06-25 10:22:18 -05:00
|
|
|
};
|
2021-08-30 02:53:59 -05:00
|
|
|
let _pctx = stdx::panic_context::enter(panic_context);
|
|
|
|
|
|
|
|
let result = f(&mut self.global_state, params);
|
|
|
|
let response = result_to_response::<R>(id, result);
|
|
|
|
|
|
|
|
self.global_state.respond(response);
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dispatches the request onto the current thread.
|
|
|
|
pub(crate) fn on_sync<R>(
|
|
|
|
&mut self,
|
|
|
|
f: fn(GlobalStateSnapshot, R::Params) -> Result<R::Result>,
|
|
|
|
) -> Result<&mut Self>
|
|
|
|
where
|
|
|
|
R: lsp_types::request::Request + 'static,
|
|
|
|
R::Params: DeserializeOwned + panic::UnwindSafe + fmt::Debug + 'static,
|
|
|
|
R::Result: Serialize + 'static,
|
|
|
|
{
|
|
|
|
let (id, params, panic_context) = match self.parse::<R>() {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return Ok(self),
|
|
|
|
};
|
|
|
|
let global_state_snapshot = self.global_state.snapshot();
|
2020-10-30 13:38:29 -05:00
|
|
|
|
2021-08-22 09:41:09 -05:00
|
|
|
let result = panic::catch_unwind(move || {
|
2021-08-30 02:53:59 -05:00
|
|
|
let _pctx = stdx::panic_context::enter(panic_context);
|
|
|
|
f(global_state_snapshot, params)
|
2021-08-22 09:41:09 -05:00
|
|
|
});
|
2021-08-30 02:53:59 -05:00
|
|
|
let response = thread_result_to_response::<R>(id, result);
|
2021-08-22 09:41:09 -05:00
|
|
|
|
2020-06-25 12:23:52 -05:00
|
|
|
self.global_state.respond(response);
|
2020-06-25 10:22:18 -05:00
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Dispatches the request onto thread pool
|
|
|
|
pub(crate) fn on<R>(
|
|
|
|
&mut self,
|
|
|
|
f: fn(GlobalStateSnapshot, R::Params) -> Result<R::Result>,
|
2020-10-30 13:38:29 -05:00
|
|
|
) -> &mut Self
|
2020-06-25 10:22:18 -05:00
|
|
|
where
|
|
|
|
R: lsp_types::request::Request + 'static,
|
2021-08-22 09:41:09 -05:00
|
|
|
R::Params: DeserializeOwned + panic::UnwindSafe + Send + fmt::Debug + 'static,
|
2020-06-25 10:22:18 -05:00
|
|
|
R::Result: Serialize + 'static,
|
|
|
|
{
|
2021-08-30 02:53:59 -05:00
|
|
|
let (id, params, panic_context) = match self.parse::<R>() {
|
2020-06-25 10:22:18 -05:00
|
|
|
Some(it) => it,
|
2020-10-30 13:38:29 -05:00
|
|
|
None => return self,
|
2020-06-25 10:22:18 -05:00
|
|
|
};
|
|
|
|
|
2020-06-25 17:27:39 -05:00
|
|
|
self.global_state.task_pool.handle.spawn({
|
2020-06-25 10:22:18 -05:00
|
|
|
let world = self.global_state.snapshot();
|
|
|
|
move || {
|
2021-08-22 09:41:09 -05:00
|
|
|
let result = panic::catch_unwind(move || {
|
2021-08-30 02:53:59 -05:00
|
|
|
let _pctx = stdx::panic_context::enter(panic_context);
|
2021-08-22 09:41:09 -05:00
|
|
|
f(world, params)
|
|
|
|
});
|
2021-08-30 02:53:59 -05:00
|
|
|
let response = thread_result_to_response::<R>(id, result);
|
2021-08-22 09:41:09 -05:00
|
|
|
Task::Response(response)
|
2020-06-25 10:22:18 -05:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-30 13:38:29 -05:00
|
|
|
self
|
2020-06-25 10:22:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn finish(&mut self) {
|
2020-06-25 12:23:52 -05:00
|
|
|
if let Some(req) = self.req.take() {
|
2021-08-15 07:46:13 -05:00
|
|
|
tracing::error!("unknown request: {:?}", req);
|
2020-06-25 12:23:52 -05:00
|
|
|
let response = lsp_server::Response::new_err(
|
|
|
|
req.id,
|
|
|
|
lsp_server::ErrorCode::MethodNotFound as i32,
|
|
|
|
"unknown request".to_string(),
|
|
|
|
);
|
2020-10-30 13:38:29 -05:00
|
|
|
self.global_state.respond(response);
|
2020-06-25 10:22:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 02:53:59 -05:00
|
|
|
fn parse<R>(&mut self) -> Option<(lsp_server::RequestId, R::Params, String)>
|
2020-06-25 10:22:18 -05:00
|
|
|
where
|
|
|
|
R: lsp_types::request::Request + 'static,
|
2021-08-30 02:53:59 -05:00
|
|
|
R::Params: DeserializeOwned + fmt::Debug + 'static,
|
2020-06-25 10:22:18 -05:00
|
|
|
{
|
2020-10-30 13:38:29 -05:00
|
|
|
let req = match &self.req {
|
|
|
|
Some(req) if req.method == R::METHOD => self.req.take().unwrap(),
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
let res = crate::from_json(R::METHOD, req.params);
|
|
|
|
match res {
|
2021-08-30 02:53:59 -05:00
|
|
|
Ok(params) => {
|
|
|
|
let panic_context =
|
|
|
|
format!("\nversion: {}\nrequest: {} {:#?}", env!("REV"), R::METHOD, params);
|
|
|
|
Some((req.id, params, panic_context))
|
|
|
|
}
|
2020-10-30 13:38:29 -05:00
|
|
|
Err(err) => {
|
|
|
|
let response = lsp_server::Response::new_err(
|
|
|
|
req.id,
|
|
|
|
lsp_server::ErrorCode::InvalidParams as i32,
|
|
|
|
err.to_string(),
|
|
|
|
);
|
|
|
|
self.global_state.respond(response);
|
2021-06-12 23:05:29 -05:00
|
|
|
None
|
2020-06-25 10:22:18 -05:00
|
|
|
}
|
2020-10-30 13:38:29 -05:00
|
|
|
}
|
2020-06-25 10:22:18 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 02:53:59 -05:00
|
|
|
fn thread_result_to_response<R>(
|
2020-06-25 12:23:52 -05:00
|
|
|
id: lsp_server::RequestId,
|
2021-08-22 09:41:09 -05:00
|
|
|
result: thread::Result<Result<R::Result>>,
|
2020-06-25 12:23:52 -05:00
|
|
|
) -> lsp_server::Response
|
2020-06-25 10:22:18 -05:00
|
|
|
where
|
|
|
|
R: lsp_types::request::Request + 'static,
|
|
|
|
R::Params: DeserializeOwned + 'static,
|
|
|
|
R::Result: Serialize + 'static,
|
|
|
|
{
|
2020-06-25 12:23:52 -05:00
|
|
|
match result {
|
2021-08-30 02:53:59 -05:00
|
|
|
Ok(result) => result_to_response::<R>(id, result),
|
|
|
|
Err(panic) => {
|
|
|
|
let mut message = "server panicked".to_string();
|
|
|
|
|
|
|
|
let panic_message = panic
|
|
|
|
.downcast_ref::<String>()
|
|
|
|
.map(String::as_str)
|
|
|
|
.or_else(|| panic.downcast_ref::<&str>().copied());
|
|
|
|
|
|
|
|
if let Some(panic_message) = panic_message {
|
|
|
|
message.push_str(": ");
|
|
|
|
message.push_str(panic_message)
|
|
|
|
};
|
|
|
|
|
|
|
|
lsp_server::Response::new_err(id, lsp_server::ErrorCode::InternalError as i32, message)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn result_to_response<R>(
|
|
|
|
id: lsp_server::RequestId,
|
|
|
|
result: Result<R::Result>,
|
|
|
|
) -> lsp_server::Response
|
|
|
|
where
|
|
|
|
R: lsp_types::request::Request + 'static,
|
|
|
|
R::Params: DeserializeOwned + 'static,
|
|
|
|
R::Result: Serialize + 'static,
|
|
|
|
{
|
|
|
|
match result {
|
|
|
|
Ok(resp) => lsp_server::Response::new_ok(id, &resp),
|
|
|
|
Err(e) => match e.downcast::<LspError>() {
|
2020-06-25 10:22:18 -05:00
|
|
|
Ok(lsp_error) => lsp_server::Response::new_err(id, lsp_error.code, lsp_error.message),
|
|
|
|
Err(e) => {
|
2021-05-17 12:07:10 -05:00
|
|
|
if is_cancelled(&*e) {
|
2020-06-25 10:22:18 -05:00
|
|
|
lsp_server::Response::new_err(
|
|
|
|
id,
|
|
|
|
lsp_server::ErrorCode::ContentModified as i32,
|
|
|
|
"content modified".to_string(),
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
lsp_server::Response::new_err(
|
|
|
|
id,
|
|
|
|
lsp_server::ErrorCode::InternalError as i32,
|
|
|
|
e.to_string(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2020-06-25 12:23:52 -05:00
|
|
|
}
|
2020-06-25 10:22:18 -05:00
|
|
|
}
|
2020-06-25 10:50:47 -05:00
|
|
|
|
|
|
|
pub(crate) struct NotificationDispatcher<'a> {
|
|
|
|
pub(crate) not: Option<lsp_server::Notification>,
|
|
|
|
pub(crate) global_state: &'a mut GlobalState,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> NotificationDispatcher<'a> {
|
|
|
|
pub(crate) fn on<N>(
|
|
|
|
&mut self,
|
|
|
|
f: fn(&mut GlobalState, N::Params) -> Result<()>,
|
|
|
|
) -> Result<&mut Self>
|
|
|
|
where
|
|
|
|
N: lsp_types::notification::Notification + 'static,
|
|
|
|
N::Params: DeserializeOwned + Send + 'static,
|
|
|
|
{
|
|
|
|
let not = match self.not.take() {
|
|
|
|
Some(it) => it,
|
|
|
|
None => return Ok(self),
|
|
|
|
};
|
|
|
|
let params = match not.extract::<N::Params>(N::METHOD) {
|
|
|
|
Ok(it) => it,
|
|
|
|
Err(not) => {
|
|
|
|
self.not = Some(not);
|
|
|
|
return Ok(self);
|
|
|
|
}
|
|
|
|
};
|
2021-05-08 06:03:12 -05:00
|
|
|
let _pctx = stdx::panic_context::enter(format!(
|
|
|
|
"\nversion: {}\nnotification: {}",
|
|
|
|
env!("REV"),
|
|
|
|
N::METHOD
|
|
|
|
));
|
2020-06-25 10:50:47 -05:00
|
|
|
f(self.global_state, params)?;
|
|
|
|
Ok(self)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn finish(&mut self) {
|
|
|
|
if let Some(not) = &self.not {
|
|
|
|
if !not.method.starts_with("$/") {
|
2021-08-15 07:46:13 -05:00
|
|
|
tracing::error!("unhandled notification: {:?}", not);
|
2020-06-25 10:50:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|