sveltui-code 0.1.0-alpha.4

TextMate syntax-highlighted code preview and editor components for sveltui
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
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use sveltui::Buffer;
use sveltui::element::scroll::get_scroll;
use sveltui::element::{render_node_interactive, reset_focus_counter};
use sveltui::layout::Rect;
use sveltui::prelude::*;
use sveltui_code::prelude::*;

pub struct TestTerminal {
    manager: FocusManager,
    buffer: Buffer,
    hits: HitMap,
    width: u16,
    height: u16,
}

impl TestTerminal {
    pub fn new(width: u16, height: u16) -> Self {
        Self {
            manager: FocusManager::new(),
            buffer: Buffer::new(width, height),
            hits: HitMap::new(),
            width,
            height,
        }
    }

    pub fn draw(&mut self, build: &impl Fn() -> VNode) {
        for _ in 0..5 {
            reset_focus_counter();
            begin_frame();
            let tree = self.manager.with_context(build);
            end_frame();

            self.buffer.clear();
            self.hits.clear();

            let screen_rect = Rect::new(0, 0, self.width, self.height);
            self.manager.with_context(|| {
                render_node_interactive(&tree, screen_rect, &mut self.buffer, &mut self.hits)
            });

            let changed = self.manager.reconcile(self.hits.interactions.clone());
            let revealed = self
                .manager
                .focused()
                .is_some_and(|id| self.hits.reveal(id));
            if !changed && !revealed {
                break;
            }
        }
    }

    pub fn send_key(&mut self, code: KeyCode, modifiers: KeyModifiers) {
        let key = KeyEvent::new(code, modifiers);
        self.manager.dispatch(key);
    }

    pub fn type_char(&mut self, c: char) {
        self.send_key(KeyCode::Char(c), KeyModifiers::NONE);
    }

    pub fn type_text(&mut self, text: &str) {
        for c in text.chars() {
            if c == '\n' {
                self.send_key(KeyCode::Enter, KeyModifiers::NONE);
            } else if c == '\t' {
                self.send_key(KeyCode::Tab, KeyModifiers::NONE);
            } else {
                self.type_char(c);
            }
        }
    }

    pub fn click_at(&mut self, x: u16, y: u16) {
        self.manager.pointer_down_at(&self.hits, x, y);
    }

    pub fn scroll(&mut self, x: u16, y: u16, dx: i16, dy: i16) {
        if let Some(target) = self.hits.scroll_target(x, y) {
            self.manager.scroll(target, dx, dy);
        }
    }

    pub fn focused_id(&self) -> Option<FocusId> {
        self.manager.focused()
    }

    pub fn line(&self, y: u16) -> String {
        (0..self.width)
            .map(|x| self.buffer.get(x, y).map(|c| c.ch).unwrap_or(' '))
            .collect::<String>()
            .trim_end()
            .to_string()
    }

    pub fn cursor(&self) -> Option<(u16, u16)> {
        self.buffer.cursor()
    }

    pub fn cursor_style(&self) -> Option<CursorStyle> {
        self.buffer.cursor_style()
    }

    pub fn cell_fg(&self, x: u16, y: u16) -> Option<Color> {
        self.buffer.get(x, y).and_then(|c| c.fg)
    }
}

#[test]
fn test_live_preview_sync_per_keystroke() {
    let mut term = TestTerminal::new(100, 10);
    let (code, set_code) = create_signal(String::new());

    let build = move || {
        view! {
            <View flex_direction=FlexDirection::Row width=Dimension::Percent(1.0) height=Dimension::Percent(1.0) gap=2>
                <CodeEditor
                    value={code.get()} on_value_change={move |v: String| set_code.set(v)}
                    language="rs"
                    width=45
                    height=8
                    show_line_numbers=false
                    autofocus=true
                />
                <CodePreview
                    value={code.get()}
                    language="rs"
                    width=45
                    height=8
                    show_line_numbers=false
                />
            </View>
        }
    };

    term.draw(&build);

    // Type character 'f'
    term.type_char('f');
    term.draw(&build);
    assert_eq!(code.get(), "f");

    // Both editor and preview screen lines should now contain 'f'
    let line1 = term.line(1);
    assert!(
        line1.contains('f'),
        "Line 1 should contain typed character 'f' in both panes, got: {:?}",
        line1
    );

    // Type remaining characters of "fn main() {"
    term.type_text("n main() {");
    term.draw(&build);
    assert_eq!(code.get(), "fn main() {");

    let screen_line = term.line(1);
    assert!(
        screen_line.contains("fn main() {"),
        "Expected screen to show complete function signature, got: {:?}",
        screen_line
    );
}

#[test]
fn test_visual_code_editor_typing_and_cursor() {
    let mut term = TestTerminal::new(60, 10);
    let (code, set_code) = create_signal(String::new());

    let build = move || {
        view! {
            <CodeEditor
                value={code.get()} on_value_change={move |v: String| set_code.set(v)}
                language="rs"
                width=50
                height=8
                show_line_numbers=true
                autofocus=true
            />
        }
    };

    // 1. Initial draw - editor should be autofocus-active with line 1 gutter
    term.draw(&build);
    let l1 = term.line(1);
    assert!(
        l1.contains("1 │"),
        "Line 1 should contain gutter '1 │', but was: {:?}",
        l1
    );

    let initial_cursor = term.cursor();
    assert!(
        initial_cursor.is_some(),
        "Cursor should be visible when focused"
    );

    // 2. Type "fn main() {"
    term.type_text("fn main() {");
    term.draw(&build);

    let l1_after_typing = term.line(1);
    assert!(
        l1_after_typing.contains("1 │ fn main() {"),
        "Expected '1 │ fn main() {{', got: {:?}",
        l1_after_typing
    );

    // 3. Hit Enter -> should auto-indent 4 spaces on line 2
    term.send_key(KeyCode::Enter, KeyModifiers::NONE);
    term.draw(&build);

    let l2 = term.line(2);
    assert!(
        l2.contains("2 │     "),
        "Line 2 should auto-indent with 4 spaces after '{{', got: {:?}",
        l2
    );

    // 4. Type "let x = 42;" on line 2
    term.type_text("let x = 42;");
    term.draw(&build);

    let l2_typed = term.line(2);
    assert!(
        l2_typed.contains("2 │     let x = 42;"),
        "Line 2 should contain code, got: {:?}",
        l2_typed
    );

    // 5. Test Backspace navigation
    term.send_key(KeyCode::Backspace, KeyModifiers::NONE); // deletes ';'
    term.draw(&build);
    assert!(term.line(2).contains("2 │     let x = 42"));

    // 6. Test Undo (Ctrl+Z)
    term.send_key(KeyCode::Char('z'), KeyModifiers::CONTROL);
    term.draw(&build);
    assert!(
        term.line(2).contains("2 │     let x = 42;"),
        "Undo should restore ';'"
    );

    // 7. Verify the reactive signal contains the multiline string
    assert_eq!(code.get(), "fn main() {\n    let x = 42;");
}

#[test]
fn test_visual_cursor_movement_and_click() {
    let mut term = TestTerminal::new(60, 10);
    let (code, set_code) = create_signal(String::from("line one\nline two"));

    let build = move || {
        view! {
            <CodeEditor
                value={code.get()} on_value_change={move |v: String| set_code.set(v)}
                language="rs"
                width=50
                height=8
                show_line_numbers=true
                autofocus=true
            />
        }
    };

    term.draw(&build);

    // Move Up
    term.send_key(KeyCode::Up, KeyModifiers::NONE);
    term.draw(&build);

    // Home -> should move cursor to start of line
    term.send_key(KeyCode::Home, KeyModifiers::NONE);
    term.draw(&build);

    let (_cx, cy) = term.cursor().expect("Cursor should be set");
    assert_eq!(cy, 1, "Cursor should be on line row 1");

    // Type character at start of line
    term.type_char('#');
    term.draw(&build);

    assert!(term.line(1).contains("1 │ #line one"));
    assert_eq!(code.get(), "#line one\nline two");
}

#[test]
fn test_syntax_highlighting_colors_on_screen() {
    let mut term = TestTerminal::new(60, 10);
    let (code, set_code) = create_signal(String::from("fn test()"));

    let build = move || {
        view! {
            <CodeEditor
                value={code.get()} on_value_change={move |v: String| set_code.set(v)}
                language="rs"
                width=50
                height=8
                show_line_numbers=true
                autofocus=true
            />
        }
    };

    term.draw(&build);

    // Find the position of 'f' in "fn" on row 1
    let line1 = term.line(1);
    let fn_offset = line1
        .find("fn")
        .expect("'fn' must be present in screen line");

    let fn_color = term.cell_fg(fn_offset as u16, 1);
    assert!(
        fn_color.is_some(),
        "'fn' keyword must have syntax highlight color"
    );

    let Color::Rgb { r, g, b } = fn_color.unwrap() else {
        panic!("Expected RGB syntax color from TextMate theme");
    };
    assert!(r > 0 || g > 0 || b > 0);
}

#[test]
fn test_editor_height_fills_container() {
    let mut term = TestTerminal::new(80, 30);
    let lines = (1..=25)
        .map(|i| format!("println!(\"Line number {}\");", i))
        .collect::<Vec<_>>()
        .join("\n");
    let (code, set_code) = create_signal(lines);

    let build = move || {
        view! {
            <CodeEditor
                value={code.get()} on_value_change={move |v: String| set_code.set(v)}
                language="rs"
                width=Dimension::Percent(1.0)
                height=Dimension::Percent(1.0)
                show_line_numbers=true
                autofocus=true
            />
        }
    };

    term.draw(&build);

    // Verify that line 20 is rendered on screen (proving container fills beyond 12 lines)
    let mut found_line_20 = false;
    for y in 0..30 {
        if term.line(y).contains("Line number 20") {
            found_line_20 = true;
            break;
        }
    }
    assert!(
        found_line_20,
        "Line 20 must be visible on screen when height=Dimension::Percent(1.0) in a 30-row terminal"
    );
}

#[test]
fn test_code_preview_focus_and_keyboard_scroll() {
    let mut term = TestTerminal::new(100, 20);
    let editor_id = FocusId::new();
    let preview_id = FocusId::new();
    let content = (1..=30)
        .map(|i| format!("code line {}", i))
        .collect::<Vec<_>>()
        .join("\n");
    let (code, set_code) = create_signal(content);

    let build = move || {
        view! {
            <View flex_direction=FlexDirection::Row width=Dimension::Percent(1.0) height=Dimension::Percent(1.0) gap=1>
                <CodeEditor
                    id=editor_id
                    value={code.get()} on_value_change={move |v: String| set_code.set(v)}
                    width=Dimension::Percent(0.5)
                    height=Dimension::Percent(1.0)
                    autofocus=true
                />
                <CodePreview
                    id=preview_id
                    value={code.get()}
                    width=Dimension::Percent(0.5)
                    height=Dimension::Percent(1.0)
                />
            </View>
        }
    };

    term.draw(&build);

    // Initial state: editor is focused by autofocus
    assert_eq!(term.focused_id(), Some(editor_id));

    // Preview has single border character at top-left: column 51, row 0 (50 editor + 1 gap)
    let preview_tl_char = term.buffer.get(51, 0).map(|c| c.ch);
    assert_eq!(
        preview_tl_char,
        Some('┌'),
        "Unfocused preview should have single border"
    );

    // Click inside the preview container (column 60, row 5)
    term.click_at(60, 5);
    term.draw(&build);

    // Now preview must be focused!
    assert_eq!(
        term.focused_id(),
        Some(preview_id),
        "Clicking inside preview container should focus it"
    );

    // Focused preview has double border character at top-left
    let preview_tl_char_focused = term.buffer.get(51, 0).map(|c| c.ch);
    assert_eq!(
        preview_tl_char_focused,
        Some('╔'),
        "Focused preview should have double border"
    );

    // Test keyboard scrolling in preview
    assert_eq!(get_scroll(preview_id), (0, 0));
    term.send_key(KeyCode::Down, KeyModifiers::NONE);
    assert_eq!(
        get_scroll(preview_id).1,
        1,
        "Down arrow on focused preview should scroll down"
    );

    // Test Tab switches focus back to editor
    term.send_key(KeyCode::Tab, KeyModifiers::NONE);
    term.draw(&build);
    assert_eq!(
        term.focused_id(),
        Some(editor_id),
        "Tab should cycle focus back to editor"
    );
}

#[test]
fn test_code_editor_cursor_goes_beyond_20_lines() {
    // 40 rows high terminal
    let mut term = TestTerminal::new(80, 40);
    let lines = (1..=30)
        .map(|i| format!("println!(\"Line {}\");", i))
        .collect::<Vec<_>>()
        .join("\n");
    let (code, set_code) = create_signal(lines);

    let build = move || {
        view! {
            <CodeEditor
                value={code.get()} on_value_change={move |v: String| set_code.set(v)}
                language="rs"
                width=Dimension::Percent(1.0)
                height=Dimension::Percent(1.0)
                show_line_numbers=true
                autofocus=true
            />
        }
    };

    term.draw(&build);

    // Initial cursor: begins at line 1 (screen y = 1 inside border)
    let initial_cursor = term.cursor().expect("Cursor should be present");
    assert_eq!(
        initial_cursor.1, 1,
        "Cursor row should be at row 1 on initial load"
    );

    // Move down 25 times (well beyond line 20)
    for _ in 0..25 {
        term.send_key(KeyCode::Down, KeyModifiers::NONE);
    }
    term.draw(&build);
    let moved_cursor = term.cursor().expect("Cursor should be valid");
    assert_eq!(
        moved_cursor.1, 26,
        "Cursor must be at row 26 (line 26), well beyond line 20"
    );

    // Move down 5 more times (to line 30)
    for _ in 0..5 {
        term.send_key(KeyCode::Down, KeyModifiers::NONE);
    }
    term.draw(&build);
    let line30_cursor = term.cursor().expect("Cursor should be valid");
    assert_eq!(
        line30_cursor.1, 30,
        "Cursor must be at row 30 (line 30), well beyond line 20"
    );

    // Press Enter to insert lines beyond line 30
    for _ in 0..5 {
        term.send_key(KeyCode::Enter, KeyModifiers::NONE);
    }
    term.draw(&build);
    let after_enter_cursor = term.cursor().expect("Cursor should follow Enter");
    assert_eq!(
        after_enter_cursor.1, 35,
        "Cursor must advance past line 30 to row 35"
    );
}

#[test]
fn test_code_editor_cursor_style_choice_block_and_line() {
    let mut term = TestTerminal::new(60, 20);
    let (style, set_style) = create_signal(CursorStyle::Line);

    let build = move || {
        view! {
            <CodeEditor
                language="rs"
                width=50
                height=15
                cursor_style=style.get()
                autofocus=true
            />
        }
    };

    // Draw with Line cursor
    term.draw(&build);
    assert_eq!(
        term.cursor_style(),
        Some(CursorStyle::Line),
        "Buffer should have Line cursor style"
    );

    // Switch to Block cursor
    set_style.set(CursorStyle::Block);
    term.draw(&build);
    assert_eq!(
        term.cursor_style(),
        Some(CursorStyle::Block),
        "Buffer should reflect Block cursor style when toggled"
    );

    // Switch to Underline cursor
    set_style.set(CursorStyle::Underline);
    term.draw(&build);
    assert_eq!(
        term.cursor_style(),
        Some(CursorStyle::Underline),
        "Buffer should reflect Underline cursor style when toggled"
    );
}

#[test]
fn test_code_editor_mouse_scrolling_and_cursor_movement() {
    let mut term = TestTerminal::new(80, 20);
    let editor_id = FocusId::new();
    let lines = (1..=30)
        .map(|i| format!("fn item_{}() {{ println!(\"item {}\"); }}", i, i))
        .collect::<Vec<_>>()
        .join("\n");
    let (code, set_code) = create_signal(lines);

    let build = move || {
        view! {
            <CodeEditor
                id=editor_id
                value={code.get()} on_value_change={move |v: String| set_code.set(v)}
                language="rs"
                width=Dimension::Percent(1.0)
                height=Dimension::Percent(1.0)
                show_line_numbers=true
                autofocus=true
            />
        }
    };

    term.draw(&build);

    // Initial state: cursor is on line 1, row 1
    assert_eq!(term.cursor().map(|c| c.1), Some(1));

    // 1. Move cursor with mouse click
    // Click at screen column 15, row 5 (corresponds to line 5)
    term.click_at(15, 5);
    term.draw(&build);
    assert_eq!(
        term.cursor().map(|c| c.1),
        Some(5),
        "Mouse click at row 5 must move cursor to row 5"
    );

    // 2. Mouse wheel scroll down via container (dy = 4)
    term.scroll(15, 5, 0, 4);
    term.draw(&build);

    // Verify container scroll offset updated
    let scroll_pos = get_scroll(editor_id);
    assert_eq!(
        scroll_pos.1, 4,
        "Container scroll must be 4 after mouse wheel scroll down"
    );

    // First line on screen should now be line 5
    let top_line = term.line(1);
    assert!(
        top_line.contains("5 │"),
        "Top line should be line 5 after scrolling 4 lines down, got: {:?}",
        top_line
    );

    // 3. Move cursor with mouse click while container is scrolled
    // Click at row 3 (which now displays line 7 because scroll_y is 4: row 3 + scroll 4 = line 7)
    term.click_at(15, 3);
    term.draw(&build);
    assert_eq!(
        term.cursor().map(|c| c.1),
        Some(3),
        "Cursor should be positioned at row 3 where clicked"
    );

    // 4. Mouse wheel scroll back up via container (dy = -4)
    term.scroll(15, 5, 0, -4);
    term.draw(&build);
    assert_eq!(
        get_scroll(editor_id).1,
        0,
        "Container scroll should return to 0"
    );
    assert!(
        term.line(1).contains("1 │"),
        "Top line should be line 1 again"
    );
}