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: &str) {
467 schema.insert(
468 "description".to_string(),
469 Value::String(description.to_string()),
470 );
471 }
472
473 let empty_object = json_schema!({
474 "type": "object"
475 });
476
477 // This is a workaround for a json-language-server issue where it matches the first
478 // alternative that matches the value's shape and uses that for documentation.
479 //
480 // In the case of the array validations, it would even provide an error saying that the name
481 // must match the name of the first alternative.
482 let mut empty_action_name = json_schema!({
483 "type": "string",
484 "const": ""
485 });
486 let no_action_message = "No action named this.";
487 add_description(&mut empty_action_name, no_action_message);
488 add_deprecation(&mut empty_action_name, no_action_message.to_string());
489 let empty_action_name_with_input = json_schema!({
490 "type": "array",
491 "items": [
492 empty_action_name,
493 true
494 ],
495 "minItems": 2,
496 "maxItems": 2
497 });
498 let mut keymap_action_alternatives = vec![empty_action_name, empty_action_name_with_input];
499
500 let mut empty_schema_action_names = vec![];
501 for (name, action_schema) in action_schemas.into_iter() {
502 let description = action_schema.as_ref().and_then(|schema| {
503 schema
504 .as_object()
505 .and_then(|obj| obj.get("description"))
506 .and_then(|v| v.as_str())
507 .map(|s| s.to_string())
508 });
509
510 let deprecation = if name == NoAction.name() {
511 Some("null")
512 } else {
513 deprecations.get(name).copied()
514 };
515
516 // Add an alternative for plain action names.
517 let mut plain_action = json_schema!({
518 "type": "string",
519 "const": name
520 });
521 if let Some(message) = deprecation_messages.get(name) {
522 add_deprecation(&mut plain_action, message.to_string());
523 } else if let Some(new_name) = deprecation {
524 add_deprecation_preferred_name(&mut plain_action, new_name);
525 }
526 if let Some(description) = &description {
527 add_description(&mut plain_action, description);
528 }
529 keymap_action_alternatives.push(plain_action);
530
531 // Add an alternative for actions with data specified as a [name, data] array.
532 //
533 // When a struct with no deserializable fields is added by deriving `Action`, an empty
534 // object schema is produced. The action should be invoked without data in this case.
535 if let Some(schema) = action_schema
536 && schema != empty_object
537 {
538 let mut matches_action_name = json_schema!({
539 "const": name
540 });
541 if let Some(description) = &description {
542 add_description(&mut matches_action_name, description);
543 }
544 if let Some(message) = deprecation_messages.get(name) {
545 add_deprecation(&mut matches_action_name, message.to_string());
546 } else if let Some(new_name) = deprecation {
547 add_deprecation_preferred_name(&mut matches_action_name, new_name);
548 }
549 let action_with_input = json_schema!({
550 "type": "array",
551 "items": [matches_action_name, schema],
552 "minItems": 2,
553 "maxItems": 2
554 });
555 keymap_action_alternatives.push(action_with_input);
556 } else {
557 empty_schema_action_names.push(name);
558 }
559 }
560
561 if !empty_schema_action_names.is_empty() {
562 let action_names = json_schema!({ "enum": empty_schema_action_names });
563 let no_properties_allowed = json_schema!({
564 "type": "object",
565 "additionalProperties": false
566 });
567 let mut actions_with_empty_input = json_schema!({
568 "type": "array",
569 "items": [action_names, no_properties_allowed],
570 "minItems": 2,
571 "maxItems": 2
572 });
573 add_deprecation(
574 &mut actions_with_empty_input,
575 "This action does not take input - just the action name string should be used."
576 .to_string(),
577 );
578 keymap_action_alternatives.push(actions_with_empty_input);
579 }
580
581 // Placing null first causes json-language-server to default assuming actions should be
582 // null, so place it last.
583 keymap_action_alternatives.push(json_schema!({
584 "type": "null"
585 }));
586
587 // The `KeymapSection` schema will reference the `KeymapAction` schema by name, so setting
588 // the definition of `KeymapAction` results in the full action schema being used.
589 generator.definitions_mut().insert(
590 KeymapAction::schema_name().to_string(),
591 json!({
592 "oneOf": keymap_action_alternatives
593 }),
594 );
595
596 generator.root_schema_for::<KeymapFile>().to_value()
597 }
598
599 pub fn sections(&self) -> impl DoubleEndedIterator<Item = &KeymapSection> {
600 self.0.iter()
601 }
602
603 pub async fn load_keymap_file(fs: &Arc<dyn Fs>) -> Result<String> {
604 match fs.load(paths::keymap_file()).await {
605 result @ Ok(_) => result,
606 Err(err) => {
607 if let Some(e) = err.downcast_ref::<std::io::Error>()
608 && e.kind() == std::io::ErrorKind::NotFound
609 {
610 return Ok(crate::initial_keymap_content().to_string());
611 }
612 Err(err)
613 }
614 }
615 }
616
617 pub fn update_keybinding<'a>(
618 mut operation: KeybindUpdateOperation<'a>,
619 mut keymap_contents: String,
620 tab_size: usize,
621 keyboard_mapper: &dyn gpui::PlatformKeyboardMapper,
622 ) -> Result<String> {
623 match operation {
624 // if trying to replace a keybinding that is not user-defined, treat it as an add operation
625 KeybindUpdateOperation::Replace {
626 target_keybind_source: target_source,
627 source,
628 target,
629 } if target_source != KeybindSource::User => {
630 operation = KeybindUpdateOperation::Add {
631 source,
632 from: Some(target),
633 };
634 }
635 // if trying to remove a keybinding that is not user-defined, treat it as creating a binding
636 // that binds it to `zed::NoAction`
637 KeybindUpdateOperation::Remove {
638 target,
639 target_keybind_source,
640 } if target_keybind_source != KeybindSource::User => {
641 let mut source = target.clone();
642 source.action_name = gpui::NoAction.name();
643 source.action_arguments.take();
644 operation = KeybindUpdateOperation::Add {
645 source,
646 from: Some(target),
647 };
648 }
649 _ => {}
650 }
651
652 // Sanity check that keymap contents are valid, even though we only use it for Replace.
653 // We don't want to modify the file if it's invalid.
654 let keymap = Self::parse(&keymap_contents).context("Failed to parse keymap")?;
655
656 if let KeybindUpdateOperation::Remove { target, .. } = operation {
657 let target_action_value = target
658 .action_value()
659 .context("Failed to generate target action JSON value")?;
660 let Some((index, keystrokes_str)) =
661 find_binding(&keymap, &target, &target_action_value, keyboard_mapper)
662 else {
663 anyhow::bail!("Failed to find keybinding to remove");
664 };
665 let is_only_binding = keymap.0[index]
666 .bindings
667 .as_ref()
668 .is_none_or(|bindings| bindings.len() == 1);
669 let key_path: &[&str] = if is_only_binding {
670 &[]
671 } else {
672 &["bindings", keystrokes_str]
673 };
674 let (replace_range, replace_value) = replace_top_level_array_value_in_json_text(
675 &keymap_contents,
676 key_path,
677 None,
678 None,
679 index,
680 tab_size,
681 )
682 .context("Failed to remove keybinding")?;
683 keymap_contents.replace_range(replace_range, &replace_value);
684 return Ok(keymap_contents);
685 }
686
687 if let KeybindUpdateOperation::Replace { source, target, .. } = operation {
688 let target_action_value = target
689 .action_value()
690 .context("Failed to generate target action JSON value")?;
691 let source_action_value = source
692 .action_value()
693 .context("Failed to generate source action JSON value")?;
694
695 if let Some((index, keystrokes_str)) =
696 find_binding(&keymap, &target, &target_action_value, keyboard_mapper)
697 {
698 if target.context == source.context {
699 // if we are only changing the keybinding (common case)
700 // not the context, etc. Then just update the binding in place
701
702 let (replace_range, replace_value) =
703 replace_top_level_array_value_in_json_text(
704 &keymap_contents,
705 &["bindings", keystrokes_str],
706 Some(&source_action_value),
707 Some(&source.keystrokes_unparsed()),
708 index,
709 tab_size,
710 )
711 .context("Failed to replace keybinding")?;
712 keymap_contents.replace_range(replace_range, &replace_value);
713
714 return Ok(keymap_contents);
715 } else if keymap.0[index]
716 .bindings
717 .as_ref()
718 .is_none_or(|bindings| bindings.len() == 1)
719 {
720 // if we are replacing the only binding in the section,
721 // just update the section in place, updating the context
722 // and the binding
723
724 let (replace_range, replace_value) =
725 replace_top_level_array_value_in_json_text(
726 &keymap_contents,
727 &["bindings", keystrokes_str],
728 Some(&source_action_value),
729 Some(&source.keystrokes_unparsed()),
730 index,
731 tab_size,
732 )
733 .context("Failed to replace keybinding")?;
734 keymap_contents.replace_range(replace_range, &replace_value);
735
736 let (replace_range, replace_value) =
737 replace_top_level_array_value_in_json_text(
738 &keymap_contents,
739 &["context"],
740 source.context.map(Into::into).as_ref(),
741 None,
742 index,
743 tab_size,
744 )
745 .context("Failed to replace keybinding")?;
746 keymap_contents.replace_range(replace_range, &replace_value);
747 return Ok(keymap_contents);
748 } else {
749 // if we are replacing one of multiple bindings in a section
750 // with a context change, remove the existing binding from the
751 // section, then treat this operation as an add operation of the
752 // new binding with the updated context.
753
754 let (replace_range, replace_value) =
755 replace_top_level_array_value_in_json_text(
756 &keymap_contents,
757 &["bindings", keystrokes_str],
758 None,
759 None,
760 index,
761 tab_size,
762 )
763 .context("Failed to replace keybinding")?;
764 keymap_contents.replace_range(replace_range, &replace_value);
765 operation = KeybindUpdateOperation::Add {
766 source,
767 from: Some(target),
768 };
769 }
770 } else {
771 log::warn!(
772 "Failed to find keybinding to update `{:?} -> {}` creating new binding for `{:?} -> {}` instead",
773 target.keystrokes,
774 target_action_value,
775 source.keystrokes,
776 source_action_value,
777 );
778 operation = KeybindUpdateOperation::Add {
779 source,
780 from: Some(target),
781 };
782 }
783 }
784
785 if let KeybindUpdateOperation::Add {
786 source: keybinding,
787 from,
788 } = operation
789 {
790 let mut value = serde_json::Map::with_capacity(4);
791 if let Some(context) = keybinding.context {
792 value.insert("context".to_string(), context.into());
793 }
794 let use_key_equivalents = from.and_then(|from| {
795 let action_value = from.action_value().context("Failed to serialize action value. `use_key_equivalents` on new keybinding may be incorrect.").log_err()?;
796 let (index, _) = find_binding(&keymap, &from, &action_value, keyboard_mapper)?;
797 Some(keymap.0[index].use_key_equivalents)
798 }).unwrap_or(false);
799 if use_key_equivalents {
800 value.insert("use_key_equivalents".to_string(), true.into());
801 }
802
803 value.insert("bindings".to_string(), {
804 let mut bindings = serde_json::Map::new();
805 let action = keybinding.action_value()?;
806 bindings.insert(keybinding.keystrokes_unparsed(), action);
807 bindings.into()
808 });
809
810 let (replace_range, replace_value) = append_top_level_array_value_in_json_text(
811 &keymap_contents,
812 &value.into(),
813 tab_size,
814 )?;
815 keymap_contents.replace_range(replace_range, &replace_value);
816 }
817 return Ok(keymap_contents);
818
819 fn find_binding<'a, 'b>(
820 keymap: &'b KeymapFile,
821 target: &KeybindUpdateTarget<'a>,
822 target_action_value: &Value,
823 keyboard_mapper: &dyn gpui::PlatformKeyboardMapper,
824 ) -> Option<(usize, &'b str)> {
825 let target_context_parsed =
826 KeyBindingContextPredicate::parse(target.context.unwrap_or("")).ok();
827 for (index, section) in keymap.sections().enumerate() {
828 let section_context_parsed =
829 KeyBindingContextPredicate::parse(§ion.context).ok();
830 if section_context_parsed != target_context_parsed {
831 continue;
832 }
833 let Some(bindings) = §ion.bindings else {
834 continue;
835 };
836 for (keystrokes_str, action) in bindings {
837 let Ok(keystrokes) = keystrokes_str
838 .split_whitespace()
839 .map(|source| {
840 let keystroke = Keystroke::parse(source)?;
841 Ok(KeybindingKeystroke::new_with_mapper(
842 keystroke,
843 false,
844 keyboard_mapper,
845 ))
846 })
847 .collect::<Result<Vec<_>, InvalidKeystrokeError>>()
848 else {
849 continue;
850 };
851 if keystrokes.len() != target.keystrokes.len()
852 || !keystrokes
853 .iter()
854 .zip(target.keystrokes)
855 .all(|(a, b)| a.inner().should_match(b))
856 {
857 continue;
858 }
859 if &action.0 != target_action_value {
860 continue;
861 }
862 return Some((index, keystrokes_str));
863 }
864 }
865 None
866 }
867 }
868}
869
870#[derive(Clone, Debug)]
871pub enum KeybindUpdateOperation<'a> {
872 Replace {
873 /// Describes the keybind to create
874 source: KeybindUpdateTarget<'a>,
875 /// Describes the keybind to remove
876 target: KeybindUpdateTarget<'a>,
877 target_keybind_source: KeybindSource,
878 },
879 Add {
880 source: KeybindUpdateTarget<'a>,
881 from: Option<KeybindUpdateTarget<'a>>,
882 },
883 Remove {
884 target: KeybindUpdateTarget<'a>,
885 target_keybind_source: KeybindSource,
886 },
887}
888
889impl KeybindUpdateOperation<'_> {
890 pub fn generate_telemetry(
891 &self,
892 ) -> (
893 // The keybind that is created
894 String,
895 // The keybinding that was removed
896 String,
897 // The source of the keybinding
898 String,
899 ) {
900 let (new_binding, removed_binding, source) = match &self {
901 KeybindUpdateOperation::Replace {
902 source,
903 target,
904 target_keybind_source,
905 } => (Some(source), Some(target), Some(*target_keybind_source)),
906 KeybindUpdateOperation::Add { source, .. } => (Some(source), None, None),
907 KeybindUpdateOperation::Remove {
908 target,
909 target_keybind_source,
910 } => (None, Some(target), Some(*target_keybind_source)),
911 };
912
913 let new_binding = new_binding
914 .map(KeybindUpdateTarget::telemetry_string)
915 .unwrap_or("null".to_owned());
916 let removed_binding = removed_binding
917 .map(KeybindUpdateTarget::telemetry_string)
918 .unwrap_or("null".to_owned());
919
920 let source = source
921 .as_ref()
922 .map(KeybindSource::name)
923 .map(ToOwned::to_owned)
924 .unwrap_or("null".to_owned());
925
926 (new_binding, removed_binding, source)
927 }
928}
929
930impl<'a> KeybindUpdateOperation<'a> {
931 pub fn add(source: KeybindUpdateTarget<'a>) -> Self {
932 Self::Add { source, from: None }
933 }
934}
935
936#[derive(Debug, Clone)]
937pub struct KeybindUpdateTarget<'a> {
938 pub context: Option<&'a str>,
939 pub keystrokes: &'a [KeybindingKeystroke],
940 pub action_name: &'a str,
941 pub action_arguments: Option<&'a str>,
942}
943
944impl<'a> KeybindUpdateTarget<'a> {
945 fn action_value(&self) -> Result<Value> {
946 if self.action_name == gpui::NoAction.name() {
947 return Ok(Value::Null);
948 }
949 let action_name: Value = self.action_name.into();
950 let value = match self.action_arguments {
951 Some(args) if !args.is_empty() => {
952 let args = serde_json::from_str::<Value>(args)
953 .context("Failed to parse action arguments as JSON")?;
954 serde_json::json!([action_name, args])
955 }
956 _ => action_name,
957 };
958 Ok(value)
959 }
960
961 fn keystrokes_unparsed(&self) -> String {
962 let mut keystrokes = String::with_capacity(self.keystrokes.len() * 8);
963 for keystroke in self.keystrokes {
964 // The reason use `keystroke.unparse()` instead of `keystroke.inner.unparse()`
965 // here is that, we want the user to use `ctrl-shift-4` instead of `ctrl-$`
966 // by default on Windows.
967 keystrokes.push_str(&keystroke.unparse());
968 keystrokes.push(' ');
969 }
970 keystrokes.pop();
971 keystrokes
972 }
973
974 fn telemetry_string(&self) -> String {
975 format!(
976 "action_name: {}, context: {}, action_arguments: {}, keystrokes: {}",
977 self.action_name,
978 self.context.unwrap_or("global"),
979 self.action_arguments.unwrap_or("none"),
980 self.keystrokes_unparsed()
981 )
982 }
983}
984
985#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]
986pub enum KeybindSource {
987 User,
988 Vim,
989 Base,
990 #[default]
991 Default,
992 Unknown,
993}
994
995impl KeybindSource {
996 const BASE: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::Base as u32);
997 const DEFAULT: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::Default as u32);
998 const VIM: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::Vim as u32);
999 const USER: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::User as u32);
1000
1001 pub fn name(&self) -> &'static str {
1002 match self {
1003 KeybindSource::User => "User",
1004 KeybindSource::Default => "Default",
1005 KeybindSource::Base => "Base",
1006 KeybindSource::Vim => "Vim",
1007 KeybindSource::Unknown => "Unknown",
1008 }
1009 }
1010
1011 pub fn meta(&self) -> KeyBindingMetaIndex {
1012 match self {
1013 KeybindSource::User => Self::USER,
1014 KeybindSource::Default => Self::DEFAULT,
1015 KeybindSource::Base => Self::BASE,
1016 KeybindSource::Vim => Self::VIM,
1017 KeybindSource::Unknown => KeyBindingMetaIndex(*self as u32),
1018 }
1019 }
1020
1021 pub fn from_meta(index: KeyBindingMetaIndex) -> Self {
1022 match index {
1023 Self::USER => KeybindSource::User,
1024 Self::BASE => KeybindSource::Base,
1025 Self::DEFAULT => KeybindSource::Default,
1026 Self::VIM => KeybindSource::Vim,
1027 _ => KeybindSource::Unknown,
1028 }
1029 }
1030}
1031
1032impl From<KeyBindingMetaIndex> for KeybindSource {
1033 fn from(index: KeyBindingMetaIndex) -> Self {
1034 Self::from_meta(index)
1035 }
1036}
1037
1038impl From<KeybindSource> for KeyBindingMetaIndex {
1039 fn from(source: KeybindSource) -> Self {
1040 source.meta()
1041 }
1042}
1043
1044#[cfg(test)]
1045mod tests {
1046 use gpui::{DummyKeyboardMapper, KeybindingKeystroke, Keystroke};
1047 use unindent::Unindent;
1048
1049 use crate::{
1050 KeybindSource, KeymapFile,
1051 keymap_file::{KeybindUpdateOperation, KeybindUpdateTarget},
1052 };
1053
1054 #[test]
1055 fn can_deserialize_keymap_with_trailing_comma() {
1056 let json = indoc::indoc! {"[
1057 // Standard macOS bindings
1058 {
1059 \"bindings\": {
1060 \"up\": \"menu::SelectPrevious\",
1061 },
1062 },
1063 ]
1064 "
1065 };
1066 KeymapFile::parse(json).unwrap();
1067 }
1068
1069 #[track_caller]
1070 fn check_keymap_update(
1071 input: impl ToString,
1072 operation: KeybindUpdateOperation,
1073 expected: impl ToString,
1074 ) {
1075 let result = KeymapFile::update_keybinding(
1076 operation,
1077 input.to_string(),
1078 4,
1079 &gpui::DummyKeyboardMapper,
1080 )
1081 .expect("Update succeeded");
1082 pretty_assertions::assert_eq!(expected.to_string(), result);
1083 }
1084
1085 #[track_caller]
1086 fn parse_keystrokes(keystrokes: &str) -> Vec<KeybindingKeystroke> {
1087 keystrokes
1088 .split(' ')
1089 .map(|s| {
1090 KeybindingKeystroke::new_with_mapper(
1091 Keystroke::parse(s).expect("Keystrokes valid"),
1092 false,
1093 &DummyKeyboardMapper,
1094 )
1095 })
1096 .collect()
1097 }
1098
1099 #[test]
1100 fn keymap_update() {
1101 zlog::init_test();
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: None,
1110 }),
1111 r#"[
1112 {
1113 "bindings": {
1114 "ctrl-a": "zed::SomeAction"
1115 }
1116 }
1117 ]"#
1118 .unindent(),
1119 );
1120
1121 check_keymap_update(
1122 "[]",
1123 KeybindUpdateOperation::add(KeybindUpdateTarget {
1124 keystrokes: &parse_keystrokes("\\ a"),
1125 action_name: "zed::SomeAction",
1126 context: None,
1127 action_arguments: None,
1128 }),
1129 r#"[
1130 {
1131 "bindings": {
1132 "\\ a": "zed::SomeAction"
1133 }
1134 }
1135 ]"#
1136 .unindent(),
1137 );
1138
1139 check_keymap_update(
1140 "[]",
1141 KeybindUpdateOperation::add(KeybindUpdateTarget {
1142 keystrokes: &parse_keystrokes("ctrl-a"),
1143 action_name: "zed::SomeAction",
1144 context: None,
1145 action_arguments: Some(""),
1146 }),
1147 r#"[
1148 {
1149 "bindings": {
1150 "ctrl-a": "zed::SomeAction"
1151 }
1152 }
1153 ]"#
1154 .unindent(),
1155 );
1156
1157 check_keymap_update(
1158 r#"[
1159 {
1160 "bindings": {
1161 "ctrl-a": "zed::SomeAction"
1162 }
1163 }
1164 ]"#
1165 .unindent(),
1166 KeybindUpdateOperation::add(KeybindUpdateTarget {
1167 keystrokes: &parse_keystrokes("ctrl-b"),
1168 action_name: "zed::SomeOtherAction",
1169 context: None,
1170 action_arguments: None,
1171 }),
1172 r#"[
1173 {
1174 "bindings": {
1175 "ctrl-a": "zed::SomeAction"
1176 }
1177 },
1178 {
1179 "bindings": {
1180 "ctrl-b": "zed::SomeOtherAction"
1181 }
1182 }
1183 ]"#
1184 .unindent(),
1185 );
1186
1187 check_keymap_update(
1188 r#"[
1189 {
1190 "bindings": {
1191 "ctrl-a": "zed::SomeAction"
1192 }
1193 }
1194 ]"#
1195 .unindent(),
1196 KeybindUpdateOperation::add(KeybindUpdateTarget {
1197 keystrokes: &parse_keystrokes("ctrl-b"),
1198 action_name: "zed::SomeOtherAction",
1199 context: None,
1200 action_arguments: Some(r#"{"foo": "bar"}"#),
1201 }),
1202 r#"[
1203 {
1204 "bindings": {
1205 "ctrl-a": "zed::SomeAction"
1206 }
1207 },
1208 {
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::add(KeybindUpdateTarget {
1232 keystrokes: &parse_keystrokes("ctrl-b"),
1233 action_name: "zed::SomeOtherAction",
1234 context: Some("Zed > Editor && some_condition = true"),
1235 action_arguments: Some(r#"{"foo": "bar"}"#),
1236 }),
1237 r#"[
1238 {
1239 "bindings": {
1240 "ctrl-a": "zed::SomeAction"
1241 }
1242 },
1243 {
1244 "context": "Zed > Editor && some_condition = true",
1245 "bindings": {
1246 "ctrl-b": [
1247 "zed::SomeOtherAction",
1248 {
1249 "foo": "bar"
1250 }
1251 ]
1252 }
1253 }
1254 ]"#
1255 .unindent(),
1256 );
1257
1258 check_keymap_update(
1259 r#"[
1260 {
1261 "bindings": {
1262 "ctrl-a": "zed::SomeAction"
1263 }
1264 }
1265 ]"#
1266 .unindent(),
1267 KeybindUpdateOperation::Replace {
1268 target: KeybindUpdateTarget {
1269 keystrokes: &parse_keystrokes("ctrl-a"),
1270 action_name: "zed::SomeAction",
1271 context: None,
1272 action_arguments: None,
1273 },
1274 source: KeybindUpdateTarget {
1275 keystrokes: &parse_keystrokes("ctrl-b"),
1276 action_name: "zed::SomeOtherAction",
1277 context: None,
1278 action_arguments: Some(r#"{"foo": "bar"}"#),
1279 },
1280 target_keybind_source: KeybindSource::Base,
1281 },
1282 r#"[
1283 {
1284 "bindings": {
1285 "ctrl-a": "zed::SomeAction"
1286 }
1287 },
1288 {
1289 "bindings": {
1290 "ctrl-b": [
1291 "zed::SomeOtherAction",
1292 {
1293 "foo": "bar"
1294 }
1295 ]
1296 }
1297 }
1298 ]"#
1299 .unindent(),
1300 );
1301
1302 check_keymap_update(
1303 r#"[
1304 {
1305 "bindings": {
1306 "a": "zed::SomeAction"
1307 }
1308 }
1309 ]"#
1310 .unindent(),
1311 KeybindUpdateOperation::Replace {
1312 target: KeybindUpdateTarget {
1313 keystrokes: &parse_keystrokes("a"),
1314 action_name: "zed::SomeAction",
1315 context: None,
1316 action_arguments: None,
1317 },
1318 source: KeybindUpdateTarget {
1319 keystrokes: &parse_keystrokes("ctrl-b"),
1320 action_name: "zed::SomeOtherAction",
1321 context: None,
1322 action_arguments: Some(r#"{"foo": "bar"}"#),
1323 },
1324 target_keybind_source: KeybindSource::User,
1325 },
1326 r#"[
1327 {
1328 "bindings": {
1329 "ctrl-b": [
1330 "zed::SomeOtherAction",
1331 {
1332 "foo": "bar"
1333 }
1334 ]
1335 }
1336 }
1337 ]"#
1338 .unindent(),
1339 );
1340
1341 check_keymap_update(
1342 r#"[
1343 {
1344 "bindings": {
1345 "\\ a": "zed::SomeAction"
1346 }
1347 }
1348 ]"#
1349 .unindent(),
1350 KeybindUpdateOperation::Replace {
1351 target: KeybindUpdateTarget {
1352 keystrokes: &parse_keystrokes("\\ a"),
1353 action_name: "zed::SomeAction",
1354 context: None,
1355 action_arguments: None,
1356 },
1357 source: KeybindUpdateTarget {
1358 keystrokes: &parse_keystrokes("\\ b"),
1359 action_name: "zed::SomeOtherAction",
1360 context: None,
1361 action_arguments: Some(r#"{"foo": "bar"}"#),
1362 },
1363 target_keybind_source: KeybindSource::User,
1364 },
1365 r#"[
1366 {
1367 "bindings": {
1368 "\\ b": [
1369 "zed::SomeOtherAction",
1370 {
1371 "foo": "bar"
1372 }
1373 ]
1374 }
1375 }
1376 ]"#
1377 .unindent(),
1378 );
1379
1380 check_keymap_update(
1381 r#"[
1382 {
1383 "bindings": {
1384 "\\ a": "zed::SomeAction"
1385 }
1386 }
1387 ]"#
1388 .unindent(),
1389 KeybindUpdateOperation::Replace {
1390 target: KeybindUpdateTarget {
1391 keystrokes: &parse_keystrokes("\\ a"),
1392 action_name: "zed::SomeAction",
1393 context: None,
1394 action_arguments: None,
1395 },
1396 source: KeybindUpdateTarget {
1397 keystrokes: &parse_keystrokes("\\ a"),
1398 action_name: "zed::SomeAction",
1399 context: None,
1400 action_arguments: None,
1401 },
1402 target_keybind_source: KeybindSource::User,
1403 },
1404 r#"[
1405 {
1406 "bindings": {
1407 "\\ a": "zed::SomeAction"
1408 }
1409 }
1410 ]"#
1411 .unindent(),
1412 );
1413
1414 check_keymap_update(
1415 r#"[
1416 {
1417 "bindings": {
1418 "ctrl-a": "zed::SomeAction"
1419 }
1420 }
1421 ]"#
1422 .unindent(),
1423 KeybindUpdateOperation::Replace {
1424 target: KeybindUpdateTarget {
1425 keystrokes: &parse_keystrokes("ctrl-a"),
1426 action_name: "zed::SomeNonexistentAction",
1427 context: None,
1428 action_arguments: None,
1429 },
1430 source: KeybindUpdateTarget {
1431 keystrokes: &parse_keystrokes("ctrl-b"),
1432 action_name: "zed::SomeOtherAction",
1433 context: None,
1434 action_arguments: None,
1435 },
1436 target_keybind_source: KeybindSource::User,
1437 },
1438 r#"[
1439 {
1440 "bindings": {
1441 "ctrl-a": "zed::SomeAction"
1442 }
1443 },
1444 {
1445 "bindings": {
1446 "ctrl-b": "zed::SomeOtherAction"
1447 }
1448 }
1449 ]"#
1450 .unindent(),
1451 );
1452
1453 check_keymap_update(
1454 r#"[
1455 {
1456 "bindings": {
1457 // some comment
1458 "ctrl-a": "zed::SomeAction"
1459 // some other comment
1460 }
1461 }
1462 ]"#
1463 .unindent(),
1464 KeybindUpdateOperation::Replace {
1465 target: KeybindUpdateTarget {
1466 keystrokes: &parse_keystrokes("ctrl-a"),
1467 action_name: "zed::SomeAction",
1468 context: None,
1469 action_arguments: None,
1470 },
1471 source: KeybindUpdateTarget {
1472 keystrokes: &parse_keystrokes("ctrl-b"),
1473 action_name: "zed::SomeOtherAction",
1474 context: None,
1475 action_arguments: Some(r#"{"foo": "bar"}"#),
1476 },
1477 target_keybind_source: KeybindSource::User,
1478 },
1479 r#"[
1480 {
1481 "bindings": {
1482 // some comment
1483 "ctrl-b": [
1484 "zed::SomeOtherAction",
1485 {
1486 "foo": "bar"
1487 }
1488 ]
1489 // some other comment
1490 }
1491 }
1492 ]"#
1493 .unindent(),
1494 );
1495
1496 check_keymap_update(
1497 r#"[
1498 {
1499 "context": "SomeContext",
1500 "bindings": {
1501 "a": "foo::bar",
1502 "b": "baz::qux",
1503 }
1504 }
1505 ]"#
1506 .unindent(),
1507 KeybindUpdateOperation::Replace {
1508 target: KeybindUpdateTarget {
1509 keystrokes: &parse_keystrokes("a"),
1510 action_name: "foo::bar",
1511 context: Some("SomeContext"),
1512 action_arguments: None,
1513 },
1514 source: KeybindUpdateTarget {
1515 keystrokes: &parse_keystrokes("c"),
1516 action_name: "foo::baz",
1517 context: Some("SomeOtherContext"),
1518 action_arguments: None,
1519 },
1520 target_keybind_source: KeybindSource::User,
1521 },
1522 r#"[
1523 {
1524 "context": "SomeContext",
1525 "bindings": {
1526 "b": "baz::qux",
1527 }
1528 },
1529 {
1530 "context": "SomeOtherContext",
1531 "bindings": {
1532 "c": "foo::baz"
1533 }
1534 }
1535 ]"#
1536 .unindent(),
1537 );
1538
1539 check_keymap_update(
1540 r#"[
1541 {
1542 "context": "SomeContext",
1543 "bindings": {
1544 "a": "foo::bar",
1545 }
1546 }
1547 ]"#
1548 .unindent(),
1549 KeybindUpdateOperation::Replace {
1550 target: KeybindUpdateTarget {
1551 keystrokes: &parse_keystrokes("a"),
1552 action_name: "foo::bar",
1553 context: Some("SomeContext"),
1554 action_arguments: None,
1555 },
1556 source: KeybindUpdateTarget {
1557 keystrokes: &parse_keystrokes("c"),
1558 action_name: "foo::baz",
1559 context: Some("SomeOtherContext"),
1560 action_arguments: None,
1561 },
1562 target_keybind_source: KeybindSource::User,
1563 },
1564 r#"[
1565 {
1566 "context": "SomeOtherContext",
1567 "bindings": {
1568 "c": "foo::baz",
1569 }
1570 }
1571 ]"#
1572 .unindent(),
1573 );
1574
1575 check_keymap_update(
1576 r#"[
1577 {
1578 "context": "SomeContext",
1579 "bindings": {
1580 "a": "foo::bar",
1581 "c": "foo::baz",
1582 }
1583 },
1584 ]"#
1585 .unindent(),
1586 KeybindUpdateOperation::Remove {
1587 target: KeybindUpdateTarget {
1588 context: Some("SomeContext"),
1589 keystrokes: &parse_keystrokes("a"),
1590 action_name: "foo::bar",
1591 action_arguments: None,
1592 },
1593 target_keybind_source: KeybindSource::User,
1594 },
1595 r#"[
1596 {
1597 "context": "SomeContext",
1598 "bindings": {
1599 "c": "foo::baz",
1600 }
1601 },
1602 ]"#
1603 .unindent(),
1604 );
1605
1606 check_keymap_update(
1607 r#"[
1608 {
1609 "context": "SomeContext",
1610 "bindings": {
1611 "\\ a": "foo::bar",
1612 "c": "foo::baz",
1613 }
1614 },
1615 ]"#
1616 .unindent(),
1617 KeybindUpdateOperation::Remove {
1618 target: KeybindUpdateTarget {
1619 context: Some("SomeContext"),
1620 keystrokes: &parse_keystrokes("\\ a"),
1621 action_name: "foo::bar",
1622 action_arguments: None,
1623 },
1624 target_keybind_source: KeybindSource::User,
1625 },
1626 r#"[
1627 {
1628 "context": "SomeContext",
1629 "bindings": {
1630 "c": "foo::baz",
1631 }
1632 },
1633 ]"#
1634 .unindent(),
1635 );
1636
1637 check_keymap_update(
1638 r#"[
1639 {
1640 "context": "SomeContext",
1641 "bindings": {
1642 "a": ["foo::bar", true],
1643 "c": "foo::baz",
1644 }
1645 },
1646 ]"#
1647 .unindent(),
1648 KeybindUpdateOperation::Remove {
1649 target: KeybindUpdateTarget {
1650 context: Some("SomeContext"),
1651 keystrokes: &parse_keystrokes("a"),
1652 action_name: "foo::bar",
1653 action_arguments: Some("true"),
1654 },
1655 target_keybind_source: KeybindSource::User,
1656 },
1657 r#"[
1658 {
1659 "context": "SomeContext",
1660 "bindings": {
1661 "c": "foo::baz",
1662 }
1663 },
1664 ]"#
1665 .unindent(),
1666 );
1667
1668 check_keymap_update(
1669 r#"[
1670 {
1671 "context": "SomeContext",
1672 "bindings": {
1673 "b": "foo::baz",
1674 }
1675 },
1676 {
1677 "context": "SomeContext",
1678 "bindings": {
1679 "a": ["foo::bar", true],
1680 }
1681 },
1682 {
1683 "context": "SomeContext",
1684 "bindings": {
1685 "c": "foo::baz",
1686 }
1687 },
1688 ]"#
1689 .unindent(),
1690 KeybindUpdateOperation::Remove {
1691 target: KeybindUpdateTarget {
1692 context: Some("SomeContext"),
1693 keystrokes: &parse_keystrokes("a"),
1694 action_name: "foo::bar",
1695 action_arguments: Some("true"),
1696 },
1697 target_keybind_source: KeybindSource::User,
1698 },
1699 r#"[
1700 {
1701 "context": "SomeContext",
1702 "bindings": {
1703 "b": "foo::baz",
1704 }
1705 },
1706 {
1707 "context": "SomeContext",
1708 "bindings": {
1709 "c": "foo::baz",
1710 }
1711 },
1712 ]"#
1713 .unindent(),
1714 );
1715 check_keymap_update(
1716 r#"[
1717 {
1718 "context": "SomeOtherContext",
1719 "use_key_equivalents": true,
1720 "bindings": {
1721 "b": "foo::bar",
1722 }
1723 },
1724 ]"#
1725 .unindent(),
1726 KeybindUpdateOperation::Add {
1727 source: KeybindUpdateTarget {
1728 context: Some("SomeContext"),
1729 keystrokes: &parse_keystrokes("a"),
1730 action_name: "foo::baz",
1731 action_arguments: Some("true"),
1732 },
1733 from: Some(KeybindUpdateTarget {
1734 context: Some("SomeOtherContext"),
1735 keystrokes: &parse_keystrokes("b"),
1736 action_name: "foo::bar",
1737 action_arguments: None,
1738 }),
1739 },
1740 r#"[
1741 {
1742 "context": "SomeOtherContext",
1743 "use_key_equivalents": true,
1744 "bindings": {
1745 "b": "foo::bar",
1746 }
1747 },
1748 {
1749 "context": "SomeContext",
1750 "use_key_equivalents": true,
1751 "bindings": {
1752 "a": [
1753 "foo::baz",
1754 true
1755 ]
1756 }
1757 }
1758 ]"#
1759 .unindent(),
1760 );
1761
1762 check_keymap_update(
1763 r#"[
1764 {
1765 "context": "SomeOtherContext",
1766 "use_key_equivalents": true,
1767 "bindings": {
1768 "b": "foo::bar",
1769 }
1770 },
1771 ]"#
1772 .unindent(),
1773 KeybindUpdateOperation::Remove {
1774 target: KeybindUpdateTarget {
1775 context: Some("SomeContext"),
1776 keystrokes: &parse_keystrokes("a"),
1777 action_name: "foo::baz",
1778 action_arguments: Some("true"),
1779 },
1780 target_keybind_source: KeybindSource::Default,
1781 },
1782 r#"[
1783 {
1784 "context": "SomeOtherContext",
1785 "use_key_equivalents": true,
1786 "bindings": {
1787 "b": "foo::bar",
1788 }
1789 },
1790 {
1791 "context": "SomeContext",
1792 "bindings": {
1793 "a": null
1794 }
1795 }
1796 ]"#
1797 .unindent(),
1798 );
1799 }
1800
1801 #[test]
1802 fn test_keymap_remove() {
1803 zlog::init_test();
1804
1805 check_keymap_update(
1806 r#"
1807 [
1808 {
1809 "context": "Editor",
1810 "bindings": {
1811 "cmd-k cmd-u": "editor::ConvertToUpperCase",
1812 "cmd-k cmd-l": "editor::ConvertToLowerCase",
1813 "cmd-[": "pane::GoBack",
1814 }
1815 },
1816 ]
1817 "#,
1818 KeybindUpdateOperation::Remove {
1819 target: KeybindUpdateTarget {
1820 context: Some("Editor"),
1821 keystrokes: &parse_keystrokes("cmd-k cmd-l"),
1822 action_name: "editor::ConvertToLowerCase",
1823 action_arguments: None,
1824 },
1825 target_keybind_source: KeybindSource::User,
1826 },
1827 r#"
1828 [
1829 {
1830 "context": "Editor",
1831 "bindings": {
1832 "cmd-k cmd-u": "editor::ConvertToUpperCase",
1833 "cmd-[": "pane::GoBack",
1834 }
1835 },
1836 ]
1837 "#,
1838 );
1839 }
1840}