1use log::warn;
2pub use lsp_types::request::*;
3pub use lsp_types::*;
4
5use anyhow::{anyhow, Context, Result};
6use collections::HashMap;
7use futures::{channel::oneshot, io::BufWriter, AsyncRead, AsyncWrite, FutureExt};
8use gpui::{AppContext, AsyncAppContext, BackgroundExecutor, Task};
9use parking_lot::Mutex;
10use postage::{barrier, prelude::Stream};
11use serde::{de::DeserializeOwned, Deserialize, Serialize};
12use serde_json::{json, value::RawValue, Value};
13use smol::{
14 channel,
15 io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader},
16 process::{self, Child},
17};
18use std::{
19 ffi::OsString,
20 fmt,
21 future::Future,
22 io::Write,
23 path::PathBuf,
24 str::{self, FromStr as _},
25 sync::{
26 atomic::{AtomicI32, Ordering::SeqCst},
27 Arc, Weak,
28 },
29 time::{Duration, Instant},
30};
31use std::{path::Path, process::Stdio};
32use util::{ResultExt, TryFutureExt};
33
34const HEADER_DELIMITER: &'static [u8; 4] = b"\r\n\r\n";
35const JSON_RPC_VERSION: &str = "2.0";
36const CONTENT_LEN_HEADER: &str = "Content-Length: ";
37const LSP_REQUEST_TIMEOUT: Duration = Duration::from_secs(60 * 2);
38
39type NotificationHandler = Box<dyn Send + FnMut(Option<RequestId>, &str, AsyncAppContext)>;
40type ResponseHandler = Box<dyn Send + FnOnce(Result<String, Error>)>;
41type IoHandler = Box<dyn Send + FnMut(IoKind, &str)>;
42
43/// Kind of language server stdio given to an IO handler.
44#[derive(Debug, Clone, Copy)]
45pub enum IoKind {
46 StdOut,
47 StdIn,
48 StdErr,
49}
50
51/// Represents a launchable language server. This can either be a standalone binary or the path
52/// to a runtime with arguments to instruct it to launch the actual language server file.
53#[derive(Debug, Clone, Deserialize)]
54pub struct LanguageServerBinary {
55 pub path: PathBuf,
56 pub arguments: Vec<OsString>,
57}
58
59/// A running language server process.
60pub struct LanguageServer {
61 server_id: LanguageServerId,
62 next_id: AtomicI32,
63 outbound_tx: channel::Sender<String>,
64 name: String,
65 capabilities: ServerCapabilities,
66 code_action_kinds: Option<Vec<CodeActionKind>>,
67 notification_handlers: Arc<Mutex<HashMap<&'static str, NotificationHandler>>>,
68 response_handlers: Arc<Mutex<Option<HashMap<RequestId, ResponseHandler>>>>,
69 io_handlers: Arc<Mutex<HashMap<i32, IoHandler>>>,
70 executor: BackgroundExecutor,
71 #[allow(clippy::type_complexity)]
72 io_tasks: Mutex<Option<(Task<Option<()>>, Task<Option<()>>)>>,
73 output_done_rx: Mutex<Option<barrier::Receiver>>,
74 root_path: PathBuf,
75 _server: Option<Mutex<Child>>,
76}
77
78/// Identifies a running language server.
79#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
80#[repr(transparent)]
81pub struct LanguageServerId(pub usize);
82
83/// Handle to a language server RPC activity subscription.
84pub enum Subscription {
85 Notification {
86 method: &'static str,
87 notification_handlers: Option<Arc<Mutex<HashMap<&'static str, NotificationHandler>>>>,
88 },
89 Io {
90 id: i32,
91 io_handlers: Option<Weak<Mutex<HashMap<i32, IoHandler>>>>,
92 },
93}
94
95/// Language server protocol RPC request message ID.
96///
97/// [LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#requestMessage)
98#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
99#[serde(untagged)]
100pub enum RequestId {
101 Int(i32),
102 Str(String),
103}
104
105/// Language server protocol RPC request message.
106///
107/// [LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#requestMessage)
108#[derive(Serialize, Deserialize)]
109pub struct Request<'a, T> {
110 jsonrpc: &'static str,
111 id: RequestId,
112 method: &'a str,
113 params: T,
114}
115
116/// Language server protocol RPC request response message before it is deserialized into a concrete type.
117#[derive(Serialize, Deserialize)]
118struct AnyResponse<'a> {
119 jsonrpc: &'a str,
120 id: RequestId,
121 #[serde(default)]
122 error: Option<Error>,
123 #[serde(borrow)]
124 result: Option<&'a RawValue>,
125}
126
127/// Language server protocol RPC request response message.
128///
129/// [LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#responseMessage)
130#[derive(Serialize)]
131struct Response<T> {
132 jsonrpc: &'static str,
133 id: RequestId,
134 result: Option<T>,
135 error: Option<Error>,
136}
137
138/// Language server protocol RPC notification message.
139///
140/// [LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#notificationMessage)
141#[derive(Serialize, Deserialize)]
142struct Notification<'a, T> {
143 jsonrpc: &'static str,
144 #[serde(borrow)]
145 method: &'a str,
146 params: T,
147}
148
149/// Language server RPC notification message before it is deserialized into a concrete type.
150#[derive(Debug, Clone, Deserialize)]
151struct AnyNotification<'a> {
152 #[serde(default)]
153 id: Option<RequestId>,
154 #[serde(borrow)]
155 method: &'a str,
156 #[serde(borrow, default)]
157 params: Option<&'a RawValue>,
158}
159
160#[derive(Debug, Serialize, Deserialize)]
161struct Error {
162 message: String,
163}
164
165impl LanguageServer {
166 /// Starts a language server process.
167 pub fn new(
168 stderr_capture: Arc<Mutex<Option<String>>>,
169 server_id: LanguageServerId,
170 binary: LanguageServerBinary,
171 root_path: &Path,
172 code_action_kinds: Option<Vec<CodeActionKind>>,
173 cx: AsyncAppContext,
174 ) -> Result<Self> {
175 let working_dir = if root_path.is_dir() {
176 root_path
177 } else {
178 root_path.parent().unwrap_or_else(|| Path::new("/"))
179 };
180
181 let mut server = process::Command::new(&binary.path)
182 .current_dir(working_dir)
183 .args(binary.arguments)
184 .stdin(Stdio::piped())
185 .stdout(Stdio::piped())
186 .stderr(Stdio::piped())
187 .kill_on_drop(true)
188 .spawn()?;
189
190 let stdin = server.stdin.take().unwrap();
191 let stdout = server.stdout.take().unwrap();
192 let stderr = server.stderr.take().unwrap();
193 let mut server = Self::new_internal(
194 server_id.clone(),
195 stdin,
196 stdout,
197 Some(stderr),
198 stderr_capture,
199 Some(server),
200 root_path,
201 code_action_kinds,
202 cx,
203 move |notification| {
204 log::info!(
205 "{} unhandled notification {}:\n{}",
206 server_id,
207 notification.method,
208 serde_json::to_string_pretty(
209 ¬ification
210 .params
211 .and_then(|params| Value::from_str(params.get()).ok())
212 .unwrap_or(Value::Null)
213 )
214 .unwrap(),
215 );
216 },
217 );
218
219 if let Some(name) = binary.path.file_name() {
220 server.name = name.to_string_lossy().to_string();
221 }
222
223 Ok(server)
224 }
225
226 fn new_internal<Stdin, Stdout, Stderr, F>(
227 server_id: LanguageServerId,
228 stdin: Stdin,
229 stdout: Stdout,
230 stderr: Option<Stderr>,
231 stderr_capture: Arc<Mutex<Option<String>>>,
232 server: Option<Child>,
233 root_path: &Path,
234 code_action_kinds: Option<Vec<CodeActionKind>>,
235 cx: AsyncAppContext,
236 on_unhandled_notification: F,
237 ) -> Self
238 where
239 Stdin: AsyncWrite + Unpin + Send + 'static,
240 Stdout: AsyncRead + Unpin + Send + 'static,
241 Stderr: AsyncRead + Unpin + Send + 'static,
242 F: FnMut(AnyNotification) + 'static + Send + Sync + Clone,
243 {
244 let (outbound_tx, outbound_rx) = channel::unbounded::<String>();
245 let (output_done_tx, output_done_rx) = barrier::channel();
246 let notification_handlers =
247 Arc::new(Mutex::new(HashMap::<_, NotificationHandler>::default()));
248 let response_handlers =
249 Arc::new(Mutex::new(Some(HashMap::<_, ResponseHandler>::default())));
250 let io_handlers = Arc::new(Mutex::new(HashMap::default()));
251
252 let stdout_input_task = cx.spawn({
253 let on_unhandled_notification = on_unhandled_notification.clone();
254 let notification_handlers = notification_handlers.clone();
255 let response_handlers = response_handlers.clone();
256 let io_handlers = io_handlers.clone();
257 move |cx| {
258 Self::handle_input(
259 stdout,
260 on_unhandled_notification,
261 notification_handlers,
262 response_handlers,
263 io_handlers,
264 cx,
265 )
266 .log_err()
267 }
268 });
269 let stderr_input_task = stderr
270 .map(|stderr| {
271 let io_handlers = io_handlers.clone();
272 let stderr_captures = stderr_capture.clone();
273 cx.spawn(|_| Self::handle_stderr(stderr, io_handlers, stderr_captures).log_err())
274 })
275 .unwrap_or_else(|| Task::Ready(Some(None)));
276 let input_task = cx.spawn(|_| async move {
277 let (stdout, stderr) = futures::join!(stdout_input_task, stderr_input_task);
278 stdout.or(stderr)
279 });
280 let output_task = cx.background_executor().spawn({
281 Self::handle_output(
282 stdin,
283 outbound_rx,
284 output_done_tx,
285 response_handlers.clone(),
286 io_handlers.clone(),
287 )
288 .log_err()
289 });
290
291 Self {
292 server_id,
293 notification_handlers,
294 response_handlers,
295 io_handlers,
296 name: Default::default(),
297 capabilities: Default::default(),
298 code_action_kinds,
299 next_id: Default::default(),
300 outbound_tx,
301 executor: cx.background_executor().clone(),
302 io_tasks: Mutex::new(Some((input_task, output_task))),
303 output_done_rx: Mutex::new(Some(output_done_rx)),
304 root_path: root_path.to_path_buf(),
305 _server: server.map(|server| Mutex::new(server)),
306 }
307 }
308
309 /// List of code action kinds this language server reports being able to emit.
310 pub fn code_action_kinds(&self) -> Option<Vec<CodeActionKind>> {
311 self.code_action_kinds.clone()
312 }
313
314 async fn handle_input<Stdout, F>(
315 stdout: Stdout,
316 mut on_unhandled_notification: F,
317 notification_handlers: Arc<Mutex<HashMap<&'static str, NotificationHandler>>>,
318 response_handlers: Arc<Mutex<Option<HashMap<RequestId, ResponseHandler>>>>,
319 io_handlers: Arc<Mutex<HashMap<i32, IoHandler>>>,
320 cx: AsyncAppContext,
321 ) -> anyhow::Result<()>
322 where
323 Stdout: AsyncRead + Unpin + Send + 'static,
324 F: FnMut(AnyNotification) + 'static + Send,
325 {
326 let mut stdout = BufReader::new(stdout);
327 let _clear_response_handlers = util::defer({
328 let response_handlers = response_handlers.clone();
329 move || {
330 response_handlers.lock().take();
331 }
332 });
333 let mut buffer = Vec::new();
334 loop {
335 buffer.clear();
336
337 read_headers(&mut stdout, &mut buffer).await?;
338
339 let headers = std::str::from_utf8(&buffer)?;
340
341 let message_len = headers
342 .split("\n")
343 .find(|line| line.starts_with(CONTENT_LEN_HEADER))
344 .and_then(|line| line.strip_prefix(CONTENT_LEN_HEADER))
345 .ok_or_else(|| anyhow!("invalid LSP message header {headers:?}"))?
346 .trim_end()
347 .parse()?;
348
349 buffer.resize(message_len, 0);
350 stdout.read_exact(&mut buffer).await?;
351
352 if let Ok(message) = str::from_utf8(&buffer) {
353 log::trace!("incoming message: {message}");
354 for handler in io_handlers.lock().values_mut() {
355 handler(IoKind::StdOut, message);
356 }
357 }
358
359 if let Ok(msg) = serde_json::from_slice::<AnyNotification>(&buffer) {
360 if let Some(handler) = notification_handlers.lock().get_mut(msg.method) {
361 handler(
362 msg.id,
363 msg.params.map(|params| params.get()).unwrap_or("null"),
364 cx.clone(),
365 );
366 } else {
367 on_unhandled_notification(msg);
368 }
369 } else if let Ok(AnyResponse {
370 id, error, result, ..
371 }) = serde_json::from_slice(&buffer)
372 {
373 if let Some(handler) = response_handlers
374 .lock()
375 .as_mut()
376 .and_then(|handlers| handlers.remove(&id))
377 {
378 if let Some(error) = error {
379 handler(Err(error));
380 } else if let Some(result) = result {
381 handler(Ok(result.get().into()));
382 } else {
383 handler(Ok("null".into()));
384 }
385 }
386 } else {
387 warn!(
388 "failed to deserialize LSP message:\n{}",
389 std::str::from_utf8(&buffer)?
390 );
391 }
392
393 // Don't starve the main thread when receiving lots of messages at once.
394 smol::future::yield_now().await;
395 }
396 }
397
398 async fn handle_stderr<Stderr>(
399 stderr: Stderr,
400 io_handlers: Arc<Mutex<HashMap<i32, IoHandler>>>,
401 stderr_capture: Arc<Mutex<Option<String>>>,
402 ) -> anyhow::Result<()>
403 where
404 Stderr: AsyncRead + Unpin + Send + 'static,
405 {
406 let mut stderr = BufReader::new(stderr);
407 let mut buffer = Vec::new();
408
409 loop {
410 buffer.clear();
411
412 let bytes_read = stderr.read_until(b'\n', &mut buffer).await?;
413 if bytes_read == 0 {
414 return Ok(());
415 }
416
417 if let Ok(message) = str::from_utf8(&buffer) {
418 log::trace!("incoming stderr message:{message}");
419 for handler in io_handlers.lock().values_mut() {
420 handler(IoKind::StdErr, message);
421 }
422
423 if let Some(stderr) = stderr_capture.lock().as_mut() {
424 stderr.push_str(message);
425 }
426 }
427
428 // Don't starve the main thread when receiving lots of messages at once.
429 smol::future::yield_now().await;
430 }
431 }
432
433 async fn handle_output<Stdin>(
434 stdin: Stdin,
435 outbound_rx: channel::Receiver<String>,
436 output_done_tx: barrier::Sender,
437 response_handlers: Arc<Mutex<Option<HashMap<RequestId, ResponseHandler>>>>,
438 io_handlers: Arc<Mutex<HashMap<i32, IoHandler>>>,
439 ) -> anyhow::Result<()>
440 where
441 Stdin: AsyncWrite + Unpin + Send + 'static,
442 {
443 let mut stdin = BufWriter::new(stdin);
444 let _clear_response_handlers = util::defer({
445 let response_handlers = response_handlers.clone();
446 move || {
447 response_handlers.lock().take();
448 }
449 });
450 let mut content_len_buffer = Vec::new();
451 while let Ok(message) = outbound_rx.recv().await {
452 log::trace!("outgoing message:{}", message);
453 for handler in io_handlers.lock().values_mut() {
454 handler(IoKind::StdIn, &message);
455 }
456
457 content_len_buffer.clear();
458 write!(content_len_buffer, "{}", message.len()).unwrap();
459 stdin.write_all(CONTENT_LEN_HEADER.as_bytes()).await?;
460 stdin.write_all(&content_len_buffer).await?;
461 stdin.write_all("\r\n\r\n".as_bytes()).await?;
462 stdin.write_all(message.as_bytes()).await?;
463 stdin.flush().await?;
464 }
465 drop(output_done_tx);
466 Ok(())
467 }
468
469 /// Initializes a language server by sending the `Initialize` request.
470 /// Note that `options` is used directly to construct [`InitializeParams`], which is why it is owned.
471 ///
472 /// [LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#initialize)
473 pub fn initialize(
474 mut self,
475 options: Option<Value>,
476 cx: &AppContext,
477 ) -> Task<Result<Arc<Self>>> {
478 let root_uri = Url::from_file_path(&self.root_path).unwrap();
479 #[allow(deprecated)]
480 let params = InitializeParams {
481 process_id: None,
482 root_path: None,
483 root_uri: Some(root_uri.clone()),
484 initialization_options: options,
485 capabilities: ClientCapabilities {
486 workspace: Some(WorkspaceClientCapabilities {
487 configuration: Some(true),
488 did_change_watched_files: Some(DidChangeWatchedFilesClientCapabilities {
489 dynamic_registration: Some(true),
490 relative_pattern_support: Some(true),
491 }),
492 did_change_configuration: Some(DynamicRegistrationClientCapabilities {
493 dynamic_registration: Some(true),
494 }),
495 workspace_folders: Some(true),
496 symbol: Some(WorkspaceSymbolClientCapabilities {
497 resolve_support: None,
498 ..WorkspaceSymbolClientCapabilities::default()
499 }),
500 inlay_hint: Some(InlayHintWorkspaceClientCapabilities {
501 refresh_support: Some(true),
502 }),
503 diagnostic: Some(DiagnosticWorkspaceClientCapabilities {
504 refresh_support: None,
505 }),
506 workspace_edit: Some(WorkspaceEditClientCapabilities {
507 resource_operations: Some(vec![
508 ResourceOperationKind::Create,
509 ResourceOperationKind::Rename,
510 ResourceOperationKind::Delete,
511 ]),
512 document_changes: Some(true),
513 ..WorkspaceEditClientCapabilities::default()
514 }),
515 ..Default::default()
516 }),
517 text_document: Some(TextDocumentClientCapabilities {
518 definition: Some(GotoCapability {
519 link_support: Some(true),
520 dynamic_registration: None,
521 }),
522 code_action: Some(CodeActionClientCapabilities {
523 code_action_literal_support: Some(CodeActionLiteralSupport {
524 code_action_kind: CodeActionKindLiteralSupport {
525 value_set: vec![
526 CodeActionKind::REFACTOR.as_str().into(),
527 CodeActionKind::QUICKFIX.as_str().into(),
528 CodeActionKind::SOURCE.as_str().into(),
529 ],
530 },
531 }),
532 data_support: Some(true),
533 resolve_support: Some(CodeActionCapabilityResolveSupport {
534 properties: vec!["edit".to_string(), "command".to_string()],
535 }),
536 ..Default::default()
537 }),
538 completion: Some(CompletionClientCapabilities {
539 completion_item: Some(CompletionItemCapability {
540 snippet_support: Some(true),
541 resolve_support: Some(CompletionItemCapabilityResolveSupport {
542 properties: vec![
543 "documentation".to_string(),
544 "additionalTextEdits".to_string(),
545 ],
546 }),
547 ..Default::default()
548 }),
549 completion_list: Some(CompletionListCapability {
550 item_defaults: Some(vec![
551 "commitCharacters".to_owned(),
552 "editRange".to_owned(),
553 "insertTextMode".to_owned(),
554 "data".to_owned(),
555 ]),
556 }),
557 ..Default::default()
558 }),
559 rename: Some(RenameClientCapabilities {
560 prepare_support: Some(true),
561 ..Default::default()
562 }),
563 hover: Some(HoverClientCapabilities {
564 content_format: Some(vec![MarkupKind::Markdown]),
565 dynamic_registration: None,
566 }),
567 inlay_hint: Some(InlayHintClientCapabilities {
568 resolve_support: Some(InlayHintResolveClientCapabilities {
569 properties: vec![
570 "textEdits".to_string(),
571 "tooltip".to_string(),
572 "label.tooltip".to_string(),
573 "label.location".to_string(),
574 "label.command".to_string(),
575 ],
576 }),
577 dynamic_registration: Some(false),
578 }),
579 publish_diagnostics: Some(PublishDiagnosticsClientCapabilities {
580 related_information: Some(true),
581 ..Default::default()
582 }),
583 formatting: Some(DynamicRegistrationClientCapabilities {
584 dynamic_registration: None,
585 }),
586 on_type_formatting: Some(DynamicRegistrationClientCapabilities {
587 dynamic_registration: None,
588 }),
589 diagnostic: Some(DiagnosticClientCapabilities {
590 related_document_support: Some(true),
591 dynamic_registration: None,
592 }),
593 ..Default::default()
594 }),
595 experimental: Some(json!({
596 "serverStatusNotification": true,
597 })),
598 window: Some(WindowClientCapabilities {
599 work_done_progress: Some(true),
600 ..Default::default()
601 }),
602 general: None,
603 },
604 trace: None,
605 workspace_folders: Some(vec![WorkspaceFolder {
606 uri: root_uri,
607 name: Default::default(),
608 }]),
609 client_info: Some(ClientInfo {
610 name: release_channel::ReleaseChannel::global(cx)
611 .display_name()
612 .to_string(),
613 version: Some(release_channel::AppVersion::global(cx).to_string()),
614 }),
615 locale: None,
616 };
617
618 cx.spawn(|_| async move {
619 let response = self.request::<request::Initialize>(params).await?;
620 if let Some(info) = response.server_info {
621 self.name = info.name;
622 }
623 self.capabilities = response.capabilities;
624
625 self.notify::<notification::Initialized>(InitializedParams {})?;
626 Ok(Arc::new(self))
627 })
628 }
629
630 /// Sends a shutdown request to the language server process and prepares the [`LanguageServer`] to be dropped.
631 pub fn shutdown(&self) -> Option<impl 'static + Send + Future<Output = Option<()>>> {
632 if let Some(tasks) = self.io_tasks.lock().take() {
633 let response_handlers = self.response_handlers.clone();
634 let next_id = AtomicI32::new(self.next_id.load(SeqCst));
635 let outbound_tx = self.outbound_tx.clone();
636 let executor = self.executor.clone();
637 let mut output_done = self.output_done_rx.lock().take().unwrap();
638 let shutdown_request = Self::request_internal::<request::Shutdown>(
639 &next_id,
640 &response_handlers,
641 &outbound_tx,
642 &executor,
643 (),
644 );
645 let exit = Self::notify_internal::<notification::Exit>(&outbound_tx, ());
646 outbound_tx.close();
647 Some(
648 async move {
649 log::debug!("language server shutdown started");
650 shutdown_request.await?;
651 response_handlers.lock().take();
652 exit?;
653 output_done.recv().await;
654 log::debug!("language server shutdown finished");
655 drop(tasks);
656 anyhow::Ok(())
657 }
658 .log_err(),
659 )
660 } else {
661 None
662 }
663 }
664
665 /// Register a handler to handle incoming LSP notifications.
666 ///
667 /// [LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#notificationMessage)
668 #[must_use]
669 pub fn on_notification<T, F>(&self, f: F) -> Subscription
670 where
671 T: notification::Notification,
672 F: 'static + Send + FnMut(T::Params, AsyncAppContext),
673 {
674 self.on_custom_notification(T::METHOD, f)
675 }
676
677 /// Register a handler to handle incoming LSP requests.
678 ///
679 /// [LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#requestMessage)
680 #[must_use]
681 pub fn on_request<T, F, Fut>(&self, f: F) -> Subscription
682 where
683 T: request::Request,
684 T::Params: 'static + Send,
685 F: 'static + FnMut(T::Params, AsyncAppContext) -> Fut + Send,
686 Fut: 'static + Future<Output = Result<T::Result>>,
687 {
688 self.on_custom_request(T::METHOD, f)
689 }
690
691 /// Registers a handler to inspect all language server process stdio.
692 #[must_use]
693 pub fn on_io<F>(&self, f: F) -> Subscription
694 where
695 F: 'static + Send + FnMut(IoKind, &str),
696 {
697 let id = self.next_id.fetch_add(1, SeqCst);
698 self.io_handlers.lock().insert(id, Box::new(f));
699 Subscription::Io {
700 id,
701 io_handlers: Some(Arc::downgrade(&self.io_handlers)),
702 }
703 }
704
705 /// Removes a request handler registers via [`Self::on_request`].
706 pub fn remove_request_handler<T: request::Request>(&self) {
707 self.notification_handlers.lock().remove(T::METHOD);
708 }
709
710 /// Removes a notification handler registers via [`Self::on_notification`].
711 pub fn remove_notification_handler<T: notification::Notification>(&self) {
712 self.notification_handlers.lock().remove(T::METHOD);
713 }
714
715 /// Checks if a notification handler has been registered via [`Self::on_notification`].
716 pub fn has_notification_handler<T: notification::Notification>(&self) -> bool {
717 self.notification_handlers.lock().contains_key(T::METHOD)
718 }
719
720 #[must_use]
721 fn on_custom_notification<Params, F>(&self, method: &'static str, mut f: F) -> Subscription
722 where
723 F: 'static + FnMut(Params, AsyncAppContext) + Send,
724 Params: DeserializeOwned,
725 {
726 let prev_handler = self.notification_handlers.lock().insert(
727 method,
728 Box::new(move |_, params, cx| {
729 if let Some(params) = serde_json::from_str(params).log_err() {
730 f(params, cx);
731 }
732 }),
733 );
734 assert!(
735 prev_handler.is_none(),
736 "registered multiple handlers for the same LSP method"
737 );
738 Subscription::Notification {
739 method,
740 notification_handlers: Some(self.notification_handlers.clone()),
741 }
742 }
743
744 #[must_use]
745 fn on_custom_request<Params, Res, Fut, F>(&self, method: &'static str, mut f: F) -> Subscription
746 where
747 F: 'static + FnMut(Params, AsyncAppContext) -> Fut + Send,
748 Fut: 'static + Future<Output = Result<Res>>,
749 Params: DeserializeOwned + Send + 'static,
750 Res: Serialize,
751 {
752 let outbound_tx = self.outbound_tx.clone();
753 let prev_handler = self.notification_handlers.lock().insert(
754 method,
755 Box::new(move |id, params, cx| {
756 if let Some(id) = id {
757 match serde_json::from_str(params) {
758 Ok(params) => {
759 let response = f(params, cx.clone());
760 cx.foreground_executor()
761 .spawn({
762 let outbound_tx = outbound_tx.clone();
763 async move {
764 let response = match response.await {
765 Ok(result) => Response {
766 jsonrpc: JSON_RPC_VERSION,
767 id,
768 result: Some(result),
769 error: None,
770 },
771 Err(error) => Response {
772 jsonrpc: JSON_RPC_VERSION,
773 id,
774 result: None,
775 error: Some(Error {
776 message: error.to_string(),
777 }),
778 },
779 };
780 if let Some(response) =
781 serde_json::to_string(&response).log_err()
782 {
783 outbound_tx.try_send(response).ok();
784 }
785 }
786 })
787 .detach();
788 }
789
790 Err(error) => {
791 log::error!(
792 "error deserializing {} request: {:?}, message: {:?}",
793 method,
794 error,
795 params
796 );
797 let response = AnyResponse {
798 jsonrpc: JSON_RPC_VERSION,
799 id,
800 result: None,
801 error: Some(Error {
802 message: error.to_string(),
803 }),
804 };
805 if let Some(response) = serde_json::to_string(&response).log_err() {
806 outbound_tx.try_send(response).ok();
807 }
808 }
809 }
810 }
811 }),
812 );
813 assert!(
814 prev_handler.is_none(),
815 "registered multiple handlers for the same LSP method"
816 );
817 Subscription::Notification {
818 method,
819 notification_handlers: Some(self.notification_handlers.clone()),
820 }
821 }
822
823 /// Get the name of the running language server.
824 pub fn name(&self) -> &str {
825 &self.name
826 }
827
828 /// Get the reported capabilities of the running language server.
829 pub fn capabilities(&self) -> &ServerCapabilities {
830 &self.capabilities
831 }
832
833 /// Get the id of the running language server.
834 pub fn server_id(&self) -> LanguageServerId {
835 self.server_id
836 }
837
838 /// Get the root path of the project the language server is running against.
839 pub fn root_path(&self) -> &PathBuf {
840 &self.root_path
841 }
842
843 /// Sends a RPC request to the language server.
844 ///
845 /// [LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#requestMessage)
846 pub fn request<T: request::Request>(
847 &self,
848 params: T::Params,
849 ) -> impl Future<Output = Result<T::Result>>
850 where
851 T::Result: 'static + Send,
852 {
853 Self::request_internal::<T>(
854 &self.next_id,
855 &self.response_handlers,
856 &self.outbound_tx,
857 &self.executor,
858 params,
859 )
860 }
861
862 fn request_internal<T: request::Request>(
863 next_id: &AtomicI32,
864 response_handlers: &Mutex<Option<HashMap<RequestId, ResponseHandler>>>,
865 outbound_tx: &channel::Sender<String>,
866 executor: &BackgroundExecutor,
867 params: T::Params,
868 ) -> impl 'static + Future<Output = anyhow::Result<T::Result>>
869 where
870 T::Result: 'static + Send,
871 {
872 let id = next_id.fetch_add(1, SeqCst);
873 let message = serde_json::to_string(&Request {
874 jsonrpc: JSON_RPC_VERSION,
875 id: RequestId::Int(id),
876 method: T::METHOD,
877 params,
878 })
879 .unwrap();
880
881 let (tx, rx) = oneshot::channel();
882 let handle_response = response_handlers
883 .lock()
884 .as_mut()
885 .ok_or_else(|| anyhow!("server shut down"))
886 .map(|handlers| {
887 let executor = executor.clone();
888 handlers.insert(
889 RequestId::Int(id),
890 Box::new(move |result| {
891 executor
892 .spawn(async move {
893 let response = match result {
894 Ok(response) => match serde_json::from_str(&response) {
895 Ok(deserialized) => Ok(deserialized),
896 Err(error) => {
897 log::error!("failed to deserialize response from language server: {}. Response from language server: {:?}", error, response);
898 Err(error).context("failed to deserialize response")
899 }
900 }
901 Err(error) => Err(anyhow!("{}", error.message)),
902 };
903 _ = tx.send(response);
904 })
905 .detach();
906 }),
907 );
908 });
909
910 let send = outbound_tx
911 .try_send(message)
912 .context("failed to write to language server's stdin");
913
914 let outbound_tx = outbound_tx.downgrade();
915 let mut timeout = executor.timer(LSP_REQUEST_TIMEOUT).fuse();
916 let started = Instant::now();
917 async move {
918 handle_response?;
919 send?;
920
921 let cancel_on_drop = util::defer(move || {
922 if let Some(outbound_tx) = outbound_tx.upgrade() {
923 Self::notify_internal::<notification::Cancel>(
924 &outbound_tx,
925 CancelParams {
926 id: NumberOrString::Number(id as i32),
927 },
928 )
929 .log_err();
930 }
931 });
932
933 let method = T::METHOD;
934 futures::select! {
935 response = rx.fuse() => {
936 let elapsed = started.elapsed();
937 log::trace!("Took {elapsed:?} to receive response to {method:?} id {id}");
938 cancel_on_drop.abort();
939 response?
940 }
941
942 _ = timeout => {
943 log::error!("Cancelled LSP request task for {method:?} id {id} which took over {LSP_REQUEST_TIMEOUT:?}");
944 anyhow::bail!("LSP request timeout");
945 }
946 }
947 }
948 }
949
950 /// Sends a RPC notification to the language server.
951 ///
952 /// [LSP Specification](https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#notificationMessage)
953 pub fn notify<T: notification::Notification>(&self, params: T::Params) -> Result<()> {
954 Self::notify_internal::<T>(&self.outbound_tx, params)
955 }
956
957 fn notify_internal<T: notification::Notification>(
958 outbound_tx: &channel::Sender<String>,
959 params: T::Params,
960 ) -> Result<()> {
961 let message = serde_json::to_string(&Notification {
962 jsonrpc: JSON_RPC_VERSION,
963 method: T::METHOD,
964 params,
965 })
966 .unwrap();
967 outbound_tx.try_send(message)?;
968 Ok(())
969 }
970}
971
972impl Drop for LanguageServer {
973 fn drop(&mut self) {
974 if let Some(shutdown) = self.shutdown() {
975 self.executor.spawn(shutdown).detach();
976 }
977 }
978}
979
980impl Subscription {
981 /// Detaching a subscription handle prevents it from unsubscribing on drop.
982 pub fn detach(&mut self) {
983 match self {
984 Subscription::Notification {
985 notification_handlers,
986 ..
987 } => *notification_handlers = None,
988 Subscription::Io { io_handlers, .. } => *io_handlers = None,
989 }
990 }
991}
992
993impl fmt::Display for LanguageServerId {
994 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
995 self.0.fmt(f)
996 }
997}
998
999impl fmt::Debug for LanguageServer {
1000 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1001 f.debug_struct("LanguageServer")
1002 .field("id", &self.server_id.0)
1003 .field("name", &self.name)
1004 .finish_non_exhaustive()
1005 }
1006}
1007
1008impl Drop for Subscription {
1009 fn drop(&mut self) {
1010 match self {
1011 Subscription::Notification {
1012 method,
1013 notification_handlers,
1014 } => {
1015 if let Some(handlers) = notification_handlers {
1016 handlers.lock().remove(method);
1017 }
1018 }
1019 Subscription::Io { id, io_handlers } => {
1020 if let Some(io_handlers) = io_handlers.as_ref().and_then(|h| h.upgrade()) {
1021 io_handlers.lock().remove(id);
1022 }
1023 }
1024 }
1025 }
1026}
1027
1028/// Mock language server for use in tests.
1029#[cfg(any(test, feature = "test-support"))]
1030#[derive(Clone)]
1031pub struct FakeLanguageServer {
1032 pub server: Arc<LanguageServer>,
1033 notifications_rx: channel::Receiver<(String, String)>,
1034}
1035
1036#[cfg(any(test, feature = "test-support"))]
1037impl FakeLanguageServer {
1038 /// Construct a fake language server.
1039 pub fn new(
1040 name: String,
1041 capabilities: ServerCapabilities,
1042 cx: AsyncAppContext,
1043 ) -> (LanguageServer, FakeLanguageServer) {
1044 let (stdin_writer, stdin_reader) = async_pipe::pipe();
1045 let (stdout_writer, stdout_reader) = async_pipe::pipe();
1046 let (notifications_tx, notifications_rx) = channel::unbounded();
1047
1048 let server = LanguageServer::new_internal(
1049 LanguageServerId(0),
1050 stdin_writer,
1051 stdout_reader,
1052 None::<async_pipe::PipeReader>,
1053 Arc::new(Mutex::new(None)),
1054 None,
1055 Path::new("/"),
1056 None,
1057 cx.clone(),
1058 |_| {},
1059 );
1060 let fake = FakeLanguageServer {
1061 server: Arc::new(LanguageServer::new_internal(
1062 LanguageServerId(0),
1063 stdout_writer,
1064 stdin_reader,
1065 None::<async_pipe::PipeReader>,
1066 Arc::new(Mutex::new(None)),
1067 None,
1068 Path::new("/"),
1069 None,
1070 cx,
1071 move |msg| {
1072 notifications_tx
1073 .try_send((
1074 msg.method.to_string(),
1075 msg.params
1076 .map(|raw_value| raw_value.get())
1077 .unwrap_or("null")
1078 .to_string(),
1079 ))
1080 .ok();
1081 },
1082 )),
1083 notifications_rx,
1084 };
1085 fake.handle_request::<request::Initialize, _, _>({
1086 let capabilities = capabilities;
1087 move |_, _| {
1088 let capabilities = capabilities.clone();
1089 let name = name.clone();
1090 async move {
1091 Ok(InitializeResult {
1092 capabilities,
1093 server_info: Some(ServerInfo {
1094 name,
1095 ..Default::default()
1096 }),
1097 })
1098 }
1099 }
1100 });
1101
1102 (server, fake)
1103 }
1104}
1105
1106#[cfg(any(test, feature = "test-support"))]
1107impl LanguageServer {
1108 pub fn full_capabilities() -> ServerCapabilities {
1109 ServerCapabilities {
1110 document_highlight_provider: Some(OneOf::Left(true)),
1111 code_action_provider: Some(CodeActionProviderCapability::Simple(true)),
1112 document_formatting_provider: Some(OneOf::Left(true)),
1113 document_range_formatting_provider: Some(OneOf::Left(true)),
1114 definition_provider: Some(OneOf::Left(true)),
1115 type_definition_provider: Some(TypeDefinitionProviderCapability::Simple(true)),
1116 ..Default::default()
1117 }
1118 }
1119}
1120
1121#[cfg(any(test, feature = "test-support"))]
1122impl FakeLanguageServer {
1123 /// See [`LanguageServer::notify`].
1124 pub fn notify<T: notification::Notification>(&self, params: T::Params) {
1125 self.server.notify::<T>(params).ok();
1126 }
1127
1128 /// See [`LanguageServer::request`].
1129 pub async fn request<T>(&self, params: T::Params) -> Result<T::Result>
1130 where
1131 T: request::Request,
1132 T::Result: 'static + Send,
1133 {
1134 self.server.executor.start_waiting();
1135 self.server.request::<T>(params).await
1136 }
1137
1138 /// Attempts [`Self::try_receive_notification`], unwrapping if it has not received the specified type yet.
1139 pub async fn receive_notification<T: notification::Notification>(&mut self) -> T::Params {
1140 self.server.executor.start_waiting();
1141 self.try_receive_notification::<T>().await.unwrap()
1142 }
1143
1144 /// Consumes the notification channel until it finds a notification for the specified type.
1145 pub async fn try_receive_notification<T: notification::Notification>(
1146 &mut self,
1147 ) -> Option<T::Params> {
1148 use futures::StreamExt as _;
1149
1150 loop {
1151 let (method, params) = self.notifications_rx.next().await?;
1152 if method == T::METHOD {
1153 return Some(serde_json::from_str::<T::Params>(¶ms).unwrap());
1154 } else {
1155 log::info!("skipping message in fake language server {:?}", params);
1156 }
1157 }
1158 }
1159
1160 /// Registers a handler for a specific kind of request. Removes any existing handler for specified request type.
1161 pub fn handle_request<T, F, Fut>(
1162 &self,
1163 mut handler: F,
1164 ) -> futures::channel::mpsc::UnboundedReceiver<()>
1165 where
1166 T: 'static + request::Request,
1167 T::Params: 'static + Send,
1168 F: 'static + Send + FnMut(T::Params, gpui::AsyncAppContext) -> Fut,
1169 Fut: 'static + Send + Future<Output = Result<T::Result>>,
1170 {
1171 let (responded_tx, responded_rx) = futures::channel::mpsc::unbounded();
1172 self.server.remove_request_handler::<T>();
1173 self.server
1174 .on_request::<T, _, _>(move |params, cx| {
1175 let result = handler(params, cx.clone());
1176 let responded_tx = responded_tx.clone();
1177 let executor = cx.background_executor().clone();
1178 async move {
1179 executor.simulate_random_delay().await;
1180 let result = result.await;
1181 responded_tx.unbounded_send(()).ok();
1182 result
1183 }
1184 })
1185 .detach();
1186 responded_rx
1187 }
1188
1189 /// Registers a handler for a specific kind of notification. Removes any existing handler for specified notification type.
1190 pub fn handle_notification<T, F>(
1191 &self,
1192 mut handler: F,
1193 ) -> futures::channel::mpsc::UnboundedReceiver<()>
1194 where
1195 T: 'static + notification::Notification,
1196 T::Params: 'static + Send,
1197 F: 'static + Send + FnMut(T::Params, gpui::AsyncAppContext),
1198 {
1199 let (handled_tx, handled_rx) = futures::channel::mpsc::unbounded();
1200 self.server.remove_notification_handler::<T>();
1201 self.server
1202 .on_notification::<T, _>(move |params, cx| {
1203 handler(params, cx.clone());
1204 handled_tx.unbounded_send(()).ok();
1205 })
1206 .detach();
1207 handled_rx
1208 }
1209
1210 /// Removes any existing handler for specified notification type.
1211 pub fn remove_request_handler<T>(&mut self)
1212 where
1213 T: 'static + request::Request,
1214 {
1215 self.server.remove_request_handler::<T>();
1216 }
1217
1218 /// Simulate that the server has started work and notifies about its progress with the specified token.
1219 pub async fn start_progress(&self, token: impl Into<String>) {
1220 let token = token.into();
1221 self.request::<request::WorkDoneProgressCreate>(WorkDoneProgressCreateParams {
1222 token: NumberOrString::String(token.clone()),
1223 })
1224 .await
1225 .unwrap();
1226 self.notify::<notification::Progress>(ProgressParams {
1227 token: NumberOrString::String(token),
1228 value: ProgressParamsValue::WorkDone(WorkDoneProgress::Begin(Default::default())),
1229 });
1230 }
1231
1232 /// Simulate that the server has completed work and notifies about that with the specified token.
1233 pub fn end_progress(&self, token: impl Into<String>) {
1234 self.notify::<notification::Progress>(ProgressParams {
1235 token: NumberOrString::String(token.into()),
1236 value: ProgressParamsValue::WorkDone(WorkDoneProgress::End(Default::default())),
1237 });
1238 }
1239}
1240
1241pub(self) async fn read_headers<Stdout>(
1242 reader: &mut BufReader<Stdout>,
1243 buffer: &mut Vec<u8>,
1244) -> Result<()>
1245where
1246 Stdout: AsyncRead + Unpin + Send + 'static,
1247{
1248 loop {
1249 if buffer.len() >= HEADER_DELIMITER.len()
1250 && buffer[(buffer.len() - HEADER_DELIMITER.len())..] == HEADER_DELIMITER[..]
1251 {
1252 return Ok(());
1253 }
1254
1255 if reader.read_until(b'\n', buffer).await? == 0 {
1256 return Err(anyhow!("cannot read LSP message headers"));
1257 }
1258 }
1259}
1260
1261#[cfg(test)]
1262mod tests {
1263 use super::*;
1264 use gpui::TestAppContext;
1265
1266 #[ctor::ctor]
1267 fn init_logger() {
1268 if std::env::var("RUST_LOG").is_ok() {
1269 env_logger::init();
1270 }
1271 }
1272
1273 #[gpui::test]
1274 async fn test_fake(cx: &mut TestAppContext) {
1275 cx.update(|cx| {
1276 release_channel::init("0.0.0", cx);
1277 });
1278 let (server, mut fake) =
1279 FakeLanguageServer::new("the-lsp".to_string(), Default::default(), cx.to_async());
1280
1281 let (message_tx, message_rx) = channel::unbounded();
1282 let (diagnostics_tx, diagnostics_rx) = channel::unbounded();
1283 server
1284 .on_notification::<notification::ShowMessage, _>(move |params, _| {
1285 message_tx.try_send(params).unwrap()
1286 })
1287 .detach();
1288 server
1289 .on_notification::<notification::PublishDiagnostics, _>(move |params, _| {
1290 diagnostics_tx.try_send(params).unwrap()
1291 })
1292 .detach();
1293
1294 let server = cx.update(|cx| server.initialize(None, cx)).await.unwrap();
1295 server
1296 .notify::<notification::DidOpenTextDocument>(DidOpenTextDocumentParams {
1297 text_document: TextDocumentItem::new(
1298 Url::from_str("file://a/b").unwrap(),
1299 "rust".to_string(),
1300 0,
1301 "".to_string(),
1302 ),
1303 })
1304 .unwrap();
1305 assert_eq!(
1306 fake.receive_notification::<notification::DidOpenTextDocument>()
1307 .await
1308 .text_document
1309 .uri
1310 .as_str(),
1311 "file://a/b"
1312 );
1313
1314 fake.notify::<notification::ShowMessage>(ShowMessageParams {
1315 typ: MessageType::ERROR,
1316 message: "ok".to_string(),
1317 });
1318 fake.notify::<notification::PublishDiagnostics>(PublishDiagnosticsParams {
1319 uri: Url::from_str("file://b/c").unwrap(),
1320 version: Some(5),
1321 diagnostics: vec![],
1322 });
1323 assert_eq!(message_rx.recv().await.unwrap().message, "ok");
1324 assert_eq!(
1325 diagnostics_rx.recv().await.unwrap().uri.as_str(),
1326 "file://b/c"
1327 );
1328
1329 fake.handle_request::<request::Shutdown, _, _>(|_, _| async move { Ok(()) });
1330
1331 drop(server);
1332 fake.receive_notification::<notification::Exit>().await;
1333 }
1334
1335 #[gpui::test]
1336 async fn test_read_headers() {
1337 let mut buf = Vec::new();
1338 let mut reader = smol::io::BufReader::new(b"Content-Length: 123\r\n\r\n" as &[u8]);
1339 read_headers(&mut reader, &mut buf).await.unwrap();
1340 assert_eq!(buf, b"Content-Length: 123\r\n\r\n");
1341
1342 let mut buf = Vec::new();
1343 let mut reader = smol::io::BufReader::new(b"Content-Type: application/vscode-jsonrpc\r\nContent-Length: 1235\r\n\r\n{\"somecontent\":123}" as &[u8]);
1344 read_headers(&mut reader, &mut buf).await.unwrap();
1345 assert_eq!(
1346 buf,
1347 b"Content-Type: application/vscode-jsonrpc\r\nContent-Length: 1235\r\n\r\n"
1348 );
1349
1350 let mut buf = Vec::new();
1351 let mut reader = smol::io::BufReader::new(b"Content-Length: 1235\r\nContent-Type: application/vscode-jsonrpc\r\n\r\n{\"somecontent\":true}" as &[u8]);
1352 read_headers(&mut reader, &mut buf).await.unwrap();
1353 assert_eq!(
1354 buf,
1355 b"Content-Length: 1235\r\nContent-Type: application/vscode-jsonrpc\r\n\r\n"
1356 );
1357 }
1358
1359 #[gpui::test]
1360 fn test_deserialize_string_digit_id() {
1361 let json = r#"{"jsonrpc":"2.0","id":"2","method":"workspace/configuration","params":{"items":[{"scopeUri":"file:///Users/mph/Devel/personal/hello-scala/","section":"metals"}]}}"#;
1362 let notification = serde_json::from_str::<AnyNotification>(json)
1363 .expect("message with string id should be parsed");
1364 let expected_id = RequestId::Str("2".to_string());
1365 assert_eq!(notification.id, Some(expected_id));
1366 }
1367
1368 #[gpui::test]
1369 fn test_deserialize_string_id() {
1370 let json = r#"{"jsonrpc":"2.0","id":"anythingAtAll","method":"workspace/configuration","params":{"items":[{"scopeUri":"file:///Users/mph/Devel/personal/hello-scala/","section":"metals"}]}}"#;
1371 let notification = serde_json::from_str::<AnyNotification>(json)
1372 .expect("message with string id should be parsed");
1373 let expected_id = RequestId::Str("anythingAtAll".to_string());
1374 assert_eq!(notification.id, Some(expected_id));
1375 }
1376
1377 #[gpui::test]
1378 fn test_deserialize_int_id() {
1379 let json = r#"{"jsonrpc":"2.0","id":2,"method":"workspace/configuration","params":{"items":[{"scopeUri":"file:///Users/mph/Devel/personal/hello-scala/","section":"metals"}]}}"#;
1380 let notification = serde_json::from_str::<AnyNotification>(json)
1381 .expect("message with string id should be parsed");
1382 let expected_id = RequestId::Int(2);
1383 assert_eq!(notification.id, Some(expected_id));
1384 }
1385}