keymap_file.rs

   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            keymap_contents.replace_range(replace_range, &replace_value);
 683            return Ok(keymap_contents);
 684        }
 685
 686        if let KeybindUpdateOperation::Replace { source, target, .. } = operation {
 687            let target_action_value = target
 688                .action_value()
 689                .context("Failed to generate target action JSON value")?;
 690            let source_action_value = source
 691                .action_value()
 692                .context("Failed to generate source action JSON value")?;
 693
 694            if let Some((index, keystrokes_str)) =
 695                find_binding(&keymap, &target, &target_action_value, keyboard_mapper)
 696            {
 697                if target.context == source.context {
 698                    // if we are only changing the keybinding (common case)
 699                    // not the context, etc. Then just update the binding in place
 700
 701                    let (replace_range, replace_value) = replace_top_level_array_value_in_json_text(
 702                        &keymap_contents,
 703                        &["bindings", keystrokes_str],
 704                        Some(&source_action_value),
 705                        Some(&source.keystrokes_unparsed()),
 706                        index,
 707                        tab_size,
 708                    );
 709                    keymap_contents.replace_range(replace_range, &replace_value);
 710
 711                    return Ok(keymap_contents);
 712                } else if keymap.0[index]
 713                    .bindings
 714                    .as_ref()
 715                    .is_none_or(|bindings| bindings.len() == 1)
 716                {
 717                    // if we are replacing the only binding in the section,
 718                    // just update the section in place, updating the context
 719                    // and the binding
 720
 721                    let (replace_range, replace_value) = replace_top_level_array_value_in_json_text(
 722                        &keymap_contents,
 723                        &["bindings", keystrokes_str],
 724                        Some(&source_action_value),
 725                        Some(&source.keystrokes_unparsed()),
 726                        index,
 727                        tab_size,
 728                    );
 729                    keymap_contents.replace_range(replace_range, &replace_value);
 730
 731                    let (replace_range, replace_value) = replace_top_level_array_value_in_json_text(
 732                        &keymap_contents,
 733                        &["context"],
 734                        source.context.map(Into::into).as_ref(),
 735                        None,
 736                        index,
 737                        tab_size,
 738                    );
 739                    keymap_contents.replace_range(replace_range, &replace_value);
 740                    return Ok(keymap_contents);
 741                } else {
 742                    // if we are replacing one of multiple bindings in a section
 743                    // with a context change, remove the existing binding from the
 744                    // section, then treat this operation as an add operation of the
 745                    // new binding with the updated context.
 746
 747                    let (replace_range, replace_value) = replace_top_level_array_value_in_json_text(
 748                        &keymap_contents,
 749                        &["bindings", keystrokes_str],
 750                        None,
 751                        None,
 752                        index,
 753                        tab_size,
 754                    );
 755                    keymap_contents.replace_range(replace_range, &replace_value);
 756                    operation = KeybindUpdateOperation::Add {
 757                        source,
 758                        from: Some(target),
 759                    };
 760                }
 761            } else {
 762                log::warn!(
 763                    "Failed to find keybinding to update `{:?} -> {}` creating new binding for `{:?} -> {}` instead",
 764                    target.keystrokes,
 765                    target_action_value,
 766                    source.keystrokes,
 767                    source_action_value,
 768                );
 769                operation = KeybindUpdateOperation::Add {
 770                    source,
 771                    from: Some(target),
 772                };
 773            }
 774        }
 775
 776        if let KeybindUpdateOperation::Add {
 777            source: keybinding,
 778            from,
 779        } = operation
 780        {
 781            let mut value = serde_json::Map::with_capacity(4);
 782            if let Some(context) = keybinding.context {
 783                value.insert("context".to_string(), context.into());
 784            }
 785            let use_key_equivalents = from.and_then(|from| {
 786                let action_value = from.action_value().context("Failed to serialize action value. `use_key_equivalents` on new keybinding may be incorrect.").log_err()?;
 787                let (index, _) = find_binding(&keymap, &from, &action_value, keyboard_mapper)?;
 788                Some(keymap.0[index].use_key_equivalents)
 789            }).unwrap_or(false);
 790            if use_key_equivalents {
 791                value.insert("use_key_equivalents".to_string(), true.into());
 792            }
 793
 794            value.insert("bindings".to_string(), {
 795                let mut bindings = serde_json::Map::new();
 796                let action = keybinding.action_value()?;
 797                bindings.insert(keybinding.keystrokes_unparsed(), action);
 798                bindings.into()
 799            });
 800
 801            let (replace_range, replace_value) = append_top_level_array_value_in_json_text(
 802                &keymap_contents,
 803                &value.into(),
 804                tab_size,
 805            );
 806            keymap_contents.replace_range(replace_range, &replace_value);
 807        }
 808        return Ok(keymap_contents);
 809
 810        fn find_binding<'a, 'b>(
 811            keymap: &'b KeymapFile,
 812            target: &KeybindUpdateTarget<'a>,
 813            target_action_value: &Value,
 814            keyboard_mapper: &dyn gpui::PlatformKeyboardMapper,
 815        ) -> Option<(usize, &'b str)> {
 816            let target_context_parsed =
 817                KeyBindingContextPredicate::parse(target.context.unwrap_or("")).ok();
 818            for (index, section) in keymap.sections().enumerate() {
 819                let section_context_parsed =
 820                    KeyBindingContextPredicate::parse(&section.context).ok();
 821                if section_context_parsed != target_context_parsed {
 822                    continue;
 823                }
 824                let Some(bindings) = &section.bindings else {
 825                    continue;
 826                };
 827                for (keystrokes_str, action) in bindings {
 828                    let Ok(keystrokes) = keystrokes_str
 829                        .split_whitespace()
 830                        .map(|source| {
 831                            let keystroke = Keystroke::parse(source)?;
 832                            Ok(KeybindingKeystroke::new_with_mapper(
 833                                keystroke,
 834                                false,
 835                                keyboard_mapper,
 836                            ))
 837                        })
 838                        .collect::<Result<Vec<_>, InvalidKeystrokeError>>()
 839                    else {
 840                        continue;
 841                    };
 842                    if keystrokes.len() != target.keystrokes.len()
 843                        || !keystrokes
 844                            .iter()
 845                            .zip(target.keystrokes)
 846                            .all(|(a, b)| a.inner().should_match(b))
 847                    {
 848                        continue;
 849                    }
 850                    if &action.0 != target_action_value {
 851                        continue;
 852                    }
 853                    return Some((index, keystrokes_str));
 854                }
 855            }
 856            None
 857        }
 858    }
 859}
 860
 861#[derive(Clone, Debug)]
 862pub enum KeybindUpdateOperation<'a> {
 863    Replace {
 864        /// Describes the keybind to create
 865        source: KeybindUpdateTarget<'a>,
 866        /// Describes the keybind to remove
 867        target: KeybindUpdateTarget<'a>,
 868        target_keybind_source: KeybindSource,
 869    },
 870    Add {
 871        source: KeybindUpdateTarget<'a>,
 872        from: Option<KeybindUpdateTarget<'a>>,
 873    },
 874    Remove {
 875        target: KeybindUpdateTarget<'a>,
 876        target_keybind_source: KeybindSource,
 877    },
 878}
 879
 880impl KeybindUpdateOperation<'_> {
 881    pub fn generate_telemetry(
 882        &self,
 883    ) -> (
 884        // The keybind that is created
 885        String,
 886        // The keybinding that was removed
 887        String,
 888        // The source of the keybinding
 889        String,
 890    ) {
 891        let (new_binding, removed_binding, source) = match &self {
 892            KeybindUpdateOperation::Replace {
 893                source,
 894                target,
 895                target_keybind_source,
 896            } => (Some(source), Some(target), Some(*target_keybind_source)),
 897            KeybindUpdateOperation::Add { source, .. } => (Some(source), None, None),
 898            KeybindUpdateOperation::Remove {
 899                target,
 900                target_keybind_source,
 901            } => (None, Some(target), Some(*target_keybind_source)),
 902        };
 903
 904        let new_binding = new_binding
 905            .map(KeybindUpdateTarget::telemetry_string)
 906            .unwrap_or("null".to_owned());
 907        let removed_binding = removed_binding
 908            .map(KeybindUpdateTarget::telemetry_string)
 909            .unwrap_or("null".to_owned());
 910
 911        let source = source
 912            .as_ref()
 913            .map(KeybindSource::name)
 914            .map(ToOwned::to_owned)
 915            .unwrap_or("null".to_owned());
 916
 917        (new_binding, removed_binding, source)
 918    }
 919}
 920
 921impl<'a> KeybindUpdateOperation<'a> {
 922    pub fn add(source: KeybindUpdateTarget<'a>) -> Self {
 923        Self::Add { source, from: None }
 924    }
 925}
 926
 927#[derive(Debug, Clone)]
 928pub struct KeybindUpdateTarget<'a> {
 929    pub context: Option<&'a str>,
 930    pub keystrokes: &'a [KeybindingKeystroke],
 931    pub action_name: &'a str,
 932    pub action_arguments: Option<&'a str>,
 933}
 934
 935impl<'a> KeybindUpdateTarget<'a> {
 936    fn action_value(&self) -> Result<Value> {
 937        if self.action_name == gpui::NoAction.name() {
 938            return Ok(Value::Null);
 939        }
 940        let action_name: Value = self.action_name.into();
 941        let value = match self.action_arguments {
 942            Some(args) if !args.is_empty() => {
 943                let args = serde_json::from_str::<Value>(args)
 944                    .context("Failed to parse action arguments as JSON")?;
 945                serde_json::json!([action_name, args])
 946            }
 947            _ => action_name,
 948        };
 949        Ok(value)
 950    }
 951
 952    fn keystrokes_unparsed(&self) -> String {
 953        let mut keystrokes = String::with_capacity(self.keystrokes.len() * 8);
 954        for keystroke in self.keystrokes {
 955            // The reason use `keystroke.unparse()` instead of `keystroke.inner.unparse()`
 956            // here is that, we want the user to use `ctrl-shift-4` instead of `ctrl-$`
 957            // by default on Windows.
 958            keystrokes.push_str(&keystroke.unparse());
 959            keystrokes.push(' ');
 960        }
 961        keystrokes.pop();
 962        keystrokes
 963    }
 964
 965    fn telemetry_string(&self) -> String {
 966        format!(
 967            "action_name: {}, context: {}, action_arguments: {}, keystrokes: {}",
 968            self.action_name,
 969            self.context.unwrap_or("global"),
 970            self.action_arguments.unwrap_or("none"),
 971            self.keystrokes_unparsed()
 972        )
 973    }
 974}
 975
 976#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Debug)]
 977pub enum KeybindSource {
 978    User,
 979    Vim,
 980    Base,
 981    #[default]
 982    Default,
 983    Unknown,
 984}
 985
 986impl KeybindSource {
 987    const BASE: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::Base as u32);
 988    const DEFAULT: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::Default as u32);
 989    const VIM: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::Vim as u32);
 990    const USER: KeyBindingMetaIndex = KeyBindingMetaIndex(KeybindSource::User as u32);
 991
 992    pub fn name(&self) -> &'static str {
 993        match self {
 994            KeybindSource::User => "User",
 995            KeybindSource::Default => "Default",
 996            KeybindSource::Base => "Base",
 997            KeybindSource::Vim => "Vim",
 998            KeybindSource::Unknown => "Unknown",
 999        }
1000    }
1001
1002    pub fn meta(&self) -> KeyBindingMetaIndex {
1003        match self {
1004            KeybindSource::User => Self::USER,
1005            KeybindSource::Default => Self::DEFAULT,
1006            KeybindSource::Base => Self::BASE,
1007            KeybindSource::Vim => Self::VIM,
1008            KeybindSource::Unknown => KeyBindingMetaIndex(*self as u32),
1009        }
1010    }
1011
1012    pub fn from_meta(index: KeyBindingMetaIndex) -> Self {
1013        match index {
1014            Self::USER => KeybindSource::User,
1015            Self::BASE => KeybindSource::Base,
1016            Self::DEFAULT => KeybindSource::Default,
1017            Self::VIM => KeybindSource::Vim,
1018            _ => KeybindSource::Unknown,
1019        }
1020    }
1021}
1022
1023impl From<KeyBindingMetaIndex> for KeybindSource {
1024    fn from(index: KeyBindingMetaIndex) -> Self {
1025        Self::from_meta(index)
1026    }
1027}
1028
1029impl From<KeybindSource> for KeyBindingMetaIndex {
1030    fn from(source: KeybindSource) -> Self {
1031        source.meta()
1032    }
1033}
1034
1035#[cfg(test)]
1036mod tests {
1037    use gpui::{DummyKeyboardMapper, KeybindingKeystroke, Keystroke};
1038    use unindent::Unindent;
1039
1040    use crate::{
1041        KeybindSource, KeymapFile,
1042        keymap_file::{KeybindUpdateOperation, KeybindUpdateTarget},
1043    };
1044
1045    #[test]
1046    fn can_deserialize_keymap_with_trailing_comma() {
1047        let json = indoc::indoc! {"[
1048              // Standard macOS bindings
1049              {
1050                \"bindings\": {
1051                  \"up\": \"menu::SelectPrevious\",
1052                },
1053              },
1054            ]
1055                  "
1056        };
1057        KeymapFile::parse(json).unwrap();
1058    }
1059
1060    #[track_caller]
1061    fn check_keymap_update(
1062        input: impl ToString,
1063        operation: KeybindUpdateOperation,
1064        expected: impl ToString,
1065    ) {
1066        let result = KeymapFile::update_keybinding(
1067            operation,
1068            input.to_string(),
1069            4,
1070            &gpui::DummyKeyboardMapper,
1071        )
1072        .expect("Update succeeded");
1073        pretty_assertions::assert_eq!(expected.to_string(), result);
1074    }
1075
1076    #[track_caller]
1077    fn parse_keystrokes(keystrokes: &str) -> Vec<KeybindingKeystroke> {
1078        keystrokes
1079            .split(' ')
1080            .map(|s| {
1081                KeybindingKeystroke::new_with_mapper(
1082                    Keystroke::parse(s).expect("Keystrokes valid"),
1083                    false,
1084                    &DummyKeyboardMapper,
1085                )
1086            })
1087            .collect()
1088    }
1089
1090    #[test]
1091    fn keymap_update() {
1092        zlog::init_test();
1093
1094        check_keymap_update(
1095            "[]",
1096            KeybindUpdateOperation::add(KeybindUpdateTarget {
1097                keystrokes: &parse_keystrokes("ctrl-a"),
1098                action_name: "zed::SomeAction",
1099                context: None,
1100                action_arguments: None,
1101            }),
1102            r#"[
1103                {
1104                    "bindings": {
1105                        "ctrl-a": "zed::SomeAction"
1106                    }
1107                }
1108            ]"#
1109            .unindent(),
1110        );
1111
1112        check_keymap_update(
1113            "[]",
1114            KeybindUpdateOperation::add(KeybindUpdateTarget {
1115                keystrokes: &parse_keystrokes("\\ a"),
1116                action_name: "zed::SomeAction",
1117                context: None,
1118                action_arguments: None,
1119            }),
1120            r#"[
1121                {
1122                    "bindings": {
1123                        "\\ a": "zed::SomeAction"
1124                    }
1125                }
1126            ]"#
1127            .unindent(),
1128        );
1129
1130        check_keymap_update(
1131            "[]",
1132            KeybindUpdateOperation::add(KeybindUpdateTarget {
1133                keystrokes: &parse_keystrokes("ctrl-a"),
1134                action_name: "zed::SomeAction",
1135                context: None,
1136                action_arguments: Some(""),
1137            }),
1138            r#"[
1139                {
1140                    "bindings": {
1141                        "ctrl-a": "zed::SomeAction"
1142                    }
1143                }
1144            ]"#
1145            .unindent(),
1146        );
1147
1148        check_keymap_update(
1149            r#"[
1150                {
1151                    "bindings": {
1152                        "ctrl-a": "zed::SomeAction"
1153                    }
1154                }
1155            ]"#
1156            .unindent(),
1157            KeybindUpdateOperation::add(KeybindUpdateTarget {
1158                keystrokes: &parse_keystrokes("ctrl-b"),
1159                action_name: "zed::SomeOtherAction",
1160                context: None,
1161                action_arguments: None,
1162            }),
1163            r#"[
1164                {
1165                    "bindings": {
1166                        "ctrl-a": "zed::SomeAction"
1167                    }
1168                },
1169                {
1170                    "bindings": {
1171                        "ctrl-b": "zed::SomeOtherAction"
1172                    }
1173                }
1174            ]"#
1175            .unindent(),
1176        );
1177
1178        check_keymap_update(
1179            r#"[
1180                {
1181                    "bindings": {
1182                        "ctrl-a": "zed::SomeAction"
1183                    }
1184                }
1185            ]"#
1186            .unindent(),
1187            KeybindUpdateOperation::add(KeybindUpdateTarget {
1188                keystrokes: &parse_keystrokes("ctrl-b"),
1189                action_name: "zed::SomeOtherAction",
1190                context: None,
1191                action_arguments: Some(r#"{"foo": "bar"}"#),
1192            }),
1193            r#"[
1194                {
1195                    "bindings": {
1196                        "ctrl-a": "zed::SomeAction"
1197                    }
1198                },
1199                {
1200                    "bindings": {
1201                        "ctrl-b": [
1202                            "zed::SomeOtherAction",
1203                            {
1204                                "foo": "bar"
1205                            }
1206                        ]
1207                    }
1208                }
1209            ]"#
1210            .unindent(),
1211        );
1212
1213        check_keymap_update(
1214            r#"[
1215                {
1216                    "bindings": {
1217                        "ctrl-a": "zed::SomeAction"
1218                    }
1219                }
1220            ]"#
1221            .unindent(),
1222            KeybindUpdateOperation::add(KeybindUpdateTarget {
1223                keystrokes: &parse_keystrokes("ctrl-b"),
1224                action_name: "zed::SomeOtherAction",
1225                context: Some("Zed > Editor && some_condition = true"),
1226                action_arguments: Some(r#"{"foo": "bar"}"#),
1227            }),
1228            r#"[
1229                {
1230                    "bindings": {
1231                        "ctrl-a": "zed::SomeAction"
1232                    }
1233                },
1234                {
1235                    "context": "Zed > Editor && some_condition = true",
1236                    "bindings": {
1237                        "ctrl-b": [
1238                            "zed::SomeOtherAction",
1239                            {
1240                                "foo": "bar"
1241                            }
1242                        ]
1243                    }
1244                }
1245            ]"#
1246            .unindent(),
1247        );
1248
1249        check_keymap_update(
1250            r#"[
1251                {
1252                    "bindings": {
1253                        "ctrl-a": "zed::SomeAction"
1254                    }
1255                }
1256            ]"#
1257            .unindent(),
1258            KeybindUpdateOperation::Replace {
1259                target: KeybindUpdateTarget {
1260                    keystrokes: &parse_keystrokes("ctrl-a"),
1261                    action_name: "zed::SomeAction",
1262                    context: None,
1263                    action_arguments: None,
1264                },
1265                source: KeybindUpdateTarget {
1266                    keystrokes: &parse_keystrokes("ctrl-b"),
1267                    action_name: "zed::SomeOtherAction",
1268                    context: None,
1269                    action_arguments: Some(r#"{"foo": "bar"}"#),
1270                },
1271                target_keybind_source: KeybindSource::Base,
1272            },
1273            r#"[
1274                {
1275                    "bindings": {
1276                        "ctrl-a": "zed::SomeAction"
1277                    }
1278                },
1279                {
1280                    "bindings": {
1281                        "ctrl-b": [
1282                            "zed::SomeOtherAction",
1283                            {
1284                                "foo": "bar"
1285                            }
1286                        ]
1287                    }
1288                }
1289            ]"#
1290            .unindent(),
1291        );
1292
1293        check_keymap_update(
1294            r#"[
1295                {
1296                    "bindings": {
1297                        "a": "zed::SomeAction"
1298                    }
1299                }
1300            ]"#
1301            .unindent(),
1302            KeybindUpdateOperation::Replace {
1303                target: KeybindUpdateTarget {
1304                    keystrokes: &parse_keystrokes("a"),
1305                    action_name: "zed::SomeAction",
1306                    context: None,
1307                    action_arguments: None,
1308                },
1309                source: KeybindUpdateTarget {
1310                    keystrokes: &parse_keystrokes("ctrl-b"),
1311                    action_name: "zed::SomeOtherAction",
1312                    context: None,
1313                    action_arguments: Some(r#"{"foo": "bar"}"#),
1314                },
1315                target_keybind_source: KeybindSource::User,
1316            },
1317            r#"[
1318                {
1319                    "bindings": {
1320                        "ctrl-b": [
1321                            "zed::SomeOtherAction",
1322                            {
1323                                "foo": "bar"
1324                            }
1325                        ]
1326                    }
1327                }
1328            ]"#
1329            .unindent(),
1330        );
1331
1332        check_keymap_update(
1333            r#"[
1334                {
1335                    "bindings": {
1336                        "\\ a": "zed::SomeAction"
1337                    }
1338                }
1339            ]"#
1340            .unindent(),
1341            KeybindUpdateOperation::Replace {
1342                target: KeybindUpdateTarget {
1343                    keystrokes: &parse_keystrokes("\\ a"),
1344                    action_name: "zed::SomeAction",
1345                    context: None,
1346                    action_arguments: None,
1347                },
1348                source: KeybindUpdateTarget {
1349                    keystrokes: &parse_keystrokes("\\ b"),
1350                    action_name: "zed::SomeOtherAction",
1351                    context: None,
1352                    action_arguments: Some(r#"{"foo": "bar"}"#),
1353                },
1354                target_keybind_source: KeybindSource::User,
1355            },
1356            r#"[
1357                {
1358                    "bindings": {
1359                        "\\ b": [
1360                            "zed::SomeOtherAction",
1361                            {
1362                                "foo": "bar"
1363                            }
1364                        ]
1365                    }
1366                }
1367            ]"#
1368            .unindent(),
1369        );
1370
1371        check_keymap_update(
1372            r#"[
1373                {
1374                    "bindings": {
1375                        "\\ a": "zed::SomeAction"
1376                    }
1377                }
1378            ]"#
1379            .unindent(),
1380            KeybindUpdateOperation::Replace {
1381                target: KeybindUpdateTarget {
1382                    keystrokes: &parse_keystrokes("\\ a"),
1383                    action_name: "zed::SomeAction",
1384                    context: None,
1385                    action_arguments: None,
1386                },
1387                source: KeybindUpdateTarget {
1388                    keystrokes: &parse_keystrokes("\\ a"),
1389                    action_name: "zed::SomeAction",
1390                    context: None,
1391                    action_arguments: None,
1392                },
1393                target_keybind_source: KeybindSource::User,
1394            },
1395            r#"[
1396                {
1397                    "bindings": {
1398                        "\\ a": "zed::SomeAction"
1399                    }
1400                }
1401            ]"#
1402            .unindent(),
1403        );
1404
1405        check_keymap_update(
1406            r#"[
1407                {
1408                    "bindings": {
1409                        "ctrl-a": "zed::SomeAction"
1410                    }
1411                }
1412            ]"#
1413            .unindent(),
1414            KeybindUpdateOperation::Replace {
1415                target: KeybindUpdateTarget {
1416                    keystrokes: &parse_keystrokes("ctrl-a"),
1417                    action_name: "zed::SomeNonexistentAction",
1418                    context: None,
1419                    action_arguments: None,
1420                },
1421                source: KeybindUpdateTarget {
1422                    keystrokes: &parse_keystrokes("ctrl-b"),
1423                    action_name: "zed::SomeOtherAction",
1424                    context: None,
1425                    action_arguments: None,
1426                },
1427                target_keybind_source: KeybindSource::User,
1428            },
1429            r#"[
1430                {
1431                    "bindings": {
1432                        "ctrl-a": "zed::SomeAction"
1433                    }
1434                },
1435                {
1436                    "bindings": {
1437                        "ctrl-b": "zed::SomeOtherAction"
1438                    }
1439                }
1440            ]"#
1441            .unindent(),
1442        );
1443
1444        check_keymap_update(
1445            r#"[
1446                {
1447                    "bindings": {
1448                        // some comment
1449                        "ctrl-a": "zed::SomeAction"
1450                        // some other comment
1451                    }
1452                }
1453            ]"#
1454            .unindent(),
1455            KeybindUpdateOperation::Replace {
1456                target: KeybindUpdateTarget {
1457                    keystrokes: &parse_keystrokes("ctrl-a"),
1458                    action_name: "zed::SomeAction",
1459                    context: None,
1460                    action_arguments: None,
1461                },
1462                source: KeybindUpdateTarget {
1463                    keystrokes: &parse_keystrokes("ctrl-b"),
1464                    action_name: "zed::SomeOtherAction",
1465                    context: None,
1466                    action_arguments: Some(r#"{"foo": "bar"}"#),
1467                },
1468                target_keybind_source: KeybindSource::User,
1469            },
1470            r#"[
1471                {
1472                    "bindings": {
1473                        // some comment
1474                        "ctrl-b": [
1475                            "zed::SomeOtherAction",
1476                            {
1477                                "foo": "bar"
1478                            }
1479                        ]
1480                        // some other comment
1481                    }
1482                }
1483            ]"#
1484            .unindent(),
1485        );
1486
1487        check_keymap_update(
1488            r#"[
1489                {
1490                    "context": "SomeContext",
1491                    "bindings": {
1492                        "a": "foo::bar",
1493                        "b": "baz::qux",
1494                    }
1495                }
1496            ]"#
1497            .unindent(),
1498            KeybindUpdateOperation::Replace {
1499                target: KeybindUpdateTarget {
1500                    keystrokes: &parse_keystrokes("a"),
1501                    action_name: "foo::bar",
1502                    context: Some("SomeContext"),
1503                    action_arguments: None,
1504                },
1505                source: KeybindUpdateTarget {
1506                    keystrokes: &parse_keystrokes("c"),
1507                    action_name: "foo::baz",
1508                    context: Some("SomeOtherContext"),
1509                    action_arguments: None,
1510                },
1511                target_keybind_source: KeybindSource::User,
1512            },
1513            r#"[
1514                {
1515                    "context": "SomeContext",
1516                    "bindings": {
1517                        "b": "baz::qux",
1518                    }
1519                },
1520                {
1521                    "context": "SomeOtherContext",
1522                    "bindings": {
1523                        "c": "foo::baz"
1524                    }
1525                }
1526            ]"#
1527            .unindent(),
1528        );
1529
1530        check_keymap_update(
1531            r#"[
1532                {
1533                    "context": "SomeContext",
1534                    "bindings": {
1535                        "a": "foo::bar",
1536                    }
1537                }
1538            ]"#
1539            .unindent(),
1540            KeybindUpdateOperation::Replace {
1541                target: KeybindUpdateTarget {
1542                    keystrokes: &parse_keystrokes("a"),
1543                    action_name: "foo::bar",
1544                    context: Some("SomeContext"),
1545                    action_arguments: None,
1546                },
1547                source: KeybindUpdateTarget {
1548                    keystrokes: &parse_keystrokes("c"),
1549                    action_name: "foo::baz",
1550                    context: Some("SomeOtherContext"),
1551                    action_arguments: None,
1552                },
1553                target_keybind_source: KeybindSource::User,
1554            },
1555            r#"[
1556                {
1557                    "context": "SomeOtherContext",
1558                    "bindings": {
1559                        "c": "foo::baz",
1560                    }
1561                }
1562            ]"#
1563            .unindent(),
1564        );
1565
1566        check_keymap_update(
1567            r#"[
1568                {
1569                    "context": "SomeContext",
1570                    "bindings": {
1571                        "a": "foo::bar",
1572                        "c": "foo::baz",
1573                    }
1574                },
1575            ]"#
1576            .unindent(),
1577            KeybindUpdateOperation::Remove {
1578                target: KeybindUpdateTarget {
1579                    context: Some("SomeContext"),
1580                    keystrokes: &parse_keystrokes("a"),
1581                    action_name: "foo::bar",
1582                    action_arguments: None,
1583                },
1584                target_keybind_source: KeybindSource::User,
1585            },
1586            r#"[
1587                {
1588                    "context": "SomeContext",
1589                    "bindings": {
1590                        "c": "foo::baz",
1591                    }
1592                },
1593            ]"#
1594            .unindent(),
1595        );
1596
1597        check_keymap_update(
1598            r#"[
1599                {
1600                    "context": "SomeContext",
1601                    "bindings": {
1602                        "\\ a": "foo::bar",
1603                        "c": "foo::baz",
1604                    }
1605                },
1606            ]"#
1607            .unindent(),
1608            KeybindUpdateOperation::Remove {
1609                target: KeybindUpdateTarget {
1610                    context: Some("SomeContext"),
1611                    keystrokes: &parse_keystrokes("\\ a"),
1612                    action_name: "foo::bar",
1613                    action_arguments: None,
1614                },
1615                target_keybind_source: KeybindSource::User,
1616            },
1617            r#"[
1618                {
1619                    "context": "SomeContext",
1620                    "bindings": {
1621                        "c": "foo::baz",
1622                    }
1623                },
1624            ]"#
1625            .unindent(),
1626        );
1627
1628        check_keymap_update(
1629            r#"[
1630                {
1631                    "context": "SomeContext",
1632                    "bindings": {
1633                        "a": ["foo::bar", true],
1634                        "c": "foo::baz",
1635                    }
1636                },
1637            ]"#
1638            .unindent(),
1639            KeybindUpdateOperation::Remove {
1640                target: KeybindUpdateTarget {
1641                    context: Some("SomeContext"),
1642                    keystrokes: &parse_keystrokes("a"),
1643                    action_name: "foo::bar",
1644                    action_arguments: Some("true"),
1645                },
1646                target_keybind_source: KeybindSource::User,
1647            },
1648            r#"[
1649                {
1650                    "context": "SomeContext",
1651                    "bindings": {
1652                        "c": "foo::baz",
1653                    }
1654                },
1655            ]"#
1656            .unindent(),
1657        );
1658
1659        check_keymap_update(
1660            r#"[
1661                {
1662                    "context": "SomeContext",
1663                    "bindings": {
1664                        "b": "foo::baz",
1665                    }
1666                },
1667                {
1668                    "context": "SomeContext",
1669                    "bindings": {
1670                        "a": ["foo::bar", true],
1671                    }
1672                },
1673                {
1674                    "context": "SomeContext",
1675                    "bindings": {
1676                        "c": "foo::baz",
1677                    }
1678                },
1679            ]"#
1680            .unindent(),
1681            KeybindUpdateOperation::Remove {
1682                target: KeybindUpdateTarget {
1683                    context: Some("SomeContext"),
1684                    keystrokes: &parse_keystrokes("a"),
1685                    action_name: "foo::bar",
1686                    action_arguments: Some("true"),
1687                },
1688                target_keybind_source: KeybindSource::User,
1689            },
1690            r#"[
1691                {
1692                    "context": "SomeContext",
1693                    "bindings": {
1694                        "b": "foo::baz",
1695                    }
1696                },
1697                {
1698                    "context": "SomeContext",
1699                    "bindings": {
1700                        "c": "foo::baz",
1701                    }
1702                },
1703            ]"#
1704            .unindent(),
1705        );
1706        check_keymap_update(
1707            r#"[
1708                {
1709                    "context": "SomeOtherContext",
1710                    "use_key_equivalents": true,
1711                    "bindings": {
1712                        "b": "foo::bar",
1713                    }
1714                },
1715            ]"#
1716            .unindent(),
1717            KeybindUpdateOperation::Add {
1718                source: KeybindUpdateTarget {
1719                    context: Some("SomeContext"),
1720                    keystrokes: &parse_keystrokes("a"),
1721                    action_name: "foo::baz",
1722                    action_arguments: Some("true"),
1723                },
1724                from: Some(KeybindUpdateTarget {
1725                    context: Some("SomeOtherContext"),
1726                    keystrokes: &parse_keystrokes("b"),
1727                    action_name: "foo::bar",
1728                    action_arguments: None,
1729                }),
1730            },
1731            r#"[
1732                {
1733                    "context": "SomeOtherContext",
1734                    "use_key_equivalents": true,
1735                    "bindings": {
1736                        "b": "foo::bar",
1737                    }
1738                },
1739                {
1740                    "context": "SomeContext",
1741                    "use_key_equivalents": true,
1742                    "bindings": {
1743                        "a": [
1744                            "foo::baz",
1745                            true
1746                        ]
1747                    }
1748                }
1749            ]"#
1750            .unindent(),
1751        );
1752
1753        check_keymap_update(
1754            r#"[
1755                {
1756                    "context": "SomeOtherContext",
1757                    "use_key_equivalents": true,
1758                    "bindings": {
1759                        "b": "foo::bar",
1760                    }
1761                },
1762            ]"#
1763            .unindent(),
1764            KeybindUpdateOperation::Remove {
1765                target: KeybindUpdateTarget {
1766                    context: Some("SomeContext"),
1767                    keystrokes: &parse_keystrokes("a"),
1768                    action_name: "foo::baz",
1769                    action_arguments: Some("true"),
1770                },
1771                target_keybind_source: KeybindSource::Default,
1772            },
1773            r#"[
1774                {
1775                    "context": "SomeOtherContext",
1776                    "use_key_equivalents": true,
1777                    "bindings": {
1778                        "b": "foo::bar",
1779                    }
1780                },
1781                {
1782                    "context": "SomeContext",
1783                    "bindings": {
1784                        "a": null
1785                    }
1786                }
1787            ]"#
1788            .unindent(),
1789        );
1790    }
1791
1792    #[test]
1793    fn test_keymap_remove() {
1794        zlog::init_test();
1795
1796        check_keymap_update(
1797            r#"
1798            [
1799              {
1800                "context": "Editor",
1801                "bindings": {
1802                  "cmd-k cmd-u": "editor::ConvertToUpperCase",
1803                  "cmd-k cmd-l": "editor::ConvertToLowerCase",
1804                  "cmd-[": "pane::GoBack",
1805                }
1806              },
1807            ]
1808            "#,
1809            KeybindUpdateOperation::Remove {
1810                target: KeybindUpdateTarget {
1811                    context: Some("Editor"),
1812                    keystrokes: &parse_keystrokes("cmd-k cmd-l"),
1813                    action_name: "editor::ConvertToLowerCase",
1814                    action_arguments: None,
1815                },
1816                target_keybind_source: KeybindSource::User,
1817            },
1818            r#"
1819            [
1820              {
1821                "context": "Editor",
1822                "bindings": {
1823                  "cmd-k cmd-u": "editor::ConvertToUpperCase",
1824                  "cmd-[": "pane::GoBack",
1825                }
1826              },
1827            ]
1828            "#,
1829        );
1830    }
1831}