async_lsp_client/lib.rs
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
//! # LspServer
//!
//! The client used to connect to the LSP server,
//! and after the connection is completed,
//! the access to the LSP server is abstracted as a method call
//!
//! ## Lifecycle Message
//!
//! ```
//! let (server, rx) = LspServer::new("deno", ["lsp"]);
//! // initialize request
//! let initializeResult = server.initialize(initializeParams).await;
//! // initialized notification
//! server.initialized();
//! // shutdown request
//! server.shutdown();
//! // exit notification
//! server.exit();
//! ```
//!
//! ## Document Synchronization
//! ```rust
//! DidOpenTextDocument
//! server.send_notification::<DidOpenTextDocument>(DidOpenTextDocumentParams { ... }).await;
//! // DidChangeTextDocument
//! server.send_notification::<DidChangeTextDocument>(DidChangeTextDocumentParams { ... }).await;
//! // DidCloseTextDocument
//! server.send_notification::<DidCloseTextDocument>(DidCloseTextDocumentParams { ... }).await;
//! // other
//! ```
//! ## Language Features
//!
//! ```rust
//! // hover
//! server.send_request::<HoverRequest>(HoverParams { ... }).await;
//! // completion
//! server.send_request::<Completion>(CompletionParams { ... }).await;
//! // goto definition
//! server.send_request::<GotoDefinition>(GotoDefinitionParams { ... }).await;
//! // other
//! ```
//!
//! ## Receive requests and notifications from the server
//!
//! The `rx` is used to receive messages from the server. Usually, the message is received in another thread.
//!
//! ```rust
//! let server_ = server.clone(); // Clone the server is used to move.
//! tokio::spawn(async move {
//! loop {
//! let message = rx.recv().await.unwrap();
//! // Process messages
//! match message {
//! ServerMessage::Notification(_) => {},
//! // For requests, you need to send a response
//! ServerMessage::Request(req) => {
//! let id = req.id().unwrap().clone();
//! match req.method() {
//! WorkspaceConfiguration::METHOD => {
//! server_.send_response::<WorkspaceConfiguration>(id, vec![])
//! .await
//! }
//! WorkDoneProgressCreate::METHOD => {
//! server_
//! .send_response::<WorkDoneProgressCreate>(id, ())
//! .await;
//! }
//! _ => {
//! server_
//! .send_error_response(
//! id,
//! jsonrpc::Error {
//! code: jsonrpc::ErrorCode::MethodNotFound,
//! message: std::borrow::Cow::Borrowed("Method Not Found"),
//! data: req.params().cloned(),
//! },
//! )
//! .await;
//! }
//! }
//! }
//! }
//! }
//! });
//! ```
mod cancellation;
mod message;
pub use message::NotificationMessage;
use tracing::warn;
use std::{collections::HashMap, ffi::OsStr, process::Stdio, sync::Arc};
use cancellation::CancellationToken;
use message::{send_message, Message};
use serde_json::json;
use tokio::{
process::{ChildStdin, ChildStdout, Command},
sync::{
mpsc::{self, Receiver, Sender},
Notify, RwLock,
},
};
use tower_lsp::{
jsonrpc::{self, Id, Request, Response},
lsp_types::{
self,
notification::{Exit, Initialized},
request::{Initialize, Shutdown},
InitializeParams, InitializeResult, InitializedParams,
},
};
#[derive(Clone)]
pub struct LspServer {
count: Arc<RwLock<i64>>,
state: Arc<RwLock<ClientState>>,
stdin: Arc<RwLock<ChildStdin>>,
channel_map: Arc<RwLock<HashMap<Id, Sender<Response>>>>,
}
impl LspServer {
pub fn new<S, I>(program: S, args: I) -> (LspServer, Receiver<ServerMessage>)
where
S: AsRef<OsStr>,
I: IntoIterator<Item = S> + Clone,
{
let child = match Command::new(program)
.args(args.clone())
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.spawn()
{
Err(err) => panic!(
"Couldn't spawn: {:?} in {:?}",
err,
args.into_iter()
.map(|v| v.as_ref().to_str().map(|v| v.to_string()))
.collect::<Vec<_>>()
),
Ok(child) => child,
};
let stdin = child.stdin.unwrap();
let mut stdout = child.stdout.unwrap();
let channel_map = Arc::new(RwLock::new(HashMap::<Id, Sender<Response>>::new()));
let channel_map_ = Arc::clone(&channel_map);
let (tx, rx) = mpsc::channel(16);
tokio::spawn(async move { message_loop(&mut stdout, channel_map_, tx).await });
(
LspServer {
count: Arc::new(RwLock::new(0)),
state: Arc::new(RwLock::new(ClientState::Uninitialized)),
stdin: Arc::new(RwLock::new(stdin)),
channel_map,
},
rx,
)
}
pub async fn initialize(&self, params: InitializeParams) -> InitializeResult {
*self.state.write().await = ClientState::Initializing;
let initialize_result = self.send_request::<Initialize>(params).await;
initialize_result
}
pub async fn initialized(&self) {
self.send_notification::<Initialized>(InitializedParams {})
.await;
*self.state.write().await = ClientState::Initialized;
}
pub async fn send_request<R>(&self, params: R::Params) -> R::Result
where
R: lsp_types::request::Request,
{
let mut count = self.count.write().await;
*count += 1;
let id = *count;
let mut stdin = self.stdin.write().await;
send_message(
json!({
"jsonrpc": "2.0",
"id": id,
"method": R::METHOD,
"params": params
}),
&mut stdin,
)
.await;
drop(stdin);
let notify = Arc::new(Notify::new());
let mut token = CancellationToken::new(Arc::clone(¬ify));
let stdin = Arc::clone(&self.stdin);
let cancel = tokio::spawn(async move {
notify.notified().await;
let mut stdin = stdin.write().await;
send_message(
json!({
"jsonrpc": "2.0",
"method": "$/cancelRequest",
"params": {
"id": id,
}
}),
&mut stdin,
)
.await;
});
let (tx, mut rx) = mpsc::channel::<Response>(1);
self.channel_map.write().await.insert(Id::Number(id), tx);
let response = rx.recv().await.unwrap();
token.finish();
cancel.abort();
serde_json::from_value(response.result().unwrap().to_owned()).unwrap()
}
pub async fn send_response<R>(&self, id: Id, result: R::Result)
where
R: lsp_types::request::Request,
{
let mut stdin = self.stdin.write().await;
send_message(
json!({
"jsonrpc": "2.0",
"id": id,
"result": result,
}),
&mut stdin,
)
.await;
}
pub async fn send_error_response(&self, id: Id, error: jsonrpc::Error) {
let mut stdin = self.stdin.write().await;
send_message(
json!({
"jsonrpc": "2.0",
"id": id,
"error": error,
}),
&mut stdin,
)
.await;
}
pub async fn send_notification<N>(&self, params: N::Params)
where
N: lsp_types::notification::Notification,
{
let mut stdin = self.stdin.write().await;
send_message(
json!({
"jsonrpc": "2.0",
"method": N::METHOD,
"params": params
}),
&mut stdin,
)
.await;
}
pub async fn shutdown(&self) {
self.send_request::<Shutdown>(()).await;
*self.state.write().await = ClientState::ShutDown;
}
pub async fn exit(&self) {
self.send_notification::<Exit>(()).await;
*self.state.write().await = ClientState::Exited;
}
}
async fn message_loop(
stdout: &mut ChildStdout,
channel_map: Arc<RwLock<HashMap<Id, Sender<Response>>>>,
tx: Sender<ServerMessage>,
) {
loop {
let msg = message::get_message(stdout).await;
if let Some(msg) = msg {
match msg {
Message::Notification(msg) => {
tx.send(ServerMessage::Notification(msg)).await.unwrap();
}
Message::Request(req) => {
tx.send(ServerMessage::Request(req)).await.unwrap();
}
Message::Response(res) => {
let mut channel_map = channel_map.write().await;
let id = res.id().clone();
if let Some(tx) = channel_map.get(&id) {
let result = tx.send(res).await;
if let Err(err) = result {
if cfg!(feature = "tracing") {
warn!("send error: {:?}", err);
}
}
channel_map.remove(&id);
}
}
}
} else {
break;
}
}
}
#[derive(Clone, Copy)]
enum ClientState {
/// Server has not received an `initialize` request.
Uninitialized = 0,
/// Server received an `initialize` request, but has not yet responded.
Initializing = 1,
/// Server received and responded success to an `initialize` request.
Initialized = 2,
/// Server received a `shutdown` request.
ShutDown = 3,
/// Server received an `exit` notification.
Exited = 4,
}
pub enum ServerMessage {
Request(Request),
Notification(NotificationMessage),
}