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
//! Tests for buffer settings commands:
//! - Set Tab Size
//! - Toggle Indentation: Spaces ↔ Tabs
//! - Toggle Tab Indicators
//! - Toggle Line Numbers
//! - Reset Buffer Settings

use crate::common::harness::EditorTestHarness;
use crossterm::event::{KeyCode, KeyModifiers};
use fresh::config::Config;
use std::fs::File;
use std::io::Write;
use std::path::Path;
use std::time::Duration;
use tempfile::TempDir;

/// Helper to run a command from the command palette
fn run_command(harness: &mut EditorTestHarness, command_name: &str) {
    // Open command palette with Ctrl+P
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Type the command name
    harness.type_text(command_name).unwrap();
    harness.render().unwrap();

    // Press Enter to execute
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
}

/// Test that "Toggle Indentation" command toggles between spaces and tabs
#[test]
fn test_toggle_indentation_command() {
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("test.go");

    // Create an empty Go file (defaults to use_tabs=true)
    std::fs::write(&file_path, "").unwrap();

    let config = Config::default();
    let mut harness = EditorTestHarness::with_config(80, 24, config).unwrap();
    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    // Verify initial state: Tab should insert a tab character in Go files
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    let content = harness.get_buffer_content().unwrap();
    assert_eq!(content, "\t", "Initially, Go should use tabs");

    // Undo to clear the tab
    harness
        .send_key(KeyCode::Char('z'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Run "Toggle Indentation" command to switch to spaces
    run_command(&mut harness, "Toggle Indentation");

    // Now Tab should insert spaces
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    let content = harness.get_buffer_content().unwrap();
    assert!(
        !content.contains('\t'),
        "After toggle, should insert spaces, not tabs. Got: {:?}",
        content
    );
    assert!(
        content.contains("    "),
        "After toggle, should have 4 spaces. Got: {:?}",
        content
    );

    // Undo and toggle again to switch back to tabs
    harness
        .send_key(KeyCode::Char('z'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    run_command(&mut harness, "Toggle Indentation");

    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    let content = harness.get_buffer_content().unwrap();
    assert_eq!(
        content, "\t",
        "After second toggle, should be back to tabs. Got: {:?}",
        content
    );
}

/// Test that "Toggle Tab Indicators" toggles visibility
#[test]
fn test_toggle_tab_indicators_command() {
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("test.go");

    // Create a Go file with a tab character
    std::fs::write(&file_path, "\thello").unwrap();

    let config = Config::default();
    let mut harness = EditorTestHarness::with_config(80, 24, config).unwrap();
    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    // Go files hide tab indicators by default
    let screen_before = harness.screen_to_string();
    assert!(
        !screen_before.contains(''),
        "Go files should hide tab indicators by default"
    );

    // Run "Toggle Tab Indicators" command
    run_command(&mut harness, "Toggle Tab Indicators");

    // Now tab indicators should be visible
    let screen_after = harness.screen_to_string();
    assert!(
        screen_after.contains(''),
        "After toggle, tab indicators should be visible. Screen:\n{}",
        screen_after
    );

    // Toggle again - should hide them
    run_command(&mut harness, "Toggle Tab Indicators");

    let screen_final = harness.screen_to_string();
    assert!(
        !screen_final.contains(''),
        "After second toggle, tab indicators should be hidden again"
    );
}

/// Test that "Set Tab Size" command changes tab rendering width
#[test]
fn test_set_tab_size_command() {
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("test.txt");

    // Create a file with a tab followed by a marker
    std::fs::write(&file_path, "\tX").unwrap();

    // Start with tab_size = 4 (default)
    let config = Config::default();
    let mut harness = EditorTestHarness::with_config(80, 24, config).unwrap();
    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    // Helper to get X visual column relative to tab indicator
    fn get_x_visual_offset(screen: &str) -> Option<usize> {
        for line in screen.lines() {
            if line.contains('X') {
                let mut indicator_col = None;
                let mut x_col = None;
                for (col, ch) in line.chars().enumerate() {
                    if ch == '' {
                        indicator_col = Some(col);
                    }
                    if ch == 'X' {
                        x_col = Some(col);
                    }
                }
                return Some(x_col? - indicator_col?);
            }
        }
        None
    }

    // Get initial offset (tab_size=4)
    let screen_4 = harness.screen_to_string();
    let offset_4 = get_x_visual_offset(&screen_4).unwrap();
    assert_eq!(
        offset_4, 4,
        "With default tab_size=4, X should be 4 columns after indicator"
    );

    // Run "Set Tab Size" command and enter "8"
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.type_text("Set Tab Size").unwrap();
    harness.render().unwrap();
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Clear existing text and type "8"
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::CONTROL)
        .unwrap(); // Select all
    harness.type_text("8").unwrap();
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Get new offset (tab_size=8)
    let screen_8 = harness.screen_to_string();
    let offset_8 = get_x_visual_offset(&screen_8).unwrap();
    assert_eq!(
        offset_8, 8,
        "After setting tab_size=8, X should be 8 columns after indicator. Screen:\n{}",
        screen_8
    );
}

/// Test that "Reset Buffer Settings" restores config defaults
#[test]
fn test_reset_buffer_settings_command() {
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("test.go");

    // Create a Go file with a tab character
    std::fs::write(&file_path, "\thello").unwrap();

    let config = Config::default();
    let mut harness = EditorTestHarness::with_config(80, 24, config).unwrap();
    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    // Initial state: Go hides tab indicators and uses tabs
    let screen_initial = harness.screen_to_string();
    assert!(
        !screen_initial.contains(''),
        "Go files should hide tab indicators initially"
    );

    // Modify settings: toggle tab indicators and toggle indentation to spaces
    run_command(&mut harness, "Toggle Tab Indicators");
    run_command(&mut harness, "Toggle Indentation"); // Go uses tabs by default, so toggle switches to spaces

    // Verify modifications took effect
    let screen_modified = harness.screen_to_string();
    assert!(
        screen_modified.contains(''),
        "After toggle, tab indicators should be visible"
    );

    // Type a tab to verify spaces are inserted
    harness
        .send_key(KeyCode::End, KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    let content_modified = harness.get_buffer_content().unwrap();
    assert!(
        content_modified.contains("    "),
        "After toggling indentation, Tab should insert spaces"
    );

    // Undo the tab
    harness
        .send_key(KeyCode::Char('z'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Reset buffer settings
    run_command(&mut harness, "Reset Buffer Settings");

    // Verify settings are restored to Go defaults
    let screen_reset = harness.screen_to_string();
    assert!(
        !screen_reset.contains(''),
        "After reset, Go should hide tab indicators again. Screen:\n{}",
        screen_reset
    );

    // Verify tabs are restored - type a tab
    harness
        .send_key(KeyCode::End, KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    let content_reset = harness.get_buffer_content().unwrap();
    assert!(
        content_reset.ends_with('\t'),
        "After reset, Go should use tabs again. Got: {:?}",
        content_reset
    );
}

/// Delay between file writes to ensure filesystem notifications are received.
const FILE_CHANGE_DELAY: Duration = Duration::from_millis(2100);

/// Write content to a file and sync to disk to ensure filesystem notifications fire.
fn write_and_sync(path: &Path, content: &str) {
    let mut file = File::create(path).unwrap();
    file.write_all(content.as_bytes()).unwrap();
    file.sync_all().unwrap();
    drop(file);

    // Also sync the parent directory to ensure the directory entry is flushed
    if let Some(parent) = path.parent() {
        if let Ok(dir) = File::open(parent) {
            let _ = dir.sync_all();
        }
    }
}

/// Test that "Toggle Line Numbers" persists across external file changes and saves.
///
/// This test verifies that when line numbers are toggled off, the setting is
/// preserved through:
/// 1. External file modifications (auto-revert)
/// 2. User edits
/// 3. File saves
#[test]
#[cfg_attr(target_os = "macos", ignore)] // FSEvents coalescing can cause flaky timing
fn test_toggle_line_numbers_persists_across_file_changes() {
    let temp_dir = TempDir::new().unwrap();
    let file_path = temp_dir.path().join("test.txt");

    // Create initial file with some content
    write_and_sync(&file_path, "Initial line 1\nInitial line 2\nInitial line 3");

    let config = Config::default();
    let mut harness = EditorTestHarness::with_config(80, 24, config).unwrap();
    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    // Step 1: Verify line numbers are enabled by default
    let screen_initial = harness.screen_to_string();
    assert!(
        screen_initial.contains("1 │"),
        "Line numbers should be visible by default. Screen:\n{}",
        screen_initial
    );

    // Step 2: Disable line numbers using command palette
    run_command(&mut harness, "Toggle Line Numbers");

    // Step 3: Verify line numbers are now hidden
    let screen_after_toggle = harness.screen_to_string();
    assert!(
        !screen_after_toggle.contains("1 │"),
        "Line numbers should be hidden after toggle. Screen:\n{}",
        screen_after_toggle
    );
    assert!(
        !screen_after_toggle.contains("2 │"),
        "Line numbers should be hidden after toggle. Screen:\n{}",
        screen_after_toggle
    );

    // Step 4: Overwrite the file externally (not using the editor)
    harness.sleep(FILE_CHANGE_DELAY);
    write_and_sync(
        &file_path,
        "Modified line 1\nModified line 2\nModified line 3\nModified line 4",
    );

    // Wait for auto-revert to process the external change
    let expected_content = "Modified line 1\nModified line 2\nModified line 3\nModified line 4";
    harness
        .wait_until(|h| h.get_buffer_content().unwrap() == expected_content)
        .expect("Auto-revert should update buffer content");

    // Step 5: Verify line numbers are still disabled after auto-revert
    harness.render().unwrap();
    let screen_after_revert = harness.screen_to_string();
    assert!(
        !screen_after_revert.contains("1 │"),
        "Line numbers should remain hidden after auto-revert. Screen:\n{}",
        screen_after_revert
    );
    assert!(
        !screen_after_revert.contains("2 │"),
        "Line numbers should remain hidden after auto-revert. Screen:\n{}",
        screen_after_revert
    );
    // Verify the new content is displayed
    harness.assert_screen_contains("Modified line 1");

    // Step 6: Make an edit in the buffer
    harness
        .send_key(KeyCode::End, KeyModifiers::CONTROL)
        .unwrap();
    harness.type_text("\nEdited line 5").unwrap();
    harness.render().unwrap();

    // Step 7: Verify line numbers are still disabled after editing
    let screen_after_edit = harness.screen_to_string();
    assert!(
        !screen_after_edit.contains("1 │"),
        "Line numbers should remain hidden after editing. Screen:\n{}",
        screen_after_edit
    );
    assert!(
        !screen_after_edit.contains("5 │"),
        "Line numbers should remain hidden after editing. Screen:\n{}",
        screen_after_edit
    );

    // Step 8: Save the file
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Step 9: Verify line numbers are still disabled after saving
    let screen_after_save = harness.screen_to_string();
    assert!(
        !screen_after_save.contains("1 │"),
        "Line numbers should remain hidden after save. Screen:\n{}",
        screen_after_save
    );
    assert!(
        !screen_after_save.contains("5 │"),
        "Line numbers should remain hidden after save. Screen:\n{}",
        screen_after_save
    );

    // Verify the edited content is still visible
    harness.assert_screen_contains("Edited line 5");
}