reovim-server 0.14.4

Reovim server - the editing engine
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
use super::*;

use std::{ops::Range, sync::Arc};

use reovim_driver_syntax::{
    Annotation, HighlightCategory, SyntaxDriver, SyntaxDriverFactory, SyntaxEdit,
};

/// A minimal test driver for unit tests.
struct TestDriver {
    language: String,
    parsed: bool,
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl TestDriver {
    fn new(language: &str) -> Self {
        Self {
            language: language.to_string(),
            parsed: false,
        }
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
impl SyntaxDriver for TestDriver {
    fn language(&self) -> &str {
        &self.language
    }

    fn parse(&mut self, _content: &str) {
        self.parsed = true;
    }

    fn update(&mut self, _content: &str, _edit: &SyntaxEdit) {
        // No-op
    }

    fn highlights(&self, byte_range: Range<usize>) -> Vec<Annotation> {
        if self.parsed {
            vec![Annotation::new(
                byte_range.start,
                byte_range.end,
                HighlightCategory::new("comment"),
            )]
        } else {
            Vec::new()
        }
    }

    fn is_parsed(&self) -> bool {
        self.parsed
    }
}

/// A minimal test factory.
struct TestFactory;

#[cfg_attr(coverage_nightly, coverage(off))]
impl SyntaxDriverFactory for TestFactory {
    fn create(&self, language_id: &str) -> Option<Box<dyn SyntaxDriver>> {
        if language_id == "rust" {
            Some(Box::new(TestDriver::new("rust")))
        } else {
            None
        }
    }

    fn supported_languages(&self) -> Vec<&str> {
        vec!["rust"]
    }

    fn supports(&self, language_id: &str) -> bool {
        language_id == "rust"
    }
}

fn buffer_id(n: usize) -> BufferId {
    BufferId::from_raw(n)
}

// ========================================================================
// SyntaxStreamState tests
// ========================================================================

#[test]
fn test_stream_state_new() {
    let state = SyntaxStreamState::new();
    assert_eq!(state.subscriber_count(), 0);
    assert!(!state.has_subscribers());
}

#[test]
fn test_stream_state_session_extension() {
    let state = SyntaxStreamState::create();
    assert_eq!(state.subscriber_count(), 0);
}

#[test]
fn test_subscribe() {
    let mut state = SyntaxStreamState::new();
    assert_eq!(state.subscriber_count(), 0);

    let _rx1 = state.subscribe();
    assert_eq!(state.subscriber_count(), 1);
    assert!(state.has_subscribers());

    let _rx2 = state.subscribe();
    assert_eq!(state.subscriber_count(), 2);
}

#[test]
fn test_broadcast() {
    let mut state = SyntaxStreamState::new();
    let mut rx = state.subscribe();

    let update = TokenUpdate {
        buffer_id: 1,
        tokens: vec![],
        start_line: 0,
        end_line: 0,
        full_refresh: false,
        layer: "syntax".into(),
        priority: 0,
    };

    state.broadcast(&update);

    let received = rx.try_recv().expect("Should receive update");
    assert_eq!(received.buffer_id, 1);
    assert_eq!(received.layer, "syntax");
    assert_eq!(received.priority, 0);
}

#[test]
fn test_broadcast_removes_disconnected() {
    let mut state = SyntaxStreamState::new();
    let rx = state.subscribe();
    assert_eq!(state.subscriber_count(), 1);

    // Drop the receiver to disconnect
    drop(rx);

    let update = TokenUpdate {
        buffer_id: 1,
        tokens: vec![],
        start_line: 0,
        end_line: 0,
        full_refresh: false,
        layer: "syntax".into(),
        priority: 0,
    };

    state.broadcast(&update);

    // Disconnected subscriber should be removed
    assert_eq!(state.subscriber_count(), 0);
}

#[tokio::test]
async fn test_notify_edit_with_subscriber() {
    let mut syntax = SyntaxSessionState::new();
    let mut stream = SyntaxStreamState::new();
    let id = buffer_id(1);

    // Set up a driver
    syntax.set(id, Box::new(TestDriver::new("rust")));
    syntax.get_mut(id).unwrap().parse("fn main() {}");

    // Subscribe
    let mut rx = stream.subscribe();
    assert_eq!(stream.subscriber_count(), 1);

    // Create a simple edit
    let edit = SyntaxEdit::insert(0, 0, 0, 3, 0, 3);

    // Notify edit
    stream.notify_edit(&mut syntax, id, "fn main() {}", &edit, 0, 0);

    // Should receive an update
    let update = rx.try_recv().expect("Should receive update");
    assert_eq!(update.buffer_id, 1);
    assert!(!update.full_refresh);
    assert_eq!(update.layer, "syntax");
    assert_eq!(update.priority, 0);
}

#[test]
fn test_notify_edit_no_driver() {
    let mut syntax = SyntaxSessionState::new();
    let mut stream = SyntaxStreamState::new();
    let id = buffer_id(1);

    // No driver set
    let edit = SyntaxEdit::insert(0, 0, 0, 3, 0, 3);

    // Should not panic
    stream.notify_edit(&mut syntax, id, "hello", &edit, 0, 0);
}

#[test]
fn test_send_full_refresh() {
    let mut syntax = SyntaxSessionState::new();
    let mut stream = SyntaxStreamState::new();
    let id = buffer_id(1);

    // Set up a driver
    syntax.set(id, Box::new(TestDriver::new("rust")));
    syntax.get_mut(id).unwrap().parse("fn main() {}");

    // Subscribe
    let mut rx = stream.subscribe();

    // Send full refresh
    stream.send_full_refresh(&syntax, id, 10);

    // Should receive a full refresh update
    let update = rx.try_recv().expect("Should receive update");
    assert_eq!(update.buffer_id, 1);
    assert!(update.full_refresh);
    assert_eq!(update.end_line, 9); // total_lines - 1
    assert_eq!(update.layer, "syntax");
    assert_eq!(update.priority, 0);
}

#[test]
fn test_notify_edit_no_subscribers_skips_extraction() {
    let mut syntax = SyntaxSessionState::new();
    let mut stream = SyntaxStreamState::new();
    let id = buffer_id(1);

    syntax.set(id, Box::new(TestDriver::new("rust")));

    // No subscribers: notify_edit should return early after driver.update()
    let edit = SyntaxEdit {
        start_byte: 0,
        old_end_byte: 0,
        new_end_byte: 5,
        start_row: 0,
        start_col: 0,
        old_end_row: 0,
        old_end_col: 0,
        new_end_row: 0,
        new_end_col: 5,
    };
    stream.notify_edit(&mut syntax, id, "hello", &edit, 0, 0);
    // No panic, no subscribers to receive
}

#[test]
fn test_send_full_refresh_no_driver_returns_early() {
    let syntax = SyntaxSessionState::new();
    let mut stream = SyntaxStreamState::new();
    let _rx = stream.subscribe(); // Has subscriber but no driver
    let unknown = buffer_id(999);

    // Should return early (no driver)
    stream.send_full_refresh(&syntax, unknown, 10);
    // No panic
}

#[test]
fn test_send_full_refresh_no_subscribers_returns_early() {
    let mut syntax = SyntaxSessionState::new();
    let stream = SyntaxStreamState::new();
    let id = buffer_id(1);
    syntax.set(id, Box::new(TestDriver::new("rust")));

    // Has driver but no subscribers: returns early
    // Note: we need &mut self for send_full_refresh, use a mutable binding
    let mut stream = stream;
    stream.send_full_refresh(&syntax, id, 10);
    // No panic
}

// =============================================================================
// modification_to_syntax_edit tests (#655)
// =============================================================================

#[test]
fn test_modification_to_syntax_edit_insert() {
    use reovim_kernel::api::v1::events::kernel::Modification;

    let modification = Modification::Insert {
        start: (0, 5),
        text: "hello".to_string(),
        start_byte: 5,
    };
    let edit = modification_to_syntax_edit(&modification).unwrap();
    assert_eq!(edit.start_byte, 5);
    assert_eq!(edit.old_end_byte, 5); // Insert: old_end = start
    assert_eq!(edit.new_end_byte, 10); // 5 + "hello".len()
    assert_eq!(edit.start_row, 0);
    assert_eq!(edit.start_col, 5);
    assert_eq!(edit.new_end_row, 0);
    assert_eq!(edit.new_end_col, 10);
}

#[test]
fn test_modification_to_syntax_edit_insert_multiline() {
    use reovim_kernel::api::v1::events::kernel::Modification;

    let modification = Modification::Insert {
        start: (1, 3),
        text: "ab\ncd".to_string(),
        start_byte: 10,
    };
    let edit = modification_to_syntax_edit(&modification).unwrap();
    assert_eq!(edit.start_byte, 10);
    assert_eq!(edit.new_end_byte, 15); // 10 + 5
    assert_eq!(edit.new_end_row, 2); // 1 + 1 newline
    assert_eq!(edit.new_end_col, 2); // "cd" after newline
}

#[test]
fn test_modification_to_syntax_edit_delete() {
    use reovim_kernel::api::v1::events::kernel::Modification;

    let modification = Modification::Delete {
        start: (0, 0),
        end: (0, 5),
        text: "hello".to_string(),
        start_byte: 0,
    };
    let edit = modification_to_syntax_edit(&modification).unwrap();
    assert_eq!(edit.start_byte, 0);
    assert_eq!(edit.old_end_byte, 5); // 0 + "hello".len()
    assert_eq!(edit.new_end_byte, 0); // Delete: new_end = start
    assert_eq!(edit.old_end_row, 0);
    assert_eq!(edit.old_end_col, 5);
}

#[test]
fn test_modification_to_syntax_edit_replace() {
    use reovim_kernel::api::v1::events::kernel::Modification;

    let modification = Modification::Replace {
        start: (0, 0),
        end: (0, 5),
        old_text: "hello".to_string(),
        new_text: "world!".to_string(),
        start_byte: 0,
    };
    let edit = modification_to_syntax_edit(&modification).unwrap();
    assert_eq!(edit.start_byte, 0);
    assert_eq!(edit.old_end_byte, 5); // "hello".len()
    assert_eq!(edit.new_end_byte, 6); // "world!".len()
    assert_eq!(edit.new_end_row, 0);
    assert_eq!(edit.new_end_col, 6);
}

#[test]
fn test_modification_to_syntax_edit_full_replace() {
    use reovim_kernel::api::v1::events::kernel::Modification;

    assert!(modification_to_syntax_edit(&Modification::FullReplace).is_none());
}

// =============================================================================
// compute_end_position tests (#655)
// =============================================================================

#[test]
fn test_compute_end_position_single_line() {
    assert_eq!(compute_end_position(0, 0, "hello"), (0, 5));
    assert_eq!(compute_end_position(2, 3, "abc"), (2, 6));
}

#[test]
fn test_compute_end_position_multi_line() {
    assert_eq!(compute_end_position(0, 0, "ab\ncd"), (1, 2));
    assert_eq!(compute_end_position(5, 10, "x\ny\nz"), (7, 1));
}

#[test]
fn test_compute_end_position_empty() {
    assert_eq!(compute_end_position(3, 7, ""), (3, 7));
}

#[test]
fn test_compute_end_position_trailing_newline() {
    assert_eq!(compute_end_position(0, 0, "abc\n"), (1, 0));
}

#[test]
fn test_debug_impl() {
    let mut state = SyntaxStreamState::new();
    let _rx = state.subscribe();

    let debug = format!("{state:?}");
    assert!(debug.contains("SyntaxStreamState"));
    assert!(debug.contains("subscriber_count"));
}

// ========================================================================
// SyntaxSessionState re-export sanity test
// ========================================================================

// ========================================================================
// build_token_update tests
// ========================================================================

#[test]
fn test_build_token_update_with_driver() {
    let mut syntax = SyntaxSessionState::new();
    let id = buffer_id(1);

    syntax.set(id, Box::new(TestDriver::new("rust")));
    syntax.get_mut(id).unwrap().parse("fn main() {}");

    let update = build_token_update(&syntax, id, 10, true);
    assert!(update.is_some());

    let update = update.unwrap();
    assert_eq!(update.buffer_id, 1);
    assert!(update.full_refresh);
    assert_eq!(update.start_line, 0);
    assert_eq!(update.end_line, 9);
    assert!(!update.tokens.is_empty());
    assert_eq!(update.layer, "syntax");
    assert_eq!(update.priority, 0);
}

#[test]
fn test_build_token_update_no_driver() {
    let syntax = SyntaxSessionState::new();
    let id = buffer_id(1);

    let update = build_token_update(&syntax, id, 10, true);
    assert!(update.is_none());
}

#[test]
fn test_build_token_update_incremental() {
    let mut syntax = SyntaxSessionState::new();
    let id = buffer_id(1);

    syntax.set(id, Box::new(TestDriver::new("rust")));
    syntax.get_mut(id).unwrap().parse("fn main() {}");

    let update = build_token_update(&syntax, id, 5, false).unwrap();
    assert!(!update.full_refresh);
    assert_eq!(update.end_line, 4);
}

#[test]
fn test_syntax_session_state_reexport() {
    // Verify re-export works: SyntaxSessionState accessible from this module
    let mut state = SyntaxSessionState::new();
    let id = buffer_id(1);

    state.set_factory(Arc::new(TestFactory));
    assert!(state.ensure_driver(id, "rust", "fn main() {}"));
    assert!(state.get(id).is_some());
}