fresh-editor 0.3.2

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
// Consolidated visual regression tests
// These two tests exercise as many UI states as possible in minimal screenshots
// Additional functional tests preserve important assertions from original tests

use crate::common::harness::EditorTestHarness;
use crate::common::tracing::init_tracing_from_env;
use crate::common::visual_testing::VisualFlow;
use crossterm::event::{KeyCode, KeyModifiers};
use fresh::model::event::{Event, OverlayFace};
use fresh::view::overlay::OverlayNamespace;
use std::fs;

/// Comprehensive visual test A:
/// - File explorer: OPEN
/// - Line wrapping: ON (default)
/// - Multiple cursors: YES (3 cursors selecting "hello")
/// - Syntax highlighting: YES (Rust code)
/// - LSP diagnostics: YES (margin bullets)
/// - Vertical scroll: YES (buffer scrolled partway)
/// - Split view: NO
/// - Command palette: CLOSED
#[test]
#[cfg_attr(windows, ignore)] // Visual regression tests require consistent terminal rendering
fn visual_comprehensive_a() {
    init_tracing_from_env();
    let mut harness = EditorTestHarness::with_temp_project(100, 30).unwrap();
    let project_dir = harness.project_dir().unwrap();

    // Create test files for the file explorer
    fs::create_dir_all(project_dir.join("src")).unwrap();
    fs::write(
        project_dir.join("src/main.rs"),
        r#"// Main entry point
fn main() {
    let hello = "world";
    let hello = "again";
    let hello = "once more";
    println!("{}", hello);
}

// Helper function
fn helper(x: i32) -> i32 {
    let unused_var = 5;
    let another_unused = 10;
    x * 2
}

// More code to enable scrolling
fn long_function() {
    println!("Line 1");
    println!("Line 2");
    println!("Line 3");
    println!("Line 4");
    println!("Line 5");
}
"#,
    )
    .unwrap();
    fs::write(
        project_dir.join("README.md"),
        "# Test Project\n\nA test project for visual regression.\n",
    )
    .unwrap();
    fs::write(
        project_dir.join("Cargo.toml"),
        "[package]\nname = \"test\"\nversion = \"0.1.0\"\n",
    )
    .unwrap();

    let mut flow = VisualFlow::new(
        "Comprehensive UI A",
        "Visual Regression",
        "File explorer open, line wrap on, multicursors, diagnostics, scrolled",
    );

    // Open the Rust file
    harness.open_file(&project_dir.join("src/main.rs")).unwrap();
    harness.render().unwrap();

    // Open file explorer with Ctrl+E
    harness
        .send_key(KeyCode::Char('e'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Wait for file explorer to load and auto-expand to show the current
    // file (issue #1569): `src/` is expanded automatically, no manual
    // navigation required. The presence of `main.rs` (the active file)
    // in the explorer pane is the persistent proof the auto-expand
    // happened. Originally also matched the transient
    // "File explorer ready" status message, but at 100×30 — the
    // canonical visual-regression terminal size — that message is
    // truncated to "File explor..." when the `{remote}` indicator
    // is on the bar, leaving the wait blocked forever.
    let _ = harness.wait_until(|h| {
        let s = h.screen_to_string();
        s.contains("main.rs")
    });
    harness.render().unwrap();

    // Focus back on the editor pane (Ctrl+E when in file explorer focuses editor)
    harness
        .send_key(KeyCode::Char('e'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Add LSP diagnostic markers (simulated)
    {
        // Add diagnostic overlay for "unused_var" on line 11
        harness
            .apply_event(Event::AddOverlay {
                namespace: Some(OverlayNamespace::from_string("lsp-diagnostic".to_string())),
                range: 230..240,
                face: OverlayFace::Background {
                    color: (60, 20, 20),
                },
                priority: 100,
                message: Some("unused variable: `unused_var`".to_string()),
                extend_to_line_end: false,
                url: None,
            })
            .unwrap();
    }

    // Scroll down a bit to show scrolled state
    for _ in 0..5 {
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    // Create multiple cursors by selecting "hello" occurrences
    // First, search for a word that appears multiple times
    harness
        .send_key(KeyCode::Char('w'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Add next occurrence (Ctrl+D)
    harness
        .send_key(KeyCode::Char('d'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness
        .send_key(KeyCode::Char('d'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Capture the comprehensive state
    harness
        .capture_visual_step(
            &mut flow,
            "state_a",
            "File explorer open + syntax highlighting + diagnostics + multicursors + scrolled",
        )
        .unwrap();
}

/// Comprehensive visual test B:
/// - File explorer: CLOSED
/// - Command palette: OPEN (with filter text)
/// - Line wrapping: OFF
/// - Horizontal scroll: YES (long line scrolled right)
/// - Split view: YES (horizontal split with two files)
/// - Vertical scroll: YES
/// - Multiple cursors: NO (single cursor)
/// - LSP diagnostics: NO
#[test]
fn visual_comprehensive_b() {
    use fresh::config::Config;

    // Configure with line wrapping disabled
    let mut config = Config::default();
    config.editor.line_wrap = false;

    let mut harness = EditorTestHarness::with_temp_project_and_config(120, 30, config).unwrap();
    let project_dir = harness.project_dir().unwrap();

    // Create test files
    fs::write(
        project_dir.join("file1.rs"),
        r#"// File 1 - Contains a very long line that will require horizontal scrolling to see the end of it completely when line wrapping is disabled
fn main() {
    let very_long_variable_name_that_extends_beyond_normal_view = "This is a string with a lot of content that goes way past the edge";
    println!("{}", very_long_variable_name_that_extends_beyond_normal_view);
}
"#,
    )
    .unwrap();
    fs::write(
        project_dir.join("file2.rs"),
        r#"// File 2 - Secondary file shown in split view
fn helper() {
    let x = 42;
    let y = x * 2;
    println!("Result: {}", y);
}
"#,
    )
    .unwrap();

    let mut flow = VisualFlow::new(
        "Comprehensive UI B",
        "Visual Regression",
        "Command palette open, split view, line wrap off, horizontal scroll",
    );

    // Open first file
    harness.open_file(&project_dir.join("file1.rs")).unwrap();
    harness.render().unwrap();

    // Create horizontal split via command palette
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.type_text("split horiz").unwrap();
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Open second file in the new split
    harness.open_file(&project_dir.join("file2.rs")).unwrap();
    harness.render().unwrap();

    // Go back to first pane and scroll right to show horizontal scroll
    harness
        .send_key(KeyCode::Char('k'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Move to the long line (line 3)
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Scroll right on the long line
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Now open command palette with some filter text showing
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.type_text("help").unwrap();
    harness.render().unwrap();

    // Wait for the command palette to fully render with the "help" filter text
    harness
        .wait_until(|h| {
            let screen = h.screen_to_string();
            screen.contains("help")
        })
        .unwrap();

    // Capture the comprehensive state
    harness
        .capture_visual_step(
            &mut flow,
            "state_b",
            "Split view + command palette open + line wrap off + horizontal scroll",
        )
        .unwrap();
}

// ============================================================================
// Functional tests (no visual captures, but preserve important assertions)
// ============================================================================

/// Test that undo after successful rename restores all occurrences in one step
#[test]
fn test_lsp_rename_undo_restores_all() {
    use lsp_types::{Position, Range, TextEdit, WorkspaceEdit};
    use std::collections::HashMap;
    use std::io::Write;

    // Create a temporary file for this test
    let temp_dir = tempfile::tempdir().unwrap();
    let test_file = temp_dir.path().join("test.rs");
    let mut file = std::fs::File::create(&test_file).unwrap();
    writeln!(file, "fn calculate(value: i32) -> i32 {{").unwrap();
    writeln!(file, "    let result = value * 2;").unwrap();
    writeln!(file, "    println!(\"Value: {{}}\", value);").unwrap();
    writeln!(file, "    result").unwrap();
    writeln!(file, "}}").unwrap();
    drop(file);

    let mut harness = EditorTestHarness::new(100, 30).unwrap();

    // Open the temporary file
    harness.open_file(&test_file).unwrap();
    harness.render().unwrap();

    // Save the original buffer content
    let original_content = harness.get_buffer_content().unwrap();
    assert!(original_content.contains("fn calculate(value: i32)"));
    assert_eq!(original_content.matches("value").count(), 3);

    // Create file URI from the temp file path
    let file_uri = fresh_core::file_uri::path_to_lsp_uri(&test_file).unwrap();

    // Simulate LSP WorkspaceEdit response with multiple edits
    #[allow(clippy::mutable_key_type)]
    let mut changes = HashMap::new();
    changes.insert(
        file_uri.clone(),
        vec![
            TextEdit {
                range: Range {
                    start: Position {
                        line: 0,
                        character: 13,
                    },
                    end: Position {
                        line: 0,
                        character: 18,
                    },
                },
                new_text: "amount".to_string(),
            },
            TextEdit {
                range: Range {
                    start: Position {
                        line: 1,
                        character: 17,
                    },
                    end: Position {
                        line: 1,
                        character: 22,
                    },
                },
                new_text: "amount".to_string(),
            },
            TextEdit {
                range: Range {
                    start: Position {
                        line: 2,
                        character: 28,
                    },
                    end: Position {
                        line: 2,
                        character: 33,
                    },
                },
                new_text: "amount".to_string(),
            },
        ],
    );

    let workspace_edit = WorkspaceEdit {
        changes: Some(changes),
        document_changes: None,
        change_annotations: None,
    };

    // Call handle_rename_response directly to simulate LSP rename response
    harness
        .editor_mut()
        .handle_rename_response(1, Ok(workspace_edit))
        .unwrap();
    harness.render().unwrap();

    // Verify all occurrences were renamed
    let renamed_content = harness.get_buffer_content().unwrap();
    assert!(
        renamed_content.contains("fn calculate(amount: i32)"),
        "Parameter should be renamed to 'amount'"
    );
    assert_eq!(
        renamed_content.matches("amount").count(),
        3,
        "Should have 3 occurrences of 'amount'"
    );
    assert_eq!(
        renamed_content.matches("value").count(),
        0,
        "Should have no occurrences of 'value' as identifier"
    );

    // Perform undo (Ctrl+Z)
    harness
        .send_key(KeyCode::Char('z'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Verify ALL occurrences are restored to original in ONE undo step
    let after_undo_content = harness.get_buffer_content().unwrap();
    assert_eq!(
        after_undo_content, original_content,
        "Single undo should restore all occurrences to 'value'"
    );
    assert!(
        after_undo_content.contains("fn calculate(value: i32)"),
        "Parameter should be restored to 'value'"
    );
    assert_eq!(
        after_undo_content.matches("value").count(),
        3,
        "Should have 3 occurrences of 'value' after undo"
    );

    // Verify we can redo (Ctrl+Y)
    harness
        .send_key(KeyCode::Char('y'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    let after_redo_content = harness.get_buffer_content().unwrap();
    assert_eq!(
        after_redo_content, renamed_content,
        "Redo should restore the renamed content"
    );
}

/// Test syntax highlighting for multiple languages (functional assertions only)
#[test]
fn test_multi_language_highlighting() {
    let mut harness = EditorTestHarness::with_temp_project(80, 30).unwrap();
    let project_dir = harness.project_dir().unwrap();

    // All supported languages with their test files
    let test_files = [
        (
            "Rust",
            "hello.rs",
            include_str!("../fixtures/syntax_highlighting/hello.rs"),
        ),
        (
            "Python",
            "hello.py",
            include_str!("../fixtures/syntax_highlighting/hello.py"),
        ),
        (
            "JavaScript",
            "hello.js",
            include_str!("../fixtures/syntax_highlighting/hello.js"),
        ),
        (
            "TypeScript",
            "hello.ts",
            include_str!("../fixtures/syntax_highlighting/hello.ts"),
        ),
        (
            "HTML",
            "hello.html",
            include_str!("../fixtures/syntax_highlighting/hello.html"),
        ),
        (
            "CSS",
            "hello.css",
            include_str!("../fixtures/syntax_highlighting/hello.css"),
        ),
        (
            "C",
            "hello.c",
            include_str!("../fixtures/syntax_highlighting/hello.c"),
        ),
        (
            "C++",
            "hello.cpp",
            include_str!("../fixtures/syntax_highlighting/hello.cpp"),
        ),
        (
            "Go",
            "hello.go",
            include_str!("../fixtures/syntax_highlighting/hello.go"),
        ),
        (
            "JSON",
            "hello.json",
            include_str!("../fixtures/syntax_highlighting/hello.json"),
        ),
        (
            "Java",
            "hello.java",
            include_str!("../fixtures/syntax_highlighting/hello.java"),
        ),
        (
            "C#",
            "hello.cs",
            include_str!("../fixtures/syntax_highlighting/hello.cs"),
        ),
        (
            "PHP",
            "hello.php",
            include_str!("../fixtures/syntax_highlighting/hello.php"),
        ),
        (
            "Ruby",
            "hello.rb",
            include_str!("../fixtures/syntax_highlighting/hello.rb"),
        ),
        (
            "Bash",
            "hello.sh",
            include_str!("../fixtures/syntax_highlighting/hello.sh"),
        ),
        (
            "Lua",
            "hello.lua",
            include_str!("../fixtures/syntax_highlighting/hello.lua"),
        ),
    ];

    // Create all test files
    for (_, filename, content) in &test_files {
        fs::write(project_dir.join(filename), content).unwrap();
    }

    // Test each language
    for (lang_name, filename, _) in &test_files {
        harness.open_file(&project_dir.join(filename)).unwrap();
        harness.render().unwrap();

        // Verify multiple colors are present (indicating highlighting is working)
        let buffer = harness.buffer();
        let unique_colors = count_unique_colors(buffer);

        // Note: C# may have fewer colors due to missing HIGHLIGHTS_QUERY in crate 0.23.1
        let min_colors = if *lang_name == "C#" { 1 } else { 3 };

        assert!(
            unique_colors >= min_colors,
            "{} highlighting should use at least {} different colors, found {}",
            lang_name,
            min_colors,
            unique_colors
        );
    }
}

/// Helper to count unique foreground colors in a buffer (for verifying syntax highlighting)
fn count_unique_colors(buffer: &ratatui::buffer::Buffer) -> usize {
    use std::collections::HashSet;
    let mut colors = HashSet::new();

    for cell in buffer.content() {
        colors.insert(cell.fg);
    }

    colors.len()
}