1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
//! A visitor for downcasting arbitrary request (JSON) into a specific type.
use std::{fmt, panic};
use serde::{de::DeserializeOwned, Serialize};
use crate::{
global_state::{GlobalState, GlobalStateSnapshot},
lsp_utils::is_canceled,
main_loop::Task,
LspError, Result,
};
pub(crate) struct RequestDispatcher<'a> {
pub(crate) req: Option<lsp_server::Request>,
pub(crate) global_state: &'a mut GlobalState,
}
impl<'a> RequestDispatcher<'a> {
/// Dispatches the request onto the current thread
pub(crate) fn on_sync<R>(
&mut self,
f: fn(&mut GlobalState, 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) = match self.parse::<R>() {
Some(it) => it,
None => return Ok(self),
};
let world = panic::AssertUnwindSafe(&mut *self.global_state);
let response = panic::catch_unwind(move || {
let _pctx = stdx::panic_context::enter(format!("request: {} {:#?}", R::METHOD, params));
let result = f(world.0, params);
result_to_response::<R>(id, result)
})
.map_err(|_err| format!("sync task {:?} panicked", R::METHOD))?;
self.global_state.respond(response);
Ok(self)
}
/// Dispatches the request onto thread pool
pub(crate) fn on<R>(
&mut self,
f: fn(GlobalStateSnapshot, R::Params) -> Result<R::Result>,
) -> &mut Self
where
R: lsp_types::request::Request + 'static,
R::Params: DeserializeOwned + Send + fmt::Debug + 'static,
R::Result: Serialize + 'static,
{
let (id, params) = match self.parse::<R>() {
Some(it) => it,
None => return self,
};
self.global_state.task_pool.handle.spawn({
let world = self.global_state.snapshot();
move || {
let _pctx =
stdx::panic_context::enter(format!("request: {} {:#?}", R::METHOD, params));
let result = f(world, params);
Task::Response(result_to_response::<R>(id, result))
}
});
self
}
pub(crate) fn finish(&mut self) {
if let Some(req) = self.req.take() {
log::error!("unknown request: {:?}", req);
let response = lsp_server::Response::new_err(
req.id,
lsp_server::ErrorCode::MethodNotFound as i32,
"unknown request".to_string(),
);
self.global_state.respond(response);
}
}
fn parse<R>(&mut self) -> Option<(lsp_server::RequestId, R::Params)>
where
R: lsp_types::request::Request + 'static,
R::Params: DeserializeOwned + 'static,
{
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 {
Ok(params) => return Some((req.id, params)),
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);
return None;
}
}
}
}
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>() {
Ok(lsp_error) => lsp_server::Response::new_err(id, lsp_error.code, lsp_error.message),
Err(e) => {
if is_canceled(&*e) {
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(),
)
}
}
},
}
}
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);
}
};
let _pctx = stdx::panic_context::enter(format!("notification: {}", N::METHOD));
f(self.global_state, params)?;
Ok(self)
}
pub(crate) fn finish(&mut self) {
if let Some(not) = &self.not {
if !not.method.starts_with("$/") {
log::error!("unhandled notification: {:?}", not);
}
}
}
}
|