1use std::{
2 cmp::{self},
3 ops::{Not as _, Range},
4 sync::Arc,
5 time::Duration,
6};
7
8use anyhow::{Context as _, anyhow};
9use collections::{HashMap, HashSet};
10use editor::{CompletionProvider, Editor, EditorEvent};
11use fs::Fs;
12use fuzzy::{StringMatch, StringMatchCandidate};
13use gpui::{
14 Action, AppContext as _, AsyncApp, Axis, ClickEvent, Context, DismissEvent, Entity,
15 EventEmitter, FocusHandle, Focusable, Global, IsZero, KeyContext, Keystroke, MouseButton,
16 Point, ScrollStrategy, ScrollWheelEvent, Stateful, StyledText, Subscription, Task,
17 TextStyleRefinement, WeakEntity, actions, anchored, deferred, div,
18};
19use language::{Language, LanguageConfig, ToOffset as _};
20use notifications::status_toast::{StatusToast, ToastIcon};
21use project::Project;
22use settings::{BaseKeymap, KeybindSource, KeymapFile, Settings as _, SettingsAssets};
23use ui::{
24 ActiveTheme as _, App, Banner, BorrowAppContext, ContextMenu, IconButtonShape, Indicator,
25 Modal, ModalFooter, ModalHeader, ParentElement as _, Render, Section, SharedString,
26 Styled as _, Tooltip, Window, prelude::*,
27};
28use ui_input::SingleLineInput;
29use util::ResultExt;
30use workspace::{
31 Item, ModalView, SerializableItem, Workspace, notifications::NotifyTaskExt as _,
32 register_serializable_item,
33};
34
35use crate::{
36 keybindings::persistence::KEYBINDING_EDITORS,
37 ui_components::{
38 keystroke_input::{ClearKeystrokes, KeystrokeInput, StartRecording, StopRecording},
39 table::{ColumnWidths, ResizeBehavior, Table, TableInteractionState},
40 },
41};
42
43const NO_ACTION_ARGUMENTS_TEXT: SharedString = SharedString::new_static("<no arguments>");
44
45actions!(
46 zed,
47 [
48 /// Opens the keymap editor.
49 OpenKeymapEditor
50 ]
51);
52
53actions!(
54 keymap_editor,
55 [
56 /// Edits the selected key binding.
57 EditBinding,
58 /// Creates a new key binding for the selected action.
59 CreateBinding,
60 /// Deletes the selected key binding.
61 DeleteBinding,
62 /// Copies the action name to clipboard.
63 CopyAction,
64 /// Copies the context predicate to clipboard.
65 CopyContext,
66 /// Toggles Conflict Filtering
67 ToggleConflictFilter,
68 /// Toggle Keystroke search
69 ToggleKeystrokeSearch,
70 /// Toggles exact matching for keystroke search
71 ToggleExactKeystrokeMatching,
72 /// Shows matching keystrokes for the currently selected binding
73 ShowMatchingKeybinds
74 ]
75);
76
77pub fn init(cx: &mut App) {
78 let keymap_event_channel = KeymapEventChannel::new();
79 cx.set_global(keymap_event_channel);
80
81 cx.on_action(|_: &OpenKeymapEditor, cx| {
82 workspace::with_active_or_new_workspace(cx, move |workspace, window, cx| {
83 workspace
84 .with_local_workspace(window, cx, |workspace, window, cx| {
85 let existing = workspace
86 .active_pane()
87 .read(cx)
88 .items()
89 .find_map(|item| item.downcast::<KeymapEditor>());
90
91 if let Some(existing) = existing {
92 workspace.activate_item(&existing, true, true, window, cx);
93 } else {
94 let keymap_editor =
95 cx.new(|cx| KeymapEditor::new(workspace.weak_handle(), window, cx));
96 workspace.add_item_to_active_pane(
97 Box::new(keymap_editor),
98 None,
99 true,
100 window,
101 cx,
102 );
103 }
104 })
105 .detach();
106 })
107 });
108
109 register_serializable_item::<KeymapEditor>(cx);
110}
111
112pub struct KeymapEventChannel {}
113
114impl Global for KeymapEventChannel {}
115
116impl KeymapEventChannel {
117 fn new() -> Self {
118 Self {}
119 }
120
121 pub fn trigger_keymap_changed(cx: &mut App) {
122 let Some(_event_channel) = cx.try_global::<Self>() else {
123 // don't panic if no global defined. This usually happens in tests
124 return;
125 };
126 cx.update_global(|_event_channel: &mut Self, _| {
127 /* triggers observers in KeymapEditors */
128 });
129 }
130}
131
132#[derive(Default, PartialEq)]
133enum SearchMode {
134 #[default]
135 Normal,
136 KeyStroke {
137 exact_match: bool,
138 },
139}
140
141impl SearchMode {
142 fn invert(&self) -> Self {
143 match self {
144 SearchMode::Normal => SearchMode::KeyStroke { exact_match: false },
145 SearchMode::KeyStroke { .. } => SearchMode::Normal,
146 }
147 }
148
149 fn exact_match(&self) -> bool {
150 match self {
151 SearchMode::Normal => false,
152 SearchMode::KeyStroke { exact_match } => *exact_match,
153 }
154 }
155}
156
157#[derive(Default, PartialEq, Copy, Clone)]
158enum FilterState {
159 #[default]
160 All,
161 Conflicts,
162}
163
164impl FilterState {
165 fn invert(&self) -> Self {
166 match self {
167 FilterState::All => FilterState::Conflicts,
168 FilterState::Conflicts => FilterState::All,
169 }
170 }
171}
172
173#[derive(Debug, Default, PartialEq, Eq, Clone, Hash)]
174struct ActionMapping {
175 keystrokes: Vec<Keystroke>,
176 context: Option<SharedString>,
177}
178
179#[derive(Debug)]
180struct KeybindConflict {
181 first_conflict_index: usize,
182 remaining_conflict_amount: usize,
183}
184
185impl KeybindConflict {
186 fn from_iter<'a>(mut indices: impl Iterator<Item = &'a ConflictOrigin>) -> Option<Self> {
187 indices.next().map(|origin| Self {
188 first_conflict_index: origin.index,
189 remaining_conflict_amount: indices.count(),
190 })
191 }
192}
193
194#[derive(Clone, Copy, PartialEq)]
195struct ConflictOrigin {
196 override_source: KeybindSource,
197 overridden_source: Option<KeybindSource>,
198 index: usize,
199}
200
201impl ConflictOrigin {
202 fn new(source: KeybindSource, index: usize) -> Self {
203 Self {
204 override_source: source,
205 index,
206 overridden_source: None,
207 }
208 }
209
210 fn with_overridden_source(self, source: KeybindSource) -> Self {
211 Self {
212 overridden_source: Some(source),
213 ..self
214 }
215 }
216
217 fn get_conflict_with(&self, other: &Self) -> Option<Self> {
218 if self.override_source == KeybindSource::User
219 && other.override_source == KeybindSource::User
220 {
221 Some(
222 Self::new(KeybindSource::User, other.index)
223 .with_overridden_source(self.override_source),
224 )
225 } else if self.override_source > other.override_source {
226 Some(other.with_overridden_source(self.override_source))
227 } else {
228 None
229 }
230 }
231
232 fn is_user_keybind_conflict(&self) -> bool {
233 self.override_source == KeybindSource::User
234 && self.overridden_source == Some(KeybindSource::User)
235 }
236}
237
238#[derive(Default)]
239struct ConflictState {
240 conflicts: Vec<Option<ConflictOrigin>>,
241 keybind_mapping: HashMap<ActionMapping, Vec<ConflictOrigin>>,
242 has_user_conflicts: bool,
243}
244
245impl ConflictState {
246 fn new(key_bindings: &[ProcessedBinding]) -> Self {
247 let mut action_keybind_mapping: HashMap<_, Vec<ConflictOrigin>> = HashMap::default();
248
249 let mut largest_index = 0;
250 for (index, binding) in key_bindings
251 .iter()
252 .enumerate()
253 .flat_map(|(index, binding)| Some(index).zip(binding.keybind_information()))
254 {
255 action_keybind_mapping
256 .entry(binding.get_action_mapping())
257 .or_default()
258 .push(ConflictOrigin::new(binding.source, index));
259 largest_index = index;
260 }
261
262 let mut conflicts = vec![None; largest_index + 1];
263 let mut has_user_conflicts = false;
264
265 for indices in action_keybind_mapping.values_mut() {
266 indices.sort_unstable_by_key(|origin| origin.override_source);
267 let Some((fst, snd)) = indices.get(0).zip(indices.get(1)) else {
268 continue;
269 };
270
271 for origin in indices.iter() {
272 conflicts[origin.index] =
273 origin.get_conflict_with(if origin == fst { &snd } else { &fst })
274 }
275
276 has_user_conflicts |= fst.override_source == KeybindSource::User
277 && snd.override_source == KeybindSource::User;
278 }
279
280 Self {
281 conflicts,
282 keybind_mapping: action_keybind_mapping,
283 has_user_conflicts,
284 }
285 }
286
287 fn conflicting_indices_for_mapping(
288 &self,
289 action_mapping: &ActionMapping,
290 keybind_idx: Option<usize>,
291 ) -> Option<KeybindConflict> {
292 self.keybind_mapping
293 .get(action_mapping)
294 .and_then(|indices| {
295 KeybindConflict::from_iter(
296 indices
297 .iter()
298 .filter(|&conflict| Some(conflict.index) != keybind_idx),
299 )
300 })
301 }
302
303 fn conflict_for_idx(&self, idx: usize) -> Option<ConflictOrigin> {
304 self.conflicts.get(idx).copied().flatten()
305 }
306
307 fn has_user_conflict(&self, candidate_idx: usize) -> bool {
308 self.conflict_for_idx(candidate_idx)
309 .is_some_and(|conflict| conflict.is_user_keybind_conflict())
310 }
311
312 fn any_user_binding_conflicts(&self) -> bool {
313 self.has_user_conflicts
314 }
315}
316
317struct KeymapEditor {
318 workspace: WeakEntity<Workspace>,
319 focus_handle: FocusHandle,
320 _keymap_subscription: Subscription,
321 keybindings: Vec<ProcessedBinding>,
322 keybinding_conflict_state: ConflictState,
323 filter_state: FilterState,
324 search_mode: SearchMode,
325 search_query_debounce: Option<Task<()>>,
326 // corresponds 1 to 1 with keybindings
327 string_match_candidates: Arc<Vec<StringMatchCandidate>>,
328 matches: Vec<StringMatch>,
329 table_interaction_state: Entity<TableInteractionState>,
330 filter_editor: Entity<Editor>,
331 keystroke_editor: Entity<KeystrokeInput>,
332 selected_index: Option<usize>,
333 context_menu: Option<(Entity<ContextMenu>, Point<Pixels>, Subscription)>,
334 previous_edit: Option<PreviousEdit>,
335 humanized_action_names: HumanizedActionNameCache,
336 current_widths: Entity<ColumnWidths<6>>,
337 show_hover_menus: bool,
338 /// In order for the JSON LSP to run in the actions arguments editor, we
339 /// require a backing file In order to avoid issues (primarily log spam)
340 /// with drop order between the buffer, file, worktree, etc, we create a
341 /// temporary directory for these backing files in the keymap editor struct
342 /// instead of here. This has the added benefit of only having to create a
343 /// worktree and directory once, although the perf improvement is negligible.
344 action_args_temp_dir_worktree: Option<Entity<project::Worktree>>,
345 action_args_temp_dir: Option<tempfile::TempDir>,
346}
347
348enum PreviousEdit {
349 /// When deleting, we want to maintain the same scroll position
350 ScrollBarOffset(Point<Pixels>),
351 /// When editing or creating, because the new keybinding could be in a different position in the sort order
352 /// we store metadata about the new binding (either the modified version or newly created one)
353 /// and upon reload, we search for this binding in the list of keybindings, and if we find the one that matches
354 /// this metadata, we set the selected index to it and scroll to it,
355 /// and if we don't find it, we scroll to 0 and don't set a selected index
356 Keybinding {
357 action_mapping: ActionMapping,
358 action_name: &'static str,
359 /// The scrollbar position to fallback to if we don't find the keybinding during a refresh
360 /// this can happen if there's a filter applied to the search and the keybinding modification
361 /// filters the binding from the search results
362 fallback: Point<Pixels>,
363 },
364}
365
366impl EventEmitter<()> for KeymapEditor {}
367
368impl Focusable for KeymapEditor {
369 fn focus_handle(&self, cx: &App) -> gpui::FocusHandle {
370 if self.selected_index.is_some() {
371 self.focus_handle.clone()
372 } else {
373 self.filter_editor.focus_handle(cx)
374 }
375 }
376}
377/// Helper function to check if two keystroke sequences match exactly
378fn keystrokes_match_exactly(keystrokes1: &[Keystroke], keystrokes2: &[Keystroke]) -> bool {
379 keystrokes1.len() == keystrokes2.len()
380 && keystrokes1
381 .iter()
382 .zip(keystrokes2)
383 .all(|(k1, k2)| k1.key == k2.key && k1.modifiers == k2.modifiers)
384}
385
386impl KeymapEditor {
387 fn new(workspace: WeakEntity<Workspace>, window: &mut Window, cx: &mut Context<Self>) -> Self {
388 let _keymap_subscription =
389 cx.observe_global_in::<KeymapEventChannel>(window, Self::on_keymap_changed);
390 let table_interaction_state = TableInteractionState::new(window, cx);
391
392 let keystroke_editor = cx.new(|cx| {
393 let mut keystroke_editor = KeystrokeInput::new(None, window, cx);
394 keystroke_editor.set_search(true);
395 keystroke_editor
396 });
397
398 let filter_editor = cx.new(|cx| {
399 let mut editor = Editor::single_line(window, cx);
400 editor.set_placeholder_text("Filter action names…", cx);
401 editor
402 });
403
404 cx.subscribe(&filter_editor, |this, _, e: &EditorEvent, cx| {
405 if !matches!(e, EditorEvent::BufferEdited) {
406 return;
407 }
408
409 this.on_query_changed(cx);
410 })
411 .detach();
412
413 cx.subscribe(&keystroke_editor, |this, _, _, cx| {
414 if matches!(this.search_mode, SearchMode::Normal) {
415 return;
416 }
417
418 this.on_query_changed(cx);
419 })
420 .detach();
421
422 cx.spawn({
423 let workspace = workspace.clone();
424 async move |this, cx| {
425 let temp_dir = tempfile::tempdir_in(paths::temp_dir())?;
426 let worktree = workspace
427 .update(cx, |ws, cx| {
428 ws.project()
429 .update(cx, |p, cx| p.create_worktree(temp_dir.path(), false, cx))
430 })?
431 .await?;
432 this.update(cx, |this, _| {
433 this.action_args_temp_dir = Some(temp_dir);
434 this.action_args_temp_dir_worktree = Some(worktree);
435 })
436 }
437 })
438 .detach();
439
440 let mut this = Self {
441 workspace,
442 keybindings: vec![],
443 keybinding_conflict_state: ConflictState::default(),
444 filter_state: FilterState::default(),
445 search_mode: SearchMode::default(),
446 string_match_candidates: Arc::new(vec![]),
447 matches: vec![],
448 focus_handle: cx.focus_handle(),
449 _keymap_subscription,
450 table_interaction_state,
451 filter_editor,
452 keystroke_editor,
453 selected_index: None,
454 context_menu: None,
455 previous_edit: None,
456 search_query_debounce: None,
457 humanized_action_names: HumanizedActionNameCache::new(cx),
458 show_hover_menus: true,
459 action_args_temp_dir: None,
460 action_args_temp_dir_worktree: None,
461 current_widths: cx.new(|cx| ColumnWidths::new(cx)),
462 };
463
464 this.on_keymap_changed(window, cx);
465
466 this
467 }
468
469 fn current_action_query(&self, cx: &App) -> String {
470 self.filter_editor.read(cx).text(cx)
471 }
472
473 fn current_keystroke_query(&self, cx: &App) -> Vec<Keystroke> {
474 match self.search_mode {
475 SearchMode::KeyStroke { .. } => self
476 .keystroke_editor
477 .read(cx)
478 .keystrokes()
479 .iter()
480 .cloned()
481 .collect(),
482 SearchMode::Normal => Default::default(),
483 }
484 }
485
486 fn on_query_changed(&mut self, cx: &mut Context<Self>) {
487 let action_query = self.current_action_query(cx);
488 let keystroke_query = self.current_keystroke_query(cx);
489 let exact_match = self.search_mode.exact_match();
490
491 let timer = cx.background_executor().timer(Duration::from_secs(1));
492 self.search_query_debounce = Some(cx.background_spawn({
493 let action_query = action_query.clone();
494 let keystroke_query = keystroke_query.clone();
495 async move {
496 timer.await;
497
498 let keystroke_query = keystroke_query
499 .into_iter()
500 .map(|keystroke| keystroke.unparse())
501 .collect::<Vec<String>>()
502 .join(" ");
503
504 telemetry::event!(
505 "Keystroke Search Completed",
506 action_query = action_query,
507 keystroke_query = keystroke_query,
508 keystroke_exact_match = exact_match
509 )
510 }
511 }));
512 cx.spawn(async move |this, cx| {
513 Self::update_matches(this.clone(), action_query, keystroke_query, cx).await?;
514 this.update(cx, |this, cx| {
515 this.scroll_to_item(0, ScrollStrategy::Top, cx)
516 })
517 })
518 .detach();
519 }
520
521 async fn update_matches(
522 this: WeakEntity<Self>,
523 action_query: String,
524 keystroke_query: Vec<Keystroke>,
525 cx: &mut AsyncApp,
526 ) -> anyhow::Result<()> {
527 let action_query = command_palette::normalize_action_query(&action_query);
528 let (string_match_candidates, keybind_count) = this.read_with(cx, |this, _| {
529 (this.string_match_candidates.clone(), this.keybindings.len())
530 })?;
531 let executor = cx.background_executor().clone();
532 let mut matches = fuzzy::match_strings(
533 &string_match_candidates,
534 &action_query,
535 true,
536 true,
537 keybind_count,
538 &Default::default(),
539 executor,
540 )
541 .await;
542 this.update(cx, |this, cx| {
543 match this.filter_state {
544 FilterState::Conflicts => {
545 matches.retain(|candidate| {
546 this.keybinding_conflict_state
547 .has_user_conflict(candidate.candidate_id)
548 });
549 }
550 FilterState::All => {}
551 }
552
553 match this.search_mode {
554 SearchMode::KeyStroke { exact_match } => {
555 matches.retain(|item| {
556 this.keybindings[item.candidate_id]
557 .keystrokes()
558 .is_some_and(|keystrokes| {
559 if exact_match {
560 keystrokes_match_exactly(&keystroke_query, keystrokes)
561 } else if keystroke_query.len() > keystrokes.len() {
562 return false;
563 } else {
564 for keystroke_offset in 0..keystrokes.len() {
565 let mut found_count = 0;
566 let mut query_cursor = 0;
567 let mut keystroke_cursor = keystroke_offset;
568 while query_cursor < keystroke_query.len()
569 && keystroke_cursor < keystrokes.len()
570 {
571 let query = &keystroke_query[query_cursor];
572 let keystroke = &keystrokes[keystroke_cursor];
573 let matches =
574 query.modifiers.is_subset_of(&keystroke.modifiers)
575 && ((query.key.is_empty()
576 || query.key == keystroke.key)
577 && query
578 .key_char
579 .as_ref()
580 .map_or(true, |q_kc| {
581 q_kc == &keystroke.key
582 }));
583 if matches {
584 found_count += 1;
585 query_cursor += 1;
586 }
587 keystroke_cursor += 1;
588 }
589
590 if found_count == keystroke_query.len() {
591 return true;
592 }
593 }
594 return false;
595 }
596 })
597 });
598 }
599 SearchMode::Normal => {}
600 }
601
602 if action_query.is_empty() {
603 matches.sort_by(|item1, item2| {
604 let binding1 = &this.keybindings[item1.candidate_id];
605 let binding2 = &this.keybindings[item2.candidate_id];
606
607 binding1.cmp(binding2)
608 });
609 }
610 this.selected_index.take();
611 this.matches = matches;
612
613 cx.notify();
614 })
615 }
616
617 fn get_conflict(&self, row_index: usize) -> Option<ConflictOrigin> {
618 self.matches.get(row_index).and_then(|candidate| {
619 self.keybinding_conflict_state
620 .conflict_for_idx(candidate.candidate_id)
621 })
622 }
623
624 fn process_bindings(
625 json_language: Arc<Language>,
626 zed_keybind_context_language: Arc<Language>,
627 humanized_action_names: &HumanizedActionNameCache,
628 cx: &mut App,
629 ) -> (Vec<ProcessedBinding>, Vec<StringMatchCandidate>) {
630 let key_bindings_ptr = cx.key_bindings();
631 let lock = key_bindings_ptr.borrow();
632 let key_bindings = lock.bindings();
633 let mut unmapped_action_names =
634 HashSet::from_iter(cx.all_action_names().into_iter().copied());
635 let action_documentation = cx.action_documentation();
636 let mut generator = KeymapFile::action_schema_generator();
637 let actions_with_schemas = HashSet::from_iter(
638 cx.action_schemas(&mut generator)
639 .into_iter()
640 .filter_map(|(name, schema)| schema.is_some().then_some(name)),
641 );
642
643 let mut processed_bindings = Vec::new();
644 let mut string_match_candidates = Vec::new();
645
646 for key_binding in key_bindings {
647 let source = key_binding
648 .meta()
649 .map(KeybindSource::from_meta)
650 .unwrap_or(KeybindSource::Unknown);
651
652 let keystroke_text = ui::text_for_keystrokes(key_binding.keystrokes(), cx);
653 let ui_key_binding = ui::KeyBinding::new_from_gpui(key_binding.clone(), cx)
654 .vim_mode(source == KeybindSource::Vim);
655
656 let context = key_binding
657 .predicate()
658 .map(|predicate| {
659 KeybindContextString::Local(
660 predicate.to_string().into(),
661 zed_keybind_context_language.clone(),
662 )
663 })
664 .unwrap_or(KeybindContextString::Global);
665
666 let action_name = key_binding.action().name();
667 unmapped_action_names.remove(&action_name);
668
669 let action_arguments = key_binding
670 .action_input()
671 .map(|arguments| SyntaxHighlightedText::new(arguments, json_language.clone()));
672 let action_information = ActionInformation::new(
673 action_name,
674 action_arguments,
675 &actions_with_schemas,
676 &action_documentation,
677 &humanized_action_names,
678 );
679
680 let index = processed_bindings.len();
681 let string_match_candidate =
682 StringMatchCandidate::new(index, &action_information.humanized_name);
683 processed_bindings.push(ProcessedBinding::new_mapped(
684 keystroke_text,
685 ui_key_binding,
686 context,
687 source,
688 action_information,
689 ));
690 string_match_candidates.push(string_match_candidate);
691 }
692
693 for action_name in unmapped_action_names.into_iter() {
694 let index = processed_bindings.len();
695 let action_information = ActionInformation::new(
696 action_name,
697 None,
698 &actions_with_schemas,
699 &action_documentation,
700 &humanized_action_names,
701 );
702 let string_match_candidate =
703 StringMatchCandidate::new(index, &action_information.humanized_name);
704
705 processed_bindings.push(ProcessedBinding::Unmapped(action_information));
706 string_match_candidates.push(string_match_candidate);
707 }
708
709 (processed_bindings, string_match_candidates)
710 }
711
712 fn on_keymap_changed(&mut self, window: &mut Window, cx: &mut Context<KeymapEditor>) {
713 let workspace = self.workspace.clone();
714 cx.spawn_in(window, async move |this, cx| {
715 let json_language = load_json_language(workspace.clone(), cx).await;
716 let zed_keybind_context_language =
717 load_keybind_context_language(workspace.clone(), cx).await;
718
719 let (action_query, keystroke_query) = this.update(cx, |this, cx| {
720 let (key_bindings, string_match_candidates) = Self::process_bindings(
721 json_language,
722 zed_keybind_context_language,
723 &this.humanized_action_names,
724 cx,
725 );
726
727 this.keybinding_conflict_state = ConflictState::new(&key_bindings);
728
729 this.keybindings = key_bindings;
730 this.string_match_candidates = Arc::new(string_match_candidates);
731 this.matches = this
732 .string_match_candidates
733 .iter()
734 .enumerate()
735 .map(|(ix, candidate)| StringMatch {
736 candidate_id: ix,
737 score: 0.0,
738 positions: vec![],
739 string: candidate.string.clone(),
740 })
741 .collect();
742 (
743 this.current_action_query(cx),
744 this.current_keystroke_query(cx),
745 )
746 })?;
747 // calls cx.notify
748 Self::update_matches(this.clone(), action_query, keystroke_query, cx).await?;
749 this.update_in(cx, |this, window, cx| {
750 if let Some(previous_edit) = this.previous_edit.take() {
751 match previous_edit {
752 // should remove scroll from process_query
753 PreviousEdit::ScrollBarOffset(offset) => {
754 this.table_interaction_state.update(cx, |table, _| {
755 table.set_scrollbar_offset(Axis::Vertical, offset)
756 })
757 // set selected index and scroll
758 }
759 PreviousEdit::Keybinding {
760 action_mapping,
761 action_name,
762 fallback,
763 } => {
764 let scroll_position =
765 this.matches.iter().enumerate().find_map(|(index, item)| {
766 let binding = &this.keybindings[item.candidate_id];
767 if binding.get_action_mapping().is_some_and(|binding_mapping| {
768 binding_mapping == action_mapping
769 }) && binding.action().name == action_name
770 {
771 Some(index)
772 } else {
773 None
774 }
775 });
776
777 if let Some(scroll_position) = scroll_position {
778 this.select_index(
779 scroll_position,
780 Some(ScrollStrategy::Top),
781 window,
782 cx,
783 );
784 } else {
785 this.table_interaction_state.update(cx, |table, _| {
786 table.set_scrollbar_offset(Axis::Vertical, fallback)
787 });
788 }
789 cx.notify();
790 }
791 }
792 }
793 })
794 })
795 .detach_and_log_err(cx);
796 }
797
798 fn key_context(&self) -> KeyContext {
799 let mut dispatch_context = KeyContext::new_with_defaults();
800 dispatch_context.add("KeymapEditor");
801 dispatch_context.add("menu");
802
803 dispatch_context
804 }
805
806 fn scroll_to_item(&self, index: usize, strategy: ScrollStrategy, cx: &mut App) {
807 let index = usize::min(index, self.matches.len().saturating_sub(1));
808 self.table_interaction_state.update(cx, |this, _cx| {
809 this.scroll_handle.scroll_to_item(index, strategy);
810 });
811 }
812
813 fn focus_search(
814 &mut self,
815 _: &search::FocusSearch,
816 window: &mut Window,
817 cx: &mut Context<Self>,
818 ) {
819 if !self
820 .filter_editor
821 .focus_handle(cx)
822 .contains_focused(window, cx)
823 {
824 window.focus(&self.filter_editor.focus_handle(cx));
825 } else {
826 self.filter_editor.update(cx, |editor, cx| {
827 editor.select_all(&Default::default(), window, cx);
828 });
829 }
830 self.selected_index.take();
831 }
832
833 fn selected_keybind_index(&self) -> Option<usize> {
834 self.selected_index
835 .and_then(|match_index| self.matches.get(match_index))
836 .map(|r#match| r#match.candidate_id)
837 }
838
839 fn selected_keybind_and_index(&self) -> Option<(&ProcessedBinding, usize)> {
840 self.selected_keybind_index()
841 .map(|keybind_index| (&self.keybindings[keybind_index], keybind_index))
842 }
843
844 fn selected_binding(&self) -> Option<&ProcessedBinding> {
845 self.selected_keybind_index()
846 .and_then(|keybind_index| self.keybindings.get(keybind_index))
847 }
848
849 fn select_index(
850 &mut self,
851 index: usize,
852 scroll: Option<ScrollStrategy>,
853 window: &mut Window,
854 cx: &mut Context<Self>,
855 ) {
856 if self.selected_index != Some(index) {
857 self.selected_index = Some(index);
858 if let Some(scroll_strategy) = scroll {
859 self.scroll_to_item(index, scroll_strategy, cx);
860 }
861 window.focus(&self.focus_handle);
862 cx.notify();
863 }
864 }
865
866 fn create_context_menu(
867 &mut self,
868 position: Point<Pixels>,
869 window: &mut Window,
870 cx: &mut Context<Self>,
871 ) {
872 self.context_menu = self.selected_binding().map(|selected_binding| {
873 let selected_binding_has_no_context = selected_binding
874 .context()
875 .and_then(KeybindContextString::local)
876 .is_none();
877
878 let selected_binding_is_unbound = selected_binding.is_unbound();
879
880 let context_menu = ContextMenu::build(window, cx, |menu, _window, _cx| {
881 menu.context(self.focus_handle.clone())
882 .action_disabled_when(
883 selected_binding_is_unbound,
884 "Edit",
885 Box::new(EditBinding),
886 )
887 .action("Create", Box::new(CreateBinding))
888 .action_disabled_when(
889 selected_binding_is_unbound,
890 "Delete",
891 Box::new(DeleteBinding),
892 )
893 .separator()
894 .action("Copy Action", Box::new(CopyAction))
895 .action_disabled_when(
896 selected_binding_has_no_context,
897 "Copy Context",
898 Box::new(CopyContext),
899 )
900 .separator()
901 .action_disabled_when(
902 selected_binding_has_no_context,
903 "Show Matching Keybindings",
904 Box::new(ShowMatchingKeybinds),
905 )
906 });
907
908 let context_menu_handle = context_menu.focus_handle(cx);
909 window.defer(cx, move |window, _cx| window.focus(&context_menu_handle));
910 let subscription = cx.subscribe_in(
911 &context_menu,
912 window,
913 |this, _, _: &DismissEvent, window, cx| {
914 this.dismiss_context_menu(window, cx);
915 },
916 );
917 (context_menu, position, subscription)
918 });
919
920 cx.notify();
921 }
922
923 fn dismiss_context_menu(&mut self, window: &mut Window, cx: &mut Context<Self>) {
924 self.context_menu.take();
925 window.focus(&self.focus_handle);
926 cx.notify();
927 }
928
929 fn context_menu_deployed(&self) -> bool {
930 self.context_menu.is_some()
931 }
932
933 fn create_row_button(
934 &self,
935 index: usize,
936 conflict: Option<ConflictOrigin>,
937 cx: &mut Context<Self>,
938 ) -> IconButton {
939 if self.filter_state != FilterState::Conflicts
940 && let Some(conflict) = conflict
941 {
942 if conflict.is_user_keybind_conflict() {
943 base_button_style(index, IconName::Warning)
944 .icon_color(Color::Warning)
945 .tooltip(|window, cx| {
946 Tooltip::with_meta(
947 "View conflicts",
948 Some(&ToggleConflictFilter),
949 "Use alt+click to show all conflicts",
950 window,
951 cx,
952 )
953 })
954 .on_click(cx.listener(move |this, click: &ClickEvent, window, cx| {
955 if click.modifiers().alt {
956 this.set_filter_state(FilterState::Conflicts, cx);
957 } else {
958 this.select_index(index, None, window, cx);
959 this.open_edit_keybinding_modal(false, window, cx);
960 cx.stop_propagation();
961 }
962 }))
963 } else if self.search_mode.exact_match() {
964 base_button_style(index, IconName::Info)
965 .tooltip(|window, cx| {
966 Tooltip::with_meta(
967 "Edit this binding",
968 Some(&ShowMatchingKeybinds),
969 "This binding is overridden by other bindings.",
970 window,
971 cx,
972 )
973 })
974 .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| {
975 this.select_index(index, None, window, cx);
976 this.open_edit_keybinding_modal(false, window, cx);
977 cx.stop_propagation();
978 }))
979 } else {
980 base_button_style(index, IconName::Info)
981 .tooltip(|window, cx| {
982 Tooltip::with_meta(
983 "Show matching keybinds",
984 Some(&ShowMatchingKeybinds),
985 "This binding is overridden by other bindings.\nUse alt+click to edit this binding",
986 window,
987 cx,
988 )
989 })
990 .on_click(cx.listener(move |this, click: &ClickEvent, window, cx| {
991 if click.modifiers().alt {
992 this.select_index(index, None, window, cx);
993 this.open_edit_keybinding_modal(false, window, cx);
994 cx.stop_propagation();
995 } else {
996 this.show_matching_keystrokes(&Default::default(), window, cx);
997 }
998 }))
999 }
1000 } else {
1001 base_button_style(index, IconName::Pencil)
1002 .visible_on_hover(if self.selected_index == Some(index) {
1003 "".into()
1004 } else if self.show_hover_menus {
1005 row_group_id(index)
1006 } else {
1007 "never-show".into()
1008 })
1009 .when(
1010 self.show_hover_menus && !self.context_menu_deployed(),
1011 |this| this.tooltip(Tooltip::for_action_title("Edit Keybinding", &EditBinding)),
1012 )
1013 .on_click(cx.listener(move |this, _, window, cx| {
1014 this.select_index(index, None, window, cx);
1015 this.open_edit_keybinding_modal(false, window, cx);
1016 cx.stop_propagation();
1017 }))
1018 }
1019 }
1020
1021 fn render_no_matches_hint(&self, _window: &mut Window, _cx: &App) -> AnyElement {
1022 let hint = match (self.filter_state, &self.search_mode) {
1023 (FilterState::Conflicts, _) => {
1024 if self.keybinding_conflict_state.any_user_binding_conflicts() {
1025 "No conflicting keybinds found that match the provided query"
1026 } else {
1027 "No conflicting keybinds found"
1028 }
1029 }
1030 (FilterState::All, SearchMode::KeyStroke { .. }) => {
1031 "No keybinds found matching the entered keystrokes"
1032 }
1033 (FilterState::All, SearchMode::Normal) => "No matches found for the provided query",
1034 };
1035
1036 Label::new(hint).color(Color::Muted).into_any_element()
1037 }
1038
1039 fn select_next(&mut self, _: &menu::SelectNext, window: &mut Window, cx: &mut Context<Self>) {
1040 self.show_hover_menus = false;
1041 if let Some(selected) = self.selected_index {
1042 let selected = selected + 1;
1043 if selected >= self.matches.len() {
1044 self.select_last(&Default::default(), window, cx);
1045 } else {
1046 self.select_index(selected, Some(ScrollStrategy::Center), window, cx);
1047 }
1048 } else {
1049 self.select_first(&Default::default(), window, cx);
1050 }
1051 }
1052
1053 fn select_previous(
1054 &mut self,
1055 _: &menu::SelectPrevious,
1056 window: &mut Window,
1057 cx: &mut Context<Self>,
1058 ) {
1059 self.show_hover_menus = false;
1060 if let Some(selected) = self.selected_index {
1061 if selected == 0 {
1062 return;
1063 }
1064
1065 let selected = selected - 1;
1066
1067 if selected >= self.matches.len() {
1068 self.select_last(&Default::default(), window, cx);
1069 } else {
1070 self.select_index(selected, Some(ScrollStrategy::Center), window, cx);
1071 }
1072 } else {
1073 self.select_last(&Default::default(), window, cx);
1074 }
1075 }
1076
1077 fn select_first(&mut self, _: &menu::SelectFirst, window: &mut Window, cx: &mut Context<Self>) {
1078 self.show_hover_menus = false;
1079 if self.matches.get(0).is_some() {
1080 self.select_index(0, Some(ScrollStrategy::Center), window, cx);
1081 }
1082 }
1083
1084 fn select_last(&mut self, _: &menu::SelectLast, window: &mut Window, cx: &mut Context<Self>) {
1085 self.show_hover_menus = false;
1086 if self.matches.last().is_some() {
1087 let index = self.matches.len() - 1;
1088 self.select_index(index, Some(ScrollStrategy::Center), window, cx);
1089 }
1090 }
1091
1092 fn open_edit_keybinding_modal(
1093 &mut self,
1094 create: bool,
1095 window: &mut Window,
1096 cx: &mut Context<Self>,
1097 ) {
1098 self.show_hover_menus = false;
1099 let Some((keybind, keybind_index)) = self.selected_keybind_and_index() else {
1100 return;
1101 };
1102 let keybind = keybind.clone();
1103 let keymap_editor = cx.entity();
1104
1105 let keystroke = keybind.keystroke_text().cloned().unwrap_or_default();
1106 let arguments = keybind
1107 .action()
1108 .arguments
1109 .as_ref()
1110 .map(|arguments| arguments.text.clone());
1111 let context = keybind
1112 .context()
1113 .map(|context| context.local_str().unwrap_or("global"));
1114 let action = keybind.action().name;
1115 let source = keybind.keybind_source().map(|source| source.name());
1116
1117 telemetry::event!(
1118 "Edit Keybinding Modal Opened",
1119 keystroke = keystroke,
1120 action = action,
1121 source = source,
1122 context = context,
1123 arguments = arguments,
1124 );
1125
1126 let temp_dir = self.action_args_temp_dir.as_ref().map(|dir| dir.path());
1127
1128 self.workspace
1129 .update(cx, |workspace, cx| {
1130 let fs = workspace.app_state().fs.clone();
1131 let workspace_weak = cx.weak_entity();
1132 workspace.toggle_modal(window, cx, |window, cx| {
1133 let modal = KeybindingEditorModal::new(
1134 create,
1135 keybind,
1136 keybind_index,
1137 keymap_editor,
1138 temp_dir,
1139 workspace_weak,
1140 fs,
1141 window,
1142 cx,
1143 );
1144 window.focus(&modal.focus_handle(cx));
1145 modal
1146 });
1147 })
1148 .log_err();
1149 }
1150
1151 fn edit_binding(&mut self, _: &EditBinding, window: &mut Window, cx: &mut Context<Self>) {
1152 self.open_edit_keybinding_modal(false, window, cx);
1153 }
1154
1155 fn create_binding(&mut self, _: &CreateBinding, window: &mut Window, cx: &mut Context<Self>) {
1156 self.open_edit_keybinding_modal(true, window, cx);
1157 }
1158
1159 fn delete_binding(&mut self, _: &DeleteBinding, window: &mut Window, cx: &mut Context<Self>) {
1160 let Some(to_remove) = self.selected_binding().cloned() else {
1161 return;
1162 };
1163
1164 let std::result::Result::Ok(fs) = self
1165 .workspace
1166 .read_with(cx, |workspace, _| workspace.app_state().fs.clone())
1167 else {
1168 return;
1169 };
1170 let tab_size = cx.global::<settings::SettingsStore>().json_tab_size();
1171 self.previous_edit = Some(PreviousEdit::ScrollBarOffset(
1172 self.table_interaction_state
1173 .read(cx)
1174 .get_scrollbar_offset(Axis::Vertical),
1175 ));
1176 cx.spawn(async move |_, _| remove_keybinding(to_remove, &fs, tab_size).await)
1177 .detach_and_notify_err(window, cx);
1178 }
1179
1180 fn copy_context_to_clipboard(
1181 &mut self,
1182 _: &CopyContext,
1183 _window: &mut Window,
1184 cx: &mut Context<Self>,
1185 ) {
1186 let context = self
1187 .selected_binding()
1188 .and_then(|binding| binding.context())
1189 .and_then(KeybindContextString::local_str)
1190 .map(|context| context.to_string());
1191 let Some(context) = context else {
1192 return;
1193 };
1194
1195 telemetry::event!("Keybinding Context Copied", context = context.clone());
1196 cx.write_to_clipboard(gpui::ClipboardItem::new_string(context.clone()));
1197 }
1198
1199 fn copy_action_to_clipboard(
1200 &mut self,
1201 _: &CopyAction,
1202 _window: &mut Window,
1203 cx: &mut Context<Self>,
1204 ) {
1205 let action = self
1206 .selected_binding()
1207 .map(|binding| binding.action().name.to_string());
1208 let Some(action) = action else {
1209 return;
1210 };
1211
1212 telemetry::event!("Keybinding Action Copied", action = action.clone());
1213 cx.write_to_clipboard(gpui::ClipboardItem::new_string(action.clone()));
1214 }
1215
1216 fn toggle_conflict_filter(
1217 &mut self,
1218 _: &ToggleConflictFilter,
1219 _: &mut Window,
1220 cx: &mut Context<Self>,
1221 ) {
1222 self.set_filter_state(self.filter_state.invert(), cx);
1223 }
1224
1225 fn set_filter_state(&mut self, filter_state: FilterState, cx: &mut Context<Self>) {
1226 if self.filter_state != filter_state {
1227 self.filter_state = filter_state;
1228 self.on_query_changed(cx);
1229 }
1230 }
1231
1232 fn toggle_keystroke_search(
1233 &mut self,
1234 _: &ToggleKeystrokeSearch,
1235 window: &mut Window,
1236 cx: &mut Context<Self>,
1237 ) {
1238 self.search_mode = self.search_mode.invert();
1239 self.on_query_changed(cx);
1240
1241 match self.search_mode {
1242 SearchMode::KeyStroke { .. } => {
1243 self.keystroke_editor.update(cx, |editor, cx| {
1244 editor.start_recording(&StartRecording, window, cx);
1245 });
1246 }
1247 SearchMode::Normal => {
1248 self.keystroke_editor.update(cx, |editor, cx| {
1249 editor.stop_recording(&StopRecording, window, cx);
1250 editor.clear_keystrokes(&ClearKeystrokes, window, cx);
1251 });
1252 window.focus(&self.filter_editor.focus_handle(cx));
1253 }
1254 }
1255 }
1256
1257 fn toggle_exact_keystroke_matching(
1258 &mut self,
1259 _: &ToggleExactKeystrokeMatching,
1260 _: &mut Window,
1261 cx: &mut Context<Self>,
1262 ) {
1263 let SearchMode::KeyStroke { exact_match } = &mut self.search_mode else {
1264 return;
1265 };
1266
1267 *exact_match = !(*exact_match);
1268 self.on_query_changed(cx);
1269 }
1270
1271 fn show_matching_keystrokes(
1272 &mut self,
1273 _: &ShowMatchingKeybinds,
1274 _: &mut Window,
1275 cx: &mut Context<Self>,
1276 ) {
1277 let Some(selected_binding) = self.selected_binding() else {
1278 return;
1279 };
1280
1281 let keystrokes = selected_binding
1282 .keystrokes()
1283 .map(Vec::from)
1284 .unwrap_or_default();
1285
1286 self.filter_state = FilterState::All;
1287 self.search_mode = SearchMode::KeyStroke { exact_match: true };
1288
1289 self.keystroke_editor.update(cx, |editor, cx| {
1290 editor.set_keystrokes(keystrokes, cx);
1291 });
1292 }
1293}
1294
1295struct HumanizedActionNameCache {
1296 cache: HashMap<&'static str, SharedString>,
1297}
1298
1299impl HumanizedActionNameCache {
1300 fn new(cx: &App) -> Self {
1301 let cache = HashMap::from_iter(cx.all_action_names().into_iter().map(|&action_name| {
1302 (
1303 action_name,
1304 command_palette::humanize_action_name(action_name).into(),
1305 )
1306 }));
1307 Self { cache }
1308 }
1309
1310 fn get(&self, action_name: &'static str) -> SharedString {
1311 match self.cache.get(action_name) {
1312 Some(name) => name.clone(),
1313 None => action_name.into(),
1314 }
1315 }
1316}
1317
1318#[derive(Clone)]
1319struct KeybindInformation {
1320 keystroke_text: SharedString,
1321 ui_binding: ui::KeyBinding,
1322 context: KeybindContextString,
1323 source: KeybindSource,
1324}
1325
1326impl KeybindInformation {
1327 fn get_action_mapping(&self) -> ActionMapping {
1328 ActionMapping {
1329 keystrokes: self.ui_binding.keystrokes.clone(),
1330 context: self.context.local().cloned(),
1331 }
1332 }
1333}
1334
1335#[derive(Clone)]
1336struct ActionInformation {
1337 name: &'static str,
1338 humanized_name: SharedString,
1339 arguments: Option<SyntaxHighlightedText>,
1340 documentation: Option<&'static str>,
1341 has_schema: bool,
1342}
1343
1344impl ActionInformation {
1345 fn new(
1346 action_name: &'static str,
1347 action_arguments: Option<SyntaxHighlightedText>,
1348 actions_with_schemas: &HashSet<&'static str>,
1349 action_documentation: &HashMap<&'static str, &'static str>,
1350 action_name_cache: &HumanizedActionNameCache,
1351 ) -> Self {
1352 Self {
1353 humanized_name: action_name_cache.get(action_name),
1354 has_schema: actions_with_schemas.contains(action_name),
1355 arguments: action_arguments,
1356 documentation: action_documentation.get(action_name).copied(),
1357 name: action_name,
1358 }
1359 }
1360}
1361
1362#[derive(Clone)]
1363enum ProcessedBinding {
1364 Mapped(KeybindInformation, ActionInformation),
1365 Unmapped(ActionInformation),
1366}
1367
1368impl ProcessedBinding {
1369 fn new_mapped(
1370 keystroke_text: impl Into<SharedString>,
1371 ui_key_binding: ui::KeyBinding,
1372 context: KeybindContextString,
1373 source: KeybindSource,
1374 action_information: ActionInformation,
1375 ) -> Self {
1376 Self::Mapped(
1377 KeybindInformation {
1378 keystroke_text: keystroke_text.into(),
1379 ui_binding: ui_key_binding,
1380 context,
1381 source,
1382 },
1383 action_information,
1384 )
1385 }
1386
1387 fn is_unbound(&self) -> bool {
1388 matches!(self, Self::Unmapped(_))
1389 }
1390
1391 fn get_action_mapping(&self) -> Option<ActionMapping> {
1392 self.keybind_information()
1393 .map(|keybind| keybind.get_action_mapping())
1394 }
1395
1396 fn keystrokes(&self) -> Option<&[Keystroke]> {
1397 self.ui_key_binding()
1398 .map(|binding| binding.keystrokes.as_slice())
1399 }
1400
1401 fn keybind_information(&self) -> Option<&KeybindInformation> {
1402 match self {
1403 Self::Mapped(keybind_information, _) => Some(keybind_information),
1404 Self::Unmapped(_) => None,
1405 }
1406 }
1407
1408 fn keybind_source(&self) -> Option<KeybindSource> {
1409 self.keybind_information().map(|keybind| keybind.source)
1410 }
1411
1412 fn context(&self) -> Option<&KeybindContextString> {
1413 self.keybind_information().map(|keybind| &keybind.context)
1414 }
1415
1416 fn ui_key_binding(&self) -> Option<&ui::KeyBinding> {
1417 self.keybind_information()
1418 .map(|keybind| &keybind.ui_binding)
1419 }
1420
1421 fn keystroke_text(&self) -> Option<&SharedString> {
1422 self.keybind_information()
1423 .map(|binding| &binding.keystroke_text)
1424 }
1425
1426 fn action(&self) -> &ActionInformation {
1427 match self {
1428 Self::Mapped(_, action) | Self::Unmapped(action) => action,
1429 }
1430 }
1431
1432 fn cmp(&self, other: &Self) -> cmp::Ordering {
1433 match (self, other) {
1434 (Self::Mapped(keybind1, action1), Self::Mapped(keybind2, action2)) => {
1435 match keybind1.source.cmp(&keybind2.source) {
1436 cmp::Ordering::Equal => action1.humanized_name.cmp(&action2.humanized_name),
1437 ordering => ordering,
1438 }
1439 }
1440 (Self::Mapped(_, _), Self::Unmapped(_)) => cmp::Ordering::Less,
1441 (Self::Unmapped(_), Self::Mapped(_, _)) => cmp::Ordering::Greater,
1442 (Self::Unmapped(action1), Self::Unmapped(action2)) => {
1443 action1.humanized_name.cmp(&action2.humanized_name)
1444 }
1445 }
1446 }
1447}
1448
1449#[derive(Clone, Debug, IntoElement, PartialEq, Eq, Hash)]
1450enum KeybindContextString {
1451 Global,
1452 Local(SharedString, Arc<Language>),
1453}
1454
1455impl KeybindContextString {
1456 const GLOBAL: SharedString = SharedString::new_static("<global>");
1457
1458 pub fn local(&self) -> Option<&SharedString> {
1459 match self {
1460 KeybindContextString::Global => None,
1461 KeybindContextString::Local(name, _) => Some(name),
1462 }
1463 }
1464
1465 pub fn local_str(&self) -> Option<&str> {
1466 match self {
1467 KeybindContextString::Global => None,
1468 KeybindContextString::Local(name, _) => Some(name),
1469 }
1470 }
1471}
1472
1473impl RenderOnce for KeybindContextString {
1474 fn render(self, _window: &mut Window, cx: &mut App) -> impl IntoElement {
1475 match self {
1476 KeybindContextString::Global => {
1477 muted_styled_text(KeybindContextString::GLOBAL.clone(), cx).into_any_element()
1478 }
1479 KeybindContextString::Local(name, language) => {
1480 SyntaxHighlightedText::new(name, language).into_any_element()
1481 }
1482 }
1483 }
1484}
1485
1486fn muted_styled_text(text: SharedString, cx: &App) -> StyledText {
1487 let len = text.len();
1488 StyledText::new(text).with_highlights([(
1489 0..len,
1490 gpui::HighlightStyle::color(cx.theme().colors().text_muted),
1491 )])
1492}
1493
1494impl Item for KeymapEditor {
1495 type Event = ();
1496
1497 fn tab_content_text(&self, _detail: usize, _cx: &App) -> ui::SharedString {
1498 "Keymap Editor".into()
1499 }
1500}
1501
1502impl Render for KeymapEditor {
1503 fn render(&mut self, _window: &mut Window, cx: &mut ui::Context<Self>) -> impl ui::IntoElement {
1504 let row_count = self.matches.len();
1505 let theme = cx.theme();
1506 let focus_handle = &self.focus_handle;
1507
1508 v_flex()
1509 .id("keymap-editor")
1510 .track_focus(focus_handle)
1511 .key_context(self.key_context())
1512 .on_action(cx.listener(Self::select_next))
1513 .on_action(cx.listener(Self::select_previous))
1514 .on_action(cx.listener(Self::select_first))
1515 .on_action(cx.listener(Self::select_last))
1516 .on_action(cx.listener(Self::focus_search))
1517 .on_action(cx.listener(Self::edit_binding))
1518 .on_action(cx.listener(Self::create_binding))
1519 .on_action(cx.listener(Self::delete_binding))
1520 .on_action(cx.listener(Self::copy_action_to_clipboard))
1521 .on_action(cx.listener(Self::copy_context_to_clipboard))
1522 .on_action(cx.listener(Self::toggle_conflict_filter))
1523 .on_action(cx.listener(Self::toggle_keystroke_search))
1524 .on_action(cx.listener(Self::toggle_exact_keystroke_matching))
1525 .on_action(cx.listener(Self::show_matching_keystrokes))
1526 .on_mouse_move(cx.listener(|this, _, _window, _cx| {
1527 this.show_hover_menus = true;
1528 }))
1529 .size_full()
1530 .p_2()
1531 .gap_1()
1532 .bg(theme.colors().editor_background)
1533 .child(
1534 v_flex()
1535 .gap_2()
1536 .child(
1537 h_flex()
1538 .gap_2()
1539 .child(
1540 div()
1541 .key_context({
1542 let mut context = KeyContext::new_with_defaults();
1543 context.add("BufferSearchBar");
1544 context
1545 })
1546 .size_full()
1547 .h_8()
1548 .pl_2()
1549 .pr_1()
1550 .py_1()
1551 .border_1()
1552 .border_color(theme.colors().border)
1553 .rounded_lg()
1554 .child(self.filter_editor.clone()),
1555 )
1556 .child(
1557 IconButton::new(
1558 "KeymapEditorToggleFiltersIcon",
1559 IconName::Keyboard,
1560 )
1561 .shape(ui::IconButtonShape::Square)
1562 .tooltip({
1563 let focus_handle = focus_handle.clone();
1564
1565 move |window, cx| {
1566 Tooltip::for_action_in(
1567 "Search by Keystroke",
1568 &ToggleKeystrokeSearch,
1569 &focus_handle.clone(),
1570 window,
1571 cx,
1572 )
1573 }
1574 })
1575 .toggle_state(matches!(
1576 self.search_mode,
1577 SearchMode::KeyStroke { .. }
1578 ))
1579 .on_click(|_, window, cx| {
1580 window.dispatch_action(ToggleKeystrokeSearch.boxed_clone(), cx);
1581 }),
1582 )
1583 .child(
1584 IconButton::new("KeymapEditorConflictIcon", IconName::Warning)
1585 .shape(ui::IconButtonShape::Square)
1586 .when(
1587 self.keybinding_conflict_state.any_user_binding_conflicts(),
1588 |this| {
1589 this.indicator(Indicator::dot().color(Color::Warning))
1590 },
1591 )
1592 .tooltip({
1593 let filter_state = self.filter_state;
1594 let focus_handle = focus_handle.clone();
1595
1596 move |window, cx| {
1597 Tooltip::for_action_in(
1598 match filter_state {
1599 FilterState::All => "Show Conflicts",
1600 FilterState::Conflicts => "Hide Conflicts",
1601 },
1602 &ToggleConflictFilter,
1603 &focus_handle.clone(),
1604 window,
1605 cx,
1606 )
1607 }
1608 })
1609 .selected_icon_color(Color::Warning)
1610 .toggle_state(matches!(
1611 self.filter_state,
1612 FilterState::Conflicts
1613 ))
1614 .on_click(|_, window, cx| {
1615 window.dispatch_action(
1616 ToggleConflictFilter.boxed_clone(),
1617 cx,
1618 );
1619 }),
1620 ),
1621 )
1622 .when_some(
1623 match self.search_mode {
1624 SearchMode::Normal => None,
1625 SearchMode::KeyStroke { exact_match } => Some(exact_match),
1626 },
1627 |this, exact_match| {
1628 this.child(
1629 h_flex()
1630 .map(|this| {
1631 if self
1632 .keybinding_conflict_state
1633 .any_user_binding_conflicts()
1634 {
1635 this.pr(rems_from_px(54.))
1636 } else {
1637 this.pr_7()
1638 }
1639 })
1640 .gap_2()
1641 .child(self.keystroke_editor.clone())
1642 .child(
1643 IconButton::new(
1644 "keystrokes-exact-match",
1645 IconName::CaseSensitive,
1646 )
1647 .tooltip({
1648 let keystroke_focus_handle =
1649 self.keystroke_editor.read(cx).focus_handle(cx);
1650
1651 move |window, cx| {
1652 Tooltip::for_action_in(
1653 "Toggle Exact Match Mode",
1654 &ToggleExactKeystrokeMatching,
1655 &keystroke_focus_handle,
1656 window,
1657 cx,
1658 )
1659 }
1660 })
1661 .shape(IconButtonShape::Square)
1662 .toggle_state(exact_match)
1663 .on_click(
1664 cx.listener(|_, _, window, cx| {
1665 window.dispatch_action(
1666 ToggleExactKeystrokeMatching.boxed_clone(),
1667 cx,
1668 );
1669 }),
1670 ),
1671 ),
1672 )
1673 },
1674 ),
1675 )
1676 .child(
1677 Table::new()
1678 .interactable(&self.table_interaction_state)
1679 .striped()
1680 .empty_table_callback({
1681 let this = cx.entity();
1682 move |window, cx| this.read(cx).render_no_matches_hint(window, cx)
1683 })
1684 .column_widths([
1685 DefiniteLength::Absolute(AbsoluteLength::Pixels(px(36.))),
1686 DefiniteLength::Fraction(0.25),
1687 DefiniteLength::Fraction(0.20),
1688 DefiniteLength::Fraction(0.14),
1689 DefiniteLength::Fraction(0.45),
1690 DefiniteLength::Fraction(0.08),
1691 ])
1692 .resizable_columns(
1693 [
1694 ResizeBehavior::None,
1695 ResizeBehavior::Resizable,
1696 ResizeBehavior::Resizable,
1697 ResizeBehavior::Resizable,
1698 ResizeBehavior::Resizable,
1699 ResizeBehavior::Resizable, // this column doesn't matter
1700 ],
1701 &self.current_widths,
1702 cx,
1703 )
1704 .header(["", "Action", "Arguments", "Keystrokes", "Context", "Source"])
1705 .uniform_list(
1706 "keymap-editor-table",
1707 row_count,
1708 cx.processor(move |this, range: Range<usize>, _window, cx| {
1709 let context_menu_deployed = this.context_menu_deployed();
1710 range
1711 .filter_map(|index| {
1712 let candidate_id = this.matches.get(index)?.candidate_id;
1713 let binding = &this.keybindings[candidate_id];
1714 let action_name = binding.action().name;
1715 let conflict = this.get_conflict(index);
1716 let is_overridden = conflict.is_some_and(|conflict| {
1717 !conflict.is_user_keybind_conflict()
1718 });
1719
1720 let icon = this.create_row_button(index, conflict, cx);
1721
1722 let action = div()
1723 .id(("keymap action", index))
1724 .child({
1725 if action_name != gpui::NoAction.name() {
1726 binding
1727 .action()
1728 .humanized_name
1729 .clone()
1730 .into_any_element()
1731 } else {
1732 const NULL: SharedString =
1733 SharedString::new_static("<null>");
1734 muted_styled_text(NULL.clone(), cx)
1735 .into_any_element()
1736 }
1737 })
1738 .when(
1739 !context_menu_deployed
1740 && this.show_hover_menus
1741 && !is_overridden,
1742 |this| {
1743 this.tooltip({
1744 let action_name = binding.action().name;
1745 let action_docs =
1746 binding.action().documentation;
1747 move |_, cx| {
1748 let action_tooltip =
1749 Tooltip::new(action_name);
1750 let action_tooltip = match action_docs {
1751 Some(docs) => action_tooltip.meta(docs),
1752 None => action_tooltip,
1753 };
1754 cx.new(|_| action_tooltip).into()
1755 }
1756 })
1757 },
1758 )
1759 .into_any_element();
1760
1761 let keystrokes = binding.ui_key_binding().cloned().map_or(
1762 binding
1763 .keystroke_text()
1764 .cloned()
1765 .unwrap_or_default()
1766 .into_any_element(),
1767 IntoElement::into_any_element,
1768 );
1769
1770 let action_arguments = match binding.action().arguments.clone()
1771 {
1772 Some(arguments) => arguments.into_any_element(),
1773 None => {
1774 if binding.action().has_schema {
1775 muted_styled_text(NO_ACTION_ARGUMENTS_TEXT, cx)
1776 .into_any_element()
1777 } else {
1778 gpui::Empty.into_any_element()
1779 }
1780 }
1781 };
1782
1783 let context = binding.context().cloned().map_or(
1784 gpui::Empty.into_any_element(),
1785 |context| {
1786 let is_local = context.local().is_some();
1787
1788 div()
1789 .id(("keymap context", index))
1790 .child(context.clone())
1791 .when(
1792 is_local
1793 && !context_menu_deployed
1794 && !is_overridden
1795 && this.show_hover_menus,
1796 |this| {
1797 this.tooltip(Tooltip::element({
1798 move |_, _| {
1799 context.clone().into_any_element()
1800 }
1801 }))
1802 },
1803 )
1804 .into_any_element()
1805 },
1806 );
1807
1808 let source = binding
1809 .keybind_source()
1810 .map(|source| source.name())
1811 .unwrap_or_default()
1812 .into_any_element();
1813
1814 Some([
1815 icon.into_any_element(),
1816 action,
1817 action_arguments,
1818 keystrokes,
1819 context,
1820 source,
1821 ])
1822 })
1823 .collect()
1824 }),
1825 )
1826 .map_row(cx.processor(
1827 |this, (row_index, row): (usize, Stateful<Div>), _window, cx| {
1828 let conflict = this.get_conflict(row_index);
1829 let is_selected = this.selected_index == Some(row_index);
1830
1831 let row_id = row_group_id(row_index);
1832
1833 div()
1834 .id(("keymap-row-wrapper", row_index))
1835 .child(
1836 row.id(row_id.clone())
1837 .on_any_mouse_down(cx.listener(
1838 move |this,
1839 mouse_down_event: &gpui::MouseDownEvent,
1840 window,
1841 cx| {
1842 match mouse_down_event.button {
1843 MouseButton::Right => {
1844 this.select_index(
1845 row_index, None, window, cx,
1846 );
1847 this.create_context_menu(
1848 mouse_down_event.position,
1849 window,
1850 cx,
1851 );
1852 }
1853 _ => {}
1854 }
1855 },
1856 ))
1857 .on_click(cx.listener(
1858 move |this, event: &ClickEvent, window, cx| {
1859 this.select_index(row_index, None, window, cx);
1860 if event.click_count() == 2 {
1861 this.open_edit_keybinding_modal(
1862 false, window, cx,
1863 );
1864 }
1865 },
1866 ))
1867 .group(row_id)
1868 .when(
1869 conflict.is_some_and(|conflict| {
1870 !conflict.is_user_keybind_conflict()
1871 }),
1872 |row| {
1873 const OVERRIDDEN_OPACITY: f32 = 0.5;
1874 row.opacity(OVERRIDDEN_OPACITY)
1875 },
1876 )
1877 .when_some(
1878 conflict.filter(|conflict| {
1879 !this.context_menu_deployed() &&
1880 !conflict.is_user_keybind_conflict()
1881 }),
1882 |row, conflict| {
1883 let overriding_binding = this.keybindings.get(conflict.index);
1884 let context = overriding_binding.and_then(|binding| {
1885 match conflict.override_source {
1886 KeybindSource::User => Some("your keymap"),
1887 KeybindSource::Vim => Some("the vim keymap"),
1888 KeybindSource::Base => Some("your base keymap"),
1889 _ => {
1890 log::error!("Unexpected override from the {} keymap", conflict.override_source.name());
1891 None
1892 }
1893 }.map(|source| format!("This keybinding is overridden by the '{}' binding from {}.", binding.action().humanized_name, source))
1894 }).unwrap_or_else(|| "This binding is overridden.".to_string());
1895
1896 row.tooltip(Tooltip::text(context))},
1897 ),
1898 )
1899 .border_2()
1900 .when(
1901 conflict.is_some_and(|conflict| {
1902 conflict.is_user_keybind_conflict()
1903 }),
1904 |row| row.bg(cx.theme().status().error_background),
1905 )
1906 .when(is_selected, |row| {
1907 row.border_color(cx.theme().colors().panel_focused_border)
1908 })
1909 .into_any_element()
1910 }),
1911 ),
1912 )
1913 .on_scroll_wheel(cx.listener(|this, event: &ScrollWheelEvent, _, cx| {
1914 // This ensures that the menu is not dismissed in cases where scroll events
1915 // with a delta of zero are emitted
1916 if !event.delta.pixel_delta(px(1.)).y.is_zero() {
1917 this.context_menu.take();
1918 cx.notify();
1919 }
1920 }))
1921 .children(self.context_menu.as_ref().map(|(menu, position, _)| {
1922 deferred(
1923 anchored()
1924 .position(*position)
1925 .anchor(gpui::Corner::TopLeft)
1926 .child(menu.clone()),
1927 )
1928 .with_priority(1)
1929 }))
1930 }
1931}
1932
1933fn row_group_id(row_index: usize) -> SharedString {
1934 SharedString::new(format!("keymap-table-row-{}", row_index))
1935}
1936
1937fn base_button_style(row_index: usize, icon: IconName) -> IconButton {
1938 IconButton::new(("keymap-icon", row_index), icon)
1939 .shape(IconButtonShape::Square)
1940 .size(ButtonSize::Compact)
1941}
1942
1943#[derive(Debug, Clone, IntoElement)]
1944struct SyntaxHighlightedText {
1945 text: SharedString,
1946 language: Arc<Language>,
1947}
1948
1949impl SyntaxHighlightedText {
1950 pub fn new(text: impl Into<SharedString>, language: Arc<Language>) -> Self {
1951 Self {
1952 text: text.into(),
1953 language,
1954 }
1955 }
1956}
1957
1958impl RenderOnce for SyntaxHighlightedText {
1959 fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement {
1960 let text_style = window.text_style();
1961 let syntax_theme = cx.theme().syntax();
1962
1963 let text = self.text.clone();
1964
1965 let highlights = self
1966 .language
1967 .highlight_text(&text.as_ref().into(), 0..text.len());
1968 let mut runs = Vec::with_capacity(highlights.len());
1969 let mut offset = 0;
1970
1971 for (highlight_range, highlight_id) in highlights {
1972 // Add un-highlighted text before the current highlight
1973 if highlight_range.start > offset {
1974 runs.push(text_style.to_run(highlight_range.start - offset));
1975 }
1976
1977 let mut run_style = text_style.clone();
1978 if let Some(highlight_style) = highlight_id.style(syntax_theme) {
1979 run_style = run_style.highlight(highlight_style);
1980 }
1981 // add the highlighted range
1982 runs.push(run_style.to_run(highlight_range.len()));
1983 offset = highlight_range.end;
1984 }
1985
1986 // Add any remaining un-highlighted text
1987 if offset < text.len() {
1988 runs.push(text_style.to_run(text.len() - offset));
1989 }
1990
1991 StyledText::new(text).with_runs(runs)
1992 }
1993}
1994
1995#[derive(PartialEq)]
1996struct InputError {
1997 severity: ui::Severity,
1998 content: SharedString,
1999}
2000
2001impl InputError {
2002 fn warning(message: impl Into<SharedString>) -> Self {
2003 Self {
2004 severity: ui::Severity::Warning,
2005 content: message.into(),
2006 }
2007 }
2008
2009 fn error(message: anyhow::Error) -> Self {
2010 Self {
2011 severity: ui::Severity::Error,
2012 content: message.to_string().into(),
2013 }
2014 }
2015}
2016
2017struct KeybindingEditorModal {
2018 creating: bool,
2019 editing_keybind: ProcessedBinding,
2020 editing_keybind_idx: usize,
2021 keybind_editor: Entity<KeystrokeInput>,
2022 context_editor: Entity<SingleLineInput>,
2023 action_arguments_editor: Option<Entity<ActionArgumentsEditor>>,
2024 fs: Arc<dyn Fs>,
2025 error: Option<InputError>,
2026 keymap_editor: Entity<KeymapEditor>,
2027 workspace: WeakEntity<Workspace>,
2028 focus_state: KeybindingEditorModalFocusState,
2029}
2030
2031impl ModalView for KeybindingEditorModal {}
2032
2033impl EventEmitter<DismissEvent> for KeybindingEditorModal {}
2034
2035impl Focusable for KeybindingEditorModal {
2036 fn focus_handle(&self, cx: &App) -> FocusHandle {
2037 self.keybind_editor.focus_handle(cx)
2038 }
2039}
2040
2041impl KeybindingEditorModal {
2042 pub fn new(
2043 create: bool,
2044 editing_keybind: ProcessedBinding,
2045 editing_keybind_idx: usize,
2046 keymap_editor: Entity<KeymapEditor>,
2047 action_args_temp_dir: Option<&std::path::Path>,
2048 workspace: WeakEntity<Workspace>,
2049 fs: Arc<dyn Fs>,
2050 window: &mut Window,
2051 cx: &mut App,
2052 ) -> Self {
2053 let keybind_editor = cx
2054 .new(|cx| KeystrokeInput::new(editing_keybind.keystrokes().map(Vec::from), window, cx));
2055
2056 let context_editor: Entity<SingleLineInput> = cx.new(|cx| {
2057 let input = SingleLineInput::new(window, cx, "Keybinding Context")
2058 .label("Edit Context")
2059 .label_size(LabelSize::Default);
2060
2061 if let Some(context) = editing_keybind
2062 .context()
2063 .and_then(KeybindContextString::local)
2064 {
2065 input.editor().update(cx, |editor, cx| {
2066 editor.set_text(context.clone(), window, cx);
2067 });
2068 }
2069
2070 let editor_entity = input.editor().clone();
2071 let workspace = workspace.clone();
2072 cx.spawn(async move |_input_handle, cx| {
2073 let contexts = cx
2074 .background_spawn(async { collect_contexts_from_assets() })
2075 .await;
2076
2077 let language = load_keybind_context_language(workspace, cx).await;
2078 editor_entity
2079 .update(cx, |editor, cx| {
2080 if let Some(buffer) = editor.buffer().read(cx).as_singleton() {
2081 buffer.update(cx, |buffer, cx| {
2082 buffer.set_language(Some(language), cx);
2083 });
2084 }
2085 editor.set_completion_provider(Some(std::rc::Rc::new(
2086 KeyContextCompletionProvider { contexts },
2087 )));
2088 })
2089 .context("Failed to load completions for keybinding context")
2090 })
2091 .detach_and_log_err(cx);
2092
2093 input
2094 });
2095
2096 let action_arguments_editor = editing_keybind.action().has_schema.then(|| {
2097 let arguments = editing_keybind
2098 .action()
2099 .arguments
2100 .as_ref()
2101 .map(|args| args.text.clone());
2102 cx.new(|cx| {
2103 ActionArgumentsEditor::new(
2104 editing_keybind.action().name,
2105 arguments,
2106 action_args_temp_dir,
2107 workspace.clone(),
2108 window,
2109 cx,
2110 )
2111 })
2112 });
2113
2114 let focus_state = KeybindingEditorModalFocusState::new(
2115 keybind_editor.focus_handle(cx),
2116 action_arguments_editor
2117 .as_ref()
2118 .map(|args_editor| args_editor.focus_handle(cx)),
2119 context_editor.focus_handle(cx),
2120 );
2121
2122 Self {
2123 creating: create,
2124 editing_keybind,
2125 editing_keybind_idx,
2126 fs,
2127 keybind_editor,
2128 context_editor,
2129 action_arguments_editor,
2130 error: None,
2131 keymap_editor,
2132 workspace,
2133 focus_state,
2134 }
2135 }
2136
2137 fn set_error(&mut self, error: InputError, cx: &mut Context<Self>) -> bool {
2138 if self.error.as_ref().is_some_and(|old_error| {
2139 old_error.severity == ui::Severity::Warning && *old_error == error
2140 }) {
2141 false
2142 } else {
2143 self.error = Some(error);
2144 cx.notify();
2145 true
2146 }
2147 }
2148
2149 fn validate_action_arguments(&self, cx: &App) -> anyhow::Result<Option<String>> {
2150 let action_arguments = self
2151 .action_arguments_editor
2152 .as_ref()
2153 .map(|arguments_editor| arguments_editor.read(cx).editor.read(cx).text(cx))
2154 .filter(|args| !args.is_empty());
2155
2156 let value = action_arguments
2157 .as_ref()
2158 .map(|args| {
2159 serde_json::from_str(args).context("Failed to parse action arguments as JSON")
2160 })
2161 .transpose()?;
2162
2163 cx.build_action(&self.editing_keybind.action().name, value)
2164 .context("Failed to validate action arguments")?;
2165 Ok(action_arguments)
2166 }
2167
2168 fn validate_keystrokes(&self, cx: &App) -> anyhow::Result<Vec<Keystroke>> {
2169 let new_keystrokes = self
2170 .keybind_editor
2171 .read_with(cx, |editor, _| editor.keystrokes().to_vec());
2172 anyhow::ensure!(!new_keystrokes.is_empty(), "Keystrokes cannot be empty");
2173 Ok(new_keystrokes)
2174 }
2175
2176 fn validate_context(&self, cx: &App) -> anyhow::Result<Option<String>> {
2177 let new_context = self
2178 .context_editor
2179 .read_with(cx, |input, cx| input.editor().read(cx).text(cx));
2180 let Some(context) = new_context.is_empty().not().then_some(new_context) else {
2181 return Ok(None);
2182 };
2183 gpui::KeyBindingContextPredicate::parse(&context).context("Failed to parse key context")?;
2184
2185 Ok(Some(context))
2186 }
2187
2188 fn save_or_display_error(&mut self, cx: &mut Context<Self>) {
2189 self.save(cx).map_err(|err| self.set_error(err, cx)).ok();
2190 }
2191
2192 fn save(&mut self, cx: &mut Context<Self>) -> Result<(), InputError> {
2193 let existing_keybind = self.editing_keybind.clone();
2194 let fs = self.fs.clone();
2195 let tab_size = cx.global::<settings::SettingsStore>().json_tab_size();
2196
2197 let new_keystrokes = self
2198 .validate_keystrokes(cx)
2199 .map_err(InputError::error)?
2200 .into_iter()
2201 .map(remove_key_char)
2202 .collect::<Vec<_>>();
2203
2204 let new_context = self.validate_context(cx).map_err(InputError::error)?;
2205 let new_action_args = self
2206 .validate_action_arguments(cx)
2207 .map_err(InputError::error)?;
2208
2209 let action_mapping = ActionMapping {
2210 keystrokes: new_keystrokes,
2211 context: new_context.map(SharedString::from),
2212 };
2213
2214 let conflicting_indices = self
2215 .keymap_editor
2216 .read(cx)
2217 .keybinding_conflict_state
2218 .conflicting_indices_for_mapping(
2219 &action_mapping,
2220 self.creating.not().then_some(self.editing_keybind_idx),
2221 );
2222
2223 conflicting_indices.map(|KeybindConflict {
2224 first_conflict_index,
2225 remaining_conflict_amount,
2226 }|
2227 {
2228 let conflicting_action_name = self
2229 .keymap_editor
2230 .read(cx)
2231 .keybindings
2232 .get(first_conflict_index)
2233 .map(|keybind| keybind.action().name);
2234
2235 let warning_message = match conflicting_action_name {
2236 Some(name) => {
2237 if remaining_conflict_amount > 0 {
2238 format!(
2239 "Your keybind would conflict with the \"{}\" action and {} other bindings",
2240 name, remaining_conflict_amount
2241 )
2242 } else {
2243 format!("Your keybind would conflict with the \"{}\" action", name)
2244 }
2245 }
2246 None => {
2247 log::info!(
2248 "Could not find action in keybindings with index {}",
2249 first_conflict_index
2250 );
2251 "Your keybind would conflict with other actions".to_string()
2252 }
2253 };
2254
2255 let warning = InputError::warning(warning_message);
2256 if self.error.as_ref().is_some_and(|old_error| *old_error == warning) {
2257 Ok(())
2258 } else {
2259 Err(warning)
2260 }
2261 }).unwrap_or(Ok(()))?;
2262
2263 let create = self.creating;
2264
2265 cx.spawn(async move |this, cx| {
2266 let action_name = existing_keybind.action().name;
2267 let humanized_action_name = existing_keybind.action().humanized_name.clone();
2268
2269 match save_keybinding_update(
2270 create,
2271 existing_keybind,
2272 &action_mapping,
2273 new_action_args.as_deref(),
2274 &fs,
2275 tab_size,
2276 )
2277 .await
2278 {
2279 Ok(_) => {
2280 this.update(cx, |this, cx| {
2281 this.keymap_editor.update(cx, |keymap, cx| {
2282 keymap.previous_edit = Some(PreviousEdit::Keybinding {
2283 action_mapping,
2284 action_name,
2285 fallback: keymap
2286 .table_interaction_state
2287 .read(cx)
2288 .get_scrollbar_offset(Axis::Vertical),
2289 });
2290 let status_toast = StatusToast::new(
2291 format!("Saved edits to the {} action.", humanized_action_name),
2292 cx,
2293 move |this, _cx| {
2294 this.icon(ToastIcon::new(IconName::Check).color(Color::Success))
2295 .dismiss_button(true)
2296 // .action("Undo", f) todo: wire the undo functionality
2297 },
2298 );
2299
2300 this.workspace
2301 .update(cx, |workspace, cx| {
2302 workspace.toggle_status_toast(status_toast, cx);
2303 })
2304 .log_err();
2305 });
2306 cx.emit(DismissEvent);
2307 })
2308 .ok();
2309 }
2310 Err(err) => {
2311 this.update(cx, |this, cx| {
2312 this.set_error(InputError::error(err), cx);
2313 })
2314 .log_err();
2315 }
2316 }
2317 })
2318 .detach();
2319
2320 Ok(())
2321 }
2322
2323 fn key_context(&self) -> KeyContext {
2324 let mut key_context = KeyContext::new_with_defaults();
2325 key_context.add("KeybindEditorModal");
2326 key_context
2327 }
2328
2329 fn focus_next(&mut self, _: &menu::SelectNext, window: &mut Window, cx: &mut Context<Self>) {
2330 self.focus_state.focus_next(window, cx);
2331 }
2332
2333 fn focus_prev(
2334 &mut self,
2335 _: &menu::SelectPrevious,
2336 window: &mut Window,
2337 cx: &mut Context<Self>,
2338 ) {
2339 self.focus_state.focus_previous(window, cx);
2340 }
2341
2342 fn confirm(&mut self, _: &menu::Confirm, _window: &mut Window, cx: &mut Context<Self>) {
2343 self.save_or_display_error(cx);
2344 }
2345
2346 fn cancel(&mut self, _: &menu::Cancel, _: &mut Window, cx: &mut Context<Self>) {
2347 cx.emit(DismissEvent);
2348 }
2349
2350 fn get_matching_bindings_count(&self, cx: &Context<Self>) -> usize {
2351 let current_keystrokes = self.keybind_editor.read(cx).keystrokes().to_vec();
2352
2353 if current_keystrokes.is_empty() {
2354 return 0;
2355 }
2356
2357 self.keymap_editor
2358 .read(cx)
2359 .keybindings
2360 .iter()
2361 .enumerate()
2362 .filter(|(idx, binding)| {
2363 // Don't count the binding we're currently editing
2364 if !self.creating && *idx == self.editing_keybind_idx {
2365 return false;
2366 }
2367
2368 binding
2369 .keystrokes()
2370 .map(|keystrokes| keystrokes_match_exactly(keystrokes, ¤t_keystrokes))
2371 .unwrap_or(false)
2372 })
2373 .count()
2374 }
2375
2376 fn show_matching_bindings(&mut self, _window: &mut Window, cx: &mut Context<Self>) {
2377 let keystrokes = self.keybind_editor.read(cx).keystrokes().to_vec();
2378
2379 // Dismiss the modal
2380 cx.emit(DismissEvent);
2381
2382 // Update the keymap editor to show matching keystrokes
2383 self.keymap_editor.update(cx, |editor, cx| {
2384 editor.filter_state = FilterState::All;
2385 editor.search_mode = SearchMode::KeyStroke { exact_match: true };
2386 editor.keystroke_editor.update(cx, |keystroke_editor, cx| {
2387 keystroke_editor.set_keystrokes(keystrokes, cx);
2388 });
2389 });
2390 }
2391}
2392
2393fn remove_key_char(Keystroke { modifiers, key, .. }: Keystroke) -> Keystroke {
2394 Keystroke {
2395 modifiers,
2396 key,
2397 ..Default::default()
2398 }
2399}
2400
2401impl Render for KeybindingEditorModal {
2402 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2403 let theme = cx.theme().colors();
2404 let matching_bindings_count = self.get_matching_bindings_count(cx);
2405
2406 v_flex()
2407 .w(rems(34.))
2408 .elevation_3(cx)
2409 .key_context(self.key_context())
2410 .on_action(cx.listener(Self::focus_next))
2411 .on_action(cx.listener(Self::focus_prev))
2412 .on_action(cx.listener(Self::confirm))
2413 .on_action(cx.listener(Self::cancel))
2414 .child(
2415 Modal::new("keybinding_editor_modal", None)
2416 .header(
2417 ModalHeader::new().child(
2418 v_flex()
2419 .w_full()
2420 .pb_1p5()
2421 .mb_1()
2422 .gap_0p5()
2423 .border_b_1()
2424 .border_color(theme.border_variant)
2425 .child(Label::new(
2426 self.editing_keybind.action().humanized_name.clone(),
2427 ))
2428 .when_some(
2429 self.editing_keybind.action().documentation,
2430 |this, docs| {
2431 this.child(
2432 Label::new(docs)
2433 .size(LabelSize::Small)
2434 .color(Color::Muted),
2435 )
2436 },
2437 ),
2438 ),
2439 )
2440 .section(
2441 Section::new().child(
2442 v_flex()
2443 .gap_2p5()
2444 .child(
2445 v_flex()
2446 .gap_1()
2447 .child(Label::new("Edit Keystroke"))
2448 .child(self.keybind_editor.clone())
2449 .child(h_flex().gap_px().when(
2450 matching_bindings_count > 0,
2451 |this| {
2452 let label = format!(
2453 "There {} {} {} with the same keystrokes.",
2454 if matching_bindings_count == 1 {
2455 "is"
2456 } else {
2457 "are"
2458 },
2459 matching_bindings_count,
2460 if matching_bindings_count == 1 {
2461 "binding"
2462 } else {
2463 "bindings"
2464 }
2465 );
2466
2467 this.child(
2468 Label::new(label)
2469 .size(LabelSize::Small)
2470 .color(Color::Muted),
2471 )
2472 .child(
2473 Button::new("show_matching", "View")
2474 .label_size(LabelSize::Small)
2475 .icon(IconName::ArrowUpRight)
2476 .icon_color(Color::Muted)
2477 .icon_size(IconSize::Small)
2478 .on_click(cx.listener(
2479 |this, _, window, cx| {
2480 this.show_matching_bindings(
2481 window, cx,
2482 );
2483 },
2484 )),
2485 )
2486 },
2487 )),
2488 )
2489 .when_some(self.action_arguments_editor.clone(), |this, editor| {
2490 this.child(
2491 v_flex()
2492 .gap_1()
2493 .child(Label::new("Edit Arguments"))
2494 .child(editor),
2495 )
2496 })
2497 .child(self.context_editor.clone())
2498 .when_some(self.error.as_ref(), |this, error| {
2499 this.child(
2500 Banner::new()
2501 .severity(error.severity)
2502 .child(Label::new(error.content.clone())),
2503 )
2504 }),
2505 ),
2506 )
2507 .footer(
2508 ModalFooter::new().end_slot(
2509 h_flex()
2510 .gap_1()
2511 .child(
2512 Button::new("cancel", "Cancel")
2513 .on_click(cx.listener(|_, _, _, cx| cx.emit(DismissEvent))),
2514 )
2515 .child(Button::new("save-btn", "Save").on_click(cx.listener(
2516 |this, _event, _window, cx| {
2517 this.save_or_display_error(cx);
2518 },
2519 ))),
2520 ),
2521 ),
2522 )
2523 }
2524}
2525
2526struct KeybindingEditorModalFocusState {
2527 handles: Vec<FocusHandle>,
2528}
2529
2530impl KeybindingEditorModalFocusState {
2531 fn new(
2532 keystrokes: FocusHandle,
2533 action_input: Option<FocusHandle>,
2534 context: FocusHandle,
2535 ) -> Self {
2536 Self {
2537 handles: Vec::from_iter(
2538 [Some(keystrokes), action_input, Some(context)]
2539 .into_iter()
2540 .flatten(),
2541 ),
2542 }
2543 }
2544
2545 fn focused_index(&self, window: &Window, cx: &App) -> Option<i32> {
2546 self.handles
2547 .iter()
2548 .position(|handle| handle.contains_focused(window, cx))
2549 .map(|i| i as i32)
2550 }
2551
2552 fn focus_index(&self, mut index: i32, window: &mut Window) {
2553 if index < 0 {
2554 index = self.handles.len() as i32 - 1;
2555 }
2556 if index >= self.handles.len() as i32 {
2557 index = 0;
2558 }
2559 window.focus(&self.handles[index as usize]);
2560 }
2561
2562 fn focus_next(&self, window: &mut Window, cx: &App) {
2563 let index_to_focus = if let Some(index) = self.focused_index(window, cx) {
2564 index + 1
2565 } else {
2566 0
2567 };
2568 self.focus_index(index_to_focus, window);
2569 }
2570
2571 fn focus_previous(&self, window: &mut Window, cx: &App) {
2572 let index_to_focus = if let Some(index) = self.focused_index(window, cx) {
2573 index - 1
2574 } else {
2575 self.handles.len() as i32 - 1
2576 };
2577 self.focus_index(index_to_focus, window);
2578 }
2579}
2580
2581struct ActionArgumentsEditor {
2582 editor: Entity<Editor>,
2583 focus_handle: FocusHandle,
2584 is_loading: bool,
2585 /// See documentation in `KeymapEditor` for why a temp dir is needed.
2586 /// This field exists because the keymap editor temp dir creation may fail,
2587 /// and rather than implement a complicated retry mechanism, we simply
2588 /// fallback to trying to create a temporary directory in this editor on
2589 /// demand. Of note is that the TempDir struct will remove the directory
2590 /// when dropped.
2591 backup_temp_dir: Option<tempfile::TempDir>,
2592}
2593
2594impl Focusable for ActionArgumentsEditor {
2595 fn focus_handle(&self, _cx: &App) -> FocusHandle {
2596 self.focus_handle.clone()
2597 }
2598}
2599
2600impl ActionArgumentsEditor {
2601 fn new(
2602 action_name: &'static str,
2603 arguments: Option<SharedString>,
2604 temp_dir: Option<&std::path::Path>,
2605 workspace: WeakEntity<Workspace>,
2606 window: &mut Window,
2607 cx: &mut Context<Self>,
2608 ) -> Self {
2609 let focus_handle = cx.focus_handle();
2610 cx.on_focus_in(&focus_handle, window, |this, window, cx| {
2611 this.editor.focus_handle(cx).focus(window);
2612 })
2613 .detach();
2614 let editor = cx.new(|cx| {
2615 let mut editor = Editor::auto_height_unbounded(1, window, cx);
2616 Self::set_editor_text(&mut editor, arguments.clone(), window, cx);
2617 editor.set_read_only(true);
2618 editor
2619 });
2620
2621 let temp_dir = temp_dir.map(|path| path.to_owned());
2622 cx.spawn_in(window, async move |this, cx| {
2623 let result = async {
2624 let (project, fs) = workspace.read_with(cx, |workspace, _cx| {
2625 (
2626 workspace.project().downgrade(),
2627 workspace.app_state().fs.clone(),
2628 )
2629 })?;
2630
2631 let file_name =
2632 project::lsp_store::json_language_server_ext::normalized_action_file_name(
2633 action_name,
2634 );
2635
2636 let (buffer, backup_temp_dir) =
2637 Self::create_temp_buffer(temp_dir, file_name.clone(), project.clone(), fs, cx)
2638 .await
2639 .context(concat!(
2640 "Failed to create temporary buffer for action arguments. ",
2641 "Auto-complete will not work"
2642 ))?;
2643
2644 let editor = cx.new_window_entity(|window, cx| {
2645 let multi_buffer = cx.new(|cx| editor::MultiBuffer::singleton(buffer, cx));
2646 let mut editor = Editor::new(
2647 editor::EditorMode::Full {
2648 scale_ui_elements_with_buffer_font_size: true,
2649 show_active_line_background: false,
2650 sized_by_content: true,
2651 },
2652 multi_buffer,
2653 project.upgrade(),
2654 window,
2655 cx,
2656 );
2657 editor.set_searchable(false);
2658 editor.disable_scrollbars_and_minimap(window, cx);
2659 editor.set_show_edit_predictions(Some(false), window, cx);
2660 editor.set_show_gutter(false, cx);
2661 Self::set_editor_text(&mut editor, arguments, window, cx);
2662 editor
2663 })?;
2664
2665 this.update_in(cx, |this, window, cx| {
2666 if this.editor.focus_handle(cx).is_focused(window) {
2667 editor.focus_handle(cx).focus(window);
2668 }
2669 this.editor = editor;
2670 this.backup_temp_dir = backup_temp_dir;
2671 this.is_loading = false;
2672 })?;
2673
2674 anyhow::Ok(())
2675 }
2676 .await;
2677 if result.is_err() {
2678 let json_language = load_json_language(workspace.clone(), cx).await;
2679 this.update(cx, |this, cx| {
2680 this.editor.update(cx, |editor, cx| {
2681 if let Some(buffer) = editor.buffer().read(cx).as_singleton() {
2682 buffer.update(cx, |buffer, cx| {
2683 buffer.set_language(Some(json_language.clone()), cx)
2684 });
2685 }
2686 })
2687 // .context("Failed to load JSON language for editing keybinding action arguments input")
2688 })
2689 .ok();
2690 this.update(cx, |this, _cx| {
2691 this.is_loading = false;
2692 })
2693 .ok();
2694 }
2695 return result;
2696 })
2697 .detach_and_log_err(cx);
2698 Self {
2699 editor,
2700 focus_handle,
2701 is_loading: true,
2702 backup_temp_dir: None,
2703 }
2704 }
2705
2706 fn set_editor_text(
2707 editor: &mut Editor,
2708 arguments: Option<SharedString>,
2709 window: &mut Window,
2710 cx: &mut Context<Editor>,
2711 ) {
2712 if let Some(arguments) = arguments {
2713 editor.set_text(arguments, window, cx);
2714 } else {
2715 // TODO: default value from schema?
2716 editor.set_placeholder_text("Action Arguments", cx);
2717 }
2718 }
2719
2720 async fn create_temp_buffer(
2721 temp_dir: Option<std::path::PathBuf>,
2722 file_name: String,
2723 project: WeakEntity<Project>,
2724 fs: Arc<dyn Fs>,
2725 cx: &mut AsyncApp,
2726 ) -> anyhow::Result<(Entity<language::Buffer>, Option<tempfile::TempDir>)> {
2727 let (temp_file_path, temp_dir) = {
2728 let file_name = file_name.clone();
2729 async move {
2730 let temp_dir_backup = match temp_dir.as_ref() {
2731 Some(_) => None,
2732 None => {
2733 let temp_dir = paths::temp_dir();
2734 let sub_temp_dir = tempfile::Builder::new()
2735 .tempdir_in(temp_dir)
2736 .context("Failed to create temporary directory")?;
2737 Some(sub_temp_dir)
2738 }
2739 };
2740 let dir_path = temp_dir.as_deref().unwrap_or_else(|| {
2741 temp_dir_backup
2742 .as_ref()
2743 .expect("created backup tempdir")
2744 .path()
2745 });
2746 let path = dir_path.join(file_name);
2747 fs.create_file(
2748 &path,
2749 fs::CreateOptions {
2750 ignore_if_exists: true,
2751 overwrite: true,
2752 },
2753 )
2754 .await
2755 .context("Failed to create temporary file")?;
2756 anyhow::Ok((path, temp_dir_backup))
2757 }
2758 }
2759 .await
2760 .context("Failed to create backing file")?;
2761
2762 project
2763 .update(cx, |project, cx| {
2764 project.open_local_buffer(temp_file_path, cx)
2765 })?
2766 .await
2767 .context("Failed to create buffer")
2768 .map(|buffer| (buffer, temp_dir))
2769 }
2770}
2771
2772impl Render for ActionArgumentsEditor {
2773 fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
2774 let background_color;
2775 let border_color;
2776 let text_style = {
2777 let colors = cx.theme().colors();
2778 let settings = theme::ThemeSettings::get_global(cx);
2779 background_color = colors.editor_background;
2780 border_color = if self.is_loading {
2781 colors.border_disabled
2782 } else {
2783 colors.border_variant
2784 };
2785 TextStyleRefinement {
2786 font_size: Some(rems(0.875).into()),
2787 font_weight: Some(settings.buffer_font.weight),
2788 line_height: Some(relative(1.2)),
2789 font_style: Some(gpui::FontStyle::Normal),
2790 color: self.is_loading.then_some(colors.text_disabled),
2791 ..Default::default()
2792 }
2793 };
2794
2795 self.editor
2796 .update(cx, |editor, _| editor.set_text_style_refinement(text_style));
2797
2798 return v_flex().w_full().child(
2799 h_flex()
2800 .min_h_8()
2801 .min_w_48()
2802 .px_2()
2803 .py_1p5()
2804 .flex_grow()
2805 .rounded_lg()
2806 .bg(background_color)
2807 .border_1()
2808 .border_color(border_color)
2809 .track_focus(&self.focus_handle)
2810 .child(self.editor.clone()),
2811 );
2812 }
2813}
2814
2815struct KeyContextCompletionProvider {
2816 contexts: Vec<SharedString>,
2817}
2818
2819impl CompletionProvider for KeyContextCompletionProvider {
2820 fn completions(
2821 &self,
2822 _excerpt_id: editor::ExcerptId,
2823 buffer: &Entity<language::Buffer>,
2824 buffer_position: language::Anchor,
2825 _trigger: editor::CompletionContext,
2826 _window: &mut Window,
2827 cx: &mut Context<Editor>,
2828 ) -> gpui::Task<anyhow::Result<Vec<project::CompletionResponse>>> {
2829 let buffer = buffer.read(cx);
2830 let mut count_back = 0;
2831 for char in buffer.reversed_chars_at(buffer_position) {
2832 if char.is_ascii_alphanumeric() || char == '_' {
2833 count_back += 1;
2834 } else {
2835 break;
2836 }
2837 }
2838 let start_anchor = buffer.anchor_before(
2839 buffer_position
2840 .to_offset(&buffer)
2841 .saturating_sub(count_back),
2842 );
2843 let replace_range = start_anchor..buffer_position;
2844 gpui::Task::ready(Ok(vec![project::CompletionResponse {
2845 completions: self
2846 .contexts
2847 .iter()
2848 .map(|context| project::Completion {
2849 replace_range: replace_range.clone(),
2850 label: language::CodeLabel::plain(context.to_string(), None),
2851 new_text: context.to_string(),
2852 documentation: None,
2853 source: project::CompletionSource::Custom,
2854 icon_path: None,
2855 insert_text_mode: None,
2856 confirm: None,
2857 })
2858 .collect(),
2859 is_incomplete: false,
2860 }]))
2861 }
2862
2863 fn is_completion_trigger(
2864 &self,
2865 _buffer: &Entity<language::Buffer>,
2866 _position: language::Anchor,
2867 text: &str,
2868 _trigger_in_words: bool,
2869 _menu_is_open: bool,
2870 _cx: &mut Context<Editor>,
2871 ) -> bool {
2872 text.chars().last().map_or(false, |last_char| {
2873 last_char.is_ascii_alphanumeric() || last_char == '_'
2874 })
2875 }
2876}
2877
2878async fn load_json_language(workspace: WeakEntity<Workspace>, cx: &mut AsyncApp) -> Arc<Language> {
2879 let json_language_task = workspace
2880 .read_with(cx, |workspace, cx| {
2881 workspace
2882 .project()
2883 .read(cx)
2884 .languages()
2885 .language_for_name("JSON")
2886 })
2887 .context("Failed to load JSON language")
2888 .log_err();
2889 let json_language = match json_language_task {
2890 Some(task) => task.await.context("Failed to load JSON language").log_err(),
2891 None => None,
2892 };
2893 return json_language.unwrap_or_else(|| {
2894 Arc::new(Language::new(
2895 LanguageConfig {
2896 name: "JSON".into(),
2897 ..Default::default()
2898 },
2899 Some(tree_sitter_json::LANGUAGE.into()),
2900 ))
2901 });
2902}
2903
2904async fn load_keybind_context_language(
2905 workspace: WeakEntity<Workspace>,
2906 cx: &mut AsyncApp,
2907) -> Arc<Language> {
2908 let language_task = workspace
2909 .read_with(cx, |workspace, cx| {
2910 workspace
2911 .project()
2912 .read(cx)
2913 .languages()
2914 .language_for_name("Zed Keybind Context")
2915 })
2916 .context("Failed to load Zed Keybind Context language")
2917 .log_err();
2918 let language = match language_task {
2919 Some(task) => task
2920 .await
2921 .context("Failed to load Zed Keybind Context language")
2922 .log_err(),
2923 None => None,
2924 };
2925 return language.unwrap_or_else(|| {
2926 Arc::new(Language::new(
2927 LanguageConfig {
2928 name: "Zed Keybind Context".into(),
2929 ..Default::default()
2930 },
2931 Some(tree_sitter_rust::LANGUAGE.into()),
2932 ))
2933 });
2934}
2935
2936async fn save_keybinding_update(
2937 create: bool,
2938 existing: ProcessedBinding,
2939 action_mapping: &ActionMapping,
2940 new_args: Option<&str>,
2941 fs: &Arc<dyn Fs>,
2942 tab_size: usize,
2943) -> anyhow::Result<()> {
2944 let keymap_contents = settings::KeymapFile::load_keymap_file(fs)
2945 .await
2946 .context("Failed to load keymap file")?;
2947
2948 let existing_keystrokes = existing.keystrokes().unwrap_or_default();
2949 let existing_context = existing.context().and_then(KeybindContextString::local_str);
2950 let existing_args = existing
2951 .action()
2952 .arguments
2953 .as_ref()
2954 .map(|args| args.text.as_ref());
2955
2956 let target = settings::KeybindUpdateTarget {
2957 context: existing_context,
2958 keystrokes: existing_keystrokes,
2959 action_name: &existing.action().name,
2960 action_arguments: existing_args,
2961 };
2962
2963 let source = settings::KeybindUpdateTarget {
2964 context: action_mapping.context.as_ref().map(|a| &***a),
2965 keystrokes: &action_mapping.keystrokes,
2966 action_name: &existing.action().name,
2967 action_arguments: new_args,
2968 };
2969
2970 let operation = if !create {
2971 settings::KeybindUpdateOperation::Replace {
2972 target,
2973 target_keybind_source: existing.keybind_source().unwrap_or(KeybindSource::User),
2974 source,
2975 }
2976 } else {
2977 settings::KeybindUpdateOperation::Add {
2978 source,
2979 from: Some(target),
2980 }
2981 };
2982
2983 let (new_keybinding, removed_keybinding, source) = operation.generate_telemetry();
2984
2985 let updated_keymap_contents =
2986 settings::KeymapFile::update_keybinding(operation, keymap_contents, tab_size)
2987 .map_err(|err| anyhow::anyhow!("Could not save updated keybinding: {}", err))?;
2988 fs.write(
2989 paths::keymap_file().as_path(),
2990 updated_keymap_contents.as_bytes(),
2991 )
2992 .await
2993 .context("Failed to write keymap file")?;
2994
2995 telemetry::event!(
2996 "Keybinding Updated",
2997 new_keybinding = new_keybinding,
2998 removed_keybinding = removed_keybinding,
2999 source = source
3000 );
3001 Ok(())
3002}
3003
3004async fn remove_keybinding(
3005 existing: ProcessedBinding,
3006 fs: &Arc<dyn Fs>,
3007 tab_size: usize,
3008) -> anyhow::Result<()> {
3009 let Some(keystrokes) = existing.keystrokes() else {
3010 anyhow::bail!("Cannot remove a keybinding that does not exist");
3011 };
3012 let keymap_contents = settings::KeymapFile::load_keymap_file(fs)
3013 .await
3014 .context("Failed to load keymap file")?;
3015
3016 let operation = settings::KeybindUpdateOperation::Remove {
3017 target: settings::KeybindUpdateTarget {
3018 context: existing.context().and_then(KeybindContextString::local_str),
3019 keystrokes,
3020 action_name: &existing.action().name,
3021 action_arguments: existing
3022 .action()
3023 .arguments
3024 .as_ref()
3025 .map(|arguments| arguments.text.as_ref()),
3026 },
3027 target_keybind_source: existing.keybind_source().unwrap_or(KeybindSource::User),
3028 };
3029
3030 let (new_keybinding, removed_keybinding, source) = operation.generate_telemetry();
3031 let updated_keymap_contents =
3032 settings::KeymapFile::update_keybinding(operation, keymap_contents, tab_size)
3033 .context("Failed to update keybinding")?;
3034 fs.write(
3035 paths::keymap_file().as_path(),
3036 updated_keymap_contents.as_bytes(),
3037 )
3038 .await
3039 .context("Failed to write keymap file")?;
3040
3041 telemetry::event!(
3042 "Keybinding Removed",
3043 new_keybinding = new_keybinding,
3044 removed_keybinding = removed_keybinding,
3045 source = source
3046 );
3047 Ok(())
3048}
3049
3050fn collect_contexts_from_assets() -> Vec<SharedString> {
3051 let mut keymap_assets = vec![
3052 util::asset_str::<SettingsAssets>(settings::DEFAULT_KEYMAP_PATH),
3053 util::asset_str::<SettingsAssets>(settings::VIM_KEYMAP_PATH),
3054 ];
3055 keymap_assets.extend(
3056 BaseKeymap::OPTIONS
3057 .iter()
3058 .filter_map(|(_, base_keymap)| base_keymap.asset_path())
3059 .map(util::asset_str::<SettingsAssets>),
3060 );
3061
3062 let mut contexts = HashSet::default();
3063
3064 for keymap_asset in keymap_assets {
3065 let Ok(keymap) = KeymapFile::parse(&keymap_asset) else {
3066 continue;
3067 };
3068
3069 for section in keymap.sections() {
3070 let context_expr = §ion.context;
3071 let mut queue = Vec::new();
3072 let Ok(root_context) = gpui::KeyBindingContextPredicate::parse(context_expr) else {
3073 continue;
3074 };
3075
3076 queue.push(root_context);
3077 while let Some(context) = queue.pop() {
3078 match context {
3079 gpui::KeyBindingContextPredicate::Identifier(ident) => {
3080 contexts.insert(ident);
3081 }
3082 gpui::KeyBindingContextPredicate::Equal(ident_a, ident_b) => {
3083 contexts.insert(ident_a);
3084 contexts.insert(ident_b);
3085 }
3086 gpui::KeyBindingContextPredicate::NotEqual(ident_a, ident_b) => {
3087 contexts.insert(ident_a);
3088 contexts.insert(ident_b);
3089 }
3090 gpui::KeyBindingContextPredicate::Descendant(ctx_a, ctx_b) => {
3091 queue.push(*ctx_a);
3092 queue.push(*ctx_b);
3093 }
3094 gpui::KeyBindingContextPredicate::Not(ctx) => {
3095 queue.push(*ctx);
3096 }
3097 gpui::KeyBindingContextPredicate::And(ctx_a, ctx_b) => {
3098 queue.push(*ctx_a);
3099 queue.push(*ctx_b);
3100 }
3101 gpui::KeyBindingContextPredicate::Or(ctx_a, ctx_b) => {
3102 queue.push(*ctx_a);
3103 queue.push(*ctx_b);
3104 }
3105 }
3106 }
3107 }
3108 }
3109
3110 let mut contexts = contexts.into_iter().collect::<Vec<_>>();
3111 contexts.sort();
3112
3113 return contexts;
3114}
3115
3116impl SerializableItem for KeymapEditor {
3117 fn serialized_item_kind() -> &'static str {
3118 "KeymapEditor"
3119 }
3120
3121 fn cleanup(
3122 workspace_id: workspace::WorkspaceId,
3123 alive_items: Vec<workspace::ItemId>,
3124 _window: &mut Window,
3125 cx: &mut App,
3126 ) -> gpui::Task<gpui::Result<()>> {
3127 workspace::delete_unloaded_items(
3128 alive_items,
3129 workspace_id,
3130 "keybinding_editors",
3131 &KEYBINDING_EDITORS,
3132 cx,
3133 )
3134 }
3135
3136 fn deserialize(
3137 _project: Entity<project::Project>,
3138 workspace: WeakEntity<Workspace>,
3139 workspace_id: workspace::WorkspaceId,
3140 item_id: workspace::ItemId,
3141 window: &mut Window,
3142 cx: &mut App,
3143 ) -> gpui::Task<gpui::Result<Entity<Self>>> {
3144 window.spawn(cx, async move |cx| {
3145 if KEYBINDING_EDITORS
3146 .get_keybinding_editor(item_id, workspace_id)?
3147 .is_some()
3148 {
3149 cx.update(|window, cx| cx.new(|cx| KeymapEditor::new(workspace, window, cx)))
3150 } else {
3151 Err(anyhow!("No keybinding editor to deserialize"))
3152 }
3153 })
3154 }
3155
3156 fn serialize(
3157 &mut self,
3158 workspace: &mut Workspace,
3159 item_id: workspace::ItemId,
3160 _closing: bool,
3161 _window: &mut Window,
3162 cx: &mut ui::Context<Self>,
3163 ) -> Option<gpui::Task<gpui::Result<()>>> {
3164 let workspace_id = workspace.database_id()?;
3165 Some(cx.background_spawn(async move {
3166 KEYBINDING_EDITORS
3167 .save_keybinding_editor(item_id, workspace_id)
3168 .await
3169 }))
3170 }
3171
3172 fn should_serialize(&self, _event: &Self::Event) -> bool {
3173 false
3174 }
3175}
3176
3177mod persistence {
3178 use db::{define_connection, query, sqlez_macros::sql};
3179 use workspace::WorkspaceDb;
3180
3181 define_connection! {
3182 pub static ref KEYBINDING_EDITORS: KeybindingEditorDb<WorkspaceDb> =
3183 &[sql!(
3184 CREATE TABLE keybinding_editors (
3185 workspace_id INTEGER,
3186 item_id INTEGER UNIQUE,
3187
3188 PRIMARY KEY(workspace_id, item_id),
3189 FOREIGN KEY(workspace_id) REFERENCES workspaces(workspace_id)
3190 ON DELETE CASCADE
3191 ) STRICT;
3192 )];
3193 }
3194
3195 impl KeybindingEditorDb {
3196 query! {
3197 pub async fn save_keybinding_editor(
3198 item_id: workspace::ItemId,
3199 workspace_id: workspace::WorkspaceId
3200 ) -> Result<()> {
3201 INSERT OR REPLACE INTO keybinding_editors(item_id, workspace_id)
3202 VALUES (?, ?)
3203 }
3204 }
3205
3206 query! {
3207 pub fn get_keybinding_editor(
3208 item_id: workspace::ItemId,
3209 workspace_id: workspace::WorkspaceId
3210 ) -> Result<Option<workspace::ItemId>> {
3211 SELECT item_id
3212 FROM keybinding_editors
3213 WHERE item_id = ? AND workspace_id = ?
3214 }
3215 }
3216 }
3217}