fresh-editor 0.1.90

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
//! Input handling for the Prompt (minibuffer).
//!
//! Implements the InputHandler trait for Prompt, handling text editing,
//! cursor movement, and suggestion navigation.

use super::prompt::Prompt;
use crate::input::handler::{DeferredAction, InputContext, InputHandler, InputResult};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

impl InputHandler for Prompt {
    fn handle_key_event(&mut self, event: &KeyEvent, ctx: &mut InputContext) -> InputResult {
        let ctrl = event.modifiers.contains(KeyModifiers::CONTROL);
        let alt = event.modifiers.contains(KeyModifiers::ALT);
        let shift = event.modifiers.contains(KeyModifiers::SHIFT);

        match event.code {
            // Confirmation and cancellation
            KeyCode::Enter => {
                ctx.defer(DeferredAction::ConfirmPrompt);
                InputResult::Consumed
            }
            KeyCode::Esc => {
                ctx.defer(DeferredAction::ClosePrompt);
                InputResult::Consumed
            }

            // Alt+key combinations should pass through to keybindings
            KeyCode::Char(_) if alt => InputResult::Ignored,

            // Character input (no modifiers or just shift)
            KeyCode::Char(c) if !ctrl => {
                // Delete any selection before inserting
                if self.has_selection() {
                    self.delete_selection();
                }
                if shift {
                    self.insert_char(c.to_ascii_uppercase());
                } else {
                    self.insert_char(c);
                }
                ctx.defer(DeferredAction::UpdatePromptSuggestions);
                InputResult::Consumed
            }
            KeyCode::Char(c) if ctrl => self.handle_ctrl_key(c, ctx),

            // Deletion
            KeyCode::Backspace if ctrl => {
                self.delete_word_backward();
                ctx.defer(DeferredAction::UpdatePromptSuggestions);
                InputResult::Consumed
            }
            KeyCode::Backspace => {
                if self.has_selection() {
                    self.delete_selection();
                } else {
                    self.backspace();
                }
                ctx.defer(DeferredAction::UpdatePromptSuggestions);
                InputResult::Consumed
            }
            KeyCode::Delete if ctrl => {
                self.delete_word_forward();
                ctx.defer(DeferredAction::UpdatePromptSuggestions);
                InputResult::Consumed
            }
            KeyCode::Delete => {
                if self.has_selection() {
                    self.delete_selection();
                } else {
                    self.delete();
                }
                ctx.defer(DeferredAction::UpdatePromptSuggestions);
                InputResult::Consumed
            }

            // Cursor movement
            KeyCode::Left if ctrl && shift => {
                self.move_word_left_selecting();
                InputResult::Consumed
            }
            KeyCode::Left if ctrl => {
                self.move_word_left();
                InputResult::Consumed
            }
            KeyCode::Left if shift => {
                self.move_left_selecting();
                InputResult::Consumed
            }
            KeyCode::Left => {
                self.clear_selection();
                self.cursor_left();
                InputResult::Consumed
            }
            KeyCode::Right if ctrl && shift => {
                self.move_word_right_selecting();
                InputResult::Consumed
            }
            KeyCode::Right if ctrl => {
                self.move_word_right();
                InputResult::Consumed
            }
            KeyCode::Right if shift => {
                self.move_right_selecting();
                InputResult::Consumed
            }
            KeyCode::Right => {
                self.clear_selection();
                self.cursor_right();
                InputResult::Consumed
            }
            KeyCode::Home if shift => {
                self.move_home_selecting();
                InputResult::Consumed
            }
            KeyCode::Home => {
                self.clear_selection();
                self.move_to_start();
                InputResult::Consumed
            }
            KeyCode::End if shift => {
                self.move_end_selecting();
                InputResult::Consumed
            }
            KeyCode::End => {
                self.clear_selection();
                self.move_to_end();
                InputResult::Consumed
            }

            // Suggestion navigation
            // TODO: Refactor to use callbacks - the prompt creator (e.g. SelectTheme, SelectLocale)
            // should be able to register a callback for selection changes instead of having
            // hardcoded prompt type checks here. This would make the suggestion UI more flexible
            // and allow custom handling for any prompt type without modifying this code.
            KeyCode::Up => {
                if !self.suggestions.is_empty() {
                    // Don't wrap around - stay at 0 if already at the beginning
                    if let Some(selected) = self.selected_suggestion {
                        let new_selected = if selected == 0 { 0 } else { selected - 1 };
                        self.selected_suggestion = Some(new_selected);
                        // For non-plugin prompts (except QuickOpen), update input to match selected suggestion
                        if !matches!(
                            self.prompt_type,
                            crate::view::prompt::PromptType::Plugin { .. }
                                | crate::view::prompt::PromptType::QuickOpen
                        ) {
                            if let Some(suggestion) = self.suggestions.get(new_selected) {
                                self.input = suggestion.get_value().to_string();
                                self.cursor_pos = self.input.len();
                            }
                        }
                        // For theme selection, trigger live preview
                        if matches!(
                            self.prompt_type,
                            crate::view::prompt::PromptType::SelectTheme { .. }
                        ) {
                            ctx.defer(DeferredAction::PreviewThemeFromPrompt);
                        }
                        // For plugin prompts, notify about selection change (for live preview)
                        if matches!(
                            self.prompt_type,
                            crate::view::prompt::PromptType::Plugin { .. }
                        ) {
                            ctx.defer(DeferredAction::PromptSelectionChanged {
                                selected_index: new_selected,
                            });
                        }
                    }
                } else {
                    // No suggestions - use history
                    ctx.defer(DeferredAction::PromptHistoryPrev);
                }
                InputResult::Consumed
            }
            KeyCode::Down => {
                if !self.suggestions.is_empty() {
                    // Don't wrap around - stay at end if already at the last item
                    if let Some(selected) = self.selected_suggestion {
                        let new_selected = (selected + 1).min(self.suggestions.len() - 1);
                        self.selected_suggestion = Some(new_selected);
                        // For non-plugin prompts (except QuickOpen), update input to match selected suggestion
                        if !matches!(
                            self.prompt_type,
                            crate::view::prompt::PromptType::Plugin { .. }
                                | crate::view::prompt::PromptType::QuickOpen
                        ) {
                            if let Some(suggestion) = self.suggestions.get(new_selected) {
                                self.input = suggestion.get_value().to_string();
                                self.cursor_pos = self.input.len();
                            }
                        }
                        // For theme selection, trigger live preview
                        if matches!(
                            self.prompt_type,
                            crate::view::prompt::PromptType::SelectTheme { .. }
                        ) {
                            ctx.defer(DeferredAction::PreviewThemeFromPrompt);
                        }
                        // For plugin prompts, notify about selection change (for live preview)
                        if matches!(
                            self.prompt_type,
                            crate::view::prompt::PromptType::Plugin { .. }
                        ) {
                            ctx.defer(DeferredAction::PromptSelectionChanged {
                                selected_index: new_selected,
                            });
                        }
                    }
                } else {
                    // No suggestions - use history
                    ctx.defer(DeferredAction::PromptHistoryNext);
                }
                InputResult::Consumed
            }
            KeyCode::PageUp => {
                if let Some(selected) = self.selected_suggestion {
                    self.selected_suggestion = Some(selected.saturating_sub(10));
                }
                InputResult::Consumed
            }
            KeyCode::PageDown => {
                if let Some(selected) = self.selected_suggestion {
                    let len = self.suggestions.len();
                    let new_pos = selected + 10;
                    self.selected_suggestion = Some(new_pos.min(len.saturating_sub(1)));
                }
                InputResult::Consumed
            }

            // Tab accepts suggestion
            KeyCode::Tab => {
                if let Some(selected) = self.selected_suggestion {
                    if let Some(suggestion) = self.suggestions.get(selected) {
                        if !suggestion.disabled {
                            let value = suggestion.get_value().to_string();
                            // For QuickOpen mode, preserve the prefix character
                            if matches!(
                                self.prompt_type,
                                crate::view::prompt::PromptType::QuickOpen
                            ) {
                                let prefix = self
                                    .input
                                    .chars()
                                    .next()
                                    .filter(|c| *c == '>' || *c == '#' || *c == ':');
                                if let Some(p) = prefix {
                                    self.input = format!("{}{}", p, value);
                                } else {
                                    self.input = value;
                                }
                            } else {
                                self.input = value;
                            }
                            self.cursor_pos = self.input.len();
                            self.clear_selection();
                        }
                    }
                }
                ctx.defer(DeferredAction::UpdatePromptSuggestions);
                InputResult::Consumed
            }

            _ => InputResult::Consumed, // Modal - consume all unhandled keys
        }
    }

    fn is_modal(&self) -> bool {
        true
    }
}

impl Prompt {
    fn handle_ctrl_key(&mut self, c: char, ctx: &mut InputContext) -> InputResult {
        match c {
            'a' => {
                // Select all
                self.selection_anchor = Some(0);
                self.cursor_pos = self.input.len();
                InputResult::Consumed
            }
            'c' => {
                // Copy - defer to Editor for clipboard access
                ctx.defer(DeferredAction::ExecuteAction(
                    crate::input::keybindings::Action::PromptCopy,
                ));
                InputResult::Consumed
            }
            'x' => {
                // Cut - defer to Editor for clipboard access
                ctx.defer(DeferredAction::ExecuteAction(
                    crate::input::keybindings::Action::PromptCut,
                ));
                InputResult::Consumed
            }
            'v' => {
                // Paste - defer to Editor for clipboard access
                ctx.defer(DeferredAction::ExecuteAction(
                    crate::input::keybindings::Action::PromptPaste,
                ));
                InputResult::Consumed
            }
            'k' => {
                // Delete to end of line
                self.delete_to_end();
                ctx.defer(DeferredAction::UpdatePromptSuggestions);
                InputResult::Consumed
            }
            // Pass through other Ctrl+key combinations to global keybindings (e.g., Ctrl+P to toggle Quick Open)
            _ => InputResult::Ignored,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::view::prompt::PromptType;

    fn key(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::NONE)
    }

    fn key_with_ctrl(c: char) -> KeyEvent {
        KeyEvent::new(KeyCode::Char(c), KeyModifiers::CONTROL)
    }

    fn key_with_shift(code: KeyCode) -> KeyEvent {
        KeyEvent::new(code, KeyModifiers::SHIFT)
    }

    #[test]
    fn test_prompt_character_input() {
        let mut prompt = Prompt::new("Test: ".to_string(), PromptType::Search);
        let mut ctx = InputContext::new();

        prompt.handle_key_event(
            &KeyEvent::new(KeyCode::Char('h'), KeyModifiers::NONE),
            &mut ctx,
        );
        prompt.handle_key_event(
            &KeyEvent::new(KeyCode::Char('i'), KeyModifiers::NONE),
            &mut ctx,
        );

        assert_eq!(prompt.input, "hi");
        assert_eq!(prompt.cursor_pos, 2);
    }

    #[test]
    fn test_prompt_backspace() {
        let mut prompt = Prompt::new("Test: ".to_string(), PromptType::Search);
        prompt.input = "hello".to_string();
        prompt.cursor_pos = 5;
        let mut ctx = InputContext::new();

        prompt.handle_key_event(&key(KeyCode::Backspace), &mut ctx);
        assert_eq!(prompt.input, "hell");
        assert_eq!(prompt.cursor_pos, 4);
    }

    #[test]
    fn test_prompt_cursor_movement() {
        let mut prompt = Prompt::new("Test: ".to_string(), PromptType::Search);
        prompt.input = "hello".to_string();
        prompt.cursor_pos = 5;
        let mut ctx = InputContext::new();

        // Move to start
        prompt.handle_key_event(&key(KeyCode::Home), &mut ctx);
        assert_eq!(prompt.cursor_pos, 0);

        // Move to end
        prompt.handle_key_event(&key(KeyCode::End), &mut ctx);
        assert_eq!(prompt.cursor_pos, 5);

        // Move left
        prompt.handle_key_event(&key(KeyCode::Left), &mut ctx);
        assert_eq!(prompt.cursor_pos, 4);

        // Move right
        prompt.handle_key_event(&key(KeyCode::Right), &mut ctx);
        assert_eq!(prompt.cursor_pos, 5);
    }

    #[test]
    fn test_prompt_selection() {
        let mut prompt = Prompt::new("Test: ".to_string(), PromptType::Search);
        prompt.input = "hello world".to_string();
        prompt.cursor_pos = 0;
        let mut ctx = InputContext::new();

        // Select with Shift+Right
        prompt.handle_key_event(&key_with_shift(KeyCode::Right), &mut ctx);
        prompt.handle_key_event(&key_with_shift(KeyCode::Right), &mut ctx);
        assert!(prompt.has_selection());
        assert_eq!(prompt.selected_text(), Some("he".to_string()));

        // Select all with Ctrl+A
        prompt.handle_key_event(&key_with_ctrl('a'), &mut ctx);
        assert_eq!(prompt.selected_text(), Some("hello world".to_string()));
    }

    #[test]
    fn test_prompt_enter_confirms() {
        let mut prompt = Prompt::new("Test: ".to_string(), PromptType::Search);
        let mut ctx = InputContext::new();

        prompt.handle_key_event(&key(KeyCode::Enter), &mut ctx);
        assert!(ctx
            .deferred_actions
            .iter()
            .any(|a| matches!(a, DeferredAction::ConfirmPrompt)));
    }

    #[test]
    fn test_prompt_escape_cancels() {
        let mut prompt = Prompt::new("Test: ".to_string(), PromptType::Search);
        let mut ctx = InputContext::new();

        prompt.handle_key_event(&key(KeyCode::Esc), &mut ctx);
        assert!(ctx
            .deferred_actions
            .iter()
            .any(|a| matches!(a, DeferredAction::ClosePrompt)));
    }

    #[test]
    fn test_prompt_is_modal() {
        let prompt = Prompt::new("Test: ".to_string(), PromptType::Search);
        assert!(prompt.is_modal());
    }

    #[test]
    fn test_prompt_ctrl_p_returns_ignored() {
        let mut prompt = Prompt::new("Test: ".to_string(), PromptType::Search);
        let mut ctx = InputContext::new();

        // Ctrl+P should return Ignored so it can be handled by global keybindings
        let result = prompt.handle_key_event(&key_with_ctrl('p'), &mut ctx);
        assert_eq!(result, InputResult::Ignored, "Ctrl+P should return Ignored");
    }

    #[test]
    fn test_prompt_ctrl_p_dispatch_returns_ignored() {
        let mut prompt = Prompt::new("Test: ".to_string(), PromptType::Search);
        let mut ctx = InputContext::new();

        // dispatch_input should also return Ignored for Ctrl+P (not Consumed by modal behavior)
        let result = prompt.dispatch_input(&key_with_ctrl('p'), &mut ctx);
        assert_eq!(
            result,
            InputResult::Ignored,
            "dispatch_input should return Ignored for Ctrl+P"
        );
    }
}