panache 2.57.0

An LSP, formatter, and linter for Markdown, Quarto, and R Markdown
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
//! In-process test harness for LSP integration tests.
//!
//! Drives the synchronous handlers directly over a [`GlobalState`] — no JSON-RPC
//! loop, no threads, no async. Notifications mutate the state inline; requests
//! run against a [`StateSnapshot`]. This is `#[doc(hidden)]` and exists only so
//! the external `tests/lsp` suite can exercise realistic flows.

use std::time::{Duration, Instant};

use lsp_types::notification::Notification as _;
use lsp_types::request::Request as _;
use lsp_types::*;

use super::dispatch::server_capabilities;
use super::global_state::{ClientSender, GlobalState};
use super::{documents, handlers};
use crate::salsa::Db;

/// A synchronous, in-memory driver for the LSP handlers.
pub struct LspTester {
    gs: GlobalState,
    client_rx: crossbeam_channel::Receiver<lsp_server::Message>,
}

impl Default for LspTester {
    fn default() -> Self {
        Self::new()
    }
}

impl LspTester {
    pub fn new() -> Self {
        let (tx, client_rx) = crossbeam_channel::unbounded();
        let gs = GlobalState::new(ClientSender::new(tx));
        Self { gs, client_rx }
    }

    fn snapshot(&self) -> super::global_state::StateSnapshot {
        self.gs.snapshot()
    }

    // --- lifecycle ---

    pub fn initialize(&mut self, root_uri: &str) {
        self.initialize_with_options(root_uri, None);
    }

    /// Initialize advertising pull-diagnostics client support (and refresh), so
    /// the server switches into pull mode (push suppressed).
    pub fn initialize_pull(&mut self, root_uri: &str) {
        let folder = WorkspaceFolder {
            uri: root_uri.parse().unwrap(),
            name: "workspace".to_string(),
        };
        let params = InitializeParams {
            workspace_folders: Some(vec![folder]),
            capabilities: ClientCapabilities {
                text_document: Some(TextDocumentClientCapabilities {
                    diagnostic: Some(DiagnosticClientCapabilities {
                        dynamic_registration: None,
                        related_document_support: Some(true),
                    }),
                    ..Default::default()
                }),
                workspace: Some(WorkspaceClientCapabilities {
                    diagnostic: Some(DiagnosticWorkspaceClientCapabilities {
                        refresh_support: Some(true),
                    }),
                    ..Default::default()
                }),
                ..Default::default()
            },
            ..Default::default()
        };
        self.gs.on_initialize(params);
    }

    /// Like [`Self::initialize_pull`] but without advertising
    /// `related_document_support`, so the server serves pull diagnostics while
    /// leaving `related_documents` empty.
    pub fn initialize_pull_no_related(&mut self, root_uri: &str) {
        let folder = WorkspaceFolder {
            uri: root_uri.parse().unwrap(),
            name: "workspace".to_string(),
        };
        let params = InitializeParams {
            workspace_folders: Some(vec![folder]),
            capabilities: ClientCapabilities {
                text_document: Some(TextDocumentClientCapabilities {
                    diagnostic: Some(DiagnosticClientCapabilities {
                        dynamic_registration: None,
                        related_document_support: None,
                    }),
                    ..Default::default()
                }),
                workspace: Some(WorkspaceClientCapabilities {
                    diagnostic: Some(DiagnosticWorkspaceClientCapabilities {
                        refresh_support: Some(true),
                    }),
                    ..Default::default()
                }),
                ..Default::default()
            },
            ..Default::default()
        };
        self.gs.on_initialize(params);
    }

    pub fn initialize_with_options(
        &mut self,
        root_uri: &str,
        initialization_options: Option<serde_json::Value>,
    ) {
        let folder = WorkspaceFolder {
            uri: root_uri.parse().unwrap(),
            name: "workspace".to_string(),
        };
        let params = InitializeParams {
            workspace_folders: Some(vec![folder]),
            initialization_options,
            ..Default::default()
        };
        self.gs.on_initialize(params);
    }

    pub fn initialize_result(&mut self, root_uri: &str) -> InitializeResult {
        self.initialize_result_with_options(root_uri, None)
    }

    pub fn initialize_result_with_options(
        &mut self,
        root_uri: &str,
        initialization_options: Option<serde_json::Value>,
    ) -> InitializeResult {
        self.initialize_with_options(root_uri, initialization_options);
        InitializeResult {
            capabilities: server_capabilities(),
            server_info: Some(ServerInfo {
                name: "panache-lsp".to_string(),
                version: Some(env!("CARGO_PKG_VERSION").to_string()),
            }),
        }
    }

    pub fn experimental_incremental_parsing_enabled(&self) -> bool {
        self.gs.runtime_settings.experimental_incremental_parsing
    }

    pub fn open_document(&mut self, uri: &str, content: &str, language_id: &str) {
        let params = DidOpenTextDocumentParams {
            text_document: TextDocumentItem {
                uri: uri.parse().unwrap(),
                language_id: language_id.to_string(),
                version: 0,
                text: content.to_string(),
            },
        };
        documents::did_open(&mut self.gs, params);
    }

    pub fn close_document(&mut self, uri: &str) {
        let params = DidCloseTextDocumentParams {
            text_document: TextDocumentIdentifier {
                uri: uri.parse().unwrap(),
            },
        };
        documents::did_close(&mut self.gs, params);
    }

    pub fn edit_document(&mut self, uri: &str, changes: Vec<TextDocumentContentChangeEvent>) {
        let params = DidChangeTextDocumentParams {
            text_document: VersionedTextDocumentIdentifier {
                uri: uri.parse().unwrap(),
                version: 1,
            },
            content_changes: changes,
        };
        documents::did_change(&mut self.gs, params);
    }

    pub fn save_document(&mut self, uri: &str) {
        let params = DidSaveTextDocumentParams {
            text_document: TextDocumentIdentifier {
                uri: uri.parse().unwrap(),
            },
            text: None,
        };
        documents::did_save(&mut self.gs, params);
    }

    pub fn did_change_watched_files(&mut self, files: Vec<FileEvent>) {
        let params = DidChangeWatchedFilesParams { changes: files };
        handlers::file_watcher::did_change_watched_files(&mut self.gs, params);
    }

    pub fn did_create_files(&mut self, created: Vec<&str>) {
        let params = CreateFilesParams {
            files: created
                .into_iter()
                .map(|uri| FileCreate {
                    uri: uri.to_string(),
                })
                .collect(),
        };
        handlers::file_operations::did_create_files(&mut self.gs, params);
    }

    pub fn did_delete_files(&mut self, deleted: Vec<&str>) {
        let params = DeleteFilesParams {
            files: deleted
                .into_iter()
                .map(|uri| FileDelete {
                    uri: uri.to_string(),
                })
                .collect(),
        };
        handlers::file_operations::did_delete_files(&mut self.gs, params);
    }

    pub fn did_rename_files(&mut self, renames: Vec<(String, String)>) {
        let params = RenameFilesParams {
            files: renames
                .into_iter()
                .map(|(old_uri, new_uri)| FileRename { old_uri, new_uri })
                .collect(),
        };
        handlers::file_operations::did_rename_files(&mut self.gs, params);
    }

    // --- requests ---

    pub fn format_document(&self, uri: &str) -> Option<Vec<TextEdit>> {
        let params = DocumentFormattingParams {
            text_document: text_doc(uri),
            options: fmt_options(),
            work_done_progress_params: WorkDoneProgressParams::default(),
        };
        handlers::formatting::format_document(&self.snapshot(), params)
    }

    pub fn format_range(
        &self,
        uri: &str,
        start_line: u32,
        start_char: u32,
        end_line: u32,
        end_char: u32,
    ) -> Option<Vec<TextEdit>> {
        let params = DocumentRangeFormattingParams {
            text_document: text_doc(uri),
            range: range(start_line, start_char, end_line, end_char),
            options: fmt_options(),
            work_done_progress_params: WorkDoneProgressParams::default(),
        };
        handlers::formatting::format_range(&self.snapshot(), params)
    }

    pub fn on_type_formatting(
        &self,
        uri: &str,
        line: u32,
        character: u32,
        ch: &str,
    ) -> Option<Vec<TextEdit>> {
        let params = DocumentOnTypeFormattingParams {
            text_document_position: pos_params(uri, line, character),
            ch: ch.to_owned(),
            options: fmt_options(),
        };
        handlers::formatting::format_on_type(&self.snapshot(), params)
    }

    pub fn get_symbols(&self, uri: &str) -> Option<DocumentSymbolResponse> {
        let params = DocumentSymbolParams {
            text_document: text_doc(uri),
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
        };
        handlers::document_symbols::document_symbol(&self.snapshot(), params)
    }

    pub fn document_links(&self, uri: &str) -> Option<Vec<DocumentLink>> {
        let params = DocumentLinkParams {
            text_document: text_doc(uri),
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
        };
        handlers::document_links::document_links(&self.snapshot(), params)
    }

    pub fn resolve_document_link(&self, link: DocumentLink) -> DocumentLink {
        handlers::document_links::document_link_resolve(&self.snapshot(), link)
    }

    pub fn get_workspace_symbols(&self, query: &str) -> Option<Vec<WorkspaceSymbolSummary>> {
        let params = WorkspaceSymbolParams {
            query: query.to_string(),
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
        };
        match handlers::workspace_symbols::workspace_symbol(&self.snapshot(), params) {
            Some(WorkspaceSymbolResponse::Flat(symbols)) => Some(
                symbols
                    .into_iter()
                    .map(|symbol| WorkspaceSymbolSummary {
                        name: symbol.name,
                        location: symbol.location,
                    })
                    .collect(),
            ),
            Some(WorkspaceSymbolResponse::Nested(symbols)) => Some(
                symbols
                    .into_iter()
                    .filter_map(|symbol| {
                        let location = match symbol.location {
                            OneOf::Left(location) => location,
                            OneOf::Right(_) => return None,
                        };
                        Some(WorkspaceSymbolSummary {
                            name: symbol.name,
                            location,
                        })
                    })
                    .collect(),
            ),
            None => None,
        }
    }

    pub fn get_code_actions(
        &self,
        uri: &str,
        start_line: u32,
        start_char: u32,
        end_line: u32,
        end_char: u32,
    ) -> Option<CodeActionResponse> {
        let params = CodeActionParams {
            text_document: text_doc(uri),
            range: range(start_line, start_char, end_line, end_char),
            context: CodeActionContext {
                diagnostics: vec![],
                only: None,
                trigger_kind: None,
            },
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
        };
        handlers::code_actions::code_action(&self.snapshot(), params)
    }

    pub fn get_folding_ranges(&self, uri: &str) -> Option<Vec<FoldingRange>> {
        let params = FoldingRangeParams {
            text_document: text_doc(uri),
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
        };
        handlers::folding_ranges::folding_range(&self.snapshot(), params)
    }

    pub fn semantic_tokens_full(&self, uri: &str) -> Option<SemanticTokensResult> {
        let params = SemanticTokensParams {
            text_document: text_doc(uri),
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
        };
        handlers::semantic_tokens::semantic_tokens_full(&self.snapshot(), params)
    }

    pub fn goto_definition(
        &self,
        uri: &str,
        line: u32,
        character: u32,
    ) -> Option<GotoDefinitionResponse> {
        let params = GotoDefinitionParams {
            text_document_position_params: pos_params(uri, line, character),
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
        };
        handlers::goto_definition::goto_definition(&self.snapshot(), params)
    }

    pub fn references(
        &self,
        uri: &str,
        line: u32,
        character: u32,
        include_declaration: bool,
    ) -> Option<Vec<Location>> {
        let params = ReferenceParams {
            text_document_position: pos_params(uri, line, character),
            context: ReferenceContext {
                include_declaration,
            },
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
        };
        handlers::references::references(&self.snapshot(), params)
    }

    pub fn hover(&self, uri: &str, line: u32, character: u32) -> Option<Hover> {
        let params = HoverParams {
            text_document_position_params: pos_params(uri, line, character),
            work_done_progress_params: WorkDoneProgressParams::default(),
        };
        handlers::hover::hover(&self.snapshot(), params)
    }

    pub fn completion(&self, uri: &str, line: u32, character: u32) -> Option<CompletionResponse> {
        let params = CompletionParams {
            text_document_position: pos_params(uri, line, character),
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
            context: None,
        };
        handlers::completion::completion(&self.snapshot(), params)
    }

    pub fn rename(
        &self,
        uri: &str,
        line: u32,
        character: u32,
        new_name: &str,
    ) -> Option<WorkspaceEdit> {
        let params = RenameParams {
            text_document_position: pos_params(uri, line, character),
            new_name: new_name.to_string(),
            work_done_progress_params: WorkDoneProgressParams::default(),
        };
        handlers::rename::rename(&self.snapshot(), params)
    }

    pub fn prepare_rename(
        &self,
        uri: &str,
        line: u32,
        character: u32,
    ) -> Option<PrepareRenameResponse> {
        let params = pos_params(uri, line, character);
        handlers::prepare_rename::prepare_rename(&self.snapshot(), params)
    }

    pub fn will_rename_files(&self, renames: Vec<(String, String)>) -> Option<WorkspaceEdit> {
        let params = RenameFilesParams {
            files: renames
                .into_iter()
                .map(|(old_uri, new_uri)| FileRename { old_uri, new_uri })
                .collect(),
        };
        handlers::file_rename::will_rename_files(&self.snapshot(), params)
    }

    // --- state inspection (test-only) ---

    pub fn get_document_content(&self, uri: &str) -> Option<String> {
        let state = self.gs.document_map.get(uri)?;
        Some(
            state
                .salsa_file
                .content_or_empty(&self.gs.salsa)
                .to_string(),
        )
    }

    pub fn get_document_tree(&self, uri: &str) -> Option<crate::SyntaxNode> {
        self.gs
            .document_map
            .get(uri)
            .map(|state| crate::SyntaxNode::new_root(state.tree.clone()))
    }

    pub fn get_cached_file_text(&self, path: &std::path::Path) -> Option<String> {
        let file = self.gs.salsa.file_text(path.to_path_buf())?;
        Some(file.content_or_empty(&self.gs.salsa).to_string())
    }

    // --- main-loop pumping (publishes + cancel) ---

    /// Drain any messages the server has emitted to the client since the
    /// last call. Non-blocking.
    pub fn drain_client_messages(&self) -> Vec<lsp_server::Message> {
        let mut out = Vec::new();
        while let Ok(msg) = self.client_rx.try_recv() {
            out.push(msg);
        }
        out
    }

    /// Drain `textDocument/publishDiagnostics` notifications scoped to `uri`.
    pub fn drain_publish_diagnostics(&self, uri: &str) -> Vec<PublishDiagnosticsParams> {
        let target: Uri = uri.parse().expect("valid uri");
        self.drain_all_publish_diagnostics()
            .into_iter()
            .filter(|params| params.uri == target)
            .collect()
    }

    /// Every `publishDiagnostics` notification since the last drain, across all
    /// URIs. The per-URI [`Self::drain_publish_diagnostics`] consumes all client
    /// messages, so use this when a single test must inspect publishes for more
    /// than one URI (e.g. a diagnostic present on a manifest yet absent on the
    /// document).
    pub fn drain_all_publish_diagnostics(&self) -> Vec<PublishDiagnosticsParams> {
        self.drain_client_messages()
            .into_iter()
            .filter_map(|msg| match msg {
                lsp_server::Message::Notification(n)
                    if n.method == notification::PublishDiagnostics::METHOD =>
                {
                    serde_json::from_value::<PublishDiagnosticsParams>(n.params).ok()
                }
                _ => None,
            })
            .collect()
    }

    /// Force any pending lint deadlines to be due, dispatch them onto the
    /// pool, then drain completed tasks through `on_task` until idle or
    /// `timeout` elapses. Used by tests to deterministically exercise the
    /// debounced publish path.
    pub fn pump(&mut self, timeout: Duration) {
        // Force any armed settle to be due immediately.
        if self.gs.settle_deadline.is_some() {
            self.gs.settle_deadline = Some(Instant::now());
        }
        self.gs.dispatch_due_lints();

        let receiver = self.gs.task_receiver.clone();
        let end = Instant::now() + timeout;
        loop {
            let remaining = end.saturating_duration_since(Instant::now());
            if remaining.is_zero() {
                break;
            }
            // Short ceiling so the first lull doesn't strand a slow worker
            // result; we'll retry until the overall `end` is reached.
            let step = remaining.min(Duration::from_millis(50));
            match receiver.recv_timeout(step) {
                Ok(task) => self.gs.on_task(task),
                Err(_) => {
                    if self.gs.settle_deadline.is_none() {
                        break;
                    }
                    self.gs.dispatch_due_lints();
                }
            }
        }
    }

    /// Push a `textDocument/formatting` request through the real dispatcher
    /// so it spawns onto the format pool. Returns the id for use with
    /// [`Self::send_cancel`].
    pub fn send_format_request_raw(&mut self, id: i32, uri: &str) -> lsp_server::RequestId {
        let req_id = lsp_server::RequestId::from(id);
        let params = DocumentFormattingParams {
            text_document: text_doc(uri),
            options: fmt_options(),
            work_done_progress_params: WorkDoneProgressParams::default(),
        };
        let req = lsp_server::Request::new(
            req_id.clone(),
            request::Formatting::METHOD.to_owned(),
            params,
        );
        self.gs.on_request(req);
        req_id
    }

    /// Send a `$/cancelRequest` for the given id through the real dispatcher.
    pub fn send_cancel(&mut self, id: lsp_server::RequestId) {
        // `lsp_server::RequestId` keeps its variants private; serde converts
        // both number and string forms to the LSP `NumberOrString` shape.
        let value = serde_json::to_value(&id).expect("serializable id");
        let cancel_id =
            serde_json::from_value::<NumberOrString>(value).expect("number-or-string id");
        let params = CancelParams { id: cancel_id };
        let not = lsp_server::Notification::new(notification::Cancel::METHOD.to_owned(), params);
        self.gs.on_notification(not);
    }

    pub fn get_built_in_diagnostics(
        &self,
        uri: &str,
    ) -> Option<Vec<crate::linter::diagnostics::Diagnostic>> {
        let state = self.gs.document_map.get(uri)?;
        let plan =
            crate::salsa::built_in_lint_plan(&self.gs.salsa, state.salsa_file, state.salsa_config)
                .clone();
        Some(plan.diagnostics)
    }

    /// Bench helper: re-lint **every open document** over a single snapshot
    /// (built-in only), returning the total publish count so callers can
    /// `black_box` it. This is the per-settle cost of the candidate "re-lint all
    /// open documents per quiescent settle" model (`TODO.md` rust-analyzer
    /// divergence). Unchanged docs should resolve to a `built_in_lint_plan` memo
    /// hit; the residual cost is the un-memoized text clone + `convert_diagnostic`
    /// in [`compute_publishes`](handlers::diagnostics::compute_publishes).
    pub fn relint_all_open_documents(&self) -> usize {
        let snap = self.snapshot();
        let mut total = 0;
        for key in self.gs.document_map.keys() {
            if let Ok(uri) = key.parse::<Uri>() {
                total += handlers::diagnostics::compute_publishes(&snap, &uri, false).len();
            }
        }
        total
    }

    /// Bench helper: the work a single `didChange` does in the current per-doc
    /// model — lint `uri` plus its project-graph dependents, built-in only.
    /// Baseline against which [`Self::relint_all_open_documents`] is compared.
    pub fn relint_with_dependents(&self, uri: &str) -> usize {
        let snap = self.snapshot();
        let Ok(uri) = uri.parse::<Uri>() else {
            return 0;
        };
        handlers::diagnostics::compute_publishes_with_dependents(&snap, &uri, false).len()
    }

    // --- pull diagnostics ---

    /// Whether the server is in pull-diagnostics mode (push suppressed).
    pub fn pull_diagnostics_enabled(&self) -> bool {
        self.gs.supports_pull_diagnostics
    }

    /// Pull `textDocument/diagnostic` for `uri`, optionally with a prior
    /// `result_id` (to exercise `unchanged` reports).
    pub fn document_diagnostic(
        &self,
        uri: &str,
        previous_result_id: Option<&str>,
    ) -> DocumentDiagnosticReportResult {
        let params = DocumentDiagnosticParams {
            text_document: text_doc(uri),
            identifier: None,
            previous_result_id: previous_result_id.map(str::to_string),
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
        };
        handlers::diagnostics::document_diagnostic(&self.gs, params)
    }

    /// Pull `workspace/diagnostic`, optionally with known `(uri, result_id)`
    /// pairs (to exercise `unchanged` reports).
    pub fn workspace_diagnostic(
        &self,
        previous_result_ids: Vec<(&str, &str)>,
    ) -> WorkspaceDiagnosticReportResult {
        let params = WorkspaceDiagnosticParams {
            identifier: None,
            previous_result_ids: previous_result_ids
                .into_iter()
                .map(|(uri, value)| PreviousResultId {
                    uri: uri.parse().unwrap(),
                    value: value.to_string(),
                })
                .collect(),
            work_done_progress_params: WorkDoneProgressParams::default(),
            partial_result_params: PartialResultParams::default(),
        };
        handlers::diagnostics::workspace_diagnostic(&self.gs, params)
    }

    /// Count `workspace/diagnostic/refresh` server→client requests since the last
    /// drain.
    pub fn drain_diagnostic_refresh(&self) -> usize {
        self.drain_client_messages()
            .into_iter()
            .filter(|msg| {
                matches!(
                    msg,
                    lsp_server::Message::Request(req)
                        if req.method == request::WorkspaceDiagnosticRefresh::METHOD
                )
            })
            .count()
    }
}

/// Flattened workspace symbol for assertions.
pub struct WorkspaceSymbolSummary {
    pub name: String,
    pub location: Location,
}

fn text_doc(uri: &str) -> TextDocumentIdentifier {
    TextDocumentIdentifier {
        uri: uri.parse().unwrap(),
    }
}

fn pos_params(uri: &str, line: u32, character: u32) -> TextDocumentPositionParams {
    TextDocumentPositionParams {
        text_document: text_doc(uri),
        position: Position { line, character },
    }
}

fn range(start_line: u32, start_char: u32, end_line: u32, end_char: u32) -> Range {
    Range {
        start: Position {
            line: start_line,
            character: start_char,
        },
        end: Position {
            line: end_line,
            character: end_char,
        },
    }
}

fn fmt_options() -> FormattingOptions {
    FormattingOptions {
        tab_size: 2,
        insert_spaces: true,
        ..Default::default()
    }
}