Skip to main content

fresh_core/
action.rs

1use serde::{Deserialize, Serialize};
2
3/// Context in which a keybinding is active
4#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, ts_rs::TS)]
5#[ts(export)]
6pub enum KeyContext {
7    /// Global bindings that work in all contexts (checked first with highest priority)
8    Global,
9    /// Normal editing mode
10    Normal,
11    /// Prompt/minibuffer is active
12    Prompt,
13    /// Popup window is visible
14    Popup,
15    /// File explorer has focus
16    FileExplorer,
17    /// Menu bar is active
18    Menu,
19    /// Terminal has focus
20    Terminal,
21    /// Settings modal is active
22    Settings,
23    /// Buffer-local mode context (e.g. "search-replace-list")
24    Mode(String),
25}
26
27impl KeyContext {
28    /// Check if a context should allow input
29    pub fn allows_text_input(&self) -> bool {
30        matches!(self, Self::Normal | Self::Prompt)
31    }
32
33    /// Parse context from a "when" string
34    pub fn from_when_clause(when: &str) -> Option<Self> {
35        let trimmed = when.trim();
36        if let Some(mode_name) = trimmed.strip_prefix("mode:") {
37            return Some(Self::Mode(mode_name.to_string()));
38        }
39        Some(match trimmed {
40            "global" => Self::Global,
41            "prompt" => Self::Prompt,
42            "popup" => Self::Popup,
43            "fileExplorer" | "file_explorer" => Self::FileExplorer,
44            "normal" => Self::Normal,
45            "menu" => Self::Menu,
46            "terminal" => Self::Terminal,
47            "settings" => Self::Settings,
48            _ => return None,
49        })
50    }
51
52    /// Convert context to "when" clause string
53    pub fn to_when_clause(&self) -> String {
54        match self {
55            Self::Global => "global".to_string(),
56            Self::Normal => "normal".to_string(),
57            Self::Prompt => "prompt".to_string(),
58            Self::Popup => "popup".to_string(),
59            Self::FileExplorer => "fileExplorer".to_string(),
60            Self::Menu => "menu".to_string(),
61            Self::Terminal => "terminal".to_string(),
62            Self::Settings => "settings".to_string(),
63            Self::Mode(name) => format!("mode:{}", name),
64        }
65    }
66}
67
68/// High-level actions that can be performed in the editor
69#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, ts_rs::TS)]
70#[ts(export)]
71pub enum Action {
72    // Character input
73    InsertChar(char),
74    InsertNewline,
75    InsertTab,
76
77    // Basic movement
78    MoveLeft,
79    MoveRight,
80    MoveUp,
81    MoveDown,
82    MoveWordLeft,
83    MoveWordRight,
84    MoveLineStart,
85    MoveLineEnd,
86    MovePageUp,
87    MovePageDown,
88    MoveDocumentStart,
89    MoveDocumentEnd,
90
91    // Selection movement (extends selection while moving)
92    SelectLeft,
93    SelectRight,
94    SelectUp,
95    SelectDown,
96    SelectWordLeft,
97    SelectWordRight,
98    SelectLineStart,
99    SelectLineEnd,
100    SelectDocumentStart,
101    SelectDocumentEnd,
102    SelectPageUp,
103    SelectPageDown,
104    SelectAll,
105    SelectWord,
106    SelectLine,
107    ExpandSelection,
108
109    // Block/rectangular selection (column-wise)
110    BlockSelectLeft,
111    BlockSelectRight,
112    BlockSelectUp,
113    BlockSelectDown,
114
115    // Editing
116    DeleteBackward,
117    DeleteForward,
118    DeleteWordBackward,
119    DeleteWordForward,
120    DeleteLine,
121    DeleteToLineEnd,
122    DeleteToLineStart,
123    TransposeChars,
124    OpenLine,
125
126    // View
127    Recenter,
128
129    // Selection
130    SetMark,
131
132    // Clipboard
133    Copy,
134    CopyWithTheme(String),
135    Cut,
136    Paste,
137
138    // Vi-style yank (copy without selection, then restore cursor)
139    YankWordForward,
140    YankWordBackward,
141    YankToLineEnd,
142    YankToLineStart,
143
144    // Multi-cursor
145    AddCursorAbove,
146    AddCursorBelow,
147    AddCursorNextMatch,
148    RemoveSecondaryCursors,
149
150    // File operations
151    Save,
152    SaveAs,
153    Open,
154    SwitchProject,
155    New,
156    Close,
157    CloseTab,
158    Quit,
159    Revert,
160    ToggleAutoRevert,
161    FormatBuffer,
162
163    // Navigation
164    GotoLine,
165    ScanLineIndex,
166    GoToMatchingBracket,
167    JumpToNextError,
168    JumpToPreviousError,
169
170    // Smart editing
171    SmartHome,
172    DedentSelection,
173    ToggleComment,
174
175    // Bookmarks
176    SetBookmark(char),
177    JumpToBookmark(char),
178    ClearBookmark(char),
179    ListBookmarks,
180
181    // Search options
182    ToggleSearchCaseSensitive,
183    ToggleSearchWholeWord,
184    ToggleSearchRegex,
185    ToggleSearchConfirmEach,
186
187    // Macros
188    StartMacroRecording,
189    StopMacroRecording,
190    PlayMacro(char),
191    ToggleMacroRecording(char),
192    ShowMacro(char),
193    ListMacros,
194    PromptRecordMacro,
195    PromptPlayMacro,
196    PlayLastMacro,
197
198    // Bookmarks (prompt-based)
199    PromptSetBookmark,
200    PromptJumpToBookmark,
201
202    // Undo/redo
203    Undo,
204    Redo,
205
206    // View
207    ScrollUp,
208    ScrollDown,
209    ShowHelp,
210    ShowKeyboardShortcuts,
211    ShowWarnings,
212    ShowLspStatus,
213    ClearWarnings,
214    CommandPalette,
215    ToggleLineWrap,
216    ToggleReadOnly,
217    TogglePageView,
218    SetPageWidth,
219    InspectThemeAtCursor,
220    SelectTheme,
221    SelectKeybindingMap,
222    SelectCursorStyle,
223    SelectLocale,
224
225    // Buffer/tab navigation
226    NextBuffer,
227    PrevBuffer,
228    SwitchToPreviousTab,
229    SwitchToTabByName,
230
231    // Tab scrolling
232    ScrollTabsLeft,
233    ScrollTabsRight,
234
235    // Position history navigation
236    NavigateBack,
237    NavigateForward,
238
239    // Split view operations
240    SplitHorizontal,
241    SplitVertical,
242    CloseSplit,
243    NextSplit,
244    PrevSplit,
245    IncreaseSplitSize,
246    DecreaseSplitSize,
247    ToggleMaximizeSplit,
248
249    // Prompt mode actions
250    PromptConfirm,
251    /// PromptConfirm with recorded text for macro playback
252    PromptConfirmWithText(String),
253    PromptCancel,
254    PromptBackspace,
255    PromptDelete,
256    PromptMoveLeft,
257    PromptMoveRight,
258    PromptMoveStart,
259    PromptMoveEnd,
260    PromptSelectPrev,
261    PromptSelectNext,
262    PromptPageUp,
263    PromptPageDown,
264    PromptAcceptSuggestion,
265    PromptMoveWordLeft,
266    PromptMoveWordRight,
267    // Advanced prompt editing (word operations, clipboard)
268    PromptDeleteWordForward,
269    PromptDeleteWordBackward,
270    PromptDeleteToLineEnd,
271    PromptCopy,
272    PromptCut,
273    PromptPaste,
274    // Prompt selection actions
275    PromptMoveLeftSelecting,
276    PromptMoveRightSelecting,
277    PromptMoveHomeSelecting,
278    PromptMoveEndSelecting,
279    PromptSelectWordLeft,
280    PromptSelectWordRight,
281    PromptSelectAll,
282
283    // File browser actions
284    FileBrowserToggleHidden,
285
286    // Popup mode actions
287    PopupSelectNext,
288    PopupSelectPrev,
289    PopupPageUp,
290    PopupPageDown,
291    PopupConfirm,
292    PopupCancel,
293
294    // File explorer operations
295    ToggleFileExplorer,
296    // Menu bar visibility
297    ToggleMenuBar,
298    // Tab bar visibility
299    ToggleTabBar,
300    FocusFileExplorer,
301    FocusEditor,
302    FileExplorerUp,
303    FileExplorerDown,
304    FileExplorerPageUp,
305    FileExplorerPageDown,
306    FileExplorerExpand,
307    FileExplorerCollapse,
308    FileExplorerOpen,
309    FileExplorerRefresh,
310    FileExplorerNewFile,
311    FileExplorerNewDirectory,
312    FileExplorerDelete,
313    FileExplorerRename,
314    FileExplorerToggleHidden,
315    FileExplorerToggleGitignored,
316
317    // LSP operations
318    LspCompletion,
319    LspGotoDefinition,
320    LspReferences,
321    LspRename,
322    LspHover,
323    LspSignatureHelp,
324    LspCodeActions,
325    LspRestart,
326    LspStop,
327    ToggleInlayHints,
328    ToggleMouseHover,
329
330    // View toggles
331    ToggleLineNumbers,
332    ToggleScrollSync,
333    ToggleMouseCapture,
334    ToggleDebugHighlights, // Debug mode: show highlight/overlay byte ranges
335    SetBackground,
336    SetBackgroundBlend,
337
338    // Buffer settings (per-buffer overrides)
339    SetTabSize,
340    SetLineEnding,
341    ToggleIndentationStyle,
342    ToggleTabIndicators,
343    ResetBufferSettings,
344
345    // Config operations
346    DumpConfig,
347
348    // Search and replace
349    Search,
350    FindInSelection,
351    FindNext,
352    FindPrevious,
353    FindSelectionNext,     // Quick find next occurrence of selection (Ctrl+F3)
354    FindSelectionPrevious, // Quick find previous occurrence of selection (Ctrl+Shift+F3)
355    Replace,
356    QueryReplace, // Interactive replace (y/n/!/q for each match)
357
358    // Menu navigation
359    MenuActivate,     // Open menu bar (Alt or F10)
360    MenuClose,        // Close menu (Esc)
361    MenuLeft,         // Navigate to previous menu
362    MenuRight,        // Navigate to next menu
363    MenuUp,           // Navigate to previous item in menu
364    MenuDown,         // Navigate to next item in menu
365    MenuExecute,      // Execute selected menu item (Enter)
366    MenuOpen(String), // Open a specific menu by name (e.g., "File", "Edit")
367
368    // Keybinding map switching
369    SwitchKeybindingMap(String), // Switch to a named keybinding map (e.g., "default", "emacs", "vscode")
370
371    // Plugin custom actions
372    PluginAction(String),
373
374    // Load the current buffer's contents as a plugin
375    LoadPluginFromBuffer,
376
377    // Settings operations
378    OpenSettings,        // Open the settings modal
379    CloseSettings,       // Close the settings modal
380    SettingsSave,        // Save settings changes
381    SettingsReset,       // Reset current setting to default
382    SettingsToggleFocus, // Toggle focus between category and settings panels
383    SettingsActivate,    // Activate/toggle the current setting
384    SettingsSearch,      // Start search in settings
385    SettingsHelp,        // Show settings help overlay
386    SettingsIncrement,   // Increment number value or next dropdown option
387    SettingsDecrement,   // Decrement number value or previous dropdown option
388
389    // Terminal operations
390    OpenTerminal,          // Open a new terminal in the current split
391    CloseTerminal,         // Close the current terminal
392    FocusTerminal,         // Focus the terminal buffer (if viewing terminal, focus input)
393    TerminalEscape,        // Escape from terminal mode back to editor
394    ToggleKeyboardCapture, // Toggle keyboard capture mode (all keys go to terminal)
395    TerminalPaste,         // Paste clipboard contents into terminal as a single batch
396
397    // Shell command operations
398    ShellCommand,        // Run shell command on buffer/selection, output to new buffer
399    ShellCommandReplace, // Run shell command on buffer/selection, replace content
400
401    // Case conversion
402    ToUpperCase, // Convert selection to uppercase
403    ToLowerCase, // Convert selection to lowercase
404
405    // Input calibration
406    CalibrateInput, // Open the input calibration wizard
407
408    // No-op
409    None,
410}