fresh-editor 0.1.56

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
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
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! Dynamic command registry for plugins and extensions
//!
//! This module allows plugins to register custom commands dynamically
//! while maintaining the built-in command set.

use crate::input::commands::{get_all_commands, Command, Suggestion};
use crate::input::fuzzy::fuzzy_match;
use crate::input::keybindings::Action;
use crate::input::keybindings::KeyContext;
use std::sync::{Arc, RwLock};

/// Registry for managing editor commands
///
/// Supports both built-in commands and dynamically registered plugin commands.
/// Thread-safe for use across multiple threads (e.g., from async tasks).
pub struct CommandRegistry {
    /// Built-in commands (loaded once at startup)
    builtin_commands: Vec<Command>,

    /// Plugin-registered commands (dynamically added/removed)
    plugin_commands: Arc<RwLock<Vec<Command>>>,

    /// Command usage history (most recent first)
    /// Used to sort command palette suggestions by recency
    command_history: Vec<String>,
}

impl CommandRegistry {
    /// Maximum number of commands to keep in history
    const MAX_HISTORY_SIZE: usize = 50;

    /// Create a new command registry with built-in commands
    pub fn new() -> Self {
        Self {
            builtin_commands: get_all_commands(),
            plugin_commands: Arc::new(RwLock::new(Vec::new())),
            command_history: Vec::new(),
        }
    }

    /// Record that a command was used (for history/sorting)
    ///
    /// This moves the command to the front of the history list.
    /// Recently used commands appear first in suggestions.
    pub fn record_usage(&mut self, command_name: &str) {
        // Remove existing entry if present
        self.command_history.retain(|name| name != command_name);

        // Add to front (most recent)
        self.command_history.insert(0, command_name.to_string());

        // Trim to max size
        if self.command_history.len() > Self::MAX_HISTORY_SIZE {
            self.command_history.truncate(Self::MAX_HISTORY_SIZE);
        }
    }

    /// Get the position of a command in history (0 = most recent)
    /// Returns None if command is not in history
    fn history_position(&self, command_name: &str) -> Option<usize> {
        self.command_history
            .iter()
            .position(|name| name == command_name)
    }

    /// Register a new command (typically from a plugin)
    ///
    /// If a command with the same name already exists, it will be replaced.
    /// This allows plugins to override built-in commands.
    pub fn register(&self, command: Command) {
        let mut commands = self.plugin_commands.write().unwrap();

        // Remove existing command with same name
        commands.retain(|c| c.name != command.name);

        // Add new command
        commands.push(command);
    }

    /// Unregister a command by name
    pub fn unregister(&self, name: &str) {
        let mut commands = self.plugin_commands.write().unwrap();
        commands.retain(|c| c.name != name);
    }

    /// Unregister all commands registered by a specific plugin
    pub fn unregister_by_prefix(&self, prefix: &str) {
        let mut commands = self.plugin_commands.write().unwrap();
        commands.retain(|c| !c.name.starts_with(prefix));
    }

    /// Get all commands (built-in + plugin)
    pub fn get_all(&self) -> Vec<Command> {
        let mut all_commands = self.builtin_commands.clone();

        let plugin_commands = self.plugin_commands.read().unwrap();
        all_commands.extend(plugin_commands.iter().cloned());

        all_commands
    }

    /// Filter commands by fuzzy matching query with context awareness
    ///
    /// When query is empty, commands are sorted by recency (most recently used first).
    /// When query is not empty, commands are sorted by match quality (fzf-style scoring)
    /// with recency as tiebreaker for equal scores.
    /// Disabled commands always appear after enabled ones.
    pub fn filter(
        &self,
        query: &str,
        current_context: KeyContext,
        keybinding_resolver: &crate::input::keybindings::KeybindingResolver,
        selection_active: bool,
        active_custom_contexts: &std::collections::HashSet<String>,
    ) -> Vec<Suggestion> {
        let commands = self.get_all();

        // Helper function to check if command is available in current context
        let is_available = |cmd: &Command| -> bool {
            // Check built-in contexts
            let builtin_ok = cmd.contexts.is_empty() || cmd.contexts.contains(&current_context);

            // Check custom contexts - all required custom contexts must be active
            let custom_ok = cmd.custom_contexts.is_empty()
                || cmd
                    .custom_contexts
                    .iter()
                    .all(|ctx| active_custom_contexts.contains(ctx));

            builtin_ok && custom_ok
        };

        // Filter and convert to suggestions with history position and fuzzy score
        let mut suggestions: Vec<(Suggestion, Option<usize>, i32)> = commands
            .into_iter()
            .filter_map(|cmd| {
                // Use fuzzy matching
                let fuzzy_result = fuzzy_match(query, &cmd.name);
                if !fuzzy_result.matched {
                    return None;
                }

                let mut available = is_available(&cmd);
                if cmd.action == Action::FindInSelection && !selection_active {
                    available = false;
                }
                let keybinding =
                    keybinding_resolver.get_keybinding_for_action(&cmd.action, current_context);
                let history_pos = self.history_position(&cmd.name);
                let suggestion = Suggestion::with_source(
                    cmd.name.clone(),
                    Some(cmd.description),
                    !available,
                    keybinding,
                    Some(cmd.source),
                );
                Some((suggestion, history_pos, fuzzy_result.score))
            })
            .collect();

        // Sort by:
        // 1. Disabled status (enabled first)
        // 2. Fuzzy match score (higher is better) - only when query is not empty
        // 3. History position (recent first, then never-used alphabetically)
        let has_query = !query.is_empty();
        suggestions.sort_by(|(a, a_hist, a_score), (b, b_hist, b_score)| {
            // First sort by disabled status
            match a.disabled.cmp(&b.disabled) {
                std::cmp::Ordering::Equal => {}
                other => return other,
            }

            // When there's a query, sort by fuzzy score (higher is better)
            if has_query {
                match b_score.cmp(a_score) {
                    std::cmp::Ordering::Equal => {}
                    other => return other,
                }
            }

            // Then sort by history position (lower = more recent = better)
            match (a_hist, b_hist) {
                (Some(a_pos), Some(b_pos)) => a_pos.cmp(b_pos),
                (Some(_), None) => std::cmp::Ordering::Less, // In history beats not in history
                (None, Some(_)) => std::cmp::Ordering::Greater,
                (None, None) => a.text.cmp(&b.text), // Alphabetical for never-used commands
            }
        });

        // Extract just the suggestions
        suggestions.into_iter().map(|(s, _, _)| s).collect()
    }

    /// Get count of registered plugin commands
    pub fn plugin_command_count(&self) -> usize {
        self.plugin_commands.read().unwrap().len()
    }

    /// Get count of total commands (built-in + plugin)
    pub fn total_command_count(&self) -> usize {
        self.builtin_commands.len() + self.plugin_command_count()
    }

    /// Find a command by exact name match
    pub fn find_by_name(&self, name: &str) -> Option<Command> {
        // Check plugin commands first (they can override built-in)
        {
            let plugin_commands = self.plugin_commands.read().unwrap();
            if let Some(cmd) = plugin_commands.iter().find(|c| c.name == name) {
                return Some(cmd.clone());
            }
        }

        // Then check built-in commands
        self.builtin_commands
            .iter()
            .find(|c| c.name == name)
            .cloned()
    }
}

impl Default for CommandRegistry {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::input::commands::CommandSource;
    use crate::input::keybindings::Action;

    #[test]
    fn test_command_registry_creation() {
        let registry = CommandRegistry::new();
        assert!(registry.total_command_count() > 0); // Has built-in commands
        assert_eq!(registry.plugin_command_count(), 0); // No plugin commands yet
    }

    #[test]
    fn test_register_command() {
        let registry = CommandRegistry::new();

        let custom_command = Command {
            name: "Test Command".to_string(),
            description: "A test command".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        };

        registry.register(custom_command.clone());
        assert_eq!(registry.plugin_command_count(), 1);

        let found = registry.find_by_name("Test Command");
        assert!(found.is_some());
        assert_eq!(found.unwrap().description, "A test command");
    }

    #[test]
    fn test_unregister_command() {
        let registry = CommandRegistry::new();

        let custom_command = Command {
            name: "Test Command".to_string(),
            description: "A test command".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        };

        registry.register(custom_command);
        assert_eq!(registry.plugin_command_count(), 1);

        registry.unregister("Test Command");
        assert_eq!(registry.plugin_command_count(), 0);
    }

    #[test]
    fn test_register_replaces_existing() {
        let registry = CommandRegistry::new();

        let command1 = Command {
            name: "Test Command".to_string(),
            description: "First version".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        };

        let command2 = Command {
            name: "Test Command".to_string(),
            description: "Second version".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        };

        registry.register(command1);
        assert_eq!(registry.plugin_command_count(), 1);

        registry.register(command2);
        assert_eq!(registry.plugin_command_count(), 1); // Still just one

        let found = registry.find_by_name("Test Command").unwrap();
        assert_eq!(found.description, "Second version");
    }

    #[test]
    fn test_unregister_by_prefix() {
        let registry = CommandRegistry::new();

        registry.register(Command {
            name: "Plugin A: Command 1".to_string(),
            description: "".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        registry.register(Command {
            name: "Plugin A: Command 2".to_string(),
            description: "".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        registry.register(Command {
            name: "Plugin B: Command".to_string(),
            description: "".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        assert_eq!(registry.plugin_command_count(), 3);

        registry.unregister_by_prefix("Plugin A:");
        assert_eq!(registry.plugin_command_count(), 1);

        let remaining = registry.find_by_name("Plugin B: Command");
        assert!(remaining.is_some());
    }

    #[test]
    fn test_filter_commands() {
        use crate::config::Config;
        use crate::input::keybindings::KeybindingResolver;

        let registry = CommandRegistry::new();
        let config = Config::default();
        let keybindings = KeybindingResolver::new(&config);

        registry.register(Command {
            name: "Test Save".to_string(),
            description: "Test save command".to_string(),
            action: Action::None,
            contexts: vec![KeyContext::Normal],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        let empty_contexts = std::collections::HashSet::new();
        let results = registry.filter(
            "save",
            KeyContext::Normal,
            &keybindings,
            false,
            &empty_contexts,
        );
        assert!(results.len() >= 2); // At least "Save File" + "Test Save"

        // Check that both built-in and custom commands appear
        let names: Vec<String> = results.iter().map(|s| s.text.clone()).collect();
        assert!(names.iter().any(|n| n.contains("Save")));
    }

    #[test]
    fn test_context_filtering() {
        use crate::config::Config;
        use crate::input::keybindings::KeybindingResolver;

        let registry = CommandRegistry::new();
        let config = Config::default();
        let keybindings = KeybindingResolver::new(&config);

        registry.register(Command {
            name: "Normal Only".to_string(),
            description: "Available only in normal context".to_string(),
            action: Action::None,
            contexts: vec![KeyContext::Normal],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        registry.register(Command {
            name: "Popup Only".to_string(),
            description: "Available only in popup context".to_string(),
            action: Action::None,
            contexts: vec![KeyContext::Popup],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        // In normal context, "Popup Only" should be disabled
        let empty_contexts = std::collections::HashSet::new();
        let results = registry.filter("", KeyContext::Normal, &keybindings, false, &empty_contexts);
        let popup_only = results.iter().find(|s| s.text == "Popup Only");
        assert!(popup_only.is_some());
        assert!(popup_only.unwrap().disabled);

        // In popup context, "Normal Only" should be disabled
        let results = registry.filter("", KeyContext::Popup, &keybindings, false, &empty_contexts);
        let normal_only = results.iter().find(|s| s.text == "Normal Only");
        assert!(normal_only.is_some());
        assert!(normal_only.unwrap().disabled);
    }

    #[test]
    fn test_get_all_merges_commands() {
        let registry = CommandRegistry::new();
        let initial_count = registry.total_command_count();

        registry.register(Command {
            name: "Custom 1".to_string(),
            description: "".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        registry.register(Command {
            name: "Custom 2".to_string(),
            description: "".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        let all = registry.get_all();
        assert_eq!(all.len(), initial_count + 2);
    }

    #[test]
    fn test_plugin_command_overrides_builtin() {
        let registry = CommandRegistry::new();

        // Check a built-in command exists
        let builtin = registry.find_by_name("Save File");
        assert!(builtin.is_some());
        let original_desc = builtin.unwrap().description;

        // Override it with a plugin command
        registry.register(Command {
            name: "Save File".to_string(),
            description: "Custom save implementation".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        // Should now find the custom version
        let custom = registry.find_by_name("Save File").unwrap();
        assert_eq!(custom.description, "Custom save implementation");
        assert_ne!(custom.description, original_desc);
    }

    #[test]
    fn test_record_usage() {
        let mut registry = CommandRegistry::new();

        registry.record_usage("Save File");
        assert_eq!(registry.history_position("Save File"), Some(0));

        registry.record_usage("Open File");
        assert_eq!(registry.history_position("Open File"), Some(0));
        assert_eq!(registry.history_position("Save File"), Some(1));

        // Using Save File again should move it to front
        registry.record_usage("Save File");
        assert_eq!(registry.history_position("Save File"), Some(0));
        assert_eq!(registry.history_position("Open File"), Some(1));
    }

    #[test]
    fn test_history_sorting() {
        use crate::config::Config;
        use crate::input::keybindings::KeybindingResolver;

        let mut registry = CommandRegistry::new();
        let config = Config::default();
        let keybindings = KeybindingResolver::new(&config);

        // Record some commands
        registry.record_usage("Quit");
        registry.record_usage("Save File");
        registry.record_usage("Open File");

        // Filter with empty query should return history-sorted results
        let empty_contexts = std::collections::HashSet::new();
        let results = registry.filter("", KeyContext::Normal, &keybindings, false, &empty_contexts);

        // Find positions of our test commands in results
        let open_pos = results.iter().position(|s| s.text == "Open File").unwrap();
        let save_pos = results.iter().position(|s| s.text == "Save File").unwrap();
        let quit_pos = results.iter().position(|s| s.text == "Quit").unwrap();

        // Most recently used should be first
        assert!(
            open_pos < save_pos,
            "Open File should come before Save File"
        );
        assert!(save_pos < quit_pos, "Save File should come before Quit");
    }

    #[test]
    fn test_history_max_size() {
        let mut registry = CommandRegistry::new();

        // Add more than MAX_HISTORY_SIZE commands
        for i in 0..60 {
            registry.record_usage(&format!("Command {}", i));
        }

        // Should be trimmed to MAX_HISTORY_SIZE
        assert_eq!(
            registry.command_history.len(),
            CommandRegistry::MAX_HISTORY_SIZE
        );

        // Most recent should still be at front
        assert_eq!(registry.history_position("Command 59"), Some(0));

        // Oldest should be trimmed
        assert_eq!(registry.history_position("Command 0"), None);
    }

    #[test]
    fn test_unused_commands_alphabetical() {
        use crate::config::Config;
        use crate::input::keybindings::KeybindingResolver;

        let mut registry = CommandRegistry::new();
        let config = Config::default();
        let keybindings = KeybindingResolver::new(&config);

        // Register some custom commands (never used)
        registry.register(Command {
            name: "Zebra Command".to_string(),
            description: "".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        registry.register(Command {
            name: "Alpha Command".to_string(),
            description: "".to_string(),
            action: Action::None,
            contexts: vec![],
            custom_contexts: vec![],
            source: CommandSource::Builtin,
        });

        // Use one built-in command
        registry.record_usage("Save File");

        let empty_contexts = std::collections::HashSet::new();
        let results = registry.filter("", KeyContext::Normal, &keybindings, false, &empty_contexts);

        let save_pos = results.iter().position(|s| s.text == "Save File").unwrap();
        let alpha_pos = results
            .iter()
            .position(|s| s.text == "Alpha Command")
            .unwrap();
        let zebra_pos = results
            .iter()
            .position(|s| s.text == "Zebra Command")
            .unwrap();

        // Used command should be first
        assert!(
            save_pos < alpha_pos,
            "Save File should come before Alpha Command"
        );
        // Unused commands should be alphabetical
        assert!(
            alpha_pos < zebra_pos,
            "Alpha Command should come before Zebra Command"
        );
    }

    #[test]
    fn test_required_commands_exist() {
        // This test ensures that all required command palette entries exist.
        // If this test fails, it means a command was removed or renamed.
        let registry = CommandRegistry::new();

        let required_commands = [
            // LSP commands
            ("Show Completions", Action::LspCompletion),
            ("Go to Definition", Action::LspGotoDefinition),
            ("Show Hover Info", Action::LspHover),
            ("Find References", Action::LspReferences),
            // Help commands
            ("Show Manual", Action::ShowHelp),
            ("Show Keyboard Shortcuts", Action::ShowKeyboardShortcuts),
            // Scroll commands
            ("Scroll Up", Action::ScrollUp),
            ("Scroll Down", Action::ScrollDown),
            ("Scroll Tabs Left", Action::ScrollTabsLeft),
            ("Scroll Tabs Right", Action::ScrollTabsRight),
            // Navigation commands
            ("Smart Home", Action::SmartHome),
            // Delete commands
            ("Delete Word Backward", Action::DeleteWordBackward),
            ("Delete Word Forward", Action::DeleteWordForward),
            ("Delete to End of Line", Action::DeleteToLineEnd),
        ];

        for (name, expected_action) in required_commands {
            let cmd = registry.find_by_name(name);
            assert!(
                cmd.is_some(),
                "Command '{}' should exist in command palette",
                name
            );
            assert_eq!(
                cmd.unwrap().action,
                expected_action,
                "Command '{}' should have action {:?}",
                name,
                expected_action
            );
        }
    }
}