iced-code-editor 0.7.0

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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
//! LSP synchronization for [`CodeEditor`]: attaching/detaching a client,
//! tracking buffer edits as queued `didChange` notifications, and issuing
//! hover/completion/definition/formatting requests.

use crate::buffer::TextBuffer;
use crate::canvas_editor::CodeEditor;
use crate::canvas_editor::lsp;

impl CodeEditor {
    /// Attaches an LSP client and opens a document for the current buffer.
    ///
    /// This sends an initial `did_open` with the current buffer contents and
    /// resets any pending LSP change state. It is a no-op while LSP support is
    /// disabled — see [`Self::set_lsp_enabled`].
    ///
    /// # Arguments
    ///
    /// * `client` - The LSP client to notify
    /// * `document` - Document metadata describing the buffer
    ///
    /// # Example
    ///
    /// ```
    /// use std::cell::RefCell;
    /// use std::rc::Rc;
    ///
    /// use iced_code_editor::{CodeEditor, LspClient, LspDocument};
    ///
    /// /// Records the text the editor sent on `did_open`.
    /// struct RecordingClient(Rc<RefCell<Option<String>>>);
    ///
    /// impl LspClient for RecordingClient {
    ///     fn did_open(&mut self, _document: &LspDocument, text: &str) {
    ///         *self.0.borrow_mut() = Some(text.to_string());
    ///     }
    /// }
    ///
    /// let opened = Rc::new(RefCell::new(None));
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// editor.attach_lsp(
    ///     Box::new(RecordingClient(Rc::clone(&opened))),
    ///     LspDocument::new("file:///tmp/main.rs", "rust"),
    /// );
    ///
    /// // Attaching opens the document with the buffer's current contents.
    /// assert_eq!(opened.borrow().as_deref(), Some("fn main() {}"));
    /// ```
    pub fn attach_lsp(
        &mut self,
        mut client: Box<dyn lsp::LspClient>,
        document: lsp::LspDocument,
    ) {
        if !self.lsp_enabled {
            return;
        }
        let (document, text) =
            open_lsp_document(client.as_mut(), &self.buffer, document);
        self.lsp_client = Some(client);
        self.lsp_document = Some(document);
        self.reset_lsp_shadow_state(text);
    }

    /// Opens a new document on the attached LSP client.
    ///
    /// If a document is already open, this will close it before opening the new
    /// one and reset pending change tracking. Use this when the same editor
    /// widget is reused for a different file, so the server is told about the
    /// swap rather than seeing the old document mutate into the new one.
    ///
    /// Does nothing when no client is attached.
    ///
    /// # Arguments
    ///
    /// * `document` - Document metadata describing the buffer
    ///
    /// # Example
    ///
    /// ```
    /// use std::cell::RefCell;
    /// use std::rc::Rc;
    ///
    /// use iced_code_editor::{CodeEditor, LspClient, LspDocument};
    ///
    /// /// Records the URIs the editor closed.
    /// struct ClosingClient(Rc<RefCell<Vec<String>>>);
    ///
    /// impl LspClient for ClosingClient {
    ///     fn did_close(&mut self, document: &LspDocument) {
    ///         self.0.borrow_mut().push(document.uri.clone());
    ///     }
    /// }
    ///
    /// let closed = Rc::new(RefCell::new(Vec::new()));
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// editor.attach_lsp(
    ///     Box::new(ClosingClient(Rc::clone(&closed))),
    ///     LspDocument::new("file:///tmp/first.rs", "rust"),
    /// );
    ///
    /// // Switching files closes the previous document first.
    /// editor.lsp_open_document(LspDocument::new("file:///tmp/second.rs", "rust"));
    /// assert_eq!(closed.borrow().as_slice(), ["file:///tmp/first.rs"]);
    /// ```
    pub fn lsp_open_document(&mut self, document: lsp::LspDocument) {
        let Some(client) = self.lsp_client.as_mut() else { return };
        if let Some(current) = self.lsp_document.as_ref() {
            client.did_close(current);
        }
        let (document, text) =
            open_lsp_document(client.as_mut(), &self.buffer, document);
        self.lsp_document = Some(document);
        self.reset_lsp_shadow_state(text);
    }

    /// Resets LSP shadow-sync bookkeeping after (re)opening a document whose
    /// buffer contents are `text`.
    fn reset_lsp_shadow_state(&mut self, text: String) {
        self.lsp_shadow_text = text;
        self.lsp_shadow_is_current = true;
        self.update_lsp_synced_extent();
        self.lsp_edit_snapshot = None;
        self.lsp_pending_changes.clear();
    }

    /// Runs `f` with the attached LSP client and document, if both are
    /// present. Returns `None` (without calling `f`) if either is absent.
    ///
    /// `f` receives only `client`/`document`, not `&mut self`, so callers
    /// needing other `self` state (e.g. `self.buffer`) must compute it
    /// before calling this helper and move it into the closure.
    fn with_lsp<R>(
        &mut self,
        f: impl FnOnce(&mut dyn lsp::LspClient, &lsp::LspDocument) -> R,
    ) -> Option<R> {
        let client = self.lsp_client.as_deref_mut()?;
        let document = self.lsp_document.as_ref()?;
        Some(f(client, document))
    }

    /// Like [`Self::with_lsp`], but gives `f` a mutable reference to the
    /// document (needed to bump `document.version`).
    fn with_lsp_mut_document<R>(
        &mut self,
        f: impl FnOnce(&mut dyn lsp::LspClient, &mut lsp::LspDocument) -> R,
    ) -> Option<R> {
        let client = self.lsp_client.as_deref_mut()?;
        let document = self.lsp_document.as_mut()?;
        Some(f(client, document))
    }

    /// Detaches the current LSP client and closes any open document.
    ///
    /// This clears all LSP-related state on the editor instance, including any
    /// changes queued but not yet flushed. Safe to call when nothing is
    /// attached.
    ///
    /// # Example
    ///
    /// ```
    /// use std::cell::RefCell;
    /// use std::rc::Rc;
    ///
    /// use iced_code_editor::{CodeEditor, LspClient, LspDocument};
    ///
    /// /// Records whether the document was closed.
    /// struct ClosingClient(Rc<RefCell<bool>>);
    ///
    /// impl LspClient for ClosingClient {
    ///     fn did_close(&mut self, _document: &LspDocument) {
    ///         *self.0.borrow_mut() = true;
    ///     }
    /// }
    ///
    /// let closed = Rc::new(RefCell::new(false));
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// editor.attach_lsp(
    ///     Box::new(ClosingClient(Rc::clone(&closed))),
    ///     LspDocument::new("file:///tmp/main.rs", "rust"),
    /// );
    ///
    /// editor.detach_lsp();
    /// assert!(*closed.borrow());
    ///
    /// // Detaching again is harmless.
    /// editor.detach_lsp();
    /// ```
    pub fn detach_lsp(&mut self) {
        self.with_lsp(|client, document| client.did_close(document));
        self.lsp_client = None;
        self.lsp_document = None;
        self.lsp_shadow_text = String::new();
        self.lsp_shadow_is_current = true;
        self.lsp_synced_line_count = 1;
        self.lsp_synced_last_line_len = 0;
        self.lsp_edit_snapshot = None;
        self.lsp_pending_changes.clear();
    }

    /// Sends a `did_save` notification with the current buffer contents.
    ///
    /// Call this after the host application has written the file to disk, so
    /// servers that re-run diagnostics or formatting on save see the new state.
    /// Does nothing when no client and document are attached.
    ///
    /// # Example
    ///
    /// ```
    /// use std::cell::RefCell;
    /// use std::rc::Rc;
    ///
    /// use iced_code_editor::{CodeEditor, LspClient, LspDocument};
    ///
    /// /// Records the text sent on save.
    /// struct SavingClient(Rc<RefCell<Option<String>>>);
    ///
    /// impl LspClient for SavingClient {
    ///     fn did_save(&mut self, _document: &LspDocument, text: &str) {
    ///         *self.0.borrow_mut() = Some(text.to_string());
    ///     }
    /// }
    ///
    /// let saved = Rc::new(RefCell::new(None));
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// editor.attach_lsp(
    ///     Box::new(SavingClient(Rc::clone(&saved))),
    ///     LspDocument::new("file:///tmp/main.rs", "rust"),
    /// );
    ///
    /// editor.lsp_did_save();
    /// assert_eq!(saved.borrow().as_deref(), Some("fn main() {}"));
    /// ```
    pub fn lsp_did_save(&mut self) {
        // Skip serializing the whole buffer when no client/document is
        // attached: `with_lsp` would just discard the closure unused, but the
        // full-document `to_string()` allocation would still happen on every
        // save for hosts that never enable LSP.
        if self.lsp_client.is_none() || self.lsp_document.is_none() {
            return;
        }
        let text = self.buffer.to_string();
        self.with_lsp(|client, document| client.did_save(document, &text));
    }

    /// Requests hover information at the current cursor position.
    ///
    /// The request is fire-and-forget: the reply reaches the host through
    /// whatever channel the [`LspClient`] implementation uses, not through a
    /// return value. Does nothing when no client is attached.
    ///
    /// [`LspClient`]: crate::LspClient
    ///
    /// # Example
    ///
    /// ```
    /// use std::cell::RefCell;
    /// use std::rc::Rc;
    ///
    /// use iced_code_editor::{CodeEditor, LspClient, LspDocument, LspPosition};
    ///
    /// /// Records the position each hover request asked about.
    /// struct HoverClient(Rc<RefCell<Vec<LspPosition>>>);
    ///
    /// impl LspClient for HoverClient {
    ///     fn request_hover(&mut self, _document: &LspDocument, position: LspPosition) {
    ///         self.0.borrow_mut().push(position);
    ///     }
    /// }
    ///
    /// let hovers = Rc::new(RefCell::new(Vec::new()));
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// editor.attach_lsp(
    ///     Box::new(HoverClient(Rc::clone(&hovers))),
    ///     LspDocument::new("file:///tmp/main.rs", "rust"),
    /// );
    ///
    /// editor.lsp_request_hover();
    /// // The cursor starts at the top of the document.
    /// assert_eq!(hovers.borrow().as_slice(), [LspPosition { line: 0, character: 0 }]);
    /// ```
    pub fn lsp_request_hover(&mut self) {
        let position = self.lsp_position_from_cursor();
        self.with_lsp(|client, document| {
            client.request_hover(document, position);
        });
    }

    /// Requests hover information at a canvas point.
    ///
    /// Use this to drive hover-on-mouse-move; [`Self::lsp_position_at_point`]
    /// resolves the point first, so a point over the gutter sends nothing.
    ///
    /// # Arguments
    ///
    /// * `point` - The position in canvas coordinates
    ///
    /// # Returns
    ///
    /// `true` if the point maps to a valid buffer position and the request was
    /// sent; `false` if it does not, or if no client is attached
    ///
    /// # Example
    ///
    /// ```
    /// use iced::Point;
    /// use iced_code_editor::CodeEditor;
    ///
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    ///
    /// // With no client attached there is nothing to send.
    /// assert!(!editor.lsp_request_hover_at(Point::new(50.0, 5.0)));
    /// ```
    pub fn lsp_request_hover_at(&mut self, point: iced::Point) -> bool {
        let Some(position) = self.lsp_position_from_point(point) else {
            return false;
        };
        self.with_lsp(|client, document| {
            client.request_hover(document, position);
        })
        .is_some()
    }

    /// Requests hover information at an explicit LSP position.
    ///
    /// Unlike [`Self::lsp_request_hover_at`], this skips point-to-position
    /// resolution — pair it with a position previously obtained from
    /// [`Self::lsp_hover_anchor_at_point`] so a hover that lingers stays
    /// anchored to the word rather than following the mouse.
    ///
    /// # Arguments
    ///
    /// * `position` - The zero-based document position to query
    ///
    /// # Returns
    ///
    /// `true` if a client is attached and the request was sent, `false` otherwise
    ///
    /// # Example
    ///
    /// ```
    /// use iced_code_editor::{CodeEditor, LspPosition};
    ///
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// let position = LspPosition { line: 0, character: 3 };
    ///
    /// // With no client attached there is nothing to send.
    /// assert!(!editor.lsp_request_hover_at_position(position));
    /// ```
    pub fn lsp_request_hover_at_position(
        &mut self,
        position: lsp::LspPosition,
    ) -> bool {
        self.with_lsp(|client, document| {
            client.request_hover(document, position);
        })
        .is_some()
    }

    /// Converts a canvas point to an LSP position, if possible.
    ///
    /// Requires no attached client — this is pure hit-testing against the
    /// editor's current layout.
    ///
    /// # Arguments
    ///
    /// * `point` - The position in canvas coordinates
    ///
    /// # Returns
    ///
    /// `Some(position)` when the point falls on text, `None` when it lands in
    /// the gutter
    ///
    /// # Example
    ///
    /// ```
    /// use iced::Point;
    /// use iced_code_editor::CodeEditor;
    ///
    /// let editor = CodeEditor::new("fn main() {}", "rs");
    ///
    /// // The origin sits in the line-number gutter, which maps to nothing.
    /// assert!(editor.lsp_position_at_point(Point::ORIGIN).is_none());
    ///
    /// // A point past the end of the document clamps to its last position.
    /// let position = editor
    ///     .lsp_position_at_point(Point::new(5_000.0, 5_000.0))
    ///     .expect("a point past the text clamps rather than failing");
    /// assert_eq!(position.line, 0);
    /// assert_eq!(position.character, 12);
    /// ```
    pub fn lsp_position_at_point(
        &self,
        point: iced::Point,
    ) -> Option<lsp::LspPosition> {
        self.lsp_position_from_point(point)
    }

    /// Returns the hover anchor position and its canvas point for a given
    /// cursor location.
    ///
    /// The anchor is the start of the word under the cursor. Anchoring to the
    /// word start rather than the exact mouse position keeps a hover popup from
    /// jittering as the pointer moves within one identifier, and gives the host
    /// a stable canvas point to position the popup against.
    ///
    /// # Arguments
    ///
    /// * `point` - The position in canvas coordinates
    ///
    /// # Returns
    ///
    /// `Some((position, anchor_point))` when the point falls on text, `None`
    /// when it lands in the gutter
    ///
    /// # Example
    ///
    /// ```
    /// use iced::Point;
    /// use iced_code_editor::CodeEditor;
    ///
    /// let editor = CodeEditor::new("fn main() {}", "rs");
    ///
    /// // A gutter point has no word to anchor to.
    /// assert!(editor.lsp_hover_anchor_at_point(Point::ORIGIN).is_none());
    /// ```
    pub fn lsp_hover_anchor_at_point(
        &self,
        point: iced::Point,
    ) -> Option<(lsp::LspPosition, iced::Point)> {
        let (line, col) = self.calculate_cursor_from_point(point)?;
        let line_content = self.buffer.line(line);
        let anchor_col = Self::word_start_in_line(line_content, col);
        let anchor_point =
            self.point_from_position(line, anchor_col).unwrap_or(point);
        let line = u32::try_from(line).unwrap_or(u32::MAX);
        let character = u32::try_from(anchor_col).unwrap_or(u32::MAX);
        Some((lsp::LspPosition { line, character }, anchor_point))
    }

    /// Requests completion items at the current cursor position.
    ///
    /// Like the hover requests, this is fire-and-forget; the items arrive
    /// through the [`LspClient`] implementation. Does nothing when no client is
    /// attached.
    ///
    /// [`LspClient`]: crate::LspClient
    ///
    /// # Example
    ///
    /// ```
    /// use std::cell::RefCell;
    /// use std::rc::Rc;
    ///
    /// use iced_code_editor::{CodeEditor, LspClient, LspDocument, LspPosition};
    ///
    /// /// Counts completion requests.
    /// struct CompletionClient(Rc<RefCell<usize>>);
    ///
    /// impl LspClient for CompletionClient {
    ///     fn request_completion(&mut self, _document: &LspDocument, _position: LspPosition) {
    ///         *self.0.borrow_mut() += 1;
    ///     }
    /// }
    ///
    /// let requests = Rc::new(RefCell::new(0));
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// editor.attach_lsp(
    ///     Box::new(CompletionClient(Rc::clone(&requests))),
    ///     LspDocument::new("file:///tmp/main.rs", "rust"),
    /// );
    ///
    /// editor.lsp_request_completion();
    /// assert_eq!(*requests.borrow(), 1);
    /// ```
    pub fn lsp_request_completion(&mut self) {
        let position = self.lsp_position_from_cursor();
        self.with_lsp(|client, document| {
            client.request_completion(document, position);
        });
    }

    /// Flushes pending LSP text changes to the attached client.
    ///
    /// This increments the document version and sends `did_change` with all
    /// queued changes. Changes stay queued while no client is attached, so a
    /// client attached later still receives a consistent document.
    ///
    /// Only needed when automatic flushing is off; see
    /// [`Self::set_lsp_auto_flush`].
    ///
    /// # Example
    ///
    /// ```
    /// use std::cell::RefCell;
    /// use std::rc::Rc;
    ///
    /// use iced_code_editor::{CodeEditor, LspClient, LspDocument, LspTextChange};
    ///
    /// /// Records how many changes each batch carried.
    /// struct BatchClient(Rc<RefCell<Vec<usize>>>);
    ///
    /// impl LspClient for BatchClient {
    ///     fn did_change(&mut self, _document: &LspDocument, changes: &[LspTextChange]) {
    ///         self.0.borrow_mut().push(changes.len());
    ///     }
    /// }
    ///
    /// let batches = Rc::new(RefCell::new(Vec::new()));
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    ///
    /// // Take over flushing, then drive it from a debounce timer.
    /// editor.set_lsp_auto_flush(false);
    /// editor.attach_lsp(
    ///     Box::new(BatchClient(Rc::clone(&batches))),
    ///     LspDocument::new("file:///tmp/main.rs", "rust"),
    /// );
    ///
    /// // Flushing an empty queue is a no-op, so a timer can call this
    /// // unconditionally without sending empty notifications.
    /// editor.lsp_flush_pending_changes();
    /// assert!(batches.borrow().is_empty());
    /// ```
    pub fn lsp_flush_pending_changes(&mut self) {
        if self.lsp_pending_changes.is_empty() {
            return;
        }
        // Only drain the queue once a client and document are confirmed
        // attached — otherwise the changes must stay queued for a future
        // `attach_lsp`/`lsp_open_document` flush.
        if self.lsp_client.is_none() || self.lsp_document.is_none() {
            return;
        }

        let changes = std::mem::take(&mut self.lsp_pending_changes);
        self.with_lsp_mut_document(|client, document| {
            document.version = document.version.saturating_add(1);
            client.did_change(document, &changes);
        });
    }

    /// Sets whether LSP changes are flushed automatically after edits.
    ///
    /// On by default, which is what most hosts want. Turn it off to batch
    /// rapid typing behind a debounce timer and call
    /// [`Self::lsp_flush_pending_changes`] yourself, so a chatty server isn't
    /// sent one notification per keystroke.
    ///
    /// # Arguments
    ///
    /// * `auto_flush` - `true` to flush after every edit, `false` to flush manually
    ///
    /// # Example
    ///
    /// ```
    /// use iced_code_editor::CodeEditor;
    ///
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    ///
    /// // Take over flushing so edits can be debounced by the host.
    /// editor.set_lsp_auto_flush(false);
    /// ```
    pub fn set_lsp_auto_flush(&mut self, auto_flush: bool) {
        self.lsp_auto_flush = auto_flush;
    }

    /// Converts the current cursor position into an LSP position.
    fn lsp_position_from_cursor(&self) -> lsp::LspPosition {
        let pos = self.cursors.primary_position();
        let line = u32::try_from(pos.0).unwrap_or(u32::MAX);
        let character = u32::try_from(pos.1).unwrap_or(u32::MAX);
        lsp::LspPosition { line, character }
    }

    /// Converts a canvas point into an LSP position, if it hits the buffer.
    fn lsp_position_from_point(
        &self,
        point: iced::Point,
    ) -> Option<lsp::LspPosition> {
        let (line, col) = self.calculate_cursor_from_point(point)?;
        let line = u32::try_from(line).unwrap_or(u32::MAX);
        let character = u32::try_from(col).unwrap_or(u32::MAX);
        Some(lsp::LspPosition { line, character })
    }

    /// Computes and queues the latest LSP text change for the buffer.
    ///
    /// When auto-flush is enabled, this immediately sends changes.
    pub(crate) fn enqueue_lsp_change(&mut self) {
        if self.lsp_document.is_none() {
            return;
        }

        let new_text = self.buffer.to_string();
        let change = if self.lsp_shadow_is_current {
            lsp::compute_text_change(&self.lsp_shadow_text, &new_text)
        } else {
            let end_line = self.lsp_synced_line_count.saturating_sub(1);
            Some(lsp::LspTextChange {
                range: lsp::LspRange {
                    start: lsp::LspPosition { line: 0, character: 0 },
                    end: lsp::LspPosition {
                        line: u32::try_from(end_line).unwrap_or(u32::MAX),
                        character: u32::try_from(self.lsp_synced_last_line_len)
                            .unwrap_or(u32::MAX),
                    },
                },
                text: new_text.clone(),
            })
        };
        if let Some(change) = change {
            self.lsp_pending_changes.push(change);
        }
        self.lsp_shadow_text = new_text;
        self.lsp_shadow_is_current = true;
        self.update_lsp_synced_extent();
        if self.lsp_auto_flush {
            self.lsp_flush_pending_changes();
        }
    }

    /// Queues the bounded range replacement captured before a normal editor
    /// command. Unlike `enqueue_lsp_change`, this never serializes or diffs the
    /// complete document.
    pub(crate) fn enqueue_incremental_lsp_change(&mut self) {
        if self.lsp_document.is_none() {
            self.lsp_edit_snapshot = None;
            return;
        }

        let Some(snapshot) = self.lsp_edit_snapshot.take() else {
            self.enqueue_lsp_change();
            return;
        };

        let new_line_count = self.buffer.line_count();
        let start_line =
            snapshot.start_line.min(new_line_count.saturating_sub(1));
        let new_end_exclusive = if new_line_count >= snapshot.old_line_count {
            snapshot
                .old_end_exclusive
                .saturating_add(new_line_count - snapshot.old_line_count)
                .min(new_line_count)
        } else {
            snapshot
                .old_end_exclusive
                .saturating_sub(snapshot.old_line_count - new_line_count)
                .max(start_line.saturating_add(1))
                .min(new_line_count)
        };
        let text =
            self.buffer.line_range_to_string(start_line, new_end_exclusive);
        self.lsp_pending_changes.push(lsp::LspTextChange {
            range: lsp::LspRange {
                start: lsp::LspPosition {
                    line: u32::try_from(snapshot.start_line)
                        .unwrap_or(u32::MAX),
                    character: 0,
                },
                end: snapshot.old_end,
            },
            text,
        });

        // The shadow string is intentionally not rewritten here: doing so
        // would reintroduce an O(document size) copy. The compact extent below
        // is sufficient for a rare future full-document fallback.
        self.lsp_shadow_text = String::new();
        self.lsp_shadow_is_current = false;
        self.update_lsp_synced_extent();
        if self.lsp_auto_flush {
            self.lsp_flush_pending_changes();
        }
    }

    /// Updates the compact extent of the document state represented by queued
    /// and already-flushed LSP changes.
    ///
    /// The extent is the document the server holds, which is
    /// [`crate::buffer::TextBuffer::to_string`]: with a trailing newline, that
    /// text has one more, empty line after the buffer's last one. A
    /// full-document replacement that stopped before it would leave the old
    /// newline after the new text.
    fn update_lsp_synced_extent(&mut self) {
        let line_count = self.buffer.line_count();
        if self.buffer.has_trailing_newline() {
            self.lsp_synced_line_count = line_count.saturating_add(1);
            self.lsp_synced_last_line_len = 0;
        } else {
            self.lsp_synced_line_count = line_count;
            self.lsp_synced_last_line_len =
                self.buffer.line_len(line_count.saturating_sub(1));
        }
    }

    /// Sets whether LSP support is enabled.
    ///
    /// When set to `false`, any attached LSP client is detached automatically.
    /// Calling [`attach_lsp`] while disabled is a no-op.
    ///
    /// # Example
    ///
    /// ```
    /// use iced_code_editor::CodeEditor;
    ///
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// editor.set_lsp_enabled(false);
    /// ```
    ///
    /// [`attach_lsp`]: CodeEditor::attach_lsp
    pub fn set_lsp_enabled(&mut self, enabled: bool) {
        self.lsp_enabled = enabled;
        if !enabled {
            self.detach_lsp();
        }
    }

    /// Returns whether LSP support is enabled.
    ///
    /// # Returns
    ///
    /// `true` if LSP is enabled, `false` otherwise
    ///
    /// # Example
    ///
    /// ```
    /// use iced_code_editor::CodeEditor;
    ///
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// // Enabled by default, though no client is attached until you attach one.
    /// assert!(editor.lsp_enabled());
    ///
    /// editor.set_lsp_enabled(false);
    /// assert!(!editor.lsp_enabled());
    /// ```
    pub fn lsp_enabled(&self) -> bool {
        self.lsp_enabled
    }

    /// URI of the document this editor has open with its LSP client.
    ///
    /// `None` until a document is attached or opened. Hosts use it to route
    /// per-document server events, such as diagnostics, to the right editor.
    ///
    /// # Example
    ///
    /// ```
    /// use iced_code_editor::CodeEditor;
    ///
    /// let editor = CodeEditor::new("fn main() {}", "rs");
    /// assert_eq!(editor.lsp_document_uri(), None);
    /// ```
    pub fn lsp_document_uri(&self) -> Option<&str> {
        self.lsp_document.as_ref().map(|document| document.uri.as_str())
    }

    /// Initiates a "Go to Definition" request for the symbol at the current cursor position.
    ///
    /// This method converts the current cursor coordinates into an LSP-compatible position
    /// and delegates the request to the active `LspClient`, if one is attached.
    ///
    /// Fire-and-forget: the resolved location arrives through the client
    /// implementation, and the host decides whether to follow it.
    ///
    /// # Example
    ///
    /// ```
    /// use std::cell::RefCell;
    /// use std::rc::Rc;
    ///
    /// use iced_code_editor::{CodeEditor, LspClient, LspDocument, LspPosition};
    ///
    /// /// Records the position each definition request asked about.
    /// struct DefinitionClient(Rc<RefCell<Vec<LspPosition>>>);
    ///
    /// impl LspClient for DefinitionClient {
    ///     fn request_definition(&mut self, _document: &LspDocument, position: LspPosition) {
    ///         self.0.borrow_mut().push(position);
    ///     }
    /// }
    ///
    /// let requests = Rc::new(RefCell::new(Vec::new()));
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    /// editor.attach_lsp(
    ///     Box::new(DefinitionClient(Rc::clone(&requests))),
    ///     LspDocument::new("file:///tmp/main.rs", "rust"),
    /// );
    ///
    /// editor.lsp_request_definition();
    /// assert_eq!(requests.borrow().len(), 1);
    /// ```
    pub fn lsp_request_definition(&mut self) {
        let position = self.lsp_position_from_cursor();
        self.with_lsp(|client, document| {
            client.request_definition(document, position);
        });
    }

    /// Initiates a "Go to Definition" request for the symbol at the specified screen coordinates.
    ///
    /// This is typically used for mouse interactions (e.g., Ctrl+Click). It first resolves
    /// the screen coordinates to a text position and then sends the request.
    ///
    /// # Arguments
    ///
    /// * `point` - The click position in canvas coordinates
    ///
    /// # Returns
    ///
    /// `true` if the request was successfully sent (i.e., a valid position was found and an LSP client is active),
    /// `false` otherwise.
    ///
    /// # Example
    ///
    /// ```
    /// use iced::Point;
    /// use iced_code_editor::CodeEditor;
    ///
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    ///
    /// // With no client attached there is nothing to send.
    /// assert!(!editor.lsp_request_definition_at(Point::new(50.0, 5.0)));
    /// ```
    pub fn lsp_request_definition_at(&mut self, point: iced::Point) -> bool {
        let Some(position) = self.lsp_position_from_point(point) else {
            return false;
        };
        self.with_lsp(|client, document| {
            client.request_definition(document, position);
        })
        .is_some()
    }

    /// Requests formatting edits for the whole document.
    ///
    /// The formatting options are derived from the editor's own
    /// [`IndentStyle`], so the server formats the way this editor indents.
    /// Queued changes are flushed first: the server formats the document it
    /// has, so an unflushed keystroke would come back reformatted away.
    ///
    /// Like every other request this is fire-and-forget. The reply is a batch
    /// of edits, which the host applies with
    /// [`Self::apply_lsp_text_edits`].
    ///
    /// # Returns
    ///
    /// `true` if the request was sent; `false` when no client and document are
    /// attached
    ///
    /// [`IndentStyle`]: crate::IndentStyle
    ///
    /// # Example
    ///
    /// ```
    /// use std::cell::RefCell;
    /// use std::rc::Rc;
    ///
    /// use iced_code_editor::{
    ///     CodeEditor, IndentStyle, LspClient, LspDocument, LspFormattingOptions,
    /// };
    ///
    /// /// Records the options each formatting request carried.
    /// struct FormattingClient(Rc<RefCell<Vec<LspFormattingOptions>>>);
    ///
    /// impl LspClient for FormattingClient {
    ///     fn request_formatting(
    ///         &mut self,
    ///         _document: &LspDocument,
    ///         options: LspFormattingOptions,
    ///     ) {
    ///         self.0.borrow_mut().push(options);
    ///     }
    /// }
    ///
    /// let requests = Rc::new(RefCell::new(Vec::new()));
    /// let mut editor = CodeEditor::new("fn main() {}", "rs");
    ///
    /// // Nothing is attached yet, so there is nobody to ask.
    /// assert!(!editor.lsp_request_formatting());
    ///
    /// editor.set_indent_style(IndentStyle::Spaces(2));
    /// editor.attach_lsp(
    ///     Box::new(FormattingClient(Rc::clone(&requests))),
    ///     LspDocument::new("file:///tmp/main.rs", "rust"),
    /// );
    ///
    /// assert!(editor.lsp_request_formatting());
    /// assert_eq!(requests.borrow()[0].tab_size, 2);
    /// ```
    pub fn lsp_request_formatting(&mut self) -> bool {
        self.lsp_flush_pending_changes();
        let options = lsp::LspFormattingOptions::from(self.indent_style());
        self.with_lsp(|client, document| {
            client.request_formatting(document, options);
        })
        .is_some()
    }
}

/// Sends `did_open` for `document` (after stamping `version = 1`) on
/// `client`, using `buffer`'s current contents.
///
/// Returns the stamped document and the serialized buffer text, both needed
/// by the caller to update its LSP shadow-sync bookkeeping.
///
/// Free function (not a method) so callers can hold a mutable borrow of
/// `self.lsp_client` while still passing `&self.buffer` — a method taking
/// `&mut self` here would conflict with that borrow in `lsp_open_document`.
fn open_lsp_document(
    client: &mut dyn lsp::LspClient,
    buffer: &TextBuffer,
    mut document: lsp::LspDocument,
) -> (lsp::LspDocument, String) {
    document.version = 1;
    let text = buffer.to_string();
    client.did_open(&document, &text);
    (document, text)
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::rc::Rc;

    use crate::canvas_editor::Message;

    use super::*;

    #[derive(Default)]
    struct TestLspClient {
        changes: Rc<RefCell<Vec<Vec<lsp::LspTextChange>>>>,
    }

    impl lsp::LspClient for TestLspClient {
        fn did_change(
            &mut self,
            _document: &lsp::LspDocument,
            changes: &[lsp::LspTextChange],
        ) {
            self.changes.borrow_mut().push(changes.to_vec());
        }
    }

    /// Records every [`lsp::LspClient`] method invoked on it, by name.
    #[derive(Default)]
    struct RecordingLspClient {
        calls: Rc<RefCell<Vec<String>>>,
    }

    impl lsp::LspClient for RecordingLspClient {
        fn did_open(&mut self, _document: &lsp::LspDocument, _text: &str) {
            self.calls.borrow_mut().push("did_open".to_string());
        }
        fn did_close(&mut self, _document: &lsp::LspDocument) {
            self.calls.borrow_mut().push("did_close".to_string());
        }
        fn did_save(&mut self, _document: &lsp::LspDocument, _text: &str) {
            self.calls.borrow_mut().push("did_save".to_string());
        }
        fn request_hover(
            &mut self,
            _document: &lsp::LspDocument,
            _position: lsp::LspPosition,
        ) {
            self.calls.borrow_mut().push("request_hover".to_string());
        }
        fn did_change(
            &mut self,
            _document: &lsp::LspDocument,
            _changes: &[lsp::LspTextChange],
        ) {
            self.calls.borrow_mut().push("did_change".to_string());
        }
        fn request_formatting(
            &mut self,
            _document: &lsp::LspDocument,
            _options: lsp::LspFormattingOptions,
        ) {
            self.calls.borrow_mut().push("request_formatting".to_string());
        }
    }

    #[test]
    fn test_enqueue_lsp_change_auto_flush() {
        let changes = Rc::new(RefCell::new(Vec::new()));
        let client = TestLspClient { changes: Rc::clone(&changes) };
        let mut editor = CodeEditor::new("hello", "rs");
        editor.attach_lsp(
            Box::new(client),
            lsp::LspDocument::new("file:///test.rs", "rust"),
        );
        editor.set_lsp_auto_flush(true);

        editor.buffer.insert_char(0, 5, '!');
        editor.enqueue_lsp_change();

        let changes = changes.borrow();
        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].len(), 1);
        let change = &changes[0][0];
        assert_eq!(change.text, "!");
        assert_eq!(change.range.start.line, 0);
        assert_eq!(change.range.start.character, 5);
        assert_eq!(change.range.end.line, 0);
        assert_eq!(change.range.end.character, 5);
    }

    #[test]
    fn test_editor_update_sends_bounded_incremental_lsp_change() {
        let changes = Rc::new(RefCell::new(Vec::new()));
        let client = TestLspClient { changes: Rc::clone(&changes) };
        let content = (0..10)
            .map(|line| format!("line{line}"))
            .collect::<Vec<_>>()
            .join("\n");
        let mut editor = CodeEditor::new(&content, "rs");
        editor.attach_lsp(
            Box::new(client),
            lsp::LspDocument::new("file:///large.rs", "rust"),
        );
        editor.request_focus();
        editor.has_canvas_focus = true;
        editor.focus_locked = false;
        editor.cursors.primary_mut().position = (5, 2);

        let _ = editor.update(&Message::CharacterInput('X'));

        let changes = changes.borrow();
        assert_eq!(changes.len(), 1);
        assert_eq!(changes[0].len(), 1);
        let change = &changes[0][0];
        assert_eq!(change.range.start.line, 4);
        assert_eq!(change.range.start.character, 0);
        assert_eq!(change.range.end.line, 7);
        assert_eq!(change.range.end.character, 0);
        assert_eq!(change.text, "line4\nliXne5\nline6\n");
        assert!(!editor.lsp_shadow_is_current);
        assert!(editor.lsp_shadow_text.is_empty());
    }

    #[test]
    fn test_open_lsp_document_stamps_version_and_sends_did_open() {
        let calls = Rc::new(RefCell::new(Vec::new()));
        let mut client = RecordingLspClient { calls: Rc::clone(&calls) };
        let buffer = TextBuffer::new("hello");
        let document = lsp::LspDocument::new("file:///test.rs", "rust");

        let (document, text) =
            open_lsp_document(&mut client, &buffer, document);

        assert_eq!(document.version, 1);
        assert_eq!(text, "hello");
        assert_eq!(calls.borrow().as_slice(), ["did_open"]);
    }

    #[test]
    fn test_lsp_document_uri_follows_attach_and_detach() {
        let calls = Rc::new(RefCell::new(Vec::new()));
        let client = RecordingLspClient { calls: Rc::clone(&calls) };
        let mut editor = CodeEditor::new("hello", "rs");
        assert_eq!(editor.lsp_document_uri(), None);

        editor.attach_lsp(
            Box::new(client),
            lsp::LspDocument::new("file:///test.rs", "rust"),
        );
        assert_eq!(editor.lsp_document_uri(), Some("file:///test.rs"));

        editor.detach_lsp();
        assert_eq!(editor.lsp_document_uri(), None);
    }

    #[test]
    fn test_with_lsp_runs_closure_when_client_and_document_present() {
        let calls = Rc::new(RefCell::new(Vec::new()));
        let client = RecordingLspClient { calls: Rc::clone(&calls) };
        let mut editor = CodeEditor::new("hello", "rs");
        editor.attach_lsp(
            Box::new(client),
            lsp::LspDocument::new("file:///test.rs", "rust"),
        );
        calls.borrow_mut().clear(); // attach_lsp itself already logged did_open

        let ran = editor.with_lsp(|client, document| {
            client.did_save(document, "hello");
        });

        assert!(ran.is_some());
        assert_eq!(calls.borrow().as_slice(), ["did_save"]);
    }

    #[test]
    fn test_with_lsp_returns_none_when_client_absent() {
        let mut editor = CodeEditor::new("hello", "rs");
        let ran = editor.with_lsp(|_client, _document| {});
        assert!(ran.is_none());
    }

    #[test]
    fn test_lsp_did_save_sends_current_buffer_contents() {
        let calls = Rc::new(RefCell::new(Vec::new()));
        let client = RecordingLspClient { calls: Rc::clone(&calls) };
        let mut editor = CodeEditor::new("hello", "rs");
        editor.attach_lsp(
            Box::new(client),
            lsp::LspDocument::new("file:///test.rs", "rust"),
        );
        calls.borrow_mut().clear(); // attach_lsp itself already logged did_open

        editor.lsp_did_save();

        assert_eq!(calls.borrow().as_slice(), ["did_save"]);
    }

    #[test]
    fn test_lsp_did_save_is_a_noop_without_an_attached_client() {
        // Regression: this used to serialize the whole buffer via
        // `to_string()` even with no client/document attached.
        let mut editor = CodeEditor::new("hello", "rs");
        editor.lsp_did_save(); // must not panic
    }

    #[test]
    fn test_with_lsp_returns_none_when_document_absent() {
        let calls = Rc::new(RefCell::new(Vec::new()));
        let client = RecordingLspClient { calls: Rc::clone(&calls) };
        let mut editor = CodeEditor::new("hello", "rs");
        editor.attach_lsp(
            Box::new(client),
            lsp::LspDocument::new("file:///test.rs", "rust"),
        );
        editor.lsp_document = None;

        let ran = editor.with_lsp(|_client, _document| {});
        assert!(ran.is_none());
    }

    #[test]
    #[allow(clippy::unwrap_used)]
    fn test_with_lsp_mut_document_allows_version_bump() {
        let calls = Rc::new(RefCell::new(Vec::new()));
        let client = RecordingLspClient { calls: Rc::clone(&calls) };
        let mut editor = CodeEditor::new("hello", "rs");
        editor.attach_lsp(
            Box::new(client),
            lsp::LspDocument::new("file:///test.rs", "rust"),
        );

        editor.with_lsp_mut_document(|_client, document| {
            document.version = document.version.saturating_add(1);
        });

        assert_eq!(editor.lsp_document.as_ref().unwrap().version, 2);
    }

    #[test]
    fn test_lsp_flush_pending_changes_preserves_queue_when_no_client_attached()
    {
        let mut editor = CodeEditor::new("hello", "rs");
        editor.lsp_pending_changes.push(lsp::LspTextChange {
            range: lsp::LspRange {
                start: lsp::LspPosition { line: 0, character: 0 },
                end: lsp::LspPosition { line: 0, character: 0 },
            },
            text: "x".to_string(),
        });

        editor.lsp_flush_pending_changes();

        assert_eq!(editor.lsp_pending_changes.len(), 1);
    }

    #[test]
    fn test_lsp_request_formatting_flushes_queued_changes_first() {
        // The server formats the document it holds, so a keystroke still
        // sitting in the queue has to reach it before the request does.
        let calls = Rc::new(RefCell::new(Vec::new()));
        let client = RecordingLspClient { calls: Rc::clone(&calls) };
        let mut editor = CodeEditor::new("hello", "rs");
        editor.set_lsp_auto_flush(false);
        editor.attach_lsp(
            Box::new(client),
            lsp::LspDocument::new("file:///test.rs", "rust"),
        );

        editor.buffer.insert_char(0, 5, '!');
        editor.enqueue_lsp_change();
        assert!(editor.lsp_request_formatting());

        assert_eq!(
            calls.borrow().as_slice(),
            ["did_open", "did_change", "request_formatting"]
        );
    }

    #[test]
    fn test_lsp_request_formatting_is_a_noop_without_an_attached_client() {
        let mut editor = CodeEditor::new("hello", "rs");
        assert!(!editor.lsp_request_formatting());
    }

    /// Mirrors the document the way a server does: the text from
    /// `did_open`, with every `did_change` applied to it.
    #[derive(Default)]
    struct MirrorLspClient {
        text: Rc<RefCell<String>>,
    }

    /// Byte offset of an LSP position (ASCII text, so characters are bytes).
    fn mirror_offset(text: &str, position: lsp::LspPosition) -> usize {
        let mut offset = 0;
        for (index, line) in text.split('\n').enumerate() {
            if index == position.line as usize {
                return offset + (position.character as usize).min(line.len());
            }
            offset += line.len() + 1;
        }
        text.len()
    }

    impl lsp::LspClient for MirrorLspClient {
        fn did_open(&mut self, _document: &lsp::LspDocument, text: &str) {
            *self.text.borrow_mut() = text.to_string();
        }
        fn did_change(
            &mut self,
            _document: &lsp::LspDocument,
            changes: &[lsp::LspTextChange],
        ) {
            let mut text = self.text.borrow_mut();
            for change in changes {
                let start = mirror_offset(&text, change.range.start);
                let end = mirror_offset(&text, change.range.end).max(start);
                text.replace_range(start..end, &change.text);
            }
        }
    }

    #[test]
    fn test_full_document_change_keeps_the_trailing_newline_once() {
        // Enter goes out as an incremental change, which leaves the
        // shadow text stale; the edit applied afterwards then falls back to
        // replacing the whole document. Its range must reach the end of the
        // document as the server holds it, past the trailing newline, or the
        // server ends up with the newline twice.
        for source in ["fn main() {\n    let t = 1;\n}\n", "fn main() {}"] {
            let mirror = Rc::new(RefCell::new(String::new()));
            let client = MirrorLspClient { text: Rc::clone(&mirror) };
            let mut editor = CodeEditor::new(source, "rs");
            editor.request_focus();
            editor.attach_lsp(
                Box::new(client),
                lsp::LspDocument::new("file:///test.rs", "rust"),
            );
            assert_eq!(*mirror.borrow(), source);

            let _ = editor.update(&Message::Enter);
            assert_eq!(*mirror.borrow(), editor.buffer.to_string());
            assert!(!editor.lsp_shadow_is_current, "{source:?}");

            let applied = editor.apply_lsp_text_edits(&[lsp::LspTextChange {
                range: lsp::LspRange {
                    start: lsp::LspPosition { line: 0, character: 0 },
                    end: lsp::LspPosition { line: 0, character: 0 },
                },
                text: "use std::fmt;\n\n".to_string(),
            }]);
            assert!(applied);
            assert_eq!(
                *mirror.borrow(),
                editor.buffer.to_string(),
                "{source:?}"
            );
        }
    }
}