Skip to main content

aft/lsp/
client.rs

1use std::collections::{HashMap, HashSet};
2use std::io::{self, BufReader, BufWriter};
3use std::path::{Path, PathBuf};
4use std::process::{Child, Command, Stdio};
5use std::str::FromStr;
6use std::sync::atomic::{AtomicI64, Ordering};
7use std::sync::{Arc, Mutex};
8use std::thread;
9use std::time::{Duration, Instant};
10
11use crossbeam_channel::{bounded, RecvTimeoutError, Sender};
12use serde::de::DeserializeOwned;
13use serde_json::{json, Value};
14
15use crate::lsp::child_registry::LspChildRegistry;
16use crate::lsp::jsonrpc::{
17    Notification, Request, RequestId, Response as JsonRpcResponse, ServerMessage,
18};
19use crate::lsp::position::path_to_uri;
20use crate::lsp::registry::ServerKind;
21use crate::lsp::{transport, LspError};
22
23const REQUEST_TIMEOUT: Duration = Duration::from_secs(30);
24const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5);
25const EXIT_POLL_INTERVAL: Duration = Duration::from_millis(25);
26
27type PendingMap = HashMap<RequestId, Sender<JsonRpcResponse>>;
28type WatchedFileRegistrations = Arc<Mutex<HashSet<String>>>;
29
30/// Lifecycle state of a language server.
31#[derive(Debug, Clone, Copy, PartialEq, Eq)]
32pub enum ServerState {
33    Starting,
34    Initializing,
35    Ready,
36    ShuttingDown,
37    Exited,
38}
39
40/// Events sent from background reader threads into the main loop.
41#[derive(Debug)]
42pub enum LspEvent {
43    /// Server sent a notification (e.g. publishDiagnostics).
44    Notification {
45        server_kind: ServerKind,
46        root: PathBuf,
47        method: String,
48        params: Option<Value>,
49    },
50    /// Server sent a request (e.g. workspace/configuration).
51    ServerRequest {
52        server_kind: ServerKind,
53        root: PathBuf,
54        id: RequestId,
55        method: String,
56        params: Option<Value>,
57    },
58    /// Server process exited or the transport stream closed.
59    ServerExited {
60        server_kind: ServerKind,
61        root: PathBuf,
62    },
63}
64
65/// What this server told us it can do during the LSP `initialize` handshake.
66///
67/// We capture this once and use it to route diagnostic requests:
68/// - `pull_diagnostics` → use `textDocument/diagnostic` instead of waiting for push
69/// - `workspace_diagnostics` → use `workspace/diagnostic` for directory mode
70///
71/// Defaults are conservative: `false` means "fall back to push semantics".
72#[derive(Debug, Clone, Default)]
73pub struct ServerDiagnosticCapabilities {
74    /// Server supports `textDocument/diagnostic` (LSP 3.17 per-file pull).
75    pub pull_diagnostics: bool,
76    /// Server supports `workspace/diagnostic` (LSP 3.17 workspace-wide pull).
77    pub workspace_diagnostics: bool,
78    /// `identifier` field from server's diagnosticProvider, if any.
79    /// Used to scope previousResultId tracking when multiple servers share a file.
80    pub identifier: Option<String>,
81    /// Whether the server requested workspace diagnostic refresh notifications.
82    /// We declare `refreshSupport: false` in our client capabilities so this
83    /// should always be false in practice — kept for completeness.
84    pub refresh_support: bool,
85}
86
87/// A client connected to one language server process.
88pub struct LspClient {
89    kind: ServerKind,
90    root: PathBuf,
91    state: ServerState,
92    child: Child,
93    /// Child PID captured at spawn time. Used by Drop to untrack the
94    /// PID from the shared registry; we capture once rather than reading
95    /// `child.id()` later because Drop ordering with the Child can race.
96    child_pid: u32,
97    writer: Arc<Mutex<BufWriter<std::process::ChildStdin>>>,
98
99    /// Pending request responses, keyed by request ID.
100    pending: Arc<Mutex<PendingMap>>,
101    /// Next request ID counter.
102    next_id: AtomicI64,
103    /// Diagnostic capabilities reported by the server in its initialize response.
104    /// `None` until `initialize()` succeeds; conservative defaults thereafter
105    /// when the server doesn't advertise diagnosticProvider.
106    diagnostic_caps: Option<ServerDiagnosticCapabilities>,
107    /// Whether the server advertised `workspace.didChangeWatchedFiles` support
108    /// during `initialize`. When `false` (or `None` pre-init), we skip sending
109    /// `workspace/didChangeWatchedFiles` notifications to avoid spec violations.
110    /// Intentional default: `false` (conservative — requires server opt-in).
111    supports_watched_files: bool,
112    /// Dynamic `workspace/didChangeWatchedFiles` registrations requested by
113    /// the server via `client/registerCapability`. Per LSP, the client must
114    /// not send watched-file notifications merely because a server mentions
115    /// dynamic registration during initialize; a real registration is required.
116    watched_file_registrations: WatchedFileRegistrations,
117    /// Shared registry that tracks live LSP child PIDs across the process
118    /// so the signal handler can SIGKILL them on SIGTERM/SIGINT before
119    /// aft exits. Cloned via `Arc` — multiple clients share the same set.
120    child_registry: LspChildRegistry,
121}
122
123impl LspClient {
124    /// Spawn a new language server process and start the background reader thread.
125    ///
126    /// `child_registry` is a shared handle that records this child's PID so
127    /// the signal handler can SIGKILL it on SIGTERM/SIGINT. Tests that don't
128    /// care about signal cleanup can pass `LspChildRegistry::new()`.
129    pub fn spawn(
130        kind: ServerKind,
131        root: PathBuf,
132        binary: &Path,
133        args: &[String],
134        env: &HashMap<String, String>,
135        event_tx: Sender<LspEvent>,
136        child_registry: LspChildRegistry,
137    ) -> io::Result<Self> {
138        let mut command = Command::new(binary);
139        command
140            .args(args)
141            .current_dir(&root)
142            .stdin(Stdio::piped())
143            .stdout(Stdio::piped())
144            // Use null() instead of piped() to prevent deadlock when the server
145            // writes more than ~64KB to stderr (piped buffer fills, server blocks)
146            .stderr(Stdio::null());
147        for (key, value) in env {
148            command.env(key, value);
149        }
150
151        // Put each LSP child in its own process group so we can SIGKILL the
152        // whole group on shutdown. Critical for npm-wrapped servers like
153        // biome (`node biome lsp-proxy` spawns `cli-darwin-arm64 biome
154        // lsp-proxy` as a child); killing just the wrapper PID leaves the
155        // real server orphaned to PID 1.
156        #[cfg(unix)]
157        unsafe {
158            use std::os::unix::process::CommandExt;
159            command.pre_exec(|| {
160                if libc::setsid() == -1 {
161                    return Err(io::Error::last_os_error());
162                }
163                Ok(())
164            });
165        }
166
167        let mut child = command.spawn()?;
168        let child_pid = child.id();
169        child_registry.track(child_pid);
170
171        let stdout = child
172            .stdout
173            .take()
174            .ok_or_else(|| io::Error::other("language server missing stdout pipe"))?;
175        let stdin = child
176            .stdin
177            .take()
178            .ok_or_else(|| io::Error::other("language server missing stdin pipe"))?;
179
180        let writer = Arc::new(Mutex::new(BufWriter::new(stdin)));
181        let pending = Arc::new(Mutex::new(PendingMap::new()));
182        let watched_file_registrations = Arc::new(Mutex::new(HashSet::new()));
183        let reader_pending = Arc::clone(&pending);
184        let reader_writer = Arc::clone(&writer);
185        let reader_watched_file_registrations = Arc::clone(&watched_file_registrations);
186        let reader_kind = kind.clone();
187        let reader_root = root.clone();
188
189        thread::spawn(move || {
190            let mut reader = BufReader::new(stdout);
191            loop {
192                match transport::read_message(&mut reader) {
193                    Ok(Some(ServerMessage::Response(response))) => {
194                        if let Ok(mut guard) = reader_pending.lock() {
195                            if let Some(tx) = guard.remove(&response.id) {
196                                if tx.send(response).is_err() {
197                                    log::debug!("response channel closed");
198                                }
199                            }
200                        } else {
201                            let _ = event_tx.send(LspEvent::ServerExited {
202                                server_kind: reader_kind.clone(),
203                                root: reader_root.clone(),
204                            });
205                            break;
206                        }
207                    }
208                    Ok(Some(ServerMessage::Notification { method, params })) => {
209                        let _ = event_tx.send(LspEvent::Notification {
210                            server_kind: reader_kind.clone(),
211                            root: reader_root.clone(),
212                            method,
213                            params,
214                        });
215                    }
216                    Ok(Some(ServerMessage::Request { id, method, params })) => {
217                        record_watched_file_registration(
218                            &reader_watched_file_registrations,
219                            &method,
220                            params.as_ref(),
221                        );
222                        // Auto-respond to server requests to prevent deadlocks.
223                        // Server requests (like client/registerCapability,
224                        // window/workDoneProgress/create) block the server until
225                        // we respond. If we don't respond, the server won't send
226                        // responses to OUR pending requests → deadlock.
227                        //
228                        // Dispatch by method to return correct types:
229                        // - workspace/configuration expects Vec<Value> (one per item)
230                        // - Everything else gets null (safe default for registration/progress)
231                        let response_value = if method == "workspace/configuration" {
232                            // Return an array of null configs — one per requested item.
233                            // Servers fall back to filesystem config (tsconfig, pyrightconfig, etc.)
234                            let item_count = params
235                                .as_ref()
236                                .and_then(|p| p.get("items"))
237                                .and_then(|items| items.as_array())
238                                .map_or(1, |arr| arr.len());
239                            serde_json::Value::Array(vec![serde_json::Value::Null; item_count])
240                        } else {
241                            serde_json::Value::Null
242                        };
243                        if let Ok(mut w) = reader_writer.lock() {
244                            let response = super::jsonrpc::OutgoingResponse::success(
245                                id.clone(),
246                                response_value,
247                            );
248                            let _ = transport::write_response(&mut *w, &response);
249                        }
250                        // Also forward as event for any interested handlers
251                        let _ = event_tx.send(LspEvent::ServerRequest {
252                            server_kind: reader_kind.clone(),
253                            root: reader_root.clone(),
254                            id,
255                            method,
256                            params,
257                        });
258                    }
259                    Ok(None) | Err(_) => {
260                        if let Ok(mut guard) = reader_pending.lock() {
261                            guard.clear();
262                        }
263                        let _ = event_tx.send(LspEvent::ServerExited {
264                            server_kind: reader_kind.clone(),
265                            root: reader_root.clone(),
266                        });
267                        break;
268                    }
269                }
270            }
271        });
272
273        Ok(Self {
274            kind,
275            root,
276            state: ServerState::Starting,
277            child,
278            child_pid,
279            writer,
280            pending,
281            next_id: AtomicI64::new(1),
282            diagnostic_caps: None,
283            supports_watched_files: false,
284            watched_file_registrations,
285            child_registry,
286        })
287    }
288
289    /// Send the initialize request and wait for response. Transition to Ready.
290    pub fn initialize(
291        &mut self,
292        workspace_root: &Path,
293        initialization_options: Option<serde_json::Value>,
294    ) -> Result<lsp_types::InitializeResult, LspError> {
295        self.ensure_can_send()?;
296        self.state = ServerState::Initializing;
297
298        let root_url = path_to_uri(workspace_root)?;
299        let root_uri = lsp_types::Uri::from_str(root_url.as_str()).map_err(|_| {
300            LspError::NotFound(format!(
301                "failed to convert workspace root '{}' to file URI",
302                workspace_root.display()
303            ))
304        })?;
305
306        let mut params_value = json!({
307            "processId": std::process::id(),
308            "rootUri": root_uri,
309            "capabilities": {
310                "workspace": {
311                    "workspaceFolders": true,
312                    "configuration": true,
313                    "didChangeWatchedFiles": {
314                        "dynamicRegistration": true
315                    },
316                    // LSP 3.17 workspace diagnostic pull. We declare refreshSupport=false
317                    // because we drive diagnostics on-demand via pull/push and re-query
318                    // when the agent calls lsp_diagnostics again — we don't need the
319                    // server to proactively push refresh notifications.
320                    "diagnostic": {
321                        "refreshSupport": false
322                    }
323                },
324                "textDocument": {
325                    "synchronization": {
326                        "dynamicRegistration": false,
327                        "didSave": true,
328                        "willSave": false,
329                        "willSaveWaitUntil": false
330                    },
331                    "publishDiagnostics": {
332                        "relatedInformation": true,
333                        "versionSupport": true,
334                        "codeDescriptionSupport": true,
335                        "dataSupport": true
336                    },
337                    // LSP 3.17 textDocument diagnostic pull. dynamicRegistration=false
338                    // because we use static capability discovery from the InitializeResult.
339                    // relatedDocumentSupport=true to receive cascading diagnostics for
340                    // files that became known while analyzing the requested one.
341                    "diagnostic": {
342                        "dynamicRegistration": false,
343                        "relatedDocumentSupport": true
344                    }
345                }
346            },
347            "clientInfo": {
348                "name": "aft",
349                "version": env!("CARGO_PKG_VERSION")
350            },
351            "workspaceFolders": [
352                {
353                    "uri": root_uri,
354                    "name": workspace_root
355                        .file_name()
356                        .and_then(|name| name.to_str())
357                        .unwrap_or("workspace")
358                }
359            ]
360        });
361        if let Some(initialization_options) = initialization_options {
362            params_value["initializationOptions"] = initialization_options;
363        }
364
365        let params = serde_json::from_value::<lsp_types::InitializeParams>(params_value)?;
366
367        let result_value = self.send_request_value(
368            <lsp_types::request::Initialize as lsp_types::request::Request>::METHOD,
369            params,
370        )?;
371        let result: lsp_types::InitializeResult = serde_json::from_value(result_value.clone())?;
372
373        // Capture diagnostic capabilities from the initialize response. We parse
374        // from a re-serialized JSON Value because the lsp-types crate's
375        // diagnostic_provider strict variants reject some shapes real servers
376        // emit (e.g. bare `true`), and we want defensive Default fallback.
377        let caps_value = result_value
378            .get("capabilities")
379            .cloned()
380            .unwrap_or_else(|| serde_json::to_value(&result.capabilities).unwrap_or(Value::Null));
381        self.diagnostic_caps = Some(parse_diagnostic_capabilities(&caps_value));
382
383        // Capture whether the server supports workspace/didChangeWatchedFiles.
384        // Missing capability is unsupported by default; callers must not send
385        // notifications unless the server explicitly opted in.
386        self.supports_watched_files = caps_value
387            .pointer("/workspace/didChangeWatchedFiles/dynamicRegistration")
388            .and_then(|v| v.as_bool())
389            .unwrap_or(false)
390            || caps_value
391                .pointer("/workspace/didChangeWatchedFiles")
392                .map(|v| v.is_object() || v.as_bool() == Some(true))
393                .unwrap_or(false);
394
395        self.send_notification::<lsp_types::notification::Initialized>(serde_json::from_value(
396            json!({}),
397        )?)?;
398        self.state = ServerState::Ready;
399        Ok(result)
400    }
401
402    /// Diagnostic capabilities advertised by the server. Returns `None` until
403    /// `initialize()` has succeeded; returns `Some` with conservative defaults
404    /// (all `false`) when the server didn't advertise diagnosticProvider.
405    pub fn diagnostic_capabilities(&self) -> Option<&ServerDiagnosticCapabilities> {
406        self.diagnostic_caps.as_ref()
407    }
408
409    /// Whether the server supports `workspace/didChangeWatchedFiles`.
410    /// Captured from the `initialize` response. Default `false` (conservative).
411    pub fn supports_watched_files(&self) -> bool {
412        self.supports_watched_files
413    }
414
415    /// Whether this server currently has an active dynamic watched-file
416    /// registration. This, not the initialize-time capability shape, controls
417    /// whether `workspace/didChangeWatchedFiles` may be sent.
418    pub fn has_watched_file_registration(&self) -> bool {
419        self.watched_file_registrations
420            .lock()
421            .map(|registrations| !registrations.is_empty())
422            .unwrap_or(false)
423    }
424
425    /// Send a request and wait for the response.
426    pub fn send_request<R>(&mut self, params: R::Params) -> Result<R::Result, LspError>
427    where
428        R: lsp_types::request::Request,
429        R::Params: serde::Serialize,
430        R::Result: DeserializeOwned,
431    {
432        self.ensure_can_send()?;
433
434        let value = self.send_request_value(R::METHOD, params)?;
435        serde_json::from_value(value).map_err(Into::into)
436    }
437
438    /// Send a request and wait up to `timeout` for the response. If the local
439    /// deadline expires, remove the pending response handler and notify the
440    /// server with `$/cancelRequest` so it can stop work.
441    pub fn send_request_with_timeout<R>(
442        &mut self,
443        params: R::Params,
444        timeout: Duration,
445    ) -> Result<R::Result, LspError>
446    where
447        R: lsp_types::request::Request,
448        R::Params: serde::Serialize,
449        R::Result: DeserializeOwned,
450    {
451        self.ensure_can_send()?;
452
453        let value = self.send_request_value_with_timeout(R::METHOD, params, timeout)?;
454        serde_json::from_value(value).map_err(Into::into)
455    }
456
457    fn send_request_value<P>(&mut self, method: &'static str, params: P) -> Result<Value, LspError>
458    where
459        P: serde::Serialize,
460    {
461        self.send_request_value_with_timeout(method, params, REQUEST_TIMEOUT)
462    }
463
464    fn send_request_value_with_timeout<P>(
465        &mut self,
466        method: &'static str,
467        params: P,
468        timeout: Duration,
469    ) -> Result<Value, LspError>
470    where
471        P: serde::Serialize,
472    {
473        self.ensure_can_send()?;
474
475        let id = RequestId::Int(self.next_id.fetch_add(1, Ordering::Relaxed));
476        let (tx, rx) = bounded(1);
477        {
478            let mut pending = self.lock_pending()?;
479            pending.insert(id.clone(), tx);
480        }
481
482        let request = Request::new(id.clone(), method, Some(serde_json::to_value(params)?));
483        {
484            let mut writer = self
485                .writer
486                .lock()
487                .map_err(|_| LspError::ServerNotReady("writer lock poisoned".to_string()))?;
488            if let Err(err) = transport::write_request(&mut *writer, &request) {
489                self.remove_pending(&id);
490                return Err(err.into());
491            }
492        }
493
494        let response = match rx.recv_timeout(timeout) {
495            Ok(response) => response,
496            Err(RecvTimeoutError::Timeout) => {
497                self.remove_pending(&id);
498                self.send_cancel_request(&id)?;
499                return Err(LspError::Timeout(format!(
500                    "timed out waiting for '{}' response from {:?}",
501                    method, self.kind
502                )));
503            }
504            Err(RecvTimeoutError::Disconnected) => {
505                self.remove_pending(&id);
506                return Err(LspError::ServerNotReady(format!(
507                    "language server {:?} disconnected while waiting for '{}'",
508                    self.kind, method
509                )));
510            }
511        };
512
513        if let Some(error) = response.error {
514            return Err(LspError::ServerError {
515                code: error.code,
516                message: error.message,
517            });
518        }
519
520        Ok(response.result.unwrap_or(Value::Null))
521    }
522
523    /// Send a notification (fire-and-forget).
524    pub fn send_notification<N>(&mut self, params: N::Params) -> Result<(), LspError>
525    where
526        N: lsp_types::notification::Notification,
527        N::Params: serde::Serialize,
528    {
529        self.ensure_can_send()?;
530        let notification = Notification::new(N::METHOD, Some(serde_json::to_value(params)?));
531        let mut writer = self
532            .writer
533            .lock()
534            .map_err(|_| LspError::ServerNotReady("writer lock poisoned".to_string()))?;
535        transport::write_notification(&mut *writer, &notification)?;
536        Ok(())
537    }
538
539    /// Graceful shutdown: send shutdown request, then exit notification.
540    pub fn shutdown(&mut self) -> Result<(), LspError> {
541        if self.state == ServerState::Exited {
542            self.child_registry.untrack(self.child_pid);
543            return Ok(());
544        }
545
546        if self.child.try_wait()?.is_some() {
547            self.state = ServerState::Exited;
548            self.child_registry.untrack(self.child_pid);
549            return Ok(());
550        }
551
552        if let Err(err) = self.send_request::<lsp_types::request::Shutdown>(()) {
553            self.state = ServerState::ShuttingDown;
554            if self.child.try_wait()?.is_some() {
555                self.state = ServerState::Exited;
556                return Ok(());
557            }
558            return Err(err);
559        }
560
561        self.state = ServerState::ShuttingDown;
562
563        if let Err(err) = self.send_notification::<lsp_types::notification::Exit>(()) {
564            if self.child.try_wait()?.is_some() {
565                self.state = ServerState::Exited;
566                return Ok(());
567            }
568            return Err(err);
569        }
570
571        let deadline = Instant::now() + SHUTDOWN_TIMEOUT;
572        loop {
573            if self.child.try_wait()?.is_some() {
574                self.state = ServerState::Exited;
575                return Ok(());
576            }
577            if Instant::now() >= deadline {
578                // Kill the entire process group, not just the wrapper PID, so
579                // npm-wrapped servers (biome's `node biome lsp-proxy` spawns
580                // a separate cli-darwin-arm64 child) don't leak orphans.
581                kill_lsp_child_group(&mut self.child);
582                self.state = ServerState::Exited;
583                return Err(LspError::Timeout(format!(
584                    "timed out waiting for {:?} to exit",
585                    self.kind
586                )));
587            }
588            thread::sleep(EXIT_POLL_INTERVAL);
589        }
590    }
591
592    pub fn state(&self) -> ServerState {
593        self.state
594    }
595
596    pub fn kind(&self) -> ServerKind {
597        self.kind.clone()
598    }
599
600    pub fn root(&self) -> &Path {
601        &self.root
602    }
603
604    fn ensure_can_send(&self) -> Result<(), LspError> {
605        if matches!(self.state, ServerState::ShuttingDown | ServerState::Exited) {
606            return Err(LspError::ServerNotReady(format!(
607                "language server {:?} is not ready (state: {:?})",
608                self.kind, self.state
609            )));
610        }
611        Ok(())
612    }
613
614    fn lock_pending(&self) -> Result<std::sync::MutexGuard<'_, PendingMap>, LspError> {
615        self.pending
616            .lock()
617            .map_err(|_| io::Error::other("pending response map poisoned").into())
618    }
619
620    fn remove_pending(&self, id: &RequestId) {
621        if let Ok(mut pending) = self.pending.lock() {
622            pending.remove(id);
623        }
624    }
625
626    fn send_cancel_request(&mut self, id: &RequestId) -> Result<(), LspError> {
627        let notification = Notification::new("$/cancelRequest", Some(json!({ "id": id })));
628        let mut writer = self
629            .writer
630            .lock()
631            .map_err(|_| LspError::ServerNotReady("writer lock poisoned".to_string()))?;
632        transport::write_notification(&mut *writer, &notification)?;
633        Ok(())
634    }
635}
636
637impl Drop for LspClient {
638    fn drop(&mut self) {
639        // Untrack first so the signal handler can't race with this kill and
640        // try to SIGKILL a PID that's already been reaped.
641        self.child_registry.untrack(self.child_pid);
642        kill_lsp_child_group(&mut self.child);
643    }
644}
645
646/// Force-terminate an LSP child and its entire process group on Unix.
647/// On Windows, `taskkill /F /T` kills the process tree.
648///
649/// Necessary because some LSP servers ship as npm-installed Node shims that
650/// spawn the real binary as a child. Killing only the wrapper PID leaves the
651/// real server orphaned to PID 1 and accumulates over time.
652fn kill_lsp_child_group(child: &mut std::process::Child) {
653    #[cfg(unix)]
654    {
655        let pgid = child.id() as i32;
656        crate::bash_background::process::terminate_pgid(pgid, Some(child));
657        let _ = child.wait();
658    }
659    #[cfg(not(unix))]
660    {
661        crate::bash_background::process::terminate_process(child);
662        let _ = child.wait();
663    }
664}
665
666fn record_watched_file_registration(
667    registrations: &WatchedFileRegistrations,
668    method: &str,
669    params: Option<&Value>,
670) {
671    match method {
672        "client/registerCapability" => {
673            let Some(items) = params
674                .and_then(|params| params.get("registrations"))
675                .and_then(|registrations| registrations.as_array())
676            else {
677                return;
678            };
679            if let Ok(mut guard) = registrations.lock() {
680                for item in items {
681                    if item.get("method").and_then(Value::as_str)
682                        == Some("workspace/didChangeWatchedFiles")
683                    {
684                        if let Some(id) = item.get("id").and_then(Value::as_str) {
685                            guard.insert(id.to_string());
686                        }
687                    }
688                }
689            }
690        }
691        "client/unregisterCapability" => {
692            let Some(items) = params
693                .and_then(|params| params.get("unregisterations"))
694                .and_then(|registrations| registrations.as_array())
695            else {
696                return;
697            };
698            if let Ok(mut guard) = registrations.lock() {
699                for item in items {
700                    if item.get("method").and_then(Value::as_str)
701                        == Some("workspace/didChangeWatchedFiles")
702                    {
703                        if let Some(id) = item.get("id").and_then(Value::as_str) {
704                            guard.remove(id);
705                        }
706                    }
707                }
708            }
709        }
710        _ => {}
711    }
712}
713
714/// Parse `ServerDiagnosticCapabilities` from a re-serialized
715/// `ServerCapabilities` JSON value.
716///
717/// LSP 3.17 spec for `diagnosticProvider`:
718/// - `capabilities.diagnosticProvider` may be absent (no pull support),
719///   `DiagnosticOptions`, or `DiagnosticRegistrationOptions`.
720/// - If present:
721///   - `interFileDependencies: bool` (we don't currently use this)
722///   - `workspaceDiagnostics: bool` → workspace pull support
723///   - `identifier?: string` → optional identifier scoping result IDs
724///
725/// We parse the raw JSON Value defensively: presence of any
726/// `diagnosticProvider` value (object or `true`) means the server supports
727/// at least `textDocument/diagnostic` pull.
728fn parse_diagnostic_capabilities(value: &Value) -> ServerDiagnosticCapabilities {
729    let mut caps = ServerDiagnosticCapabilities::default();
730
731    if let Some(provider) = value.get("diagnosticProvider") {
732        // diagnosticProvider can be `true` (rare) or an object. Treat both as
733        // pull_diagnostics support.
734        if provider.is_object() || provider.as_bool() == Some(true) {
735            caps.pull_diagnostics = true;
736        }
737
738        if let Some(obj) = provider.as_object() {
739            if obj
740                .get("workspaceDiagnostics")
741                .and_then(|v| v.as_bool())
742                .unwrap_or(false)
743            {
744                caps.workspace_diagnostics = true;
745            }
746            if let Some(identifier) = obj.get("identifier").and_then(|v| v.as_str()) {
747                caps.identifier = Some(identifier.to_string());
748            }
749        }
750    }
751
752    // Workspace diagnostic refresh (rare — most servers don't request this,
753    // and we declared refreshSupport=false in our client capabilities anyway).
754    if let Some(refresh) = value
755        .get("workspace")
756        .and_then(|w| w.get("diagnostic"))
757        .and_then(|d| d.get("refreshSupport"))
758        .and_then(|r| r.as_bool())
759    {
760        caps.refresh_support = refresh;
761    }
762
763    caps
764}
765
766#[cfg(test)]
767mod tests {
768    use super::*;
769
770    #[test]
771    fn parse_caps_no_diagnostic_provider() {
772        let value = json!({});
773        let caps = parse_diagnostic_capabilities(&value);
774        assert!(!caps.pull_diagnostics);
775        assert!(!caps.workspace_diagnostics);
776        assert!(caps.identifier.is_none());
777    }
778
779    #[test]
780    fn parse_caps_basic_pull_only() {
781        let value = json!({
782            "diagnosticProvider": {
783                "interFileDependencies": false,
784                "workspaceDiagnostics": false
785            }
786        });
787        let caps = parse_diagnostic_capabilities(&value);
788        assert!(caps.pull_diagnostics);
789        assert!(!caps.workspace_diagnostics);
790    }
791
792    #[test]
793    fn parse_caps_full_pull_with_workspace() {
794        let value = json!({
795            "diagnosticProvider": {
796                "interFileDependencies": true,
797                "workspaceDiagnostics": true,
798                "identifier": "rust-analyzer"
799            }
800        });
801        let caps = parse_diagnostic_capabilities(&value);
802        assert!(caps.pull_diagnostics);
803        assert!(caps.workspace_diagnostics);
804        assert_eq!(caps.identifier.as_deref(), Some("rust-analyzer"));
805    }
806
807    #[test]
808    fn parse_caps_provider_as_bare_true() {
809        // LSP 3.17 allows DiagnosticOptions OR boolean — treat true as pull_diagnostics
810        let value = json!({
811            "diagnosticProvider": true
812        });
813        let caps = parse_diagnostic_capabilities(&value);
814        assert!(caps.pull_diagnostics);
815        assert!(!caps.workspace_diagnostics);
816    }
817
818    #[test]
819    fn parse_caps_workspace_refresh_support() {
820        let value = json!({
821            "workspace": {
822                "diagnostic": {
823                    "refreshSupport": true
824                }
825            }
826        });
827        let caps = parse_diagnostic_capabilities(&value);
828        assert!(caps.refresh_support);
829        // No diagnosticProvider → pull still false
830        assert!(!caps.pull_diagnostics);
831    }
832}