spectral-m2 1.2.3

A syntax-aware terminal shell for Macaulay2
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
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
//! Optional stdio language-server process, isolated from the terminal thread.

use std::collections::VecDeque;
use std::io::BufReader;
use std::path::Path;
use std::process::{Child, Command, Stdio};
use std::sync::mpsc::{self, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};

use lsp_server::{Message, Notification, Request, RequestId, Response};
use lsp_types::notification::{
    DidChangeTextDocument, DidCloseTextDocument, DidOpenTextDocument, Exit, Initialized,
    Notification as NotificationMethod,
};
use lsp_types::request::{
    Initialize, Request as RequestMethod, SemanticTokensFullRequest, SemanticTokensRefresh,
    Shutdown,
};
use lsp_types::{
    InitializeResult, SemanticTokens, SemanticTokensLegend, SemanticTokensServerCapabilities,
    ServerInfo,
};
use serde_json::{json, Value};

use super::{Document, TOKEN_MODIFIERS, TOKEN_TYPES};

/// The language server Spectral runs, looked up on `PATH`.
pub const SERVER_EXECUTABLE: &str = "m2-ls";
const DEBOUNCE: Duration = Duration::from_millis(100);
const REQUEST_TIMEOUT: Duration = Duration::from_secs(10);
const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(1);
const WORKER_INTERVAL: Duration = Duration::from_millis(10);

/// A completed computation or a terminal client failure.
pub(super) enum Event {
    /// The server finished initializing, named as it names itself or else by its executable.
    Started(ServerInfo),
    Tokens {
        document: Arc<Document>,
        legend: SemanticTokensLegend,
        tokens: Vec<lsp_types::SemanticToken>,
    },
    /// The tokens of a documentation page, analysed apart from the session.
    PageTokens {
        document: Arc<Document>,
        legend: SemanticTokensLegend,
        tokens: Vec<lsp_types::SemanticToken>,
    },
    Failed(String),
}

#[derive(Default)]
struct Mailbox {
    latest: Option<(Arc<Document>, Instant)>,
    pages: VecDeque<Arc<Document>>,
    stop: bool,
}

/// Nonblocking handle to one optional language-server worker.
pub(super) struct Client {
    mailbox: Arc<Mutex<Mailbox>>,
    events: Receiver<Event>,
    worker: Option<JoinHandle<()>>,
}

impl Client {
    pub(super) fn start() -> Self {
        Self::spawn(Command::new(SERVER_EXECUTABLE), REQUEST_TIMEOUT)
    }

    fn spawn(mut command: Command, timeout: Duration) -> Self {
        let mailbox = Arc::new(Mutex::new(Mailbox::default()));
        let shared = mailbox.clone();
        let (sender, events) = mpsc::channel();
        let worker = thread::spawn(move || {
            let result = command
                .stdin(Stdio::piped())
                .stdout(Stdio::piped())
                .stderr(Stdio::null())
                .spawn()
                .map_err(|error| format!("cannot start {SERVER_EXECUTABLE}: {error}"))
                .and_then(|child| serve(child, &shared, &sender, timeout));
            if let Err(error) = result {
                let _ = sender.send(Event::Failed(error));
            }
        });
        Self {
            mailbox,
            events,
            worker: Some(worker),
        }
    }

    #[cfg(test)]
    pub(super) fn test_channel() -> (Self, Sender<Event>) {
        let (sender, events) = mpsc::channel();
        (
            Self {
                mailbox: Arc::new(Mutex::new(Mailbox::default())),
                events,
                worker: None,
            },
            sender,
        )
    }

    pub(super) fn update(&self, document: Arc<Document>) {
        self.mailbox.lock().unwrap().latest = Some((document, Instant::now()));
    }

    /// Queue a documentation page. Unlike the session's edits, none supersedes another.
    pub(super) fn analyse_page(&self, page: Arc<Document>) {
        self.mailbox.lock().unwrap().pages.push_back(page);
    }

    pub(super) fn poll(&self) -> Vec<Event> {
        self.events.try_iter().collect()
    }
}

impl Drop for Client {
    fn drop(&mut self) {
        self.mailbox.lock().unwrap().stop = true;
        if let Some(worker) = self.worker.take() {
            let _ = worker.join();
        }
    }
}

enum WireEvent {
    Message(Message),
    Closed(String),
}

/// Owns the child and reaps it even when initialization or protocol handling fails.
struct Process {
    child: Child,
    outgoing: Sender<Message>,
    incoming: Receiver<WireEvent>,
}

impl Process {
    fn new(mut child: Child) -> Self {
        let stdout = child.stdout.take().expect("piped server stdout");
        let mut stdin = child.stdin.take().expect("piped server stdin");
        let (incoming_tx, incoming) = mpsc::channel();
        let read_events = incoming_tx.clone();
        thread::spawn(move || {
            let mut reader = BufReader::new(stdout);
            loop {
                match Message::read(&mut reader) {
                    Ok(Some(message)) => {
                        if read_events.send(WireEvent::Message(message)).is_err() {
                            break;
                        }
                    }
                    result => {
                        let error = match result {
                            Err(error) => format!("language-server read failed: {error}"),
                            _ => "language server exited".to_string(),
                        };
                        let _ = read_events.send(WireEvent::Closed(error));
                        break;
                    }
                }
            }
        });
        let (outgoing, messages) = mpsc::channel::<Message>();
        thread::spawn(move || {
            for message in messages {
                if let Err(error) = message.write(&mut stdin) {
                    let _ = incoming_tx.send(WireEvent::Closed(format!(
                        "language-server write failed: {error}"
                    )));
                    break;
                }
            }
        });
        Self {
            child,
            outgoing,
            incoming,
        }
    }

    fn send(&self, message: impl Into<Message>) -> Result<(), String> {
        self.outgoing
            .send(message.into())
            .map_err(|_| "language-server writer stopped".to_string())
    }

    fn request(&self, id: i32, method: &str, params: Value) -> Result<(), String> {
        self.send(Request {
            id: id.into(),
            method: method.to_string(),
            params,
        })
    }

    fn notify(&self, method: &str, params: Value) -> Result<(), String> {
        self.send(Notification::new(method.to_string(), params))
    }

    fn shutdown(&mut self, initialized: bool) {
        if !initialized {
            return;
        }
        let deadline = Instant::now() + SHUTDOWN_TIMEOUT;
        let shutdown_id = RequestId::from(i32::MAX);
        let _ = self.request(i32::MAX, Shutdown::METHOD, Value::Null);
        while Instant::now() < deadline {
            if self.child.try_wait().ok().flatten().is_some() {
                return;
            }
            if let Ok(WireEvent::Message(Message::Response(response))) =
                self.incoming.recv_timeout(WORKER_INTERVAL)
            {
                if response.id == shutdown_id {
                    let _ = self.notify(Exit::METHOD, Value::Null);
                }
            }
        }
    }
}

impl Drop for Process {
    fn drop(&mut self) {
        if self.child.try_wait().ok().flatten().is_none() {
            let _ = self.child.kill();
        }
        let _ = self.child.wait();
    }
}

struct OpenDocument {
    generation: u64,
    version: i32,
    uri: String,
}

/// A semantic-token request awaiting its response. A page's document is open only
/// for this request, so it carries the URI to close.
struct InFlight {
    id: RequestId,
    document: Arc<Document>,
    sent: Instant,
    page_uri: Option<String>,
}

/// A URI for source no file holds, beside the workspace so the server resolves
/// packages as it would for the workspace's own files.
fn scratch_uri(root: &Path, kind: &str, generation: u64) -> Result<String, String> {
    let path = root.join(format!(
        ".spectral-{kind}-{}-{generation}.m2",
        std::process::id()
    ));
    url::Url::from_file_path(path)
        .map(|uri| uri.to_string())
        .map_err(|_| "cannot form source URI".to_string())
}

fn next_request_id(next_id: &mut i32) -> Result<i32, String> {
    let id = *next_id;
    *next_id = next_id
        .checked_add(1)
        .filter(|id| *id < i32::MAX)
        .ok_or("LSP request identifiers exhausted")?;
    Ok(id)
}

fn supports_full(full: &Option<lsp_types::SemanticTokensFullOptions>) -> bool {
    matches!(
        full,
        Some(
            lsp_types::SemanticTokensFullOptions::Bool(true)
                | lsp_types::SemanticTokensFullOptions::Delta { .. }
        )
    )
}

fn serve(
    child: Child,
    mailbox: &Mutex<Mailbox>,
    events: &Sender<Event>,
    timeout: Duration,
) -> Result<(), String> {
    let mut process = Process::new(child);
    let root = std::env::current_dir().map_err(|error| error.to_string())?;
    let root_uri = url::Url::from_directory_path(&root)
        .map_err(|_| "cannot form workspace URI".to_string())?;
    process.request(
        0,
        Initialize::METHOD,
        json!({
            "processId": std::process::id(),
            "rootUri": root_uri.as_str(),
            "capabilities": {
                "general": { "positionEncodings": ["utf-16"] },
                "workspace": { "semanticTokens": { "refreshSupport": true } },
                "textDocument": { "semanticTokens": {
                    "requests": { "full": true },
                    "tokenTypes": TOKEN_TYPES,
                    "tokenModifiers": TOKEN_MODIFIERS,
                    "formats": ["relative"],
                    "augmentsSyntaxTokens": true,
                    "overlappingTokenSupport": false,
                    "multilineTokenSupport": false
                }}
            },
            "initializationOptions": { "diagnostics": { "enabled": false } }
        }),
    )?;
    let started = Instant::now();
    let mut initialized = false;
    let mut legend = None;
    let mut pending = None;
    let mut latest = None;
    let mut open: Option<OpenDocument> = None;
    let mut next_id = 1i32;
    let mut pages = VecDeque::new();
    let mut in_flight: Option<InFlight> = None;
    loop {
        {
            let mut mailbox = mailbox.lock().unwrap();
            if mailbox.stop {
                drop(mailbox);
                if let Some(opened) = &open {
                    let _ = process.notify(
                        DidCloseTextDocument::METHOD,
                        json!({ "textDocument": { "uri": opened.uri } }),
                    );
                }
                process.shutdown(initialized);
                return Ok(());
            }
            if let Some(document) = mailbox.latest.take() {
                latest = Some(document.0.clone());
                pending = Some(document);
            }
            pages.append(&mut mailbox.pages);
        }
        if !initialized && started.elapsed() >= timeout {
            return Err("language-server initialization timed out".to_string());
        }
        if in_flight
            .as_ref()
            .is_some_and(|request| request.sent.elapsed() >= timeout)
        {
            return Err("semantic-token request timed out".to_string());
        }
        if initialized && legend.is_none() && (pending.is_some() || !pages.is_empty()) {
            return Err("language server does not support full semantic tokens".to_string());
        }
        if initialized
            && in_flight.is_none()
            && pending
                .as_ref()
                .is_some_and(|(_, changed): &(Arc<Document>, Instant)| {
                    changed.elapsed() >= DEBOUNCE
                })
        {
            let (document, _) = pending.take().unwrap();
            if open
                .as_ref()
                .is_none_or(|opened| opened.generation != document.generation)
            {
                if let Some(opened) = open.take() {
                    process.notify(
                        DidCloseTextDocument::METHOD,
                        json!({ "textDocument": { "uri": opened.uri } }),
                    )?;
                }
                let uri = scratch_uri(&root, "session", document.generation)?;
                process.notify(DidOpenTextDocument::METHOD, json!({ "textDocument": {
                    "uri": uri, "languageId": "macaulay2", "version": document.version, "text": document.text
                }}))?;
                open = Some(OpenDocument {
                    generation: document.generation,
                    version: document.version,
                    uri,
                });
            } else if open.as_ref().unwrap().version != document.version {
                process.notify(DidChangeTextDocument::METHOD, json!({
                    "textDocument": { "uri": open.as_ref().unwrap().uri, "version": document.version },
                    "contentChanges": [{ "text": document.text }]
                }))?;
                open.as_mut().unwrap().version = document.version;
            }
            let id = next_request_id(&mut next_id)?;
            process.request(
                id,
                SemanticTokensFullRequest::METHOD,
                json!({ "textDocument": { "uri": open.as_ref().unwrap().uri } }),
            )?;
            in_flight = Some(InFlight {
                id: id.into(),
                document,
                sent: Instant::now(),
                page_uri: None,
            });
        }
        if initialized && in_flight.is_none() {
            if let Some(document) = pages.pop_front() {
                let uri = scratch_uri(&root, "page", document.generation)?;
                process.notify(DidOpenTextDocument::METHOD, json!({ "textDocument": {
                    "uri": uri, "languageId": "macaulay2", "version": document.version, "text": document.text
                }}))?;
                let id = next_request_id(&mut next_id)?;
                process.request(
                    id,
                    SemanticTokensFullRequest::METHOD,
                    json!({ "textDocument": { "uri": uri } }),
                )?;
                in_flight = Some(InFlight {
                    id: id.into(),
                    document,
                    sent: Instant::now(),
                    page_uri: Some(uri),
                });
            }
        }
        match process.incoming.recv_timeout(WORKER_INTERVAL) {
            Ok(WireEvent::Message(Message::Response(response)))
                if response.id == RequestId::from(0) =>
            {
                let result = response.response_result.map_err(|error| {
                    format!("language-server initialization failed: {}", error.message)
                })?;
                let result: InitializeResult =
                    serde_json::from_value(result).map_err(|error| error.to_string())?;
                if result
                    .capabilities
                    .position_encoding
                    .as_ref()
                    .is_some_and(|encoding| *encoding != lsp_types::PositionEncodingKind::UTF16)
                {
                    return Err(
                        "language server selected unsupported position encoding".to_string()
                    );
                }
                legend = match result.capabilities.semantic_tokens_provider {
                    Some(SemanticTokensServerCapabilities::SemanticTokensOptions(options))
                        if supports_full(&options.full) =>
                    {
                        Some(options.legend)
                    }
                    Some(SemanticTokensServerCapabilities::SemanticTokensRegistrationOptions(
                        options,
                    )) if supports_full(&options.semantic_tokens_options.full) => {
                        Some(options.semantic_tokens_options.legend)
                    }
                    _ => None,
                };
                process.notify(Initialized::METHOD, json!({}))?;
                initialized = true;
                let _ = events.send(Event::Started(result.server_info.unwrap_or_else(|| {
                    ServerInfo {
                        name: SERVER_EXECUTABLE.to_string(),
                        version: None,
                    }
                })));
            }
            Ok(WireEvent::Message(Message::Response(response))) => {
                if in_flight
                    .as_ref()
                    .is_none_or(|request| request.id != response.id)
                {
                    continue;
                }
                let InFlight {
                    document, page_uri, ..
                } = in_flight.take().unwrap();
                if let Some(uri) = &page_uri {
                    process.notify(
                        DidCloseTextDocument::METHOD,
                        json!({ "textDocument": { "uri": uri } }),
                    )?;
                } else if latest
                    .as_ref()
                    .is_some_and(|latest| latest.version != document.version)
                {
                    continue;
                }
                let result = response
                    .response_result
                    .map_err(|error| format!("semantic-token request failed: {}", error.message))?;
                let tokens = if result.is_null() {
                    Vec::new()
                } else {
                    serde_json::from_value::<SemanticTokens>(result)
                        .map_err(|error| format!("invalid semantic tokens: {error}"))?
                        .data
                };
                let legend = legend.as_ref().unwrap().clone();
                let _ = events.send(match page_uri {
                    Some(_) => Event::PageTokens {
                        document,
                        legend,
                        tokens,
                    },
                    None => Event::Tokens {
                        document,
                        legend,
                        tokens,
                    },
                });
            }
            Ok(WireEvent::Message(Message::Request(request))) => {
                let response = if request.method == SemanticTokensRefresh::METHOD {
                    if pending.is_none() {
                        pending = latest
                            .clone()
                            .map(|document| (document, Instant::now() - DEBOUNCE));
                    }
                    Response::new_ok(request.id, Value::Null)
                } else {
                    Response::new_err(
                        request.id,
                        lsp_server::ErrorCode::MethodNotFound as i32,
                        "unsupported client request".to_string(),
                    )
                };
                process.send(response)?;
            }
            Ok(WireEvent::Message(Message::Notification(_))) => {}
            Ok(WireEvent::Closed(error)) => return Err(error),
            Err(mpsc::RecvTimeoutError::Timeout) => {}
            Err(mpsc::RecvTimeoutError::Disconnected) => {
                return Err("language-server reader stopped".to_string())
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::code::highlight::Highlighter;
    use crate::code::semantic::{SemanticHighlighter, SourceId};
    use crate::ui::controller::settings::theme::groups::HLGroup;

    // The fixture checks wire ordering independently of the client implementation.
    const SERVER: &str = r#"
import json, sys, time
mode = sys.argv[1]
def read():
    headers = {}
    while True:
        line = sys.stdin.buffer.readline()
        if not line: return None
        if line == b'\r\n': break
        key, value = line.decode().split(':', 1)
        headers[key.lower()] = value.strip()
    return json.loads(sys.stdin.buffer.read(int(headers['content-length'])))
def send(message):
    body = json.dumps(message).encode()
    sys.stdout.buffer.write(b'Content-Length: ' + str(len(body)).encode() + b'\r\n\r\n' + body)
    sys.stdout.buffer.flush()
def reply(request, value):
    send({'jsonrpc':'2.0', 'id':request['id'], 'result':value})
request = read()
assert request['method'] == 'initialize'
assert request['params']['capabilities']['textDocument']['semanticTokens']['augmentsSyntaxTokens']
if mode == 'initialize-timeout':
    time.sleep(5)
    sys.exit(0)
reply(request, {'serverInfo':{'name':'fixture-ls','version':'0.1'}, 'capabilities':{'semanticTokensProvider': {'full':True, 'legend': {'tokenTypes':['function'], 'tokenModifiers':[]}}}})
assert read()['method'] == 'initialized'
opened = None
refreshed = False
while True:
    request = read()
    if request is None: break
    method = request.get('method')
    if method == 'textDocument/didOpen':
        opened = request['params']['textDocument']
        assert opened['languageId'] == 'macaulay2'
    elif method == 'textDocument/didChange':
        assert opened is not None
        assert request['params']['textDocument']['version'] > opened['version']
        opened['version'] = request['params']['textDocument']['version']
        opened['text'] = request['params']['contentChanges'][0]['text']
    elif method == 'textDocument/didClose':
        opened = None
    elif method == 'textDocument/semanticTokens/full':
        assert opened is not None
        if mode == 'request-timeout':
            time.sleep(5)
            sys.exit(0)
        if mode == 'malformed':
            reply(request, {'data':[0,0]})
            continue
        if mode == 'exit': sys.exit(0)
        reply(request, {'data':[0,0,1,0,0]})
        if mode == 'refresh' and not refreshed:
            refreshed = True
            send({'jsonrpc':'2.0', 'id':'refresh', 'method':'workspace/semanticTokens/refresh', 'params':None})
    elif method == 'shutdown':
        reply(request, None)
    elif method == 'exit': break
    elif method is None:
        assert request['id'] == 'refresh' and request['result'] is None
    else: raise AssertionError(method)
"#;

    fn fixture(mode: &str, timeout: Duration) -> Client {
        let mut command = Command::new("python3");
        command.args(["-c", SERVER, mode]);
        Client::spawn(command, timeout)
    }

    fn document(generation: u64, version: i32, text: &str) -> Arc<Document> {
        let mut document = Document::new(generation);
        document.version = version;
        document.push(SourceId::Draft(generation), text);
        Arc::new(document)
    }

    fn next(client: &Client) -> Event {
        client
            .events
            .recv_timeout(Duration::from_secs(5))
            .expect("server fixture response")
    }

    /// The next event after the server's startup announcement.
    fn event(client: &Client) -> Event {
        match next(client) {
            Event::Started(_) => next(client),
            event => event,
        }
    }

    #[test]
    fn initialization_announces_the_server_by_name_and_version() {
        let client = fixture("normal", REQUEST_TIMEOUT);
        match next(&client) {
            Event::Started(info) => {
                assert_eq!(info.name, "fixture-ls");
                assert_eq!(info.version.as_deref(), Some("0.1"));
            }
            _ => panic!("the server announces itself first"),
        }
    }

    #[test]
    fn stdio_lifecycle_updates_versions_and_reopens_after_restart() {
        let client = fixture("normal", REQUEST_TIMEOUT);
        for document in [
            document(0, 1, "f"),
            document(0, 2, "g"),
            document(1, 3, "h"),
        ] {
            client.update(document.clone());
            match event(&client) {
                Event::Tokens {
                    document: answered,
                    tokens,
                    ..
                } => {
                    assert_eq!(answered, document);
                    assert_eq!(tokens.len(), 1);
                }
                Event::Failed(error) => panic!("{error}"),
                Event::Started(_) | Event::PageTokens { .. } => {
                    panic!("the server starts once and only the session was analysed")
                }
            }
        }
        let before = Instant::now();
        drop(client);
        assert!(before.elapsed() < SHUTDOWN_TIMEOUT + Duration::from_millis(500));
    }

    #[test]
    fn refresh_is_answered_and_requests_tokens_again() {
        let client = fixture("refresh", REQUEST_TIMEOUT);
        client.update(document(0, 1, "f"));
        assert!(matches!(event(&client), Event::Tokens { .. }));
        assert!(matches!(event(&client), Event::Tokens { .. }));
    }

    #[test]
    fn edits_are_coalesced_to_the_latest_document() {
        let client = fixture("normal", REQUEST_TIMEOUT);
        for version in 1..20 {
            client.update(document(0, version, "f"));
        }
        match event(&client) {
            Event::Tokens { document, .. } => assert_eq!(document.version, 19),
            Event::Failed(error) => panic!("{error}"),
            Event::Started(_) | Event::PageTokens { .. } => {
                panic!("the server starts once and only the session was analysed")
            }
        }
        assert!(client.events.recv_timeout(DEBOUNCE).is_err());
    }

    #[test]
    fn every_queued_page_is_analysed_on_its_own() {
        let client = fixture("normal", REQUEST_TIMEOUT);
        for page in [7, 8] {
            client.analyse_page(document(page, 1, "f"));
        }
        for page in [7, 8] {
            match event(&client) {
                Event::PageTokens {
                    document, tokens, ..
                } => {
                    assert_eq!(document.generation, page);
                    assert_eq!(tokens.len(), 1);
                }
                _ => panic!("page {page} is answered in turn"),
            }
        }
    }

    #[test]
    fn initialization_and_requests_have_bounded_timeouts() {
        for mode in ["initialize-timeout", "request-timeout"] {
            let client = fixture(mode, Duration::from_millis(200));
            client.update(document(0, 1, "f"));
            match event(&client) {
                Event::Failed(error) => assert!(error.contains("timed out"), "{error}"),
                Event::Tokens { .. } | Event::PageTokens { .. } | Event::Started(_) => {
                    panic!("fixture must time out")
                }
            }
        }
    }

    #[test]
    fn malformed_tokens_and_unexpected_exit_report_one_failure() {
        for mode in ["malformed", "exit"] {
            let client = fixture(mode, REQUEST_TIMEOUT);
            client.update(document(0, 1, "f"));
            assert!(matches!(event(&client), Event::Failed(_)));
            assert!(client.events.recv_timeout(DEBOUNCE).is_err());
        }
    }

    #[test]
    fn missing_executable_reports_a_failure_without_blocking_startup() {
        let client = Client::spawn(
            Command::new("/nonexistent/spectral-m2-language-server"),
            REQUEST_TIMEOUT,
        );
        assert!(matches!(event(&client), Event::Failed(_)));
    }

    #[test]
    #[ignore = "requires an installed m2-ls executable"]
    fn installed_server_classifies_a_documentation_page() {
        let mut highlighter = SemanticHighlighter::new();
        highlighter.configure(true, true);
        let (ring, generators) = ("R = QQ[x,y,z];", "matrix{{x,0,2},{1,2,y}}");
        highlighter.analyse_page(7, vec![ring.to_string(), generators.to_string()]);
        let deadline = Instant::now() + REQUEST_TIMEOUT;
        let page = loop {
            let mut polled = highlighter.poll();
            assert!(polled.failure.is_none(), "{:?}", polled.failure);
            if let Some(page) = polled.pages.pop() {
                break page;
            }
            assert!(Instant::now() < deadline, "no real-server tokens");
            thread::sleep(WORKER_INTERVAL);
        };
        assert_eq!(page.page, 7);
        let kind_of = |source: &str, at: usize| {
            page.sources
                .iter()
                .find(|(text, _)| text == source)
                .and_then(|(_, spans)| spans.iter().find(|span| span.content.contains(&at)))
                .map(|span| span.kind)
        };
        assert_eq!(kind_of(ring, ring.find("QQ").unwrap()), Some(HLGroup::Type));
        assert_eq!(
            kind_of(generators, generators.find("{x").unwrap() + 1),
            Some(HLGroup::RingGenerator),
            "generators are known from the earlier block"
        );
    }

    #[test]
    #[ignore = "requires an installed m2-ls executable"]
    fn installed_server_classifies_session_definitions_and_ring_generators() {
        let mut highlighter = SemanticHighlighter::new();
        highlighter.configure(true, true);
        let mut source = Document::new(0);
        let cell = SourceId::Cell {
            generation: 0,
            submission: 1,
            index: 0,
        };
        source.push(cell, "R = QQ[x, y]\nf = a -> a + x");
        source.push(SourceId::Draft(0), "f(x)");
        highlighter.update(source);
        let deadline = Instant::now() + REQUEST_TIMEOUT;
        loop {
            let polled = highlighter.poll();
            assert!(polled.failure.is_none(), "{:?}", polled.failure);
            if polled.recolored {
                break;
            }
            assert!(Instant::now() < deadline, "no real-server tokens");
            thread::sleep(WORKER_INTERVAL);
        }
        let spans = highlighter.highlight_source(cell, "R = QQ[x, y]\nf = a -> a + x");
        assert!(spans
            .iter()
            .any(|span| span.mark.unwrap_or(span.kind) == HLGroup::Type));
        assert!(spans
            .iter()
            .any(|span| span.mark.unwrap_or(span.kind) == HLGroup::RingGenerator));
        assert!(spans
            .iter()
            .any(|span| span.mark.unwrap_or(span.kind) == HLGroup::Parameter));
        let spans = highlighter.highlight_continuation(None, "f(x)");
        assert_eq!(
            spans
                .iter()
                .find(|span| span.content.contains(&0))
                .map(|span| span.mark.unwrap_or(span.kind))
                .unwrap(),
            HLGroup::Function
        );
        assert_eq!(
            spans
                .iter()
                .find(|span| span.content.contains(&2))
                .map(|span| span.mark.unwrap_or(span.kind))
                .unwrap(),
            HLGroup::RingGenerator
        );
    }
}