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