1use anyhow::{Context as _, Result};
2use collections::{BTreeMap, HashMap, IndexMap};
3use fs::Fs;
4use gpui::{
5 Action, ActionBuildError, App, InvalidKeystrokeError, KEYSTROKE_PARSE_EXPECTED_MESSAGE,
6 KeyBinding, KeyBindingContextPredicate, KeyBindingMetaIndex, KeybindingKeystroke, Keystroke,
7 NoAction, SharedString,
8};
9use schemars::{JsonSchema, json_schema};
10use serde::Deserialize;
11use serde_json::{Value, json};
12use std::borrow::Cow;
13use std::{any::TypeId, fmt::Write, rc::Rc, sync::Arc, sync::LazyLock};
14use util::ResultExt as _;
15use util::{
16 asset_str,
17 markdown::{MarkdownEscaped, MarkdownInlineCode, MarkdownString},
18};
19
20use crate::{
21 SettingsAssets, append_top_level_array_value_in_json_text, parse_json_with_comments,
22 replace_top_level_array_value_in_json_text,
23};
24
25pub trait KeyBindingValidator: Send + Sync {
26 fn action_type_id(&self) -> TypeId;
27 fn validate(&self, binding: &KeyBinding) -> Result<(), MarkdownString>;
28}
29
30pub struct KeyBindingValidatorRegistration(pub fn() -> Box<dyn KeyBindingValidator>);
31
32inventory::collect!(KeyBindingValidatorRegistration);
33
34pub(crate) static KEY_BINDING_VALIDATORS: LazyLock<BTreeMap<TypeId, Box<dyn KeyBindingValidator>>> =
35 LazyLock::new(|| {
36 let mut validators = BTreeMap::new();
37 for validator_registration in inventory::iter::<KeyBindingValidatorRegistration> {
38 let validator = validator_registration.0();
39 validators.insert(validator.action_type_id(), validator);
40 }
41 validators
42 });
43
44// Note that the doc comments on these are shown by json-language-server when editing the keymap, so
45// they should be considered user-facing documentation. Documentation is not handled well with
46// schemars-0.8 - when there are newlines, it is rendered as plaintext (see
47// https://github.com/GREsau/schemars/issues/38#issuecomment-2282883519). So for now these docs
48// avoid newlines.
49//
50// TODO: Update to schemars-1.0 once it's released, and add more docs as newlines would be
51// supported. Tracking issue is https://github.com/GREsau/schemars/issues/112.
52
53/// Keymap configuration consisting of sections. Each section may have a context predicate which
54/// determines whether its bindings are used.
55#[derive(Debug, Deserialize, Default, Clone, JsonSchema)]
56#[serde(transparent)]
57pub struct KeymapFile(Vec<KeymapSection>);
58
59/// Keymap section which binds keystrokes to actions.
60#[derive(Debug, Deserialize, Default, Clone, JsonSchema)]
61pub struct KeymapSection {
62 /// Determines when these bindings are active. When just a name is provided, like `Editor` or
63 /// `Workspace`, the bindings will be active in that context. Boolean expressions like `X && Y`,
64 /// `X || Y`, `!X` are also supported. Some more complex logic including checking OS and the
65 /// current file extension are also supported - see [the
66 /// documentation](https://zed.dev/docs/key-bindings#contexts) for more details.
67 #[serde(default)]
68 pub context: String,
69 /// This option enables specifying keys based on their position on a QWERTY keyboard, by using
70 /// position-equivalent mappings for some non-QWERTY keyboards. This is currently only supported
71 /// on macOS. See the documentation for more details.
72 #[serde(default)]
73 use_key_equivalents: bool,
74 /// This keymap section's bindings, as a JSON object mapping keystrokes to actions. The
75 /// keystrokes key is a string representing a sequence of keystrokes to type, where the
76 /// keystrokes are separated by whitespace. Each keystroke is a sequence of modifiers (`ctrl`,
77 /// `alt`, `shift`, `fn`, `cmd`, `super`, or `win`) followed by a key, separated by `-`. The
78 /// order of bindings does matter. When the same keystrokes are bound at the same context depth,
79 /// the binding that occurs later in the file is preferred. For displaying keystrokes in the UI,
80 /// the later binding for the same action is preferred.
81 #[serde(default)]
82 bindings: Option<IndexMap<String, KeymapAction>>,
83 #[serde(flatten)]
84 unrecognized_fields: IndexMap<String, Value>,
85 // This struct intentionally uses permissive types for its fields, rather than validating during
86 // deserialization. The purpose of this is to allow loading the portion of the keymap that doesn't
87 // have errors. The downside of this is that the errors are not reported with line+column info.
88 // Unfortunately the implementations of the `Spanned` types for preserving this information are
89 // highly inconvenient (`serde_spanned`) and in some cases don't work at all here
90 // (`json_spanned_>value`). Serde should really have builtin support for this.
91}
92
93impl KeymapSection {
94 pub fn bindings(&self) -> impl DoubleEndedIterator<Item = (&String, &KeymapAction)> {
95 self.bindings.iter().flatten()
96 }
97}
98
99/// Keymap action as a JSON value, since it can either be null for no action, or the name of the
100/// action, or an array of the name of the action and the action input.
101///
102/// Unlike the other json types involved in keymaps (including actions), this doc-comment will not
103/// be included in the generated JSON schema, as it manually defines its `JsonSchema` impl. The
104/// actual schema used for it is automatically generated in `KeymapFile::generate_json_schema`.
105#[derive(Debug, Deserialize, Default, Clone)]
106#[serde(transparent)]
107pub struct KeymapAction(Value);
108
109impl std::fmt::Display for KeymapAction {
110 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
111 match &self.0 {
112 Value::String(s) => write!(f, "{}", s),
113 Value::Array(arr) => {
114 let strings: Vec<String> = arr.iter().map(|v| v.to_string()).collect();
115 write!(f, "{}", strings.join(", "))
116 }
117 _ => write!(f, "{}", self.0),
118 }
119 }
120}
121
122impl JsonSchema for KeymapAction {
123 /// This is used when generating the JSON schema for the `KeymapAction` type, so that it can
124 /// reference the keymap action schema.
125 fn schema_name() -> Cow<'static, str> {
126 "KeymapAction".into()
127 }
128
129 /// This schema will be replaced with the full action schema in
130 /// `KeymapFile::generate_json_schema`.
131 fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
132 json_schema!(true)
133 }
134}
135
136#[derive(Debug)]
137#[must_use]
138pub enum KeymapFileLoadResult {
139 Success {
140 key_bindings: Vec<KeyBinding>,
141 },
142 SomeFailedToLoad {
143 key_bindings: Vec<KeyBinding>,
144 error_message: MarkdownString,
145 },
146 JsonParseFailure {
147 error: anyhow::Error,
148 },
149}
150
151impl KeymapFile {
152 pub fn parse(content: &str) -> anyhow::Result<Self> {
153 parse_json_with_comments::<Self>(content)
154 }
155
156 pub fn load_asset(
157 asset_path: &str,
158 source: Option<KeybindSource>,
159 cx: &App,
160 ) -> anyhow::Result<Vec<KeyBinding>> {
161 match Self::load(asset_str::<SettingsAssets>(asset_path).as_ref(), cx) {
162 KeymapFileLoadResult::Success { mut key_bindings } => match source {
163 Some(source) => Ok({
164 for key_binding in &mut key_bindings {
165 key_binding.set_meta(source.meta());
166 }
167 key_bindings
168 }),
169 None => Ok(key_bindings),
170 },
171 KeymapFileLoadResult::SomeFailedToLoad { error_message, .. } => {
172 anyhow::bail!("Error loading built-in keymap \"{asset_path}\": {error_message}",)
173 }
174 KeymapFileLoadResult::JsonParseFailure { error } => {
175 anyhow::bail!("JSON parse error in built-in keymap \"{asset_path}\": {error}")
176 }
177 }
178 }
179
180 #[cfg(feature = "test-support")]
181 pub fn load_asset_allow_partial_failure(
182 asset_path: &str,
183 cx: &App,
184 ) -> anyhow::Result<Vec<KeyBinding>> {
185 match Self::load(asset_str::<SettingsAssets>(asset_path).as_ref(), cx) {
186 KeymapFileLoadResult::SomeFailedToLoad {
187 key_bindings,
188 error_message,
189 ..
190 } if key_bindings.is_empty() => {
191 anyhow::bail!("Error loading built-in keymap \"{asset_path}\": {error_message}",)
192 }
193 KeymapFileLoadResult::Success { key_bindings, .. }
194 | KeymapFileLoadResult::SomeFailedToLoad { key_bindings, .. } => Ok(key_bindings),
195 KeymapFileLoadResult::JsonParseFailure { error } => {
196 anyhow::bail!("JSON parse error in built-in keymap \"{asset_path}\": {error}")
197 }
198 }
199 }
200
201 #[cfg(feature = "test-support")]
202 pub fn load_panic_on_failure(content: &str, cx: &App) -> Vec<KeyBinding> {
203 match Self::load(content, cx) {
204 KeymapFileLoadResult::Success { key_bindings, .. } => key_bindings,
205 KeymapFileLoadResult::SomeFailedToLoad { error_message, .. } => {
206 panic!("{error_message}");
207 }
208 KeymapFileLoadResult::JsonParseFailure { error } => {
209 panic!("JSON parse error: {error}");
210 }
211 }
212 }
213
214 pub fn load(content: &str, cx: &App) -> KeymapFileLoadResult {
215 if content.is_empty() {
216 return KeymapFileLoadResult::Success {
217 key_bindings: Vec::new(),
218 };
219 }
220 let keymap_file = match Self::parse(content) {
221 Ok(keymap_file) => keymap_file,
222 Err(error) => {
223 return KeymapFileLoadResult::JsonParseFailure { error };
224 }
225 };
226
227 // Accumulate errors in order to support partial load of user keymap in the presence of
228 // errors in context and binding parsing.
229 let mut errors = Vec::new();
230 let mut key_bindings = Vec::new();
231
232 for KeymapSection {
233 context,
234 use_key_equivalents,
235 bindings,
236 unrecognized_fields,
237 } in keymap_file.0.iter()
238 {
239 let context_predicate: Option<Rc<KeyBindingContextPredicate>> = if context.is_empty() {
240 None
241 } else {
242 match KeyBindingContextPredicate::parse(context) {
243 Ok(context_predicate) => Some(context_predicate.into()),
244 Err(err) => {
245 // Leading space is to separate from the message indicating which section
246 // the error occurred in.
247 errors.push((
248 context,
249 format!(" Parse error in section `context` field: {}", err),
250 ));
251 continue;
252 }
253 }
254 };
255
256 let mut section_errors = String::new();
257
258 if !unrecognized_fields.is_empty() {
259 write!(
260 section_errors,
261 "\n\n - Unrecognized fields: {}",
262 MarkdownInlineCode(&format!("{:?}", unrecognized_fields.keys()))
263 )
264 .unwrap();
265 }
266
267 if let Some(bindings) = bindings {
268 for (keystrokes, action) in bindings {
269 let result = Self::load_keybinding(
270 keystrokes,
271 action,
272 context_predicate.clone(),
273 *use_key_equivalents,
274 cx,
275 );
276 match result {
277 Ok(key_binding) => {
278 key_bindings.push(key_binding);
279 }
280 Err(err) => {
281 let mut lines = err.lines();
282 let mut indented_err = lines.next().unwrap().to_string();
283 for line in lines {
284 indented_err.push_str(" ");
285 indented_err.push_str(line);
286 indented_err.push_str("\n");
287 }
288 write!(
289 section_errors,
290 "\n\n- In binding {}, {indented_err}",
291 MarkdownInlineCode(&format!("\"{}\"", keystrokes))
292 )
293 .unwrap();
294 }
295 }
296 }
297 }
298
299 if !section_errors.is_empty() {
300 errors.push((context, section_errors))
301 }
302 }
303
304 if errors.is_empty() {
305 KeymapFileLoadResult::Success { key_bindings }
306 } else {
307 let mut error_message = "Errors in user keymap file.\n".to_owned();
308 for (context, section_errors) in errors {
309 if context.is_empty() {
310 let _ = write!(error_message, "\n\nIn section without context predicate:");
311 } else {
312 let _ = write!(
313 error_message,
314 "\n\nIn section with {}:",
315 MarkdownInlineCode(&format!("context = \"{}\"", context))
316 );
317 }
318 let _ = write!(error_message, "{section_errors}");
319 }
320 KeymapFileLoadResult::SomeFailedToLoad {
321 key_bindings,
322 error_message: MarkdownString(error_message),
323 }
324 }
325 }
326
327 fn load_keybinding(
328 keystrokes: &str,
329 action: &KeymapAction,
330 context: Option<Rc<KeyBindingContextPredicate>>,
331 use_key_equivalents: bool,
332 cx: &App,
333 ) -> std::result::Result<KeyBinding, String> {
334 let (build_result, action_input_string) = match &action.0 {
335 Value::Array(items) => {
336 if items.len() != 2 {
337 return Err(format!(
338 "expected two-element array of `[name, input]`. \
339 Instead found {}.",
340 MarkdownInlineCode(&action.0.to_string())
341 ));
342 }
343 let serde_json::Value::String(ref name) = items[0] else {
344 return Err(format!(
345 "expected two-element array of `[name, input]`, \
346 but the first element is not a string in {}.",
347 MarkdownInlineCode(&action.0.to_string())
348 ));
349 };
350 let action_input = items[1].clone();
351 let action_input_string = action_input.to_string();
352 (
353 cx.build_action(name, Some(action_input)),
354 Some(action_input_string),
355 )
356 }
357 Value::String(name) => (cx.build_action(name, None), None),
358 Value::Null => (Ok(NoAction.boxed_clone()), None),
359 _ => {
360 return Err(format!(
361 "expected two-element array of `[name, input]`. \
362 Instead found {}.",
363 MarkdownInlineCode(&action.0.to_string())
364 ));
365 }
366 };
367
368 let action = match build_result {
369 Ok(action) => action,
370 Err(ActionBuildError::NotFound { name }) => {
371 return Err(format!(
372 "didn't find an action named {}.",
373 MarkdownInlineCode(&format!("\"{}\"", &name))
374 ));
375 }
376 Err(ActionBuildError::BuildError { name, error }) => match action_input_string {
377 Some(action_input_string) => {
378 return Err(format!(
379 "can't build {} action from input value {}: {}",
380 MarkdownInlineCode(&format!("\"{}\"", &name)),
381 MarkdownInlineCode(&action_input_string),
382 MarkdownEscaped(&error.to_string())
383 ));
384 }
385 None => {
386 return Err(format!(
387 "can't build {} action - it requires input data via [name, input]: {}",
388 MarkdownInlineCode(&format!("\"{}\"", &name)),
389 MarkdownEscaped(&error.to_string())
390 ));
391 }
392 },
393 };
394
395 let key_binding = match KeyBinding::load(
396 keystrokes,
397 action,
398 context,
399 use_key_equivalents,
400 action_input_string.map(SharedString::from),
401 cx.keyboard_mapper().as_ref(),
402 ) {
403 Ok(key_binding) => key_binding,
404 Err(InvalidKeystrokeError { keystroke }) => {
405 return Err(format!(
406 "invalid keystroke {}. {}",
407 MarkdownInlineCode(&format!("\"{}\"", &keystroke)),
408 KEYSTROKE_PARSE_EXPECTED_MESSAGE
409 ));
410 }
411 };
412
413 if let Some(validator) = KEY_BINDING_VALIDATORS.get(&key_binding.action().type_id()) {
414 match validator.validate(&key_binding) {
415 Ok(()) => Ok(key_binding),
416 Err(error) => Err(error.0),
417 }
418 } else {
419 Ok(key_binding)
420 }
421 }
422
423 /// Creates a JSON schema generator, suitable for generating json schemas
424 /// for actions
425 pub fn action_schema_generator() -> schemars::SchemaGenerator {
426 schemars::generate::SchemaSettings::draft2019_09().into_generator()
427 }
428
429 pub fn generate_json_schema_for_registered_actions(cx: &mut App) -> Value {
430 // instead of using DefaultDenyUnknownFields, actions typically use
431 // `#[serde(deny_unknown_fields)]` so that these cases are reported as parse failures. This
432 // is because the rest of the keymap will still load in these cases, whereas other settings
433 // files would not.
434 let mut generator = Self::action_schema_generator();
435
436 let action_schemas = cx.action_schemas(&mut generator);
437 let deprecations = cx.deprecated_actions_to_preferred_actions();
438 let deprecation_messages = cx.action_deprecation_messages();
439 KeymapFile::generate_json_schema(
440 generator,
441 action_schemas,
442 deprecations,
443 deprecation_messages,
444 )
445 }
446
447 fn generate_json_schema(
448 mut generator: schemars::SchemaGenerator,
449 action_schemas: Vec<(&'static str, Option<schemars::Schema>)>,
450 deprecations: &HashMap<&'static str, &'static str>,
451 deprecation_messages: &HashMap<&'static str, &'static str>,
452 ) -> serde_json::Value {
453 fn add_deprecation(schema: &mut schemars::Schema, message: String) {
454 schema.insert(
455 // deprecationMessage is not part of the JSON Schema spec, but
456 // json-language-server recognizes it.
457 "deprecationMessage".to_string(),
458 Value::String(message),
459 );
460 }
461
462 fn add_deprecation_preferred_name(schema: &mut schemars::Schema, new_name: &str) {
463 add_deprecation(schema, format!("Deprecated, use {new_name}"));
464 }
465
466 fn add_description(schema: &mut schemars::Schema, description: String) {
467 schema.insert("description".to_string(), Value::String(description));
468 }
469
470 let empty_object = json_schema!({
471 "type": "object"
472 });
473
474 // This is a workaround for a json-language-server issue where it matches the first
475 // alternative that matches the value's shape and uses that for documentation.
476 //
477 // In the case of the array validations, it would even provide an error saying that the name
478 // must match the name of the first alternative.
479 let mut plain_action = json_schema!({
480 "type": "string",
481 "const": ""
482 });
483 let no_action_message = "No action named this.";
484 add_description(&mut plain_action, no_action_message.to_owned());
485 add_deprecation(&mut plain_action, no_action_message.to_owned());
486
487 let mut matches_action_name = json_schema!({
488 "const": ""
489 });
490 let no_action_message_input = "No action named this that takes input.";
491 add_description(&mut matches_action_name, no_action_message_input.to_owned());
492 add_deprecation(&mut matches_action_name, no_action_message_input.to_owned());
493
494 let action_with_input = json_schema!({
495 "type": "array",
496 "items": [
497 matches_action_name,
498 true
499 ],
500 "minItems": 2,
501 "maxItems": 2
502 });
503 let mut keymap_action_alternatives = vec![plain_action, action_with_input];
504
505 for (name, action_schema) in action_schemas.into_iter() {
506 let description = action_schema.as_ref().and_then(|schema| {
507 schema
508 .as_object()
509 .and_then(|obj| obj.get("description"))
510 .and_then(|v| v.as_str())
511 .map(|s| s.to_string())
512 });
513
514 let deprecation = if name == NoAction.name() {
515 Some("null")
516 } else {
517 deprecations.get(name).copied()
518 };
519
520 // Add an alternative for plain action names.
521 let mut plain_action = json_schema!({
522 "type": "string",
523 "const": name
524 });
525 if let Some(message) = deprecation_messages.get(name) {
526 add_deprecation(&mut plain_action, message.to_string());
527 } else if let Some(new_name) = deprecation {
528 add_deprecation_preferred_name(&mut plain_action, new_name);
529 }
530 if let Some(desc) = description.clone() {
531 add_description(&mut plain_action, desc);
532 }
533 keymap_action_alternatives.push(plain_action);
534
535 // Add an alternative for actions with data specified as a [name, data] array.
536 //
537 // When a struct with no deserializable fields is added by deriving `Action`, an empty
538 // object schema is produced. The action should be invoked without data in this case.
539 if let Some(schema) = action_schema
540 && schema != empty_object
541 {
542 let mut matches_action_name = json_schema!({
543 "const": name
544 });
545 if let Some(desc) = description.clone() {
546 add_description(&mut matches_action_name, desc);
547 }
548 if let Some(message) = deprecation_messages.get(name) {
549 add_deprecation(&mut matches_action_name, message.to_string());
550 } else if let Some(new_name) = deprecation {
551 add_deprecation_preferred_name(&mut matches_action_name, new_name);
552 }
553 let action_with_input = json_schema!({
554 "type": "array",
555 "items": [matches_action_name, schema],
556 "minItems": 2,
557 "maxItems": 2
558 });
559 keymap_action_alternatives.push(action_with_input);
560 }
561 }
562
563 // Placing null first causes json-language-server to default assuming actions should be
564 // null, so place it last.
565 keymap_action_alternatives.push(json_schema!({
566 "type": "null"
567 }));
568
569 // The `KeymapSection` schema will reference the `KeymapAction` schema by name, so setting
570 // the definition of `KeymapAction` results in the full action schema being used.
571 generator.definitions_mut().insert(
572 KeymapAction::schema_name().to_string(),
573 json!({
574 "oneOf": keymap_action_alternatives
575 }),
576 );
577
578 generator.root_schema_for::<KeymapFile>().to_value()
579 }
580
581 pub fn sections(&self) -> impl DoubleEndedIterator<Item = &KeymapSection> {
582 self.0.iter()
583 }
584
585 pub async fn load_keymap_file(fs: &Arc<dyn Fs>) -> Result<String> {
586 match fs.load(paths::keymap_file()).await {
587 result @ Ok(_) => result,
588 Err(err) => {
589 if let Some(e) = err.downcast_ref::<std::io::Error>()
590 && e.kind() == std::io::ErrorKind::NotFound
591 {
592 return Ok(crate::initial_keymap_content().to_string());
593 }
594 Err(err)
595 }
596 }
597 }
598
599 pub fn update_keybinding<'a>(
600 mut operation: KeybindUpdateOperation<'a>,
601 mut keymap_contents: String,
602 tab_size: usize,
603 keyboard_mapper: &dyn gpui::PlatformKeyboardMapper,
604 ) -> Result<String> {
605 match operation {
606 // if trying to replace a keybinding that is not user-defined, treat it as an add operation
607 KeybindUpdateOperation::Replace {
608 target_keybind_source: target_source,
609 source,
610 target,
611 } if target_source != KeybindSource::User => {
612 operation = KeybindUpdateOperation::Add {
613 source,
614 from: Some(target),
615 };
616 }
617 // if trying to remove a keybinding that is not user-defined, treat it as creating a binding
618 // that binds it to `zed::NoAction`
619 KeybindUpdateOperation::Remove {
620 target,
621 target_keybind_source,
622 } if target_keybind_source != KeybindSource::User => {
623 let mut source = target.clone();
624 source.action_name = gpui::NoAction.name();
625 source.action_arguments.take();
626 operation = KeybindUpdateOperation::Add {
627 source,
628 from: Some(target),
629 };
630 }
631 _ => {}
632 }
633
634 // Sanity check that keymap contents are valid, even though we only use it for Replace.
635 // We don't want to modify the file if it's invalid.
636 let keymap = Self::parse(&keymap_contents).context("Failed to parse keymap")?;
637
638 if let KeybindUpdateOperation::Remove { target, .. } = operation {
639 let target_action_value = target
640 .action_value()
641 .context("Failed to generate target action JSON value")?;
642 let Some((index, keystrokes_str)) =
643 find_binding(&keymap, &target, &target_action_value, keyboard_mapper)
644 else {
645 anyhow::bail!("Failed to find keybinding to remove");
646 };
647 let is_only_binding = keymap.0[index]
648 .bindings
649 .as_ref()
650 .is_none_or(|bindings| bindings.len() == 1);
651 let key_path: &[&str] = if is_only_binding {
652 &[]
653 } else {
654 &["bindings", keystrokes_str]
655 };
656 let (replace_range, replace_value) = replace_top_level_array_value_in_json_text(
657 &keymap_contents,
658 key_path,
659 None,
660 None,
661 index,
662 tab_size,
663 )
664 .context("Failed to remove keybinding")?;
665 keymap_contents.replace_range(replace_range, &replace_value);
666 return Ok(keymap_contents);
667 }
668
669 if let KeybindUpdateOperation::Replace { source, target, .. } = operation {
670 let target_action_value = target
671 .action_value()
672 .context("Failed to generate target action JSON value")?;
673 let source_action_value = source
674 .action_value()
675 .context("Failed to generate source action JSON value")?;
676
677 if let Some((index, keystrokes_str)) =
678 find_binding(&keymap, &target, &target_action_value, keyboard_mapper)
679 {
680 if target.context == source.context {
681 // if we are only changing the keybinding (common case)
682 // not the context, etc. Then just update the binding in place
683
684 let (replace_range, replace_value) =
685 replace_top_level_array_value_in_json_text(
686 &keymap_contents,
687 &["bindings", keystrokes_str],
688 Some(&source_action_value),
689 Some(&source.keystrokes_unparsed()),
690 index,
691 tab_size,
692 )
693 .context("Failed to replace keybinding")?;
694 keymap_contents.replace_range(replace_range, &replace_value);
695
696 return Ok(keymap_contents);
697 } else if keymap.0[index]
698 .bindings
699 .as_ref()
700 .is_none_or(|bindings| bindings.len() == 1)
701 {
702 // if we are replacing the only binding in the section,
703 // just update the section in place, updating the context
704 // and the binding
705
706 let (replace_range, replace_value) =
707 replace_top_level_array_value_in_json_text(
708 &keymap_contents,
709 &["bindings", keystrokes_str],
710 Some(&source_action_value),
711 Some(&source.keystrokes_unparsed()),
712 index,
713 tab_size,
714 )
715 .context("Failed to replace keybinding")?;
716 keymap_contents.replace_range(replace_range, &replace_value);
717
718 let (replace_range, replace_value) =
719 replace_top_level_array_value_in_json_text(
720 &keymap_contents,
721 &["context"],
722 source.context.map(Into::into).as_ref(),
723 None,
724 index,
725 tab_size,
726 )
727 .context("Failed to replace keybinding")?;
728 keymap_contents.replace_range(replace_range, &replace_value);
729 return Ok(keymap_contents);
730 } else {
731 // if we are replacing one of multiple bindings in a section
732 // with a context change, remove the existing binding from the
733 // section, then treat this operation as an add operation of the
734 // new binding with the updated context.
735
736 let (replace_range, replace_value) =
737 replace_top_level_array_value_in_json_text(
738 &keymap_contents,
739 &["bindings", keystrokes_str],
740 None,
741 None,
742 index,
743 tab_size,
744 )
745 .context("Failed to replace keybinding")?;
746 keymap_contents.replace_range(replace_range, &replace_value);
747 operation = KeybindUpdateOperation::Add {
748 source,
749 from: Some(target),
750 };
751 }
752 } else {
753 log::warn!(
754 "Failed to find keybinding to update `{:?} -> {}` creating new binding for `{:?} -> {}` instead",
755 target.keystrokes,
756 target_action_value,
757 source.keystrokes,
758 source_action_value,
759 );
760 operation = KeybindUpdateOperation::Add {
761 source,
762 from: Some(target),
763 };
764 }
765 }
766
767 if let KeybindUpdateOperation::Add {
768 source: keybinding,
769 from,
770 } = operation
771 {
772 let mut value = serde_json::Map::with_capacity(4);
773 if let Some(context) = keybinding.context {
774 value.insert("context".to_string(), context.into());
775 }
776 let use_key_equivalents = from.and_then(|from| {
777 let action_value = from.action_value().context("Failed to serialize action value. `use_key_equivalents` on new keybinding may be incorrect.").log_err()?;
778 let (index, _) = find_binding(&keymap, &from, &action_value, keyboard_mapper)?;
779 Some(keymap.0[index].use_key_equivalents)
780 }).unwrap_or(false);
781 if use_key_equivalents {
782 value.insert("use_key_equivalents".to_string(), true.into());
783 }
784
785 value.insert("bindings".to_string(), {
786 let mut bindings = serde_json::Map::new();
787 let action = keybinding.action_value()?;
788 bindings.insert(keybinding.keystrokes_unparsed(), action);
789 bindings.into()
790 });
791
792 let (replace_range, replace_value) = append_top_level_array_value_in_json_text(
793 &keymap_contents,
794 &value.into(),
795 tab_size,
796 )?;
797 keymap_contents.replace_range(replace_range, &replace_value);
798 }
799 return Ok(keymap_contents);
800
801 fn find_binding<'a, 'b>(
802 keymap: &'b KeymapFile,
803 target: &KeybindUpdateTarget<'a>,
804 target_action_value: &Value,
805 keyboard_mapper: &dyn gpui::PlatformKeyboardMapper,
806 ) -> Option<(usize, &'b str)> {
807 let target_context_parsed =
808 KeyBindingContextPredicate::parse(target.context.unwrap_or("")).ok();
809 for (index, section) in keymap.sections().enumerate() {
810 let section_context_parsed =
811 KeyBindingContextPredicate::parse(§ion.context).ok();
812 if section_context_parsed != target_context_parsed {
813 continue;
814 }
815 let Some(bindings) = §ion.bindings else {
816 continue;
817 };
818 for (keystrokes_str, action) in bindings {
819 let Ok(keystrokes) = keystrokes_str
820 .split_whitespace()
821 .map(|source| {
822 let keystroke = Keystroke::parse(source)?;
823 Ok(KeybindingKeystroke::new_with_mapper(
824 keystroke,
825 false,
826 keyboard_mapper,
827 ))
828 })
829 .collect::<Result<Vec<_>, InvalidKeystrokeError>>()
830 else {
831 continue;
832 };
833 if keystrokes.len() != target.keystrokes.len()
834 || !keystrokes
835 .iter()
836 .zip(target.keystrokes)
837 .all(|(a, b)| a.inner().should_match(b))
838 {
839 continue;
840 }
841 if &action.0 != target_action_value {
842 continue;
843 }
844 return Some((index, keystrokes_str));
845 }
846 }
847 None
848 }
849 }
850}
851
852#[derive(Clone, Debug)]
853pub enum KeybindUpdateOperation<'a> {
854 Replace {
855 /// Describes the keybind to create
856 source: KeybindUpdateTarget<'a>,
857 /// Describes the keybind to remove
858 target: KeybindUpdateTarget<'a>,
859 target_keybind_source: KeybindSource,
860 },
861 Add {
862 source: KeybindUpdateTarget<'a>,
863 from: Option<KeybindUpdateTarget<'a>>,
864 },
865 Remove {
866 target: KeybindUpdateTarget<'a>,
867 target_keybind_source: KeybindSource,
868 },
869}
870
871impl KeybindUpdateOperation<'_> {
872 pub fn generate_telemetry(
873 &self,
874 ) -> (
875 // The keybind that is created
876 String,
877 // The keybinding that was removed
878 String,
879 // The source of the keybinding
880 String,
881 ) {
882 let (new_binding, removed_binding, source) = match &self {
883 KeybindUpdateOperation::Replace {
884 source,
885 target,
886 target_keybind_source,
887 } => (Some(source), Some(target), Some(*target_keybind_source)),
888 KeybindUpdateOperation::Add { source, .. } => (Some(source), None, None),
889 KeybindUpdateOperation::Remove {
890 target,
891 target_keybind_source,
892 } => (None, Some(target), Some(*target_keybind_source)),
893 };
894
895 let new_binding = new_binding
896 .map(KeybindUpdateTarget::telemetry_string)
897 .unwrap_or("null".to_owned());
898 let removed_binding = removed_binding
899 .map(KeybindUpdateTarget::telemetry_string)
900 .unwrap_or("null".to_owned());
901
902 let source = source
903 .as_ref()
904 .map(KeybindSource::name)
905 .map(ToOwned::to_owned)
906 .unwrap_or("null".to_owned());
907
908 (new_binding, removed_binding, source)
909 }
910}
911
912impl<'a> KeybindUpdateOperation<'a> {
913 pub fn add(source: KeybindUpdateTarget<'a>) -> Self {
914 Self::Add { source, from: None }
915 }
916}
917
918#[derive(Debug, Clone)]
919pub struct KeybindUpdateTarget<'a> {
920 pub context: Option<&'a str>,
921 pub keystrokes: &'a [KeybindingKeystroke],
922 pub action_name: &'a str,
923 pub action_arguments: Option<&'a str>,
924}
925
926impl<'a> KeybindUpdateTarget<'a> {
927 fn action_value(&self) -> Result<Value> {
928 if self.action_name == gpui::NoAction.name() {
929 return Ok(Value::Null);
930 }
931 let action_name: Value = self.action_name.into();
932 let value = match self.action_arguments {
933 Some(args) if !args.is_empty() => {
934 let args = serde_json::from_str::<Value>(args)
935 .context("Failed to parse action arguments as JSON")?;
936 serde_json::json!([action_name, args])
937 }
938 _ => action_name,
939 };
940 Ok(value)
941 }
942
943 fn keystrokes_unparsed(&self) -> String {
944 let mut keystrokes = String::with_capacity(self.keystrokes.len() * 8);
945 for keystroke in self.keystrokes {
946 // The reason use `keystroke.unparse()` instead of `keystroke.inner.unparse()`
947 // here is that, we want the user to use `ctrl-shift-4` instead of `ctrl-$`
948 // by default on Windows.
949 keystrokes.push_str(&keystroke.unparse());
950 keystrokes.push(' ');
951 }
952 keystrokes.pop();
953 keystrokes
954 }
955
956 fn telemetry_string(&self) -> String {
957 format!(
958 "action_name: {}, context: {}, action_arguments: {}, keystrokes: {}",
959 self.action_name,
960 self.context.unwrap_or("global"),
961 self.action_arguments.unwrap_or("none"),
962 self.keystrokes_unparsed()
963 )
964 }
965}
966
967#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]
968pub enum KeybindSource {
969 User,
970 Vim,
971 Base,
972 #[default]
973 Default,
974 Unknown,
975}
976
977impl KeybindSource {
978 const BASE: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::Base as u32);
979 const DEFAULT: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::Default as u32);
980 const VIM: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::Vim as u32);
981 const USER: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::User as u32);
982
983 pub fn name(&self) -> &'static str {
984 match self {
985 KeybindSource::User => "User",
986 KeybindSource::Default => "Default",
987 KeybindSource::Base => "Base",
988 KeybindSource::Vim => "Vim",
989 KeybindSource::Unknown => "Unknown",
990 }
991 }
992
993 pub fn meta(&self) -> KeyBindingMetaIndex {
994 match self {
995 KeybindSource::User => Self::USER,
996 KeybindSource::Default => Self::DEFAULT,
997 KeybindSource::Base => Self::BASE,
998 KeybindSource::Vim => Self::VIM,
999 KeybindSource::Unknown => KeyBindingMetaIndex(*self as u32),
1000 }
1001 }
1002
1003 pub fn from_meta(index: KeyBindingMetaIndex) -> Self {
1004 match index {
1005 Self::USER => KeybindSource::User,
1006 Self::BASE => KeybindSource::Base,
1007 Self::DEFAULT => KeybindSource::Default,
1008 Self::VIM => KeybindSource::Vim,
1009 _ => KeybindSource::Unknown,
1010 }
1011 }
1012}
1013
1014impl From<KeyBindingMetaIndex> for KeybindSource {
1015 fn from(index: KeyBindingMetaIndex) -> Self {
1016 Self::from_meta(index)
1017 }
1018}
1019
1020impl From<KeybindSource> for KeyBindingMetaIndex {
1021 fn from(source: KeybindSource) -> Self {
1022 source.meta()
1023 }
1024}
1025
1026#[cfg(test)]
1027mod tests {
1028 use gpui::{DummyKeyboardMapper, KeybindingKeystroke, Keystroke};
1029 use unindent::Unindent;
1030
1031 use crate::{
1032 KeybindSource, KeymapFile,
1033 keymap_file::{KeybindUpdateOperation, KeybindUpdateTarget},
1034 };
1035
1036 #[test]
1037 fn can_deserialize_keymap_with_trailing_comma() {
1038 let json = indoc::indoc! {"[
1039 // Standard macOS bindings
1040 {
1041 \"bindings\": {
1042 \"up\": \"menu::SelectPrevious\",
1043 },
1044 },
1045 ]
1046 "
1047 };
1048 KeymapFile::parse(json).unwrap();
1049 }
1050
1051 #[track_caller]
1052 fn check_keymap_update(
1053 input: impl ToString,
1054 operation: KeybindUpdateOperation,
1055 expected: impl ToString,
1056 ) {
1057 let result = KeymapFile::update_keybinding(
1058 operation,
1059 input.to_string(),
1060 4,
1061 &gpui::DummyKeyboardMapper,
1062 )
1063 .expect("Update succeeded");
1064 pretty_assertions::assert_eq!(expected.to_string(), result);
1065 }
1066
1067 #[track_caller]
1068 fn parse_keystrokes(keystrokes: &str) -> Vec<KeybindingKeystroke> {
1069 keystrokes
1070 .split(' ')
1071 .map(|s| {
1072 KeybindingKeystroke::new_with_mapper(
1073 Keystroke::parse(s).expect("Keystrokes valid"),
1074 false,
1075 &DummyKeyboardMapper,
1076 )
1077 })
1078 .collect()
1079 }
1080
1081 #[test]
1082 fn keymap_update() {
1083 zlog::init_test();
1084
1085 check_keymap_update(
1086 "[]",
1087 KeybindUpdateOperation::add(KeybindUpdateTarget {
1088 keystrokes: &parse_keystrokes("ctrl-a"),
1089 action_name: "zed::SomeAction",
1090 context: None,
1091 action_arguments: None,
1092 }),
1093 r#"[
1094 {
1095 "bindings": {
1096 "ctrl-a": "zed::SomeAction"
1097 }
1098 }
1099 ]"#
1100 .unindent(),
1101 );
1102
1103 check_keymap_update(
1104 "[]",
1105 KeybindUpdateOperation::add(KeybindUpdateTarget {
1106 keystrokes: &parse_keystrokes("ctrl-a"),
1107 action_name: "zed::SomeAction",
1108 context: None,
1109 action_arguments: Some(""),
1110 }),
1111 r#"[
1112 {
1113 "bindings": {
1114 "ctrl-a": "zed::SomeAction"
1115 }
1116 }
1117 ]"#
1118 .unindent(),
1119 );
1120
1121 check_keymap_update(
1122 r#"[
1123 {
1124 "bindings": {
1125 "ctrl-a": "zed::SomeAction"
1126 }
1127 }
1128 ]"#
1129 .unindent(),
1130 KeybindUpdateOperation::add(KeybindUpdateTarget {
1131 keystrokes: &parse_keystrokes("ctrl-b"),
1132 action_name: "zed::SomeOtherAction",
1133 context: None,
1134 action_arguments: None,
1135 }),
1136 r#"[
1137 {
1138 "bindings": {
1139 "ctrl-a": "zed::SomeAction"
1140 }
1141 },
1142 {
1143 "bindings": {
1144 "ctrl-b": "zed::SomeOtherAction"
1145 }
1146 }
1147 ]"#
1148 .unindent(),
1149 );
1150
1151 check_keymap_update(
1152 r#"[
1153 {
1154 "bindings": {
1155 "ctrl-a": "zed::SomeAction"
1156 }
1157 }
1158 ]"#
1159 .unindent(),
1160 KeybindUpdateOperation::add(KeybindUpdateTarget {
1161 keystrokes: &parse_keystrokes("ctrl-b"),
1162 action_name: "zed::SomeOtherAction",
1163 context: None,
1164 action_arguments: Some(r#"{"foo": "bar"}"#),
1165 }),
1166 r#"[
1167 {
1168 "bindings": {
1169 "ctrl-a": "zed::SomeAction"
1170 }
1171 },
1172 {
1173 "bindings": {
1174 "ctrl-b": [
1175 "zed::SomeOtherAction",
1176 {
1177 "foo": "bar"
1178 }
1179 ]
1180 }
1181 }
1182 ]"#
1183 .unindent(),
1184 );
1185
1186 check_keymap_update(
1187 r#"[
1188 {
1189 "bindings": {
1190 "ctrl-a": "zed::SomeAction"
1191 }
1192 }
1193 ]"#
1194 .unindent(),
1195 KeybindUpdateOperation::add(KeybindUpdateTarget {
1196 keystrokes: &parse_keystrokes("ctrl-b"),
1197 action_name: "zed::SomeOtherAction",
1198 context: Some("Zed > Editor && some_condition = true"),
1199 action_arguments: Some(r#"{"foo": "bar"}"#),
1200 }),
1201 r#"[
1202 {
1203 "bindings": {
1204 "ctrl-a": "zed::SomeAction"
1205 }
1206 },
1207 {
1208 "context": "Zed > Editor && some_condition = true",
1209 "bindings": {
1210 "ctrl-b": [
1211 "zed::SomeOtherAction",
1212 {
1213 "foo": "bar"
1214 }
1215 ]
1216 }
1217 }
1218 ]"#
1219 .unindent(),
1220 );
1221
1222 check_keymap_update(
1223 r#"[
1224 {
1225 "bindings": {
1226 "ctrl-a": "zed::SomeAction"
1227 }
1228 }
1229 ]"#
1230 .unindent(),
1231 KeybindUpdateOperation::Replace {
1232 target: KeybindUpdateTarget {
1233 keystrokes: &parse_keystrokes("ctrl-a"),
1234 action_name: "zed::SomeAction",
1235 context: None,
1236 action_arguments: None,
1237 },
1238 source: KeybindUpdateTarget {
1239 keystrokes: &parse_keystrokes("ctrl-b"),
1240 action_name: "zed::SomeOtherAction",
1241 context: None,
1242 action_arguments: Some(r#"{"foo": "bar"}"#),
1243 },
1244 target_keybind_source: KeybindSource::Base,
1245 },
1246 r#"[
1247 {
1248 "bindings": {
1249 "ctrl-a": "zed::SomeAction"
1250 }
1251 },
1252 {
1253 "bindings": {
1254 "ctrl-b": [
1255 "zed::SomeOtherAction",
1256 {
1257 "foo": "bar"
1258 }
1259 ]
1260 }
1261 }
1262 ]"#
1263 .unindent(),
1264 );
1265
1266 check_keymap_update(
1267 r#"[
1268 {
1269 "bindings": {
1270 "a": "zed::SomeAction"
1271 }
1272 }
1273 ]"#
1274 .unindent(),
1275 KeybindUpdateOperation::Replace {
1276 target: KeybindUpdateTarget {
1277 keystrokes: &parse_keystrokes("a"),
1278 action_name: "zed::SomeAction",
1279 context: None,
1280 action_arguments: None,
1281 },
1282 source: KeybindUpdateTarget {
1283 keystrokes: &parse_keystrokes("ctrl-b"),
1284 action_name: "zed::SomeOtherAction",
1285 context: None,
1286 action_arguments: Some(r#"{"foo": "bar"}"#),
1287 },
1288 target_keybind_source: KeybindSource::User,
1289 },
1290 r#"[
1291 {
1292 "bindings": {
1293 "ctrl-b": [
1294 "zed::SomeOtherAction",
1295 {
1296 "foo": "bar"
1297 }
1298 ]
1299 }
1300 }
1301 ]"#
1302 .unindent(),
1303 );
1304
1305 check_keymap_update(
1306 r#"[
1307 {
1308 "bindings": {
1309 "ctrl-a": "zed::SomeAction"
1310 }
1311 }
1312 ]"#
1313 .unindent(),
1314 KeybindUpdateOperation::Replace {
1315 target: KeybindUpdateTarget {
1316 keystrokes: &parse_keystrokes("ctrl-a"),
1317 action_name: "zed::SomeNonexistentAction",
1318 context: None,
1319 action_arguments: None,
1320 },
1321 source: KeybindUpdateTarget {
1322 keystrokes: &parse_keystrokes("ctrl-b"),
1323 action_name: "zed::SomeOtherAction",
1324 context: None,
1325 action_arguments: None,
1326 },
1327 target_keybind_source: KeybindSource::User,
1328 },
1329 r#"[
1330 {
1331 "bindings": {
1332 "ctrl-a": "zed::SomeAction"
1333 }
1334 },
1335 {
1336 "bindings": {
1337 "ctrl-b": "zed::SomeOtherAction"
1338 }
1339 }
1340 ]"#
1341 .unindent(),
1342 );
1343
1344 check_keymap_update(
1345 r#"[
1346 {
1347 "bindings": {
1348 // some comment
1349 "ctrl-a": "zed::SomeAction"
1350 // some other comment
1351 }
1352 }
1353 ]"#
1354 .unindent(),
1355 KeybindUpdateOperation::Replace {
1356 target: KeybindUpdateTarget {
1357 keystrokes: &parse_keystrokes("ctrl-a"),
1358 action_name: "zed::SomeAction",
1359 context: None,
1360 action_arguments: None,
1361 },
1362 source: KeybindUpdateTarget {
1363 keystrokes: &parse_keystrokes("ctrl-b"),
1364 action_name: "zed::SomeOtherAction",
1365 context: None,
1366 action_arguments: Some(r#"{"foo": "bar"}"#),
1367 },
1368 target_keybind_source: KeybindSource::User,
1369 },
1370 r#"[
1371 {
1372 "bindings": {
1373 // some comment
1374 "ctrl-b": [
1375 "zed::SomeOtherAction",
1376 {
1377 "foo": "bar"
1378 }
1379 ]
1380 // some other comment
1381 }
1382 }
1383 ]"#
1384 .unindent(),
1385 );
1386
1387 check_keymap_update(
1388 r#"[
1389 {
1390 "context": "SomeContext",
1391 "bindings": {
1392 "a": "foo::bar",
1393 "b": "baz::qux",
1394 }
1395 }
1396 ]"#
1397 .unindent(),
1398 KeybindUpdateOperation::Replace {
1399 target: KeybindUpdateTarget {
1400 keystrokes: &parse_keystrokes("a"),
1401 action_name: "foo::bar",
1402 context: Some("SomeContext"),
1403 action_arguments: None,
1404 },
1405 source: KeybindUpdateTarget {
1406 keystrokes: &parse_keystrokes("c"),
1407 action_name: "foo::baz",
1408 context: Some("SomeOtherContext"),
1409 action_arguments: None,
1410 },
1411 target_keybind_source: KeybindSource::User,
1412 },
1413 r#"[
1414 {
1415 "context": "SomeContext",
1416 "bindings": {
1417 "b": "baz::qux",
1418 }
1419 },
1420 {
1421 "context": "SomeOtherContext",
1422 "bindings": {
1423 "c": "foo::baz"
1424 }
1425 }
1426 ]"#
1427 .unindent(),
1428 );
1429
1430 check_keymap_update(
1431 r#"[
1432 {
1433 "context": "SomeContext",
1434 "bindings": {
1435 "a": "foo::bar",
1436 }
1437 }
1438 ]"#
1439 .unindent(),
1440 KeybindUpdateOperation::Replace {
1441 target: KeybindUpdateTarget {
1442 keystrokes: &parse_keystrokes("a"),
1443 action_name: "foo::bar",
1444 context: Some("SomeContext"),
1445 action_arguments: None,
1446 },
1447 source: KeybindUpdateTarget {
1448 keystrokes: &parse_keystrokes("c"),
1449 action_name: "foo::baz",
1450 context: Some("SomeOtherContext"),
1451 action_arguments: None,
1452 },
1453 target_keybind_source: KeybindSource::User,
1454 },
1455 r#"[
1456 {
1457 "context": "SomeOtherContext",
1458 "bindings": {
1459 "c": "foo::baz",
1460 }
1461 }
1462 ]"#
1463 .unindent(),
1464 );
1465
1466 check_keymap_update(
1467 r#"[
1468 {
1469 "context": "SomeContext",
1470 "bindings": {
1471 "a": "foo::bar",
1472 "c": "foo::baz",
1473 }
1474 },
1475 ]"#
1476 .unindent(),
1477 KeybindUpdateOperation::Remove {
1478 target: KeybindUpdateTarget {
1479 context: Some("SomeContext"),
1480 keystrokes: &parse_keystrokes("a"),
1481 action_name: "foo::bar",
1482 action_arguments: None,
1483 },
1484 target_keybind_source: KeybindSource::User,
1485 },
1486 r#"[
1487 {
1488 "context": "SomeContext",
1489 "bindings": {
1490 "c": "foo::baz",
1491 }
1492 },
1493 ]"#
1494 .unindent(),
1495 );
1496
1497 check_keymap_update(
1498 r#"[
1499 {
1500 "context": "SomeContext",
1501 "bindings": {
1502 "a": ["foo::bar", true],
1503 "c": "foo::baz",
1504 }
1505 },
1506 ]"#
1507 .unindent(),
1508 KeybindUpdateOperation::Remove {
1509 target: KeybindUpdateTarget {
1510 context: Some("SomeContext"),
1511 keystrokes: &parse_keystrokes("a"),
1512 action_name: "foo::bar",
1513 action_arguments: Some("true"),
1514 },
1515 target_keybind_source: KeybindSource::User,
1516 },
1517 r#"[
1518 {
1519 "context": "SomeContext",
1520 "bindings": {
1521 "c": "foo::baz",
1522 }
1523 },
1524 ]"#
1525 .unindent(),
1526 );
1527
1528 check_keymap_update(
1529 r#"[
1530 {
1531 "context": "SomeContext",
1532 "bindings": {
1533 "b": "foo::baz",
1534 }
1535 },
1536 {
1537 "context": "SomeContext",
1538 "bindings": {
1539 "a": ["foo::bar", true],
1540 }
1541 },
1542 {
1543 "context": "SomeContext",
1544 "bindings": {
1545 "c": "foo::baz",
1546 }
1547 },
1548 ]"#
1549 .unindent(),
1550 KeybindUpdateOperation::Remove {
1551 target: KeybindUpdateTarget {
1552 context: Some("SomeContext"),
1553 keystrokes: &parse_keystrokes("a"),
1554 action_name: "foo::bar",
1555 action_arguments: Some("true"),
1556 },
1557 target_keybind_source: KeybindSource::User,
1558 },
1559 r#"[
1560 {
1561 "context": "SomeContext",
1562 "bindings": {
1563 "b": "foo::baz",
1564 }
1565 },
1566 {
1567 "context": "SomeContext",
1568 "bindings": {
1569 "c": "foo::baz",
1570 }
1571 },
1572 ]"#
1573 .unindent(),
1574 );
1575 check_keymap_update(
1576 r#"[
1577 {
1578 "context": "SomeOtherContext",
1579 "use_key_equivalents": true,
1580 "bindings": {
1581 "b": "foo::bar",
1582 }
1583 },
1584 ]"#
1585 .unindent(),
1586 KeybindUpdateOperation::Add {
1587 source: KeybindUpdateTarget {
1588 context: Some("SomeContext"),
1589 keystrokes: &parse_keystrokes("a"),
1590 action_name: "foo::baz",
1591 action_arguments: Some("true"),
1592 },
1593 from: Some(KeybindUpdateTarget {
1594 context: Some("SomeOtherContext"),
1595 keystrokes: &parse_keystrokes("b"),
1596 action_name: "foo::bar",
1597 action_arguments: None,
1598 }),
1599 },
1600 r#"[
1601 {
1602 "context": "SomeOtherContext",
1603 "use_key_equivalents": true,
1604 "bindings": {
1605 "b": "foo::bar",
1606 }
1607 },
1608 {
1609 "context": "SomeContext",
1610 "use_key_equivalents": true,
1611 "bindings": {
1612 "a": [
1613 "foo::baz",
1614 true
1615 ]
1616 }
1617 }
1618 ]"#
1619 .unindent(),
1620 );
1621
1622 check_keymap_update(
1623 r#"[
1624 {
1625 "context": "SomeOtherContext",
1626 "use_key_equivalents": true,
1627 "bindings": {
1628 "b": "foo::bar",
1629 }
1630 },
1631 ]"#
1632 .unindent(),
1633 KeybindUpdateOperation::Remove {
1634 target: KeybindUpdateTarget {
1635 context: Some("SomeContext"),
1636 keystrokes: &parse_keystrokes("a"),
1637 action_name: "foo::baz",
1638 action_arguments: Some("true"),
1639 },
1640 target_keybind_source: KeybindSource::Default,
1641 },
1642 r#"[
1643 {
1644 "context": "SomeOtherContext",
1645 "use_key_equivalents": true,
1646 "bindings": {
1647 "b": "foo::bar",
1648 }
1649 },
1650 {
1651 "context": "SomeContext",
1652 "bindings": {
1653 "a": null
1654 }
1655 }
1656 ]"#
1657 .unindent(),
1658 );
1659 }
1660
1661 #[test]
1662 fn test_keymap_remove() {
1663 zlog::init_test();
1664
1665 check_keymap_update(
1666 r#"
1667 [
1668 {
1669 "context": "Editor",
1670 "bindings": {
1671 "cmd-k cmd-u": "editor::ConvertToUpperCase",
1672 "cmd-k cmd-l": "editor::ConvertToLowerCase",
1673 "cmd-[": "pane::GoBack",
1674 }
1675 },
1676 ]
1677 "#,
1678 KeybindUpdateOperation::Remove {
1679 target: KeybindUpdateTarget {
1680 context: Some("Editor"),
1681 keystrokes: &parse_keystrokes("cmd-k cmd-l"),
1682 action_name: "editor::ConvertToLowerCase",
1683 action_arguments: None,
1684 },
1685 target_keybind_source: KeybindSource::User,
1686 },
1687 r#"
1688 [
1689 {
1690 "context": "Editor",
1691 "bindings": {
1692 "cmd-k cmd-u": "editor::ConvertToUpperCase",
1693 "cmd-[": "pane::GoBack",
1694 }
1695 },
1696 ]
1697 "#,
1698 );
1699 }
1700}