1use crate::input::keybindings::{Action, KeyContext};
4use crate::types::context_keys;
5use rust_i18n::t;
6
7#[derive(Debug, Clone, PartialEq)]
9pub enum CommandSource {
10 Builtin,
12 Plugin(String),
14}
15
16#[derive(Debug, Clone)]
18pub struct Command {
19 pub name: String,
21 pub description: String,
23 pub action: Action,
25 pub contexts: Vec<KeyContext>,
27 pub custom_contexts: Vec<String>,
30 pub source: CommandSource,
32 pub terminal_bypass: bool,
43}
44
45impl Command {
46 pub fn get_localized_name(&self) -> String {
48 if self.name.starts_with('%') {
49 if let CommandSource::Plugin(ref plugin_name) = self.source {
50 return crate::i18n::translate_plugin_string(
51 plugin_name,
52 &self.name[1..],
53 &std::collections::HashMap::new(),
54 );
55 }
56 }
57 self.name.clone()
58 }
59
60 pub fn get_localized_description(&self) -> String {
62 if self.description.starts_with('%') {
63 if let CommandSource::Plugin(ref plugin_name) = self.source {
64 return crate::i18n::translate_plugin_string(
65 plugin_name,
66 &self.description[1..],
67 &std::collections::HashMap::new(),
68 );
69 }
70 }
71 self.description.clone()
72 }
73}
74
75#[derive(Debug, Clone, PartialEq)]
77pub struct Suggestion {
78 pub text: String,
80 pub description: Option<String>,
82 pub description_spans: Option<Vec<fresh_core::api::StyledText>>,
87 pub value: Option<String>,
89 pub disabled: bool,
91 pub keybinding: Option<String>,
93 pub source: Option<CommandSource>,
95}
96
97impl Suggestion {
98 pub fn new(text: String) -> Self {
100 Self {
101 text,
102 description: None,
103 description_spans: None,
104 value: None,
105 disabled: false,
106 keybinding: None,
107 source: None,
108 }
109 }
110
111 pub fn disabled(text: String) -> Self {
113 Self {
114 text,
115 description: None,
116 description_spans: None,
117 value: None,
118 disabled: true,
119 keybinding: None,
120 source: None,
121 }
122 }
123
124 pub fn with_description(mut self, description: String) -> Self {
125 self.description = Some(description);
126 self
127 }
128
129 pub fn with_description_spans(
130 mut self,
131 spans: Option<Vec<fresh_core::api::StyledText>>,
132 ) -> Self {
133 self.description_spans = spans;
134 self
135 }
136
137 pub fn with_value(mut self, value: String) -> Self {
138 self.value = Some(value);
139 self
140 }
141
142 pub fn with_keybinding(mut self, keybinding: Option<String>) -> Self {
143 self.keybinding = keybinding;
144 self
145 }
146
147 pub fn with_source(mut self, source: Option<CommandSource>) -> Self {
148 self.source = source;
149 self
150 }
151
152 pub fn set_disabled(mut self, disabled: bool) -> Self {
153 self.disabled = disabled;
154 self
155 }
156
157 pub fn get_value(&self) -> &str {
158 self.value.as_ref().unwrap_or(&self.text)
159 }
160}
161
162struct CommandDef {
164 name_key: &'static str,
165 desc_key: &'static str,
166 action: fn() -> Action,
167 contexts: &'static [KeyContext],
168 custom_contexts: &'static [&'static str],
169}
170
171use KeyContext::{CompositeBuffer, Dock, FileExplorer, Normal, Terminal};
172
173static COMMAND_DEFS: &[CommandDef] = &[
176 CommandDef {
178 name_key: "cmd.open_file",
179 desc_key: "cmd.open_file_desc",
180 action: || Action::Open,
181 contexts: &[],
182 custom_contexts: &[],
183 },
184 CommandDef {
185 name_key: "cmd.switch_project",
186 desc_key: "cmd.switch_project_desc",
187 action: || Action::SwitchProject,
188 contexts: &[],
189 custom_contexts: &[],
190 },
191 CommandDef {
192 name_key: "cmd.save_file",
193 desc_key: "cmd.save_file_desc",
194 action: || Action::Save,
195 contexts: &[Normal],
196 custom_contexts: &[],
197 },
198 CommandDef {
199 name_key: "cmd.save_file_as",
200 desc_key: "cmd.save_file_as_desc",
201 action: || Action::SaveAs,
202 contexts: &[Normal],
203 custom_contexts: &[],
204 },
205 CommandDef {
206 name_key: "cmd.new_file",
207 desc_key: "cmd.new_file_desc",
208 action: || Action::New,
209 contexts: &[],
210 custom_contexts: &[],
211 },
212 CommandDef {
213 name_key: "cmd.close_buffer",
214 desc_key: "cmd.close_buffer_desc",
215 action: || Action::Close,
216 contexts: &[Normal, Terminal],
217 custom_contexts: &[],
218 },
219 CommandDef {
220 name_key: "cmd.close_tab",
221 desc_key: "cmd.close_tab_desc",
222 action: || Action::CloseTab,
223 contexts: &[Normal, Terminal],
224 custom_contexts: &[],
225 },
226 CommandDef {
227 name_key: "cmd.revert_file",
228 desc_key: "cmd.revert_file_desc",
229 action: || Action::Revert,
230 contexts: &[Normal],
231 custom_contexts: &[],
232 },
233 CommandDef {
234 name_key: "cmd.toggle_auto_revert",
235 desc_key: "cmd.toggle_auto_revert_desc",
236 action: || Action::ToggleAutoRevert,
237 contexts: &[],
238 custom_contexts: &[],
239 },
240 CommandDef {
241 name_key: "cmd.format_buffer",
242 desc_key: "cmd.format_buffer_desc",
243 action: || Action::FormatBuffer,
244 contexts: &[Normal],
245 custom_contexts: &[],
246 },
247 CommandDef {
248 name_key: "cmd.trim_trailing_whitespace",
249 desc_key: "cmd.trim_trailing_whitespace_desc",
250 action: || Action::TrimTrailingWhitespace,
251 contexts: &[Normal],
252 custom_contexts: &[],
253 },
254 CommandDef {
255 name_key: "cmd.ensure_final_newline",
256 desc_key: "cmd.ensure_final_newline_desc",
257 action: || Action::EnsureFinalNewline,
258 contexts: &[Normal],
259 custom_contexts: &[],
260 },
261 CommandDef {
262 name_key: "cmd.quit",
263 desc_key: "cmd.quit_desc",
264 action: || Action::Quit,
265 contexts: &[],
266 custom_contexts: &[],
267 },
268 CommandDef {
269 name_key: "cmd.detach",
270 desc_key: "cmd.detach_desc",
271 action: || Action::Detach,
272 contexts: &[],
273 custom_contexts: &[context_keys::SESSION_MODE],
274 },
275 CommandDef {
277 name_key: "cmd.quick_open_buffers",
278 desc_key: "cmd.quick_open_buffers_desc",
279 action: || Action::QuickOpenBuffers,
280 contexts: &[],
281 custom_contexts: &[],
282 },
283 CommandDef {
284 name_key: "cmd.quick_open_files",
285 desc_key: "cmd.quick_open_files_desc",
286 action: || Action::QuickOpenFiles,
287 contexts: &[],
288 custom_contexts: &[],
289 },
290 CommandDef {
292 name_key: "cmd.undo",
293 desc_key: "cmd.undo_desc",
294 action: || Action::Undo,
295 contexts: &[Normal],
296 custom_contexts: &[],
297 },
298 CommandDef {
299 name_key: "cmd.redo",
300 desc_key: "cmd.redo_desc",
301 action: || Action::Redo,
302 contexts: &[Normal],
303 custom_contexts: &[],
304 },
305 CommandDef {
306 name_key: "cmd.copy",
307 desc_key: "cmd.copy_desc",
308 action: || Action::Copy,
309 contexts: &[Normal],
310 custom_contexts: &[],
311 },
312 CommandDef {
313 name_key: "cmd.copy_with_formatting",
314 desc_key: "cmd.copy_with_formatting_desc",
315 action: || Action::CopyWithTheme(String::new()),
316 contexts: &[Normal],
317 custom_contexts: &[],
318 },
319 CommandDef {
320 name_key: "cmd.copy_file_path",
321 desc_key: "cmd.copy_file_path_desc",
322 action: || Action::CopyFilePath,
323 contexts: &[Normal],
324 custom_contexts: &[],
325 },
326 CommandDef {
327 name_key: "cmd.copy_relative_file_path",
328 desc_key: "cmd.copy_relative_file_path_desc",
329 action: || Action::CopyRelativeFilePath,
330 contexts: &[Normal],
331 custom_contexts: &[],
332 },
333 CommandDef {
334 name_key: "cmd.cut",
335 desc_key: "cmd.cut_desc",
336 action: || Action::Cut,
337 contexts: &[Normal],
338 custom_contexts: &[],
339 },
340 CommandDef {
341 name_key: "cmd.paste",
342 desc_key: "cmd.paste_desc",
343 action: || Action::Paste,
344 contexts: &[Normal],
345 custom_contexts: &[],
346 },
347 CommandDef {
348 name_key: "cmd.delete_line",
349 desc_key: "cmd.delete_line_desc",
350 action: || Action::DeleteLine,
351 contexts: &[Normal],
352 custom_contexts: &[],
353 },
354 CommandDef {
355 name_key: "cmd.delete_word_backward",
356 desc_key: "cmd.delete_word_backward_desc",
357 action: || Action::DeleteWordBackward,
358 contexts: &[Normal],
359 custom_contexts: &[],
360 },
361 CommandDef {
362 name_key: "cmd.delete_word_forward",
363 desc_key: "cmd.delete_word_forward_desc",
364 action: || Action::DeleteWordForward,
365 contexts: &[Normal],
366 custom_contexts: &[],
367 },
368 CommandDef {
369 name_key: "cmd.delete_to_end_of_line",
370 desc_key: "cmd.delete_to_end_of_line_desc",
371 action: || Action::DeleteToLineEnd,
372 contexts: &[Normal],
373 custom_contexts: &[],
374 },
375 CommandDef {
376 name_key: "cmd.transpose_characters",
377 desc_key: "cmd.transpose_characters_desc",
378 action: || Action::TransposeChars,
379 contexts: &[Normal],
380 custom_contexts: &[],
381 },
382 CommandDef {
383 name_key: "cmd.transform_uppercase",
384 desc_key: "cmd.transform_uppercase_desc",
385 action: || Action::ToUpperCase,
386 contexts: &[Normal],
387 custom_contexts: &[],
388 },
389 CommandDef {
390 name_key: "cmd.transform_lowercase",
391 desc_key: "cmd.transform_lowercase_desc",
392 action: || Action::ToLowerCase,
393 contexts: &[Normal],
394 custom_contexts: &[],
395 },
396 CommandDef {
397 name_key: "cmd.sort_lines",
398 desc_key: "cmd.sort_lines_desc",
399 action: || Action::SortLines,
400 contexts: &[Normal],
401 custom_contexts: &[],
402 },
403 CommandDef {
404 name_key: "cmd.open_line",
405 desc_key: "cmd.open_line_desc",
406 action: || Action::OpenLine,
407 contexts: &[Normal],
408 custom_contexts: &[],
409 },
410 CommandDef {
411 name_key: "cmd.duplicate_line",
412 desc_key: "cmd.duplicate_line_desc",
413 action: || Action::DuplicateLine,
414 contexts: &[Normal],
415 custom_contexts: &[],
416 },
417 CommandDef {
418 name_key: "cmd.recenter",
419 desc_key: "cmd.recenter_desc",
420 action: || Action::Recenter,
421 contexts: &[Normal],
422 custom_contexts: &[],
423 },
424 CommandDef {
425 name_key: "cmd.set_mark",
426 desc_key: "cmd.set_mark_desc",
427 action: || Action::SetMark,
428 contexts: &[Normal],
429 custom_contexts: &[],
430 },
431 CommandDef {
433 name_key: "cmd.select_all",
434 desc_key: "cmd.select_all_desc",
435 action: || Action::SelectAll,
436 contexts: &[Normal],
437 custom_contexts: &[],
438 },
439 CommandDef {
440 name_key: "cmd.select_word",
441 desc_key: "cmd.select_word_desc",
442 action: || Action::SelectWord,
443 contexts: &[Normal],
444 custom_contexts: &[],
445 },
446 CommandDef {
447 name_key: "cmd.select_line",
448 desc_key: "cmd.select_line_desc",
449 action: || Action::SelectLine,
450 contexts: &[Normal],
451 custom_contexts: &[],
452 },
453 CommandDef {
454 name_key: "cmd.expand_selection",
455 desc_key: "cmd.expand_selection_desc",
456 action: || Action::ExpandSelection,
457 contexts: &[Normal],
458 custom_contexts: &[],
459 },
460 CommandDef {
462 name_key: "cmd.add_cursor_above",
463 desc_key: "cmd.add_cursor_above_desc",
464 action: || Action::AddCursorAbove,
465 contexts: &[Normal],
466 custom_contexts: &[],
467 },
468 CommandDef {
469 name_key: "cmd.add_cursor_below",
470 desc_key: "cmd.add_cursor_below_desc",
471 action: || Action::AddCursorBelow,
472 contexts: &[Normal],
473 custom_contexts: &[],
474 },
475 CommandDef {
476 name_key: "cmd.add_cursor_next_match",
477 desc_key: "cmd.add_cursor_next_match_desc",
478 action: || Action::AddCursorNextMatch,
479 contexts: &[Normal],
480 custom_contexts: &[],
481 },
482 CommandDef {
483 name_key: "cmd.add_cursors_to_line_ends",
484 desc_key: "cmd.add_cursors_to_line_ends_desc",
485 action: || Action::AddCursorsToLineEnds,
486 contexts: &[Normal],
487 custom_contexts: &[],
488 },
489 CommandDef {
490 name_key: "cmd.remove_secondary_cursors",
491 desc_key: "cmd.remove_secondary_cursors_desc",
492 action: || Action::RemoveSecondaryCursors,
493 contexts: &[Normal],
494 custom_contexts: &[],
495 },
496 CommandDef {
498 name_key: "cmd.next_buffer",
499 desc_key: "cmd.next_buffer_desc",
500 action: || Action::NextBuffer,
501 contexts: &[Normal, Terminal],
502 custom_contexts: &[],
503 },
504 CommandDef {
505 name_key: "cmd.previous_buffer",
506 desc_key: "cmd.previous_buffer_desc",
507 action: || Action::PrevBuffer,
508 contexts: &[Normal, Terminal],
509 custom_contexts: &[],
510 },
511 CommandDef {
512 name_key: "cmd.switch_to_previous_tab",
513 desc_key: "cmd.switch_to_previous_tab_desc",
514 action: || Action::SwitchToPreviousTab,
515 contexts: &[Normal, Terminal],
516 custom_contexts: &[],
517 },
518 CommandDef {
519 name_key: "cmd.switch_to_tab_by_name",
520 desc_key: "cmd.switch_to_tab_by_name_desc",
521 action: || Action::SwitchToTabByName,
522 contexts: &[Normal, Terminal],
523 custom_contexts: &[],
524 },
525 CommandDef {
527 name_key: "cmd.split_horizontal",
528 desc_key: "cmd.split_horizontal_desc",
529 action: || Action::SplitHorizontal,
530 contexts: &[Normal, Terminal],
531 custom_contexts: &[],
532 },
533 CommandDef {
534 name_key: "cmd.split_vertical",
535 desc_key: "cmd.split_vertical_desc",
536 action: || Action::SplitVertical,
537 contexts: &[Normal, Terminal],
538 custom_contexts: &[],
539 },
540 CommandDef {
541 name_key: "cmd.close_split",
542 desc_key: "cmd.close_split_desc",
543 action: || Action::CloseSplit,
544 contexts: &[Normal, Terminal],
545 custom_contexts: &[],
546 },
547 CommandDef {
548 name_key: "cmd.next_split",
549 desc_key: "cmd.next_split_desc",
550 action: || Action::NextSplit,
551 contexts: &[Normal, Terminal],
552 custom_contexts: &[],
553 },
554 CommandDef {
555 name_key: "cmd.previous_split",
556 desc_key: "cmd.previous_split_desc",
557 action: || Action::PrevSplit,
558 contexts: &[Normal, Terminal],
559 custom_contexts: &[],
560 },
561 CommandDef {
562 name_key: "cmd.next_window",
563 desc_key: "cmd.next_window_desc",
564 action: || Action::NextWindow,
565 contexts: &[],
566 custom_contexts: &[],
567 },
568 CommandDef {
569 name_key: "cmd.previous_window",
570 desc_key: "cmd.previous_window_desc",
571 action: || Action::PrevWindow,
572 contexts: &[],
573 custom_contexts: &[],
574 },
575 CommandDef {
576 name_key: "cmd.increase_split_size",
577 desc_key: "cmd.increase_split_size_desc",
578 action: || Action::IncreaseSplitSize,
579 contexts: &[Normal, Terminal],
580 custom_contexts: &[],
581 },
582 CommandDef {
583 name_key: "cmd.decrease_split_size",
584 desc_key: "cmd.decrease_split_size_desc",
585 action: || Action::DecreaseSplitSize,
586 contexts: &[Normal, Terminal],
587 custom_contexts: &[],
588 },
589 CommandDef {
590 name_key: "cmd.toggle_maximize_split",
591 desc_key: "cmd.toggle_maximize_split_desc",
592 action: || Action::ToggleMaximizeSplit,
593 contexts: &[Normal, Terminal],
594 custom_contexts: &[],
595 },
596 CommandDef {
598 name_key: "cmd.toggle_line_numbers",
599 desc_key: "cmd.toggle_line_numbers_desc",
600 action: || Action::ToggleLineNumbers,
601 contexts: &[Normal],
602 custom_contexts: &[],
603 },
604 CommandDef {
605 name_key: "cmd.toggle_scroll_sync",
606 desc_key: "cmd.toggle_scroll_sync_desc",
607 action: || Action::ToggleScrollSync,
608 contexts: &[Normal],
609 custom_contexts: &[],
610 },
611 CommandDef {
612 name_key: "cmd.toggle_fold",
613 desc_key: "cmd.toggle_fold_desc",
614 action: || Action::ToggleFold,
615 contexts: &[Normal],
616 custom_contexts: &[],
617 },
618 CommandDef {
619 name_key: "cmd.debug_toggle_highlight",
620 desc_key: "cmd.debug_toggle_highlight_desc",
621 action: || Action::ToggleDebugHighlights,
622 contexts: &[Normal],
623 custom_contexts: &[],
624 },
625 CommandDef {
627 name_key: "cmd.add_ruler",
628 desc_key: "cmd.add_ruler_desc",
629 action: || Action::AddRuler,
630 contexts: &[Normal],
631 custom_contexts: &[],
632 },
633 CommandDef {
634 name_key: "cmd.remove_ruler",
635 desc_key: "cmd.remove_ruler_desc",
636 action: || Action::RemoveRuler,
637 contexts: &[Normal],
638 custom_contexts: &[],
639 },
640 CommandDef {
642 name_key: "cmd.set_tab_size",
643 desc_key: "cmd.set_tab_size_desc",
644 action: || Action::SetTabSize,
645 contexts: &[Normal],
646 custom_contexts: &[],
647 },
648 CommandDef {
649 name_key: "cmd.set_line_ending",
650 desc_key: "cmd.set_line_ending_desc",
651 action: || Action::SetLineEnding,
652 contexts: &[Normal],
653 custom_contexts: &[],
654 },
655 CommandDef {
656 name_key: "cmd.set_encoding",
657 desc_key: "cmd.set_encoding_desc",
658 action: || Action::SetEncoding,
659 contexts: &[Normal],
660 custom_contexts: &[],
661 },
662 CommandDef {
663 name_key: "cmd.reload_with_encoding",
664 desc_key: "cmd.reload_with_encoding_desc",
665 action: || Action::ReloadWithEncoding,
666 contexts: &[Normal],
667 custom_contexts: &[],
668 },
669 CommandDef {
670 name_key: "cmd.set_language",
671 desc_key: "cmd.set_language_desc",
672 action: || Action::SetLanguage,
673 contexts: &[Normal],
674 custom_contexts: &[],
675 },
676 CommandDef {
677 name_key: "cmd.toggle_indentation",
678 desc_key: "cmd.toggle_indentation_desc",
679 action: || Action::ToggleIndentationStyle,
680 contexts: &[Normal],
681 custom_contexts: &[],
682 },
683 CommandDef {
684 name_key: "cmd.toggle_tab_indicators",
685 desc_key: "cmd.toggle_tab_indicators_desc",
686 action: || Action::ToggleTabIndicators,
687 contexts: &[Normal],
688 custom_contexts: &[],
689 },
690 CommandDef {
691 name_key: "cmd.toggle_whitespace_indicators",
692 desc_key: "cmd.toggle_whitespace_indicators_desc",
693 action: || Action::ToggleWhitespaceIndicators,
694 contexts: &[Normal],
695 custom_contexts: &[],
696 },
697 CommandDef {
698 name_key: "cmd.reset_buffer_settings",
699 desc_key: "cmd.reset_buffer_settings_desc",
700 action: || Action::ResetBufferSettings,
701 contexts: &[Normal],
702 custom_contexts: &[],
703 },
704 CommandDef {
705 name_key: "cmd.scroll_up",
706 desc_key: "cmd.scroll_up_desc",
707 action: || Action::ScrollUp,
708 contexts: &[Normal],
709 custom_contexts: &[],
710 },
711 CommandDef {
712 name_key: "cmd.scroll_down",
713 desc_key: "cmd.scroll_down_desc",
714 action: || Action::ScrollDown,
715 contexts: &[Normal],
716 custom_contexts: &[],
717 },
718 CommandDef {
719 name_key: "cmd.scroll_tabs_left",
720 desc_key: "cmd.scroll_tabs_left_desc",
721 action: || Action::ScrollTabsLeft,
722 contexts: &[Normal, Terminal],
723 custom_contexts: &[],
724 },
725 CommandDef {
726 name_key: "cmd.scroll_tabs_right",
727 desc_key: "cmd.scroll_tabs_right_desc",
728 action: || Action::ScrollTabsRight,
729 contexts: &[Normal, Terminal],
730 custom_contexts: &[],
731 },
732 CommandDef {
733 name_key: "cmd.toggle_mouse_support",
734 desc_key: "cmd.toggle_mouse_support_desc",
735 action: || Action::ToggleMouseCapture,
736 contexts: &[Normal, Terminal],
737 custom_contexts: &[],
738 },
739 CommandDef {
741 name_key: "cmd.toggle_file_explorer",
742 desc_key: "cmd.toggle_file_explorer_desc",
743 action: || Action::ToggleFileExplorer,
744 contexts: &[Normal, FileExplorer, Terminal],
745 custom_contexts: &[],
746 },
747 CommandDef {
748 name_key: "cmd.toggle_file_explorer_side",
749 desc_key: "cmd.toggle_file_explorer_side_desc",
750 action: || Action::ToggleFileExplorerSide,
751 contexts: &[Normal, FileExplorer, Terminal],
752 custom_contexts: &[],
753 },
754 CommandDef {
755 name_key: "cmd.toggle_menu_bar",
756 desc_key: "cmd.toggle_menu_bar_desc",
757 action: || Action::ToggleMenuBar,
758 contexts: &[Normal, FileExplorer, Terminal],
759 custom_contexts: &[],
760 },
761 CommandDef {
762 name_key: "cmd.toggle_tab_bar",
763 desc_key: "cmd.toggle_tab_bar_desc",
764 action: || Action::ToggleTabBar,
765 contexts: &[Normal, FileExplorer, Terminal],
766 custom_contexts: &[],
767 },
768 CommandDef {
769 name_key: "cmd.toggle_status_bar",
770 desc_key: "cmd.toggle_status_bar_desc",
771 action: || Action::ToggleStatusBar,
772 contexts: &[Normal, FileExplorer, Terminal],
773 custom_contexts: &[],
774 },
775 CommandDef {
776 name_key: "cmd.toggle_prompt_line",
777 desc_key: "cmd.toggle_prompt_line_desc",
778 action: || Action::TogglePromptLine,
779 contexts: &[Normal, FileExplorer, Terminal],
780 custom_contexts: &[],
781 },
782 CommandDef {
783 name_key: "cmd.toggle_vertical_scrollbar",
784 desc_key: "cmd.toggle_vertical_scrollbar_desc",
785 action: || Action::ToggleVerticalScrollbar,
786 contexts: &[Normal, FileExplorer, Terminal],
787 custom_contexts: &[],
788 },
789 CommandDef {
790 name_key: "cmd.toggle_horizontal_scrollbar",
791 desc_key: "cmd.toggle_horizontal_scrollbar_desc",
792 action: || Action::ToggleHorizontalScrollbar,
793 contexts: &[Normal, FileExplorer, Terminal],
794 custom_contexts: &[],
795 },
796 CommandDef {
797 name_key: "cmd.focus_file_explorer",
798 desc_key: "cmd.focus_file_explorer_desc",
799 action: || Action::FocusFileExplorer,
800 contexts: &[Normal, Terminal],
801 custom_contexts: &[],
802 },
803 CommandDef {
804 name_key: "cmd.focus_editor",
805 desc_key: "cmd.focus_editor_desc",
806 action: || Action::FocusEditor,
807 contexts: &[FileExplorer],
808 custom_contexts: &[],
809 },
810 CommandDef {
811 name_key: "cmd.toggle_dock_focus",
812 desc_key: "cmd.toggle_dock_focus_desc",
813 action: || Action::ToggleDockFocus,
814 contexts: &[Normal, FileExplorer, Terminal, CompositeBuffer, Dock],
815 custom_contexts: &[],
816 },
817 CommandDef {
818 name_key: "cmd.explorer_refresh",
819 desc_key: "cmd.explorer_refresh_desc",
820 action: || Action::FileExplorerRefresh,
821 contexts: &[FileExplorer],
822 custom_contexts: &[],
823 },
824 CommandDef {
825 name_key: "cmd.explorer_new_file",
826 desc_key: "cmd.explorer_new_file_desc",
827 action: || Action::FileExplorerNewFile,
828 contexts: &[FileExplorer],
829 custom_contexts: &[],
830 },
831 CommandDef {
832 name_key: "cmd.explorer_new_directory",
833 desc_key: "cmd.explorer_new_directory_desc",
834 action: || Action::FileExplorerNewDirectory,
835 contexts: &[FileExplorer],
836 custom_contexts: &[],
837 },
838 CommandDef {
839 name_key: "cmd.explorer_delete",
840 desc_key: "cmd.explorer_delete_desc",
841 action: || Action::FileExplorerDelete,
842 contexts: &[FileExplorer],
843 custom_contexts: &[],
844 },
845 CommandDef {
846 name_key: "cmd.explorer_rename",
847 desc_key: "cmd.explorer_rename_desc",
848 action: || Action::FileExplorerRename,
849 contexts: &[FileExplorer],
850 custom_contexts: &[],
851 },
852 CommandDef {
853 name_key: "cmd.toggle_hidden_files",
854 desc_key: "cmd.toggle_hidden_files_desc",
855 action: || Action::FileExplorerToggleHidden,
856 contexts: &[FileExplorer],
857 custom_contexts: &[],
858 },
859 CommandDef {
860 name_key: "cmd.toggle_gitignored_files",
861 desc_key: "cmd.toggle_gitignored_files_desc",
862 action: || Action::FileExplorerToggleGitignored,
863 contexts: &[FileExplorer],
864 custom_contexts: &[],
865 },
866 CommandDef {
868 name_key: "cmd.toggle_line_wrap",
869 desc_key: "cmd.toggle_line_wrap_desc",
870 action: || Action::ToggleLineWrap,
871 contexts: &[Normal],
872 custom_contexts: &[],
873 },
874 CommandDef {
875 name_key: "cmd.toggle_current_line_highlight",
876 desc_key: "cmd.toggle_current_line_highlight_desc",
877 action: || Action::ToggleCurrentLineHighlight,
878 contexts: &[Normal],
879 custom_contexts: &[],
880 },
881 CommandDef {
882 name_key: "cmd.toggle_page_view",
883 desc_key: "cmd.toggle_page_view_desc",
884 action: || Action::TogglePageView,
885 contexts: &[Normal],
886 custom_contexts: &[],
887 },
888 CommandDef {
889 name_key: "cmd.set_page_width",
890 desc_key: "cmd.set_page_width_desc",
891 action: || Action::SetPageWidth,
892 contexts: &[Normal],
893 custom_contexts: &[],
894 },
895 CommandDef {
896 name_key: "cmd.toggle_read_only",
897 desc_key: "cmd.toggle_read_only_desc",
898 action: || Action::ToggleReadOnly,
899 contexts: &[Normal],
900 custom_contexts: &[],
901 },
902 CommandDef {
903 name_key: "cmd.set_background",
904 desc_key: "cmd.set_background_desc",
905 action: || Action::SetBackground,
906 contexts: &[Normal],
907 custom_contexts: &[],
908 },
909 CommandDef {
910 name_key: "cmd.set_background_blend",
911 desc_key: "cmd.set_background_blend_desc",
912 action: || Action::SetBackgroundBlend,
913 contexts: &[Normal],
914 custom_contexts: &[],
915 },
916 CommandDef {
918 name_key: "cmd.search",
919 desc_key: "cmd.search_desc",
920 action: || Action::Search,
921 contexts: &[Normal],
922 custom_contexts: &[],
923 },
924 CommandDef {
925 name_key: "cmd.find_in_selection",
926 desc_key: "cmd.find_in_selection_desc",
927 action: || Action::FindInSelection,
928 contexts: &[Normal],
929 custom_contexts: &[],
930 },
931 CommandDef {
932 name_key: "cmd.find_next",
933 desc_key: "cmd.find_next_desc",
934 action: || Action::FindNext,
935 contexts: &[Normal],
936 custom_contexts: &[],
937 },
938 CommandDef {
939 name_key: "cmd.find_previous",
940 desc_key: "cmd.find_previous_desc",
941 action: || Action::FindPrevious,
942 contexts: &[Normal],
943 custom_contexts: &[],
944 },
945 CommandDef {
946 name_key: "cmd.find_selection_next",
947 desc_key: "cmd.find_selection_next_desc",
948 action: || Action::FindSelectionNext,
949 contexts: &[Normal],
950 custom_contexts: &[],
951 },
952 CommandDef {
953 name_key: "cmd.find_selection_previous",
954 desc_key: "cmd.find_selection_previous_desc",
955 action: || Action::FindSelectionPrevious,
956 contexts: &[Normal],
957 custom_contexts: &[],
958 },
959 CommandDef {
960 name_key: "cmd.replace",
961 desc_key: "cmd.replace_desc",
962 action: || Action::Replace,
963 contexts: &[Normal],
964 custom_contexts: &[],
965 },
966 CommandDef {
967 name_key: "cmd.query_replace",
968 desc_key: "cmd.query_replace_desc",
969 action: || Action::QueryReplace,
970 contexts: &[Normal],
971 custom_contexts: &[],
972 },
973 CommandDef {
975 name_key: "cmd.goto_line",
976 desc_key: "cmd.goto_line_desc",
977 action: || Action::GotoLine,
978 contexts: &[Normal],
979 custom_contexts: &[],
980 },
981 CommandDef {
982 name_key: "cmd.scan_line_index",
983 desc_key: "cmd.scan_line_index_desc",
984 action: || Action::ScanLineIndex,
985 contexts: &[Normal],
986 custom_contexts: &[],
987 },
988 CommandDef {
989 name_key: "cmd.smart_home",
990 desc_key: "cmd.smart_home_desc",
991 action: || Action::SmartHome,
992 contexts: &[Normal],
993 custom_contexts: &[],
994 },
995 CommandDef {
996 name_key: "cmd.show_completions",
997 desc_key: "cmd.show_completions_desc",
998 action: || Action::LspCompletion,
999 contexts: &[Normal],
1000 custom_contexts: &[],
1001 },
1002 CommandDef {
1003 name_key: "cmd.goto_definition",
1004 desc_key: "cmd.goto_definition_desc",
1005 action: || Action::LspGotoDefinition,
1006 contexts: &[Normal],
1007 custom_contexts: &[],
1008 },
1009 CommandDef {
1010 name_key: "cmd.show_hover_info",
1011 desc_key: "cmd.show_hover_info_desc",
1012 action: || Action::LspHover,
1013 contexts: &[Normal],
1014 custom_contexts: &[],
1015 },
1016 CommandDef {
1017 name_key: "cmd.find_references",
1018 desc_key: "cmd.find_references_desc",
1019 action: || Action::LspReferences,
1020 contexts: &[Normal],
1021 custom_contexts: &[],
1022 },
1023 CommandDef {
1024 name_key: "cmd.show_signature_help",
1025 desc_key: "cmd.show_signature_help_desc",
1026 action: || Action::LspSignatureHelp,
1027 contexts: &[Normal],
1028 custom_contexts: &[],
1029 },
1030 CommandDef {
1031 name_key: "cmd.code_actions",
1032 desc_key: "cmd.code_actions_desc",
1033 action: || Action::LspCodeActions,
1034 contexts: &[Normal],
1035 custom_contexts: &[],
1036 },
1037 CommandDef {
1038 name_key: "cmd.start_restart_lsp",
1039 desc_key: "cmd.start_restart_lsp_desc",
1040 action: || Action::LspRestart,
1041 contexts: &[Normal],
1042 custom_contexts: &[],
1043 },
1044 CommandDef {
1045 name_key: "cmd.stop_lsp",
1046 desc_key: "cmd.stop_lsp_desc",
1047 action: || Action::LspStop,
1048 contexts: &[Normal],
1049 custom_contexts: &[],
1050 },
1051 CommandDef {
1052 name_key: "cmd.toggle_lsp_for_buffer",
1053 desc_key: "cmd.toggle_lsp_for_buffer_desc",
1054 action: || Action::LspToggleForBuffer,
1055 contexts: &[Normal],
1056 custom_contexts: &[],
1057 },
1058 CommandDef {
1059 name_key: "cmd.toggle_mouse_hover",
1060 desc_key: "cmd.toggle_mouse_hover_desc",
1061 action: || Action::ToggleMouseHover,
1062 contexts: &[],
1063 custom_contexts: &[],
1064 },
1065 CommandDef {
1066 name_key: "cmd.navigate_back",
1067 desc_key: "cmd.navigate_back_desc",
1068 action: || Action::NavigateBack,
1069 contexts: &[Normal],
1070 custom_contexts: &[],
1071 },
1072 CommandDef {
1073 name_key: "cmd.navigate_forward",
1074 desc_key: "cmd.navigate_forward_desc",
1075 action: || Action::NavigateForward,
1076 contexts: &[Normal],
1077 custom_contexts: &[],
1078 },
1079 CommandDef {
1081 name_key: "cmd.toggle_comment",
1082 desc_key: "cmd.toggle_comment_desc",
1083 action: || Action::ToggleComment,
1084 contexts: &[Normal],
1085 custom_contexts: &[],
1086 },
1087 CommandDef {
1088 name_key: "cmd.dedent_selection",
1089 desc_key: "cmd.dedent_selection_desc",
1090 action: || Action::DedentSelection,
1091 contexts: &[Normal],
1092 custom_contexts: &[],
1093 },
1094 CommandDef {
1095 name_key: "cmd.goto_matching_bracket",
1096 desc_key: "cmd.goto_matching_bracket_desc",
1097 action: || Action::GoToMatchingBracket,
1098 contexts: &[Normal],
1099 custom_contexts: &[],
1100 },
1101 CommandDef {
1103 name_key: "cmd.jump_to_next_error",
1104 desc_key: "cmd.jump_to_next_error_desc",
1105 action: || Action::JumpToNextError,
1106 contexts: &[Normal],
1107 custom_contexts: &[],
1108 },
1109 CommandDef {
1110 name_key: "cmd.jump_to_previous_error",
1111 desc_key: "cmd.jump_to_previous_error_desc",
1112 action: || Action::JumpToPreviousError,
1113 contexts: &[Normal],
1114 custom_contexts: &[],
1115 },
1116 CommandDef {
1118 name_key: "cmd.rename_symbol",
1119 desc_key: "cmd.rename_symbol_desc",
1120 action: || Action::LspRename,
1121 contexts: &[Normal],
1122 custom_contexts: &[],
1123 },
1124 CommandDef {
1126 name_key: "cmd.list_bookmarks",
1127 desc_key: "cmd.list_bookmarks_desc",
1128 action: || Action::ListBookmarks,
1129 contexts: &[Normal],
1130 custom_contexts: &[],
1131 },
1132 CommandDef {
1133 name_key: "cmd.list_macros",
1134 desc_key: "cmd.list_macros_desc",
1135 action: || Action::ListMacros,
1136 contexts: &[Normal],
1137 custom_contexts: &[],
1138 },
1139 CommandDef {
1140 name_key: "cmd.record_macro",
1141 desc_key: "cmd.record_macro_desc",
1142 action: || Action::PromptRecordMacro,
1143 contexts: &[Normal],
1144 custom_contexts: &[],
1145 },
1146 CommandDef {
1147 name_key: "cmd.stop_recording_macro",
1148 desc_key: "cmd.stop_recording_macro_desc",
1149 action: || Action::StopMacroRecording,
1150 contexts: &[Normal],
1151 custom_contexts: &[],
1152 },
1153 CommandDef {
1154 name_key: "cmd.play_macro",
1155 desc_key: "cmd.play_macro_desc",
1156 action: || Action::PromptPlayMacro,
1157 contexts: &[Normal],
1158 custom_contexts: &[],
1159 },
1160 CommandDef {
1161 name_key: "cmd.play_last_macro",
1162 desc_key: "cmd.play_last_macro_desc",
1163 action: || Action::PlayLastMacro,
1164 contexts: &[Normal],
1165 custom_contexts: &[],
1166 },
1167 CommandDef {
1168 name_key: "cmd.set_bookmark",
1169 desc_key: "cmd.set_bookmark_desc",
1170 action: || Action::PromptSetBookmark,
1171 contexts: &[Normal],
1172 custom_contexts: &[],
1173 },
1174 CommandDef {
1175 name_key: "cmd.jump_to_bookmark",
1176 desc_key: "cmd.jump_to_bookmark_desc",
1177 action: || Action::PromptJumpToBookmark,
1178 contexts: &[Normal],
1179 custom_contexts: &[],
1180 },
1181 CommandDef {
1183 name_key: "cmd.show_manual",
1184 desc_key: "cmd.show_manual_desc",
1185 action: || Action::ShowHelp,
1186 contexts: &[],
1187 custom_contexts: &[],
1188 },
1189 CommandDef {
1190 name_key: "cmd.show_keyboard_shortcuts",
1191 desc_key: "cmd.show_keyboard_shortcuts_desc",
1192 action: || Action::ShowKeyboardShortcuts,
1193 contexts: &[],
1194 custom_contexts: &[],
1195 },
1196 CommandDef {
1197 name_key: "cmd.show_warnings",
1198 desc_key: "cmd.show_warnings_desc",
1199 action: || Action::ShowWarnings,
1200 contexts: &[],
1201 custom_contexts: &[],
1202 },
1203 CommandDef {
1204 name_key: "cmd.show_lsp_status",
1205 desc_key: "cmd.show_lsp_status_desc",
1206 action: || Action::ShowLspStatus,
1207 contexts: &[],
1208 custom_contexts: &[],
1209 },
1210 CommandDef {
1211 name_key: "cmd.show_remote_indicator_menu",
1212 desc_key: "cmd.show_remote_indicator_menu_desc",
1213 action: || Action::ShowRemoteIndicatorMenu,
1214 contexts: &[],
1215 custom_contexts: &[],
1216 },
1217 CommandDef {
1218 name_key: "cmd.clear_warnings",
1219 desc_key: "cmd.clear_warnings_desc",
1220 action: || Action::ClearWarnings,
1221 contexts: &[],
1222 custom_contexts: &[],
1223 },
1224 CommandDef {
1226 name_key: "cmd.dump_config",
1227 desc_key: "cmd.dump_config_desc",
1228 action: || Action::DumpConfig,
1229 contexts: &[],
1230 custom_contexts: &[],
1231 },
1232 CommandDef {
1233 name_key: "cmd.redraw_screen",
1234 desc_key: "cmd.redraw_screen_desc",
1235 action: || Action::RedrawScreen,
1236 contexts: &[],
1237 custom_contexts: &[],
1238 },
1239 CommandDef {
1240 name_key: "cmd.toggle_inlay_hints",
1241 desc_key: "cmd.toggle_inlay_hints_desc",
1242 action: || Action::ToggleInlayHints,
1243 contexts: &[Normal],
1244 custom_contexts: &[],
1245 },
1246 CommandDef {
1248 name_key: "cmd.select_theme",
1249 desc_key: "cmd.select_theme_desc",
1250 action: || Action::SelectTheme,
1251 contexts: &[],
1252 custom_contexts: &[],
1253 },
1254 CommandDef {
1256 name_key: "cmd.inspect_theme_at_cursor",
1257 desc_key: "cmd.inspect_theme_at_cursor_desc",
1258 action: || Action::InspectThemeAtCursor,
1259 contexts: &[Normal],
1260 custom_contexts: &[],
1261 },
1262 CommandDef {
1264 name_key: "cmd.select_keybinding_map",
1265 desc_key: "cmd.select_keybinding_map_desc",
1266 action: || Action::SelectKeybindingMap,
1267 contexts: &[],
1268 custom_contexts: &[],
1269 },
1270 CommandDef {
1272 name_key: "cmd.select_cursor_style",
1273 desc_key: "cmd.select_cursor_style_desc",
1274 action: || Action::SelectCursorStyle,
1275 contexts: &[],
1276 custom_contexts: &[],
1277 },
1278 CommandDef {
1280 name_key: "cmd.select_locale",
1281 desc_key: "cmd.select_locale_desc",
1282 action: || Action::SelectLocale,
1283 contexts: &[],
1284 custom_contexts: &[],
1285 },
1286 CommandDef {
1288 name_key: "cmd.open_settings",
1289 desc_key: "cmd.open_settings_desc",
1290 action: || Action::OpenSettings,
1291 contexts: &[],
1292 custom_contexts: &[],
1293 },
1294 CommandDef {
1296 name_key: "cmd.open_keybinding_editor",
1297 desc_key: "cmd.open_keybinding_editor_desc",
1298 action: || Action::OpenKeybindingEditor,
1299 contexts: &[],
1300 custom_contexts: &[],
1301 },
1302 CommandDef {
1304 name_key: "cmd.calibrate_input",
1305 desc_key: "cmd.calibrate_input_desc",
1306 action: || Action::CalibrateInput,
1307 contexts: &[],
1308 custom_contexts: &[],
1309 },
1310 CommandDef {
1312 name_key: "cmd.open_terminal",
1313 desc_key: "cmd.open_terminal_desc",
1314 action: || Action::OpenTerminal,
1315 contexts: &[],
1316 custom_contexts: &[],
1317 },
1318 CommandDef {
1319 name_key: "cmd.focus_terminal",
1320 desc_key: "cmd.focus_terminal_desc",
1321 action: || Action::FocusTerminal,
1322 contexts: &[Normal],
1323 custom_contexts: &[],
1324 },
1325 CommandDef {
1326 name_key: "cmd.exit_terminal_mode",
1327 desc_key: "cmd.exit_terminal_mode_desc",
1328 action: || Action::TerminalEscape,
1329 contexts: &[Terminal],
1330 custom_contexts: &[],
1331 },
1332 CommandDef {
1333 name_key: "cmd.toggle_keyboard_capture",
1334 desc_key: "cmd.toggle_keyboard_capture_desc",
1335 action: || Action::ToggleKeyboardCapture,
1336 contexts: &[Terminal],
1337 custom_contexts: &[],
1338 },
1339 CommandDef {
1341 name_key: "cmd.shell_command",
1342 desc_key: "cmd.shell_command_desc",
1343 action: || Action::ShellCommand,
1344 contexts: &[Normal],
1345 custom_contexts: &[],
1346 },
1347 CommandDef {
1348 name_key: "cmd.shell_command_replace",
1349 desc_key: "cmd.shell_command_replace_desc",
1350 action: || Action::ShellCommandReplace,
1351 contexts: &[Normal],
1352 custom_contexts: &[],
1353 },
1354 CommandDef {
1356 name_key: "cmd.event_debug",
1357 desc_key: "cmd.event_debug_desc",
1358 action: || Action::EventDebug,
1359 contexts: &[],
1360 custom_contexts: &[],
1361 },
1362 CommandDef {
1364 name_key: "cmd.suspend_process",
1365 desc_key: "cmd.suspend_process_desc",
1366 action: || Action::SuspendProcess,
1367 contexts: &[Normal, FileExplorer, Terminal],
1368 custom_contexts: &[],
1369 },
1370 CommandDef {
1372 name_key: "cmd.load_plugin_from_buffer",
1373 desc_key: "cmd.load_plugin_from_buffer_desc",
1374 action: || Action::LoadPluginFromBuffer,
1375 contexts: &[Normal],
1376 custom_contexts: &[],
1377 },
1378 CommandDef {
1380 name_key: "cmd.init_reload",
1381 desc_key: "cmd.init_reload_desc",
1382 action: || Action::InitReload,
1383 contexts: &[Normal],
1384 custom_contexts: &[],
1385 },
1386 CommandDef {
1387 name_key: "cmd.init_edit",
1388 desc_key: "cmd.init_edit_desc",
1389 action: || Action::InitEdit,
1390 contexts: &[Normal],
1391 custom_contexts: &[],
1392 },
1393 CommandDef {
1394 name_key: "cmd.init_check",
1395 desc_key: "cmd.init_check_desc",
1396 action: || Action::InitCheck,
1397 contexts: &[Normal],
1398 custom_contexts: &[],
1399 },
1400 CommandDef {
1405 name_key: "cmd.resume_live_grep",
1406 desc_key: "cmd.resume_live_grep_desc",
1407 action: || Action::ResumeLiveGrep,
1408 contexts: &[Normal],
1409 custom_contexts: &[],
1410 },
1411 CommandDef {
1412 name_key: "cmd.toggle_utility_dock",
1413 desc_key: "cmd.toggle_utility_dock_desc",
1414 action: || Action::ToggleUtilityDock,
1415 contexts: &[Normal],
1416 custom_contexts: &[],
1417 },
1418 CommandDef {
1419 name_key: "cmd.open_terminal_in_dock",
1420 desc_key: "cmd.open_terminal_in_dock_desc",
1421 action: || Action::OpenTerminalInDock,
1422 contexts: &[Normal],
1423 custom_contexts: &[],
1424 },
1425 CommandDef {
1428 name_key: "cmd.workspace_trust",
1429 desc_key: "cmd.workspace_trust_desc",
1430 action: || Action::WorkspaceTrustPrompt,
1431 contexts: &[],
1432 custom_contexts: &[],
1433 },
1434];
1435
1436pub fn get_all_commands() -> Vec<Command> {
1438 COMMAND_DEFS
1439 .iter()
1440 .map(|def| Command {
1441 name: t!(def.name_key).to_string(),
1442 description: t!(def.desc_key).to_string(),
1443 action: (def.action)(),
1444 contexts: def.contexts.to_vec(),
1445 custom_contexts: def.custom_contexts.iter().map(|s| s.to_string()).collect(),
1446 source: CommandSource::Builtin,
1447 terminal_bypass: false,
1451 })
1452 .collect()
1453}
1454
1455pub fn filter_commands(
1457 query: &str,
1458 current_context: KeyContext,
1459 keybinding_resolver: &crate::input::keybindings::KeybindingResolver,
1460) -> Vec<Suggestion> {
1461 let query_lower = query.to_lowercase();
1462 let commands = get_all_commands();
1463
1464 let is_available = |cmd: &Command| -> bool {
1466 cmd.contexts.is_empty() || cmd.contexts.contains(¤t_context)
1468 };
1469
1470 let matches_query = |cmd: &Command| -> bool {
1472 if query.is_empty() {
1473 return true;
1474 }
1475
1476 let name_lower = cmd.name.to_lowercase();
1477 let mut query_chars = query_lower.chars();
1478 let mut current_char = query_chars.next();
1479
1480 for name_char in name_lower.chars() {
1481 if let Some(qc) = current_char {
1482 if qc == name_char {
1483 current_char = query_chars.next();
1484 }
1485 } else {
1486 break;
1487 }
1488 }
1489
1490 current_char.is_none() };
1492
1493 let current_context_ref = ¤t_context;
1495 let mut suggestions: Vec<Suggestion> = commands
1496 .into_iter()
1497 .filter(|cmd| matches_query(cmd))
1498 .map(|cmd| {
1499 let available = is_available(&cmd);
1500 let keybinding = keybinding_resolver
1501 .get_keybinding_for_action(&cmd.action, current_context_ref.clone());
1502 Suggestion::new(cmd.name.clone())
1503 .with_description(cmd.description)
1504 .set_disabled(!available)
1505 .with_keybinding(keybinding)
1506 })
1507 .collect();
1508
1509 suggestions.sort_by_key(|s| s.disabled);
1511
1512 suggestions
1513}