iced-code-editor 0.4.1

A custom code editor widget for the Iced GUI framework with syntax highlighting, line numbers, and scrolling support.
Documentation
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
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
//! Wire format for the LSP stdio transport.
//!
//! Everything between the raw byte stream and the editor-facing
//! [`LspEvent`](super::LspEvent): `Content-Length` framing, the bounded reads
//! that keep a malformed or hostile server from exhausting memory, dispatch of
//! incoming JSON-RPC messages, and parsing of the three response shapes the
//! editor asks for.
//!
//! These are free functions with no client state: the reader thread owns the
//! stream and calls into here, so nothing in this module needs to know the
//! process exists.

use std::collections::HashMap;
use std::io::{BufRead, Read};
use std::sync::{Arc, Mutex, mpsc};

use serde_json::json;

use super::pending::{LspRequestKind, PendingRequest};
use super::{LspEvent, LspPosition, LspRange};

/// JSON-RPC method name for server-push progress notifications.
const METHOD_PROGRESS: &str = "$/progress";
/// JSON-RPC method name sent by the server when it creates a work-done token.
const METHOD_WORK_DONE_PROGRESS_CREATE: &str = "window/workDoneProgress/create";
/// Progress `kind` value that signals the end of a work-done sequence.
const PROGRESS_KIND_END: &str = "end";
/// Maximum accepted `Content-Length` for a single LSP frame (64 MiB).
///
/// The body length is attacker-controlled input: it comes from the language
/// server process, whose binary is resolved through `PATH` or an environment
/// variable. Without a cap, a malformed or hostile server announcing a
/// multi-gigabyte frame would force an unbounded allocation and take the
/// editor down. Real LSP payloads stay orders of magnitude below this.
const MAX_MESSAGE_BYTES: usize = 64 * 1024 * 1024;
/// Maximum accepted length for a single header line (8 KiB).
///
/// `BufRead::read_line` grows its buffer until it finds a newline, so a server
/// that opens a header line and never terminates it would force an unbounded
/// allocation — the same hazard [`MAX_MESSAGE_BYTES`] guards on the body, on
/// the path that reaches it. Real LSP headers are a few dozen bytes.
const MAX_HEADER_LINE_BYTES: u64 = 8 * 1024;
/// Maximum accepted number of header lines before the blank separator.
///
/// Each line is individually bounded by [`MAX_HEADER_LINE_BYTES`], so an
/// endless stream of well-formed headers is not a memory hazard — but without
/// a cap the reader would spin on it forever and never yield a message. Real
/// LSP frames send one or two headers.
const MAX_HEADER_LINES: usize = 64;
/// Maximum bytes kept from a single language-server stderr line (8 KiB).
///
/// `BufRead::lines` grows its buffer until it finds a newline, so one
/// unterminated line from a chatty or hostile server would grow without bound
/// — the same hazard [`MAX_HEADER_LINE_BYTES`] guards on the protocol stream.
/// Diagnostics are free-form text rather than framed protocol, so an oversized
/// line is truncated and reading resumes at the next line, instead of tearing
/// down the stream the way an unparseable frame must.
const MAX_LOG_LINE_BYTES: u64 = 8 * 1024;
/// Maximum bytes discarded while resynchronising after an oversized log line.
///
/// Bounds the *time* spent skipping, where [`MAX_LOG_LINE_BYTES`] bounds the
/// memory kept: a line that never ends has no newline to find, so an
/// unbudgeted skip would never return. See [`skip_to_newline`].
const MAX_LOG_SKIP_BYTES: u64 = 1024 * 1024;

/// Reads one `Content-Length`-framed message body from `reader`.
///
/// Returns `None` when the stream ends, when the body cannot be read in full,
/// when the announced frame exceeds [`MAX_MESSAGE_BYTES`], or when the header
/// block breaches [`MAX_HEADER_LINE_BYTES`] or [`MAX_HEADER_LINES`]. None of
/// these can be skipped reliably — the framing is exactly what is not
/// trustworthy — so the caller must stop reading the stream rather than try to
/// resynchronise mid-message.
///
/// Header lines other than `Content-Length` are ignored. A header block
/// carrying no `Content-Length` yields an empty body, which the caller then
/// discards as invalid JSON.
pub(super) fn read_message(reader: &mut impl BufRead) -> Option<Vec<u8>> {
    let mut content_length: Option<usize> = None;
    let mut line = String::new();

    for _ in 0..MAX_HEADER_LINES {
        line.clear();
        // Read through a `take` rather than calling `read_line` directly:
        // `read_line` grows `line` until it finds a newline, so a server that
        // never sends one would force an unbounded allocation here — the same
        // hazard `MAX_MESSAGE_BYTES` guards on the body, one step earlier.
        // Called through UFCS so `Self` is `&mut R`: written as a method call,
        // the probe derefs to `R` and `take` moves the reader out from under
        // the borrow.
        let read = Read::take(&mut *reader, MAX_HEADER_LINE_BYTES)
            .read_line(&mut line)
            .ok()?;

        // A zero-length read means end of stream, not an empty header line.
        if read == 0 {
            return None;
        }

        // Hitting the cap with no newline means the line was oversized. The
        // remainder is still queued in the stream with no way to tell where
        // the next frame begins, so give up instead of resynchronising.
        if !line.ends_with('\n') {
            return None;
        }

        let trimmed = line.trim();
        if trimmed.is_empty() {
            return read_body(reader, content_length);
        }
        if let Some(value) = trimmed.strip_prefix("Content-Length:")
            && let Ok(len) = value.trim().parse::<usize>()
        {
            content_length = Some(len);
        }
    }

    // The header block never terminated. Each line is individually bounded, so
    // this is not a memory hazard, but without a cap the loop would spin on a
    // server emitting headers forever and never yield a message.
    None
}

/// Reads one line of language-server stderr, truncated to
/// [`MAX_LOG_LINE_BYTES`].
///
/// Returns `None` at end of stream. A line longer than the cap is cut at the
/// cap, marked with a trailing `…`, and the remainder discarded (without being
/// buffered) up to the next newline, so the following lines still start on a
/// real boundary.
///
/// Invalid UTF-8 is replaced rather than treated as an error: server
/// diagnostics are not worth tearing the log stream down over.
pub(super) fn read_log_line(reader: &mut impl BufRead) -> Option<String> {
    let mut buf = Vec::new();
    // Bounded like the protocol header lines, and for the same reason; see
    // `read_message`.
    let read = Read::take(&mut *reader, MAX_LOG_LINE_BYTES)
        .read_until(b'\n', &mut buf)
        .ok()?;
    if read == 0 {
        return None;
    }

    let truncated = !buf.ends_with(b"\n");
    let mut line = String::from_utf8_lossy(&buf).into_owned();
    if truncated {
        skip_to_newline(reader);
        line.push('…');
    }
    Some(line)
}

/// Advances `reader` past the next newline, discarding at most
/// [`MAX_LOG_SKIP_BYTES`] on the way.
///
/// Used to resynchronise after an oversized log line. Works through
/// `fill_buf`/`consume` rather than `read_until` so the skipped bytes are
/// never buffered — otherwise recovering from an unbounded line would itself
/// need unbounded memory.
///
/// The byte budget matters as much as the buffering: a server emitting a line
/// that never ends has no newline to find, and an unbudgeted skip would spin
/// in this loop for as long as it kept talking. Giving up instead lets the
/// next [`read_log_line`] surface the following chunk as its own truncated
/// line, so the reader always makes forward progress.
fn skip_to_newline(reader: &mut impl BufRead) {
    let mut skipped: u64 = 0;
    while skipped < MAX_LOG_SKIP_BYTES {
        let Ok(available) = reader.fill_buf() else { return };
        if available.is_empty() {
            return;
        }
        if let Some(index) = available.iter().position(|byte| *byte == b'\n') {
            reader.consume(index + 1);
            return;
        }
        let len = available.len();
        reader.consume(len);
        skipped = skipped.saturating_add(len as u64);
    }
}

/// Reads the body announced by a header block's `Content-Length`.
///
/// A missing `Content-Length` is treated as a zero-length body; see
/// [`read_message`].
fn read_body(
    reader: &mut impl BufRead,
    content_length: Option<usize>,
) -> Option<Vec<u8>> {
    let len = content_length.unwrap_or(0);
    if len > MAX_MESSAGE_BYTES {
        return None;
    }

    let mut buf = vec![0u8; len];
    if reader.read_exact(&mut buf).is_err() {
        return None;
    }
    Some(buf)
}

/// Frames a JSON-RPC value as `Content-Length: N\r\n\r\n<body>` bytes, ready
/// to be written to an LSP server's stdin.
///
/// Returns `None` if `value` cannot be serialized to JSON.
///
/// # Examples
///
/// ```text
/// let framed = frame_message(&serde_json::json!({"jsonrpc": "2.0"})).unwrap();
/// assert!(framed.starts_with(b"Content-Length: "));
/// ```
pub(super) fn frame_message(value: &serde_json::Value) -> Option<Vec<u8>> {
    let data = serde_json::to_vec(value).ok()?;
    let mut framed =
        format!("Content-Length: {}\r\n\r\n", data.len()).into_bytes();
    framed.extend_from_slice(&data);
    Some(framed)
}

/// Handles an LSP server request that requires a JSON-RPC response.
///
/// Currently handles `window/workDoneProgress/create` by replying with a null
/// result. Unknown methods are silently ignored.
pub(super) fn handle_server_request(
    id: u64,
    method: &str,
    tx: &mpsc::Sender<Vec<u8>>,
) {
    if method == METHOD_WORK_DONE_PROGRESS_CREATE {
        let response = json!({
            "jsonrpc": "2.0",
            "id": id,
            "result": null
        });
        if let Some(bytes) = frame_message(&response) {
            let _ = tx.send(bytes);
        }
    }
}

/// Dispatches a server response to the appropriate pending request handler.
///
/// Looks up the request kind by `id`, parses the result, and emits a
/// [`LspEvent::Hover`], [`LspEvent::Completion`], or [`LspEvent::Definition`].
pub(super) fn handle_client_response(
    id: u64,
    value: &serde_json::Value,
    pending: &Arc<Mutex<HashMap<u64, PendingRequest>>>,
    events: &mpsc::Sender<LspEvent>,
) {
    let kind = {
        let mut map = pending.lock().unwrap_or_else(|e| e.into_inner());
        map.remove(&id).map(|entry| entry.kind)
    };

    let Some(kind) = kind else { return };
    let result = value.get("result").unwrap_or(&serde_json::Value::Null);

    match kind {
        LspRequestKind::Hover => {
            let text = parse_hover_text(result).unwrap_or_default();
            let _ = events.send(LspEvent::Hover { text });
        }
        LspRequestKind::Completion => {
            let items = parse_completion_items(result);
            if !items.is_empty() {
                let _ = events.send(LspEvent::Completion { items });
            }
        }
        LspRequestKind::Definition => {
            if let Some((uri, range)) = parse_definition_location(result) {
                let _ = events.send(LspEvent::Definition { uri, range });
            }
        }
    }
}

/// Handles a server-initiated notification (e.g. `$/progress`).
///
/// Parses the progress payload and emits a [`LspEvent::Progress`].
/// Notifications for unknown methods are silently ignored.
pub(super) fn handle_server_notification(
    method: &str,
    params: &serde_json::Value,
    events: &mpsc::Sender<LspEvent>,
    server_key: &str,
) {
    if method != METHOD_PROGRESS {
        return;
    }

    let Some(token) = params.get("token").and_then(|t| {
        t.as_str()
            .map(String::from)
            .or_else(|| t.as_i64().map(|i| i.to_string()))
    }) else {
        return;
    };

    let Some(val) = params.get("value") else { return };

    let kind = val.get("kind").and_then(|k| k.as_str()).unwrap_or("");
    let title = val
        .get("title")
        .and_then(|t| t.as_str())
        .map(String::from)
        .unwrap_or_default();
    let message = val.get("message").and_then(|m| m.as_str()).map(String::from);
    let percentage =
        val.get("percentage").and_then(|p| p.as_u64()).map(|p| p as u32);
    let done = kind == PROGRESS_KIND_END;

    let _ = events.send(LspEvent::Progress {
        token,
        server_key: server_key.to_string(),
        title,
        message,
        percentage,
        done,
    });
}

// =============================================================================
// LSP Response Parsing Functions
// =============================================================================

/// Parses hover text from an LSP hover response.
fn parse_hover_text(result: &serde_json::Value) -> Option<String> {
    let contents = result.get("contents")?;
    hover_text_from_contents(contents)
}

/// Recursively extracts hover text from various content formats.
///
/// Handles strings, arrays, and objects with a `"value"` field.
fn hover_text_from_contents(value: &serde_json::Value) -> Option<String> {
    match value {
        serde_json::Value::String(text) => Some(text.clone()),
        serde_json::Value::Array(items) => {
            let parts: Vec<String> =
                items.iter().filter_map(hover_text_from_contents).collect();
            if parts.is_empty() { None } else { Some(parts.join("\n")) }
        }
        serde_json::Value::Object(map) => {
            map.get("value").and_then(|v| v.as_str()).map(String::from)
        }
        _ => None,
    }
}

/// Parses completion items from an LSP completion response.
///
/// Handles both array responses and object responses with an `"items"` field.
fn parse_completion_items(result: &serde_json::Value) -> Vec<String> {
    let mut items = Vec::new();

    if let Some(array) = result.as_array() {
        items.extend(array.iter());
    } else if let Some(array) = result.get("items").and_then(|v| v.as_array()) {
        items.extend(array.iter());
    }

    items
        .iter()
        .filter_map(|item| item.get("label").and_then(|v| v.as_str()))
        .map(String::from)
        .collect()
}

/// Parses a JSON-RPC `Range` object (`{start: {line, character}, end: {...}}`)
/// into an [`LspRange`].
///
/// Returns `None` if either endpoint is missing or has the wrong shape.
fn extract_range(range_val: &serde_json::Value) -> Option<LspRange> {
    let start = range_val.get("start")?;
    let end = range_val.get("end")?;

    Some(LspRange {
        start: LspPosition {
            line: start.get("line")?.as_u64()? as u32,
            character: start.get("character")?.as_u64()? as u32,
        },
        end: LspPosition {
            line: end.get("line")?.as_u64()? as u32,
            character: end.get("character")?.as_u64()? as u32,
        },
    })
}

/// Extracts `(uri, range)` from an LSP `Location` object.
fn extract_location(loc: &serde_json::Value) -> Option<(String, LspRange)> {
    let uri = loc.get("uri")?.as_str()?.to_string();
    let range = extract_range(loc.get("range")?)?;
    Some((uri, range))
}

/// Extracts `(uri, range)` from an LSP `LocationLink` object.
///
/// Prefers `targetSelectionRange` (the precise symbol range) and falls back
/// to `targetRange` (the enclosing range) when it is absent.
fn extract_link(link: &serde_json::Value) -> Option<(String, LspRange)> {
    let uri = link.get("targetUri")?.as_str()?.to_string();
    let range_val =
        link.get("targetSelectionRange").or(link.get("targetRange"))?;
    let range = extract_range(range_val)?;
    Some((uri, range))
}

/// Parses definition location from an LSP definition response.
///
/// Handles `Location`, `Location[]`, and `LocationLink[]` responses.
fn parse_definition_location(
    result: &serde_json::Value,
) -> Option<(String, LspRange)> {
    if let Some(array) = result.as_array() {
        if let Some(first) = array.first() {
            if first.get("targetUri").is_some() {
                extract_link(first)
            } else {
                extract_location(first)
            }
        } else {
            None
        }
    } else if result.is_object() {
        extract_location(result)
    } else {
        None
    }
}

#[cfg(test)]
mod tests {
    // Several helpers and tests below carry an `#[allow]` for `expect_used`,
    // `panic` or `unwrap_used`. In test code a panic *is* the failure report:
    // `expect("expected a Hover event")` names the broken expectation far more
    // precisely than an `assert!(false)` workaround would. The workspace denies
    // these lints to protect production code, not tests — this mirrors the
    // existing per-test allows in `update.rs` and `selection.rs`.
    use super::*;
    use crate::canvas_editor::lsp::LspPosition;
    use std::io::BufReader;
    use std::time::Instant;

    /// Returns the JSON body of a `Content-Length`-framed message taken from
    /// the writer channel.
    #[allow(clippy::expect_used)]
    fn decode_sent(data: &[u8]) -> serde_json::Value {
        let header_end = data
            .windows(4)
            .position(|w| w == b"\r\n\r\n")
            .expect("missing header separator");
        let body = &data[header_end + 4..];
        serde_json::from_slice(body).expect("invalid JSON body")
    }

    /// Builds a [`PendingRequest`] of `kind`, sent "now" for test purposes.
    fn pending_request(kind: LspRequestKind) -> PendingRequest {
        PendingRequest { kind, requested_at: Instant::now() }
    }

    // -------------------------------------------------------------------------
    // read_message
    // -------------------------------------------------------------------------

    #[test]
    fn test_read_message_reads_framed_body() {
        let mut stream = std::io::Cursor::new(
            b"Content-Length: 9\r\n\r\n{\"id\":42}".to_vec(),
        );
        assert_eq!(
            read_message(&mut stream).as_deref(),
            Some(&b"{\"id\":42}"[..])
        );
    }

    #[test]
    fn test_read_message_ignores_other_headers() {
        let mut stream = std::io::Cursor::new(
            b"Content-Type: application/vscode-jsonrpc\r\nContent-Length: 2\r\n\r\n{}"
                .to_vec(),
        );
        assert_eq!(read_message(&mut stream).as_deref(), Some(&b"{}"[..]));
    }

    #[test]
    fn test_read_message_reads_consecutive_frames() {
        let mut stream = std::io::Cursor::new(
            b"Content-Length: 2\r\n\r\n{}Content-Length: 4\r\n\r\n[1,]"
                .to_vec(),
        );
        assert_eq!(read_message(&mut stream).as_deref(), Some(&b"{}"[..]));
        assert_eq!(read_message(&mut stream).as_deref(), Some(&b"[1,]"[..]));
        assert!(read_message(&mut stream).is_none(), "stream is exhausted");
    }

    #[test]
    fn test_read_message_rejects_oversized_frame() {
        // The announced length exceeds MAX_MESSAGE_BYTES: the frame must be
        // refused without allocating it.
        let header =
            format!("Content-Length: {}\r\n\r\n", MAX_MESSAGE_BYTES + 1);
        let mut stream = std::io::Cursor::new(header.into_bytes());
        assert!(read_message(&mut stream).is_none());
    }

    #[test]
    fn test_read_message_rejects_truncated_body() {
        let mut stream =
            std::io::Cursor::new(b"Content-Length: 10\r\n\r\nabc".to_vec());
        assert!(read_message(&mut stream).is_none());
    }

    #[test]
    fn test_read_message_ends_on_empty_stream() {
        let mut stream = std::io::Cursor::new(Vec::new());
        assert!(read_message(&mut stream).is_none());
    }

    #[test]
    fn test_read_message_without_content_length_yields_empty_body() {
        // No Content-Length: the caller gets an empty body and discards it as
        // invalid JSON, then keeps reading.
        let mut stream = std::io::Cursor::new(b"X-Unknown: 1\r\n\r\n".to_vec());
        assert_eq!(read_message(&mut stream).as_deref(), Some(&b""[..]));
    }

    /// A reader that serves an endless run of `a` with no newline ever,
    /// standing in for a server that opens a header line and never closes it.
    ///
    /// It counts what it served, which is what makes the bound observable: a
    /// finite `Cursor` would end the line at EOF and so pass even with no cap
    /// at all.
    struct EndlessHeaderReader {
        served: std::rc::Rc<std::cell::Cell<usize>>,
    }

    impl Read for EndlessHeaderReader {
        fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
            buf.fill(b'a');
            self.served.set(self.served.get() + buf.len());
            Ok(buf.len())
        }
    }

    #[test]
    fn test_read_message_stops_reading_an_unterminated_header_line() {
        let served = std::rc::Rc::new(std::cell::Cell::new(0));
        let mut stream = BufReader::new(EndlessHeaderReader {
            served: std::rc::Rc::clone(&served),
        });

        assert!(read_message(&mut stream).is_none());

        // The real property: the reader gave up near the cap instead of
        // growing its buffer for as long as the server kept talking. Without
        // the cap this test would not fail, it would never return.
        let cap = MAX_HEADER_LINE_BYTES as usize;
        assert!(
            served.get() <= cap * 4,
            "read {} bytes for one header line, cap is {cap}",
            served.get()
        );
    }

    #[test]
    fn test_read_message_rejects_a_header_block_that_never_ends() {
        // Well-formed headers, but no blank separator line ever arrives. Each
        // line is individually bounded, so this is a livelock rather than a
        // memory hazard — the line cap alone would not stop it.
        let flood = "X-Pad: 1\r\n".repeat(MAX_HEADER_LINES + 10);
        let mut stream = std::io::Cursor::new(flood.into_bytes());

        assert!(read_message(&mut stream).is_none());
    }

    #[test]
    fn test_read_log_line_reads_lines_in_order() {
        let mut reader =
            BufReader::new(std::io::Cursor::new(b"first\nsecond\n".to_vec()));

        assert_eq!(read_log_line(&mut reader).as_deref(), Some("first\n"));
        assert_eq!(read_log_line(&mut reader).as_deref(), Some("second\n"));
        assert_eq!(read_log_line(&mut reader), None, "stream is exhausted");
    }

    #[test]
    fn test_read_log_line_truncates_an_oversized_line_and_resyncs() {
        let cap = MAX_LOG_LINE_BYTES as usize;
        let flood = "a".repeat(cap * 3);
        let stream = format!("{flood}\nnext\n");
        let mut reader =
            BufReader::new(std::io::Cursor::new(stream.into_bytes()));

        let line = read_log_line(&mut reader);
        assert!(
            line.as_ref().is_some_and(|line| line.len() <= cap + 8),
            "the kept prefix must be bounded by the cap"
        );
        assert!(
            line.as_ref().is_some_and(|line| line.ends_with('…')),
            "a truncated line must say so"
        );

        // The remainder was skipped rather than buffered, so the next line
        // still starts on a real boundary instead of mid-flood.
        assert_eq!(read_log_line(&mut reader).as_deref(), Some("next\n"));
    }

    #[test]
    fn test_read_log_line_replaces_invalid_utf8_instead_of_ending_the_stream() {
        // Server diagnostics are not worth tearing the log stream down over:
        // a bad byte must not cost us every later line.
        let mut reader = BufReader::new(std::io::Cursor::new(
            b"bad \xff byte\nnext\n".to_vec(),
        ));

        let line = read_log_line(&mut reader);
        assert!(
            line.as_ref().is_some_and(|line| line.contains('\u{fffd}')),
            "the invalid byte should become a replacement character, got {line:?}"
        );
        assert_eq!(read_log_line(&mut reader).as_deref(), Some("next\n"));
    }

    #[test]
    fn test_read_log_line_stops_reading_an_unterminated_line() {
        let served = std::rc::Rc::new(std::cell::Cell::new(0));
        let mut reader = BufReader::new(EndlessHeaderReader {
            served: std::rc::Rc::clone(&served),
        });

        // The line never ends, so the skip-ahead never finds a newline either;
        // both must stay bounded rather than buffering the whole flood.
        let line = read_log_line(&mut reader);
        let cap = MAX_LOG_LINE_BYTES as usize;
        assert!(line.is_some_and(|line| line.len() <= cap + 8));
    }

    #[test]
    fn test_read_message_accepts_headers_just_inside_the_caps() {
        // The caps must not reject legitimate traffic: a long-but-bounded
        // header line, and more headers than a real server sends but fewer
        // than the limit, still produce the framed body.
        let padding = "x".repeat(MAX_HEADER_LINE_BYTES as usize / 2);
        let mut frame = format!("X-Pad: {padding}\r\n");
        for _ in 0..MAX_HEADER_LINES - 3 {
            frame.push_str("X-Small: 1\r\n");
        }
        frame.push_str("Content-Length: 2\r\n\r\n{}");
        let mut stream = std::io::Cursor::new(frame.into_bytes());

        assert_eq!(read_message(&mut stream).as_deref(), Some(&b"{}"[..]));
    }

    // -------------------------------------------------------------------------
    // frame_message
    // -------------------------------------------------------------------------

    #[test]
    #[allow(clippy::unwrap_used)]
    fn test_frame_message_builds_content_length_header() {
        let value = serde_json::json!({"jsonrpc": "2.0", "id": 1});
        let data = serde_json::to_vec(&value).unwrap();
        let framed = frame_message(&value).unwrap();

        let expected_header = format!("Content-Length: {}\r\n\r\n", data.len());
        assert!(framed.starts_with(expected_header.as_bytes()));
        assert_eq!(&framed[expected_header.len()..], data.as_slice());
    }

    // -------------------------------------------------------------------------
    // extract_range / extract_location / extract_link
    // -------------------------------------------------------------------------

    #[test]
    #[allow(clippy::unwrap_used)]
    fn test_extract_range_parses_start_and_end() {
        let range_val = serde_json::json!({
            "start": { "line": 1, "character": 2 },
            "end": { "line": 3, "character": 4 }
        });
        let range = extract_range(&range_val).unwrap();
        assert_eq!(range.start, LspPosition { line: 1, character: 2 });
        assert_eq!(range.end, LspPosition { line: 3, character: 4 });
    }

    #[test]
    fn test_extract_range_missing_end_returns_none() {
        let range_val = serde_json::json!({
            "start": { "line": 1, "character": 2 }
        });
        assert!(extract_range(&range_val).is_none());
    }

    #[test]
    #[allow(clippy::unwrap_used)]
    fn test_extract_location_reads_uri_and_range() {
        let loc = serde_json::json!({
            "uri": "file:///a.rs",
            "range": {
                "start": { "line": 0, "character": 0 },
                "end": { "line": 0, "character": 1 }
            }
        });
        let (uri, range) = extract_location(&loc).unwrap();
        assert_eq!(uri, "file:///a.rs");
        assert_eq!(range.start, LspPosition { line: 0, character: 0 });
    }

    #[test]
    #[allow(clippy::unwrap_used)]
    fn test_extract_link_prefers_target_selection_range() {
        let link = serde_json::json!({
            "targetUri": "file:///b.rs",
            "targetRange": {
                "start": { "line": 10, "character": 0 },
                "end": { "line": 20, "character": 0 }
            },
            "targetSelectionRange": {
                "start": { "line": 12, "character": 4 },
                "end": { "line": 12, "character": 8 }
            }
        });
        let (uri, range) = extract_link(&link).unwrap();
        assert_eq!(uri, "file:///b.rs");
        assert_eq!(range.start, LspPosition { line: 12, character: 4 });
    }

    #[test]
    #[allow(clippy::unwrap_used)]
    fn test_extract_link_falls_back_to_target_range() {
        let link = serde_json::json!({
            "targetUri": "file:///c.rs",
            "targetRange": {
                "start": { "line": 5, "character": 0 },
                "end": { "line": 6, "character": 0 }
            }
        });
        let (uri, range) = extract_link(&link).unwrap();
        assert_eq!(uri, "file:///c.rs");
        assert_eq!(range.start, LspPosition { line: 5, character: 0 });
    }

    // -------------------------------------------------------------------------
    // handle_server_request
    // -------------------------------------------------------------------------

    #[test]
    #[allow(clippy::expect_used)]
    fn test_handle_server_request_work_done_progress_create() {
        let (tx, rx) = mpsc::channel::<Vec<u8>>();
        handle_server_request(42, METHOD_WORK_DONE_PROGRESS_CREATE, &tx);

        let bytes = rx.try_recv().expect("expected a response on the channel");
        let value = decode_sent(&bytes);
        assert_eq!(value["id"], 42);
        assert_eq!(value["jsonrpc"], "2.0");
        assert!(value["result"].is_null());
    }

    #[test]
    fn test_handle_server_request_unknown_method_ignored() {
        let (tx, rx) = mpsc::channel::<Vec<u8>>();
        handle_server_request(1, "unknown/method", &tx);
        assert!(
            rx.try_recv().is_err(),
            "unknown methods must not send a reply"
        );
    }

    // -------------------------------------------------------------------------
    // handle_client_response
    // -------------------------------------------------------------------------

    #[test]
    #[allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]
    fn test_handle_client_response_hover() {
        let (events_tx, events_rx) = mpsc::channel::<LspEvent>();
        let pending = Arc::new(Mutex::new(HashMap::new()));
        pending
            .lock()
            .unwrap()
            .insert(1u64, pending_request(LspRequestKind::Hover));

        let value = serde_json::json!({
            "id": 1,
            "result": { "contents": { "value": "hover info" } }
        });
        handle_client_response(1, &value, &pending, &events_tx);

        match events_rx.try_recv().expect("expected a Hover event") {
            LspEvent::Hover { text } => assert_eq!(text, "hover info"),
            _ => panic!("expected LspEvent::Hover"),
        }
        assert!(pending.lock().unwrap().is_empty());
    }

    #[test]
    #[allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]
    fn test_handle_client_response_completion() {
        let (events_tx, events_rx) = mpsc::channel::<LspEvent>();
        let pending = Arc::new(Mutex::new(HashMap::new()));
        pending
            .lock()
            .unwrap()
            .insert(2u64, pending_request(LspRequestKind::Completion));

        let value = serde_json::json!({
            "id": 2,
            "result": { "items": [{ "label": "foo" }, { "label": "bar" }] }
        });
        handle_client_response(2, &value, &pending, &events_tx);

        match events_rx.try_recv().expect("expected a Completion event") {
            LspEvent::Completion { items } => {
                assert_eq!(items, vec!["foo", "bar"]);
            }
            _ => panic!("expected LspEvent::Completion"),
        }
    }

    #[test]
    #[allow(clippy::expect_used, clippy::panic, clippy::unwrap_used)]
    fn test_handle_client_response_definition() {
        let (events_tx, events_rx) = mpsc::channel::<LspEvent>();
        let pending = Arc::new(Mutex::new(HashMap::new()));
        pending
            .lock()
            .unwrap()
            .insert(3u64, pending_request(LspRequestKind::Definition));

        let value = serde_json::json!({
            "id": 3,
            "result": {
                "uri": "file:///foo/bar.rs",
                "range": {
                    "start": { "line": 0, "character": 0 },
                    "end": { "line": 0, "character": 5 }
                }
            }
        });
        handle_client_response(3, &value, &pending, &events_tx);

        match events_rx.try_recv().expect("expected a Definition event") {
            LspEvent::Definition { uri, .. } => {
                assert_eq!(uri, "file:///foo/bar.rs");
            }
            _ => panic!("expected LspEvent::Definition"),
        }
    }

    #[test]
    fn test_handle_client_response_unknown_id_ignored() {
        let (events_tx, events_rx) = mpsc::channel::<LspEvent>();
        let pending = Arc::new(Mutex::new(HashMap::new()));

        let value = serde_json::json!({ "id": 99, "result": null });
        handle_client_response(99, &value, &pending, &events_tx);
        assert!(
            events_rx.try_recv().is_err(),
            "unknown IDs must not emit events"
        );
    }

    // -------------------------------------------------------------------------
    // handle_server_notification
    // -------------------------------------------------------------------------

    #[test]
    #[allow(clippy::expect_used, clippy::panic)]
    fn test_handle_server_notification_progress_done() {
        let (events_tx, events_rx) = mpsc::channel::<LspEvent>();
        let params = serde_json::json!({
            "token": "my-token",
            "value": {
                "kind": "end",
                "title": "Indexing",
                "message": "done"
            }
        });

        handle_server_notification(
            METHOD_PROGRESS,
            &params,
            &events_tx,
            "lua-ls",
        );

        match events_rx.try_recv().expect("expected a Progress event") {
            LspEvent::Progress { token, done, server_key, .. } => {
                assert_eq!(token, "my-token");
                assert!(done);
                assert_eq!(server_key, "lua-ls");
            }
            _ => panic!("expected LspEvent::Progress"),
        }
    }

    #[test]
    #[allow(clippy::expect_used, clippy::panic)]
    fn test_handle_server_notification_progress_not_done() {
        let (events_tx, events_rx) = mpsc::channel::<LspEvent>();
        let params = serde_json::json!({
            "token": "tok",
            "value": { "kind": "report", "title": "Building" }
        });

        handle_server_notification(
            METHOD_PROGRESS,
            &params,
            &events_tx,
            "rust-analyzer",
        );

        match events_rx.try_recv().expect("expected a Progress event") {
            LspEvent::Progress { done, .. } => assert!(!done),
            _ => panic!("expected LspEvent::Progress"),
        }
    }

    #[test]
    fn test_handle_server_notification_unknown_method_ignored() {
        let (events_tx, events_rx) = mpsc::channel::<LspEvent>();
        let params = serde_json::json!({});
        handle_server_notification(
            "$/somethingElse",
            &params,
            &events_tx,
            "server",
        );
        assert!(events_rx.try_recv().is_err());
    }
}