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    ScanLineIndex,
159    GoToMatchingBracket,
160    JumpToNextError,
161    JumpToPreviousError,
162
163    // Smart editing
164    SmartHome,
165    DedentSelection,
166    ToggleComment,
167
168    // Bookmarks
169    SetBookmark(char),
170    JumpToBookmark(char),
171    ClearBookmark(char),
172    ListBookmarks,
173
174    // Search options
175    ToggleSearchCaseSensitive,
176    ToggleSearchWholeWord,
177    ToggleSearchRegex,
178    ToggleSearchConfirmEach,
179
180    // Macros
181    StartMacroRecording,
182    StopMacroRecording,
183    PlayMacro(char),
184    ToggleMacroRecording(char),
185    ShowMacro(char),
186    ListMacros,
187    PromptRecordMacro,
188    PromptPlayMacro,
189    PlayLastMacro,
190
191    // Bookmarks (prompt-based)
192    PromptSetBookmark,
193    PromptJumpToBookmark,
194
195    // Undo/redo
196    Undo,
197    Redo,
198
199    // View
200    ScrollUp,
201    ScrollDown,
202    ShowHelp,
203    ShowKeyboardShortcuts,
204    ShowWarnings,
205    ShowLspStatus,
206    ClearWarnings,
207    CommandPalette,
208    ToggleLineWrap,
209    ToggleReadOnly,
210    ToggleComposeMode,
211    SetComposeWidth,
212    InspectThemeAtCursor,
213    SelectTheme,
214    SelectKeybindingMap,
215    SelectCursorStyle,
216    SelectLocale,
217
218    // Buffer/tab navigation
219    NextBuffer,
220    PrevBuffer,
221    SwitchToPreviousTab,
222    SwitchToTabByName,
223
224    // Tab scrolling
225    ScrollTabsLeft,
226    ScrollTabsRight,
227
228    // Position history navigation
229    NavigateBack,
230    NavigateForward,
231
232    // Split view operations
233    SplitHorizontal,
234    SplitVertical,
235    CloseSplit,
236    NextSplit,
237    PrevSplit,
238    IncreaseSplitSize,
239    DecreaseSplitSize,
240    ToggleMaximizeSplit,
241
242    // Prompt mode actions
243    PromptConfirm,
244    /// PromptConfirm with recorded text for macro playback
245    PromptConfirmWithText(String),
246    PromptCancel,
247    PromptBackspace,
248    PromptDelete,
249    PromptMoveLeft,
250    PromptMoveRight,
251    PromptMoveStart,
252    PromptMoveEnd,
253    PromptSelectPrev,
254    PromptSelectNext,
255    PromptPageUp,
256    PromptPageDown,
257    PromptAcceptSuggestion,
258    PromptMoveWordLeft,
259    PromptMoveWordRight,
260    // Advanced prompt editing (word operations, clipboard)
261    PromptDeleteWordForward,
262    PromptDeleteWordBackward,
263    PromptDeleteToLineEnd,
264    PromptCopy,
265    PromptCut,
266    PromptPaste,
267    // Prompt selection actions
268    PromptMoveLeftSelecting,
269    PromptMoveRightSelecting,
270    PromptMoveHomeSelecting,
271    PromptMoveEndSelecting,
272    PromptSelectWordLeft,
273    PromptSelectWordRight,
274    PromptSelectAll,
275
276    // File browser actions
277    FileBrowserToggleHidden,
278
279    // Popup mode actions
280    PopupSelectNext,
281    PopupSelectPrev,
282    PopupPageUp,
283    PopupPageDown,
284    PopupConfirm,
285    PopupCancel,
286
287    // File explorer operations
288    ToggleFileExplorer,
289    // Menu bar visibility
290    ToggleMenuBar,
291    // Tab bar visibility
292    ToggleTabBar,
293    FocusFileExplorer,
294    FocusEditor,
295    FileExplorerUp,
296    FileExplorerDown,
297    FileExplorerPageUp,
298    FileExplorerPageDown,
299    FileExplorerExpand,
300    FileExplorerCollapse,
301    FileExplorerOpen,
302    FileExplorerRefresh,
303    FileExplorerNewFile,
304    FileExplorerNewDirectory,
305    FileExplorerDelete,
306    FileExplorerRename,
307    FileExplorerToggleHidden,
308    FileExplorerToggleGitignored,
309
310    // LSP operations
311    LspCompletion,
312    LspGotoDefinition,
313    LspReferences,
314    LspRename,
315    LspHover,
316    LspSignatureHelp,
317    LspCodeActions,
318    LspRestart,
319    LspStop,
320    ToggleInlayHints,
321    ToggleMouseHover,
322
323    // View toggles
324    ToggleLineNumbers,
325    ToggleScrollSync,
326    ToggleMouseCapture,
327    ToggleDebugHighlights, // Debug mode: show highlight/overlay byte ranges
328    SetBackground,
329    SetBackgroundBlend,
330
331    // Buffer settings (per-buffer overrides)
332    SetTabSize,
333    SetLineEnding,
334    ToggleIndentationStyle,
335    ToggleTabIndicators,
336    ResetBufferSettings,
337
338    // Config operations
339    DumpConfig,
340
341    // Search and replace
342    Search,
343    FindInSelection,
344    FindNext,
345    FindPrevious,
346    FindSelectionNext,     // Quick find next occurrence of selection (Ctrl+F3)
347    FindSelectionPrevious, // Quick find previous occurrence of selection (Ctrl+Shift+F3)
348    Replace,
349    QueryReplace, // Interactive replace (y/n/!/q for each match)
350
351    // Menu navigation
352    MenuActivate,     // Open menu bar (Alt or F10)
353    MenuClose,        // Close menu (Esc)
354    MenuLeft,         // Navigate to previous menu
355    MenuRight,        // Navigate to next menu
356    MenuUp,           // Navigate to previous item in menu
357    MenuDown,         // Navigate to next item in menu
358    MenuExecute,      // Execute selected menu item (Enter)
359    MenuOpen(String), // Open a specific menu by name (e.g., "File", "Edit")
360
361    // Keybinding map switching
362    SwitchKeybindingMap(String), // Switch to a named keybinding map (e.g., "default", "emacs", "vscode")
363
364    // Plugin custom actions
365    PluginAction(String),
366
367    // Load the current buffer's contents as a plugin
368    LoadPluginFromBuffer,
369
370    // Settings operations
371    OpenSettings,        // Open the settings modal
372    CloseSettings,       // Close the settings modal
373    SettingsSave,        // Save settings changes
374    SettingsReset,       // Reset current setting to default
375    SettingsToggleFocus, // Toggle focus between category and settings panels
376    SettingsActivate,    // Activate/toggle the current setting
377    SettingsSearch,      // Start search in settings
378    SettingsHelp,        // Show settings help overlay
379    SettingsIncrement,   // Increment number value or next dropdown option
380    SettingsDecrement,   // Decrement number value or previous dropdown option
381
382    // Terminal operations
383    OpenTerminal,          // Open a new terminal in the current split
384    CloseTerminal,         // Close the current terminal
385    FocusTerminal,         // Focus the terminal buffer (if viewing terminal, focus input)
386    TerminalEscape,        // Escape from terminal mode back to editor
387    ToggleKeyboardCapture, // Toggle keyboard capture mode (all keys go to terminal)
388    TerminalPaste,         // Paste clipboard contents into terminal as a single batch
389
390    // Shell command operations
391    ShellCommand,        // Run shell command on buffer/selection, output to new buffer
392    ShellCommandReplace, // Run shell command on buffer/selection, replace content
393
394    // Case conversion
395    ToUpperCase, // Convert selection to uppercase
396    ToLowerCase, // Convert selection to lowercase
397
398    // Input calibration
399    CalibrateInput, // Open the input calibration wizard
400
401    // No-op
402    None,
403}