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("\\ a"),
1107 action_name: "zed::SomeAction",
1108 context: None,
1109 action_arguments: None,
1110 }),
1111 r#"[
1112 {
1113 "bindings": {
1114 "\\ a": "zed::SomeAction"
1115 }
1116 }
1117 ]"#
1118 .unindent(),
1119 );
1120
1121 check_keymap_update(
1122 "[]",
1123 KeybindUpdateOperation::add(KeybindUpdateTarget {
1124 keystrokes: &parse_keystrokes("ctrl-a"),
1125 action_name: "zed::SomeAction",
1126 context: None,
1127 action_arguments: Some(""),
1128 }),
1129 r#"[
1130 {
1131 "bindings": {
1132 "ctrl-a": "zed::SomeAction"
1133 }
1134 }
1135 ]"#
1136 .unindent(),
1137 );
1138
1139 check_keymap_update(
1140 r#"[
1141 {
1142 "bindings": {
1143 "ctrl-a": "zed::SomeAction"
1144 }
1145 }
1146 ]"#
1147 .unindent(),
1148 KeybindUpdateOperation::add(KeybindUpdateTarget {
1149 keystrokes: &parse_keystrokes("ctrl-b"),
1150 action_name: "zed::SomeOtherAction",
1151 context: None,
1152 action_arguments: None,
1153 }),
1154 r#"[
1155 {
1156 "bindings": {
1157 "ctrl-a": "zed::SomeAction"
1158 }
1159 },
1160 {
1161 "bindings": {
1162 "ctrl-b": "zed::SomeOtherAction"
1163 }
1164 }
1165 ]"#
1166 .unindent(),
1167 );
1168
1169 check_keymap_update(
1170 r#"[
1171 {
1172 "bindings": {
1173 "ctrl-a": "zed::SomeAction"
1174 }
1175 }
1176 ]"#
1177 .unindent(),
1178 KeybindUpdateOperation::add(KeybindUpdateTarget {
1179 keystrokes: &parse_keystrokes("ctrl-b"),
1180 action_name: "zed::SomeOtherAction",
1181 context: None,
1182 action_arguments: Some(r#"{"foo": "bar"}"#),
1183 }),
1184 r#"[
1185 {
1186 "bindings": {
1187 "ctrl-a": "zed::SomeAction"
1188 }
1189 },
1190 {
1191 "bindings": {
1192 "ctrl-b": [
1193 "zed::SomeOtherAction",
1194 {
1195 "foo": "bar"
1196 }
1197 ]
1198 }
1199 }
1200 ]"#
1201 .unindent(),
1202 );
1203
1204 check_keymap_update(
1205 r#"[
1206 {
1207 "bindings": {
1208 "ctrl-a": "zed::SomeAction"
1209 }
1210 }
1211 ]"#
1212 .unindent(),
1213 KeybindUpdateOperation::add(KeybindUpdateTarget {
1214 keystrokes: &parse_keystrokes("ctrl-b"),
1215 action_name: "zed::SomeOtherAction",
1216 context: Some("Zed > Editor && some_condition = true"),
1217 action_arguments: Some(r#"{"foo": "bar"}"#),
1218 }),
1219 r#"[
1220 {
1221 "bindings": {
1222 "ctrl-a": "zed::SomeAction"
1223 }
1224 },
1225 {
1226 "context": "Zed > Editor && some_condition = true",
1227 "bindings": {
1228 "ctrl-b": [
1229 "zed::SomeOtherAction",
1230 {
1231 "foo": "bar"
1232 }
1233 ]
1234 }
1235 }
1236 ]"#
1237 .unindent(),
1238 );
1239
1240 check_keymap_update(
1241 r#"[
1242 {
1243 "bindings": {
1244 "ctrl-a": "zed::SomeAction"
1245 }
1246 }
1247 ]"#
1248 .unindent(),
1249 KeybindUpdateOperation::Replace {
1250 target: KeybindUpdateTarget {
1251 keystrokes: &parse_keystrokes("ctrl-a"),
1252 action_name: "zed::SomeAction",
1253 context: None,
1254 action_arguments: None,
1255 },
1256 source: KeybindUpdateTarget {
1257 keystrokes: &parse_keystrokes("ctrl-b"),
1258 action_name: "zed::SomeOtherAction",
1259 context: None,
1260 action_arguments: Some(r#"{"foo": "bar"}"#),
1261 },
1262 target_keybind_source: KeybindSource::Base,
1263 },
1264 r#"[
1265 {
1266 "bindings": {
1267 "ctrl-a": "zed::SomeAction"
1268 }
1269 },
1270 {
1271 "bindings": {
1272 "ctrl-b": [
1273 "zed::SomeOtherAction",
1274 {
1275 "foo": "bar"
1276 }
1277 ]
1278 }
1279 }
1280 ]"#
1281 .unindent(),
1282 );
1283
1284 check_keymap_update(
1285 r#"[
1286 {
1287 "bindings": {
1288 "a": "zed::SomeAction"
1289 }
1290 }
1291 ]"#
1292 .unindent(),
1293 KeybindUpdateOperation::Replace {
1294 target: KeybindUpdateTarget {
1295 keystrokes: &parse_keystrokes("a"),
1296 action_name: "zed::SomeAction",
1297 context: None,
1298 action_arguments: None,
1299 },
1300 source: KeybindUpdateTarget {
1301 keystrokes: &parse_keystrokes("ctrl-b"),
1302 action_name: "zed::SomeOtherAction",
1303 context: None,
1304 action_arguments: Some(r#"{"foo": "bar"}"#),
1305 },
1306 target_keybind_source: KeybindSource::User,
1307 },
1308 r#"[
1309 {
1310 "bindings": {
1311 "ctrl-b": [
1312 "zed::SomeOtherAction",
1313 {
1314 "foo": "bar"
1315 }
1316 ]
1317 }
1318 }
1319 ]"#
1320 .unindent(),
1321 );
1322
1323 check_keymap_update(
1324 r#"[
1325 {
1326 "bindings": {
1327 "\\ a": "zed::SomeAction"
1328 }
1329 }
1330 ]"#
1331 .unindent(),
1332 KeybindUpdateOperation::Replace {
1333 target: KeybindUpdateTarget {
1334 keystrokes: &parse_keystrokes("\\ a"),
1335 action_name: "zed::SomeAction",
1336 context: None,
1337 action_arguments: None,
1338 },
1339 source: KeybindUpdateTarget {
1340 keystrokes: &parse_keystrokes("\\ b"),
1341 action_name: "zed::SomeOtherAction",
1342 context: None,
1343 action_arguments: Some(r#"{"foo": "bar"}"#),
1344 },
1345 target_keybind_source: KeybindSource::User,
1346 },
1347 r#"[
1348 {
1349 "bindings": {
1350 "\\ b": [
1351 "zed::SomeOtherAction",
1352 {
1353 "foo": "bar"
1354 }
1355 ]
1356 }
1357 }
1358 ]"#
1359 .unindent(),
1360 );
1361
1362 check_keymap_update(
1363 r#"[
1364 {
1365 "bindings": {
1366 "\\ a": "zed::SomeAction"
1367 }
1368 }
1369 ]"#
1370 .unindent(),
1371 KeybindUpdateOperation::Replace {
1372 target: KeybindUpdateTarget {
1373 keystrokes: &parse_keystrokes("\\ a"),
1374 action_name: "zed::SomeAction",
1375 context: None,
1376 action_arguments: None,
1377 },
1378 source: KeybindUpdateTarget {
1379 keystrokes: &parse_keystrokes("\\ a"),
1380 action_name: "zed::SomeAction",
1381 context: None,
1382 action_arguments: None,
1383 },
1384 target_keybind_source: KeybindSource::User,
1385 },
1386 r#"[
1387 {
1388 "bindings": {
1389 "\\ a": "zed::SomeAction"
1390 }
1391 }
1392 ]"#
1393 .unindent(),
1394 );
1395
1396 check_keymap_update(
1397 r#"[
1398 {
1399 "bindings": {
1400 "ctrl-a": "zed::SomeAction"
1401 }
1402 }
1403 ]"#
1404 .unindent(),
1405 KeybindUpdateOperation::Replace {
1406 target: KeybindUpdateTarget {
1407 keystrokes: &parse_keystrokes("ctrl-a"),
1408 action_name: "zed::SomeNonexistentAction",
1409 context: None,
1410 action_arguments: None,
1411 },
1412 source: KeybindUpdateTarget {
1413 keystrokes: &parse_keystrokes("ctrl-b"),
1414 action_name: "zed::SomeOtherAction",
1415 context: None,
1416 action_arguments: None,
1417 },
1418 target_keybind_source: KeybindSource::User,
1419 },
1420 r#"[
1421 {
1422 "bindings": {
1423 "ctrl-a": "zed::SomeAction"
1424 }
1425 },
1426 {
1427 "bindings": {
1428 "ctrl-b": "zed::SomeOtherAction"
1429 }
1430 }
1431 ]"#
1432 .unindent(),
1433 );
1434
1435 check_keymap_update(
1436 r#"[
1437 {
1438 "bindings": {
1439 // some comment
1440 "ctrl-a": "zed::SomeAction"
1441 // some other comment
1442 }
1443 }
1444 ]"#
1445 .unindent(),
1446 KeybindUpdateOperation::Replace {
1447 target: KeybindUpdateTarget {
1448 keystrokes: &parse_keystrokes("ctrl-a"),
1449 action_name: "zed::SomeAction",
1450 context: None,
1451 action_arguments: None,
1452 },
1453 source: KeybindUpdateTarget {
1454 keystrokes: &parse_keystrokes("ctrl-b"),
1455 action_name: "zed::SomeOtherAction",
1456 context: None,
1457 action_arguments: Some(r#"{"foo": "bar"}"#),
1458 },
1459 target_keybind_source: KeybindSource::User,
1460 },
1461 r#"[
1462 {
1463 "bindings": {
1464 // some comment
1465 "ctrl-b": [
1466 "zed::SomeOtherAction",
1467 {
1468 "foo": "bar"
1469 }
1470 ]
1471 // some other comment
1472 }
1473 }
1474 ]"#
1475 .unindent(),
1476 );
1477
1478 check_keymap_update(
1479 r#"[
1480 {
1481 "context": "SomeContext",
1482 "bindings": {
1483 "a": "foo::bar",
1484 "b": "baz::qux",
1485 }
1486 }
1487 ]"#
1488 .unindent(),
1489 KeybindUpdateOperation::Replace {
1490 target: KeybindUpdateTarget {
1491 keystrokes: &parse_keystrokes("a"),
1492 action_name: "foo::bar",
1493 context: Some("SomeContext"),
1494 action_arguments: None,
1495 },
1496 source: KeybindUpdateTarget {
1497 keystrokes: &parse_keystrokes("c"),
1498 action_name: "foo::baz",
1499 context: Some("SomeOtherContext"),
1500 action_arguments: None,
1501 },
1502 target_keybind_source: KeybindSource::User,
1503 },
1504 r#"[
1505 {
1506 "context": "SomeContext",
1507 "bindings": {
1508 "b": "baz::qux",
1509 }
1510 },
1511 {
1512 "context": "SomeOtherContext",
1513 "bindings": {
1514 "c": "foo::baz"
1515 }
1516 }
1517 ]"#
1518 .unindent(),
1519 );
1520
1521 check_keymap_update(
1522 r#"[
1523 {
1524 "context": "SomeContext",
1525 "bindings": {
1526 "a": "foo::bar",
1527 }
1528 }
1529 ]"#
1530 .unindent(),
1531 KeybindUpdateOperation::Replace {
1532 target: KeybindUpdateTarget {
1533 keystrokes: &parse_keystrokes("a"),
1534 action_name: "foo::bar",
1535 context: Some("SomeContext"),
1536 action_arguments: None,
1537 },
1538 source: KeybindUpdateTarget {
1539 keystrokes: &parse_keystrokes("c"),
1540 action_name: "foo::baz",
1541 context: Some("SomeOtherContext"),
1542 action_arguments: None,
1543 },
1544 target_keybind_source: KeybindSource::User,
1545 },
1546 r#"[
1547 {
1548 "context": "SomeOtherContext",
1549 "bindings": {
1550 "c": "foo::baz",
1551 }
1552 }
1553 ]"#
1554 .unindent(),
1555 );
1556
1557 check_keymap_update(
1558 r#"[
1559 {
1560 "context": "SomeContext",
1561 "bindings": {
1562 "a": "foo::bar",
1563 "c": "foo::baz",
1564 }
1565 },
1566 ]"#
1567 .unindent(),
1568 KeybindUpdateOperation::Remove {
1569 target: KeybindUpdateTarget {
1570 context: Some("SomeContext"),
1571 keystrokes: &parse_keystrokes("a"),
1572 action_name: "foo::bar",
1573 action_arguments: None,
1574 },
1575 target_keybind_source: KeybindSource::User,
1576 },
1577 r#"[
1578 {
1579 "context": "SomeContext",
1580 "bindings": {
1581 "c": "foo::baz",
1582 }
1583 },
1584 ]"#
1585 .unindent(),
1586 );
1587
1588 check_keymap_update(
1589 r#"[
1590 {
1591 "context": "SomeContext",
1592 "bindings": {
1593 "\\ a": "foo::bar",
1594 "c": "foo::baz",
1595 }
1596 },
1597 ]"#
1598 .unindent(),
1599 KeybindUpdateOperation::Remove {
1600 target: KeybindUpdateTarget {
1601 context: Some("SomeContext"),
1602 keystrokes: &parse_keystrokes("\\ a"),
1603 action_name: "foo::bar",
1604 action_arguments: None,
1605 },
1606 target_keybind_source: KeybindSource::User,
1607 },
1608 r#"[
1609 {
1610 "context": "SomeContext",
1611 "bindings": {
1612 "c": "foo::baz",
1613 }
1614 },
1615 ]"#
1616 .unindent(),
1617 );
1618
1619 check_keymap_update(
1620 r#"[
1621 {
1622 "context": "SomeContext",
1623 "bindings": {
1624 "a": ["foo::bar", true],
1625 "c": "foo::baz",
1626 }
1627 },
1628 ]"#
1629 .unindent(),
1630 KeybindUpdateOperation::Remove {
1631 target: KeybindUpdateTarget {
1632 context: Some("SomeContext"),
1633 keystrokes: &parse_keystrokes("a"),
1634 action_name: "foo::bar",
1635 action_arguments: Some("true"),
1636 },
1637 target_keybind_source: KeybindSource::User,
1638 },
1639 r#"[
1640 {
1641 "context": "SomeContext",
1642 "bindings": {
1643 "c": "foo::baz",
1644 }
1645 },
1646 ]"#
1647 .unindent(),
1648 );
1649
1650 check_keymap_update(
1651 r#"[
1652 {
1653 "context": "SomeContext",
1654 "bindings": {
1655 "b": "foo::baz",
1656 }
1657 },
1658 {
1659 "context": "SomeContext",
1660 "bindings": {
1661 "a": ["foo::bar", true],
1662 }
1663 },
1664 {
1665 "context": "SomeContext",
1666 "bindings": {
1667 "c": "foo::baz",
1668 }
1669 },
1670 ]"#
1671 .unindent(),
1672 KeybindUpdateOperation::Remove {
1673 target: KeybindUpdateTarget {
1674 context: Some("SomeContext"),
1675 keystrokes: &parse_keystrokes("a"),
1676 action_name: "foo::bar",
1677 action_arguments: Some("true"),
1678 },
1679 target_keybind_source: KeybindSource::User,
1680 },
1681 r#"[
1682 {
1683 "context": "SomeContext",
1684 "bindings": {
1685 "b": "foo::baz",
1686 }
1687 },
1688 {
1689 "context": "SomeContext",
1690 "bindings": {
1691 "c": "foo::baz",
1692 }
1693 },
1694 ]"#
1695 .unindent(),
1696 );
1697 check_keymap_update(
1698 r#"[
1699 {
1700 "context": "SomeOtherContext",
1701 "use_key_equivalents": true,
1702 "bindings": {
1703 "b": "foo::bar",
1704 }
1705 },
1706 ]"#
1707 .unindent(),
1708 KeybindUpdateOperation::Add {
1709 source: KeybindUpdateTarget {
1710 context: Some("SomeContext"),
1711 keystrokes: &parse_keystrokes("a"),
1712 action_name: "foo::baz",
1713 action_arguments: Some("true"),
1714 },
1715 from: Some(KeybindUpdateTarget {
1716 context: Some("SomeOtherContext"),
1717 keystrokes: &parse_keystrokes("b"),
1718 action_name: "foo::bar",
1719 action_arguments: None,
1720 }),
1721 },
1722 r#"[
1723 {
1724 "context": "SomeOtherContext",
1725 "use_key_equivalents": true,
1726 "bindings": {
1727 "b": "foo::bar",
1728 }
1729 },
1730 {
1731 "context": "SomeContext",
1732 "use_key_equivalents": true,
1733 "bindings": {
1734 "a": [
1735 "foo::baz",
1736 true
1737 ]
1738 }
1739 }
1740 ]"#
1741 .unindent(),
1742 );
1743
1744 check_keymap_update(
1745 r#"[
1746 {
1747 "context": "SomeOtherContext",
1748 "use_key_equivalents": true,
1749 "bindings": {
1750 "b": "foo::bar",
1751 }
1752 },
1753 ]"#
1754 .unindent(),
1755 KeybindUpdateOperation::Remove {
1756 target: KeybindUpdateTarget {
1757 context: Some("SomeContext"),
1758 keystrokes: &parse_keystrokes("a"),
1759 action_name: "foo::baz",
1760 action_arguments: Some("true"),
1761 },
1762 target_keybind_source: KeybindSource::Default,
1763 },
1764 r#"[
1765 {
1766 "context": "SomeOtherContext",
1767 "use_key_equivalents": true,
1768 "bindings": {
1769 "b": "foo::bar",
1770 }
1771 },
1772 {
1773 "context": "SomeContext",
1774 "bindings": {
1775 "a": null
1776 }
1777 }
1778 ]"#
1779 .unindent(),
1780 );
1781 }
1782
1783 #[test]
1784 fn test_keymap_remove() {
1785 zlog::init_test();
1786
1787 check_keymap_update(
1788 r#"
1789 [
1790 {
1791 "context": "Editor",
1792 "bindings": {
1793 "cmd-k cmd-u": "editor::ConvertToUpperCase",
1794 "cmd-k cmd-l": "editor::ConvertToLowerCase",
1795 "cmd-[": "pane::GoBack",
1796 }
1797 },
1798 ]
1799 "#,
1800 KeybindUpdateOperation::Remove {
1801 target: KeybindUpdateTarget {
1802 context: Some("Editor"),
1803 keystrokes: &parse_keystrokes("cmd-k cmd-l"),
1804 action_name: "editor::ConvertToLowerCase",
1805 action_arguments: None,
1806 },
1807 target_keybind_source: KeybindSource::User,
1808 },
1809 r#"
1810 [
1811 {
1812 "context": "Editor",
1813 "bindings": {
1814 "cmd-k cmd-u": "editor::ConvertToUpperCase",
1815 "cmd-[": "pane::GoBack",
1816 }
1817 },
1818 ]
1819 "#,
1820 );
1821 }
1822}