keymap ui: Fix remove key mapping bug (cherry-pick #34683) (#34730)

Cherry-picked keymap ui: Fix remove key mapping bug (#34683)

Release Notes:

- N/A

---------

Co-authored-by: Ben Kunkle <ben@zed.dev>

Co-authored-by: Anthony Eid <56899983+Anthony-Eid@users.noreply.github.com>
Co-authored-by: Ben Kunkle <ben@zed.dev>
This commit is contained in:
gcp-cherry-pick-bot[bot] 2025-07-18 15:52:36 -04:00 committed by GitHub
parent 3f305fa805
commit 234a4f86ba
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 2 deletions

View file

@ -1623,4 +1623,44 @@ mod tests {
.unindent(),
);
}
#[test]
fn test_keymap_remove() {
zlog::init_test();
check_keymap_update(
r#"
[
{
"context": "Editor",
"bindings": {
"cmd-k cmd-u": "editor::ConvertToUpperCase",
"cmd-k cmd-l": "editor::ConvertToLowerCase",
"cmd-[": "pane::GoBack",
}
},
]
"#,
KeybindUpdateOperation::Remove {
target: KeybindUpdateTarget {
context: Some("Editor"),
keystrokes: &parse_keystrokes("cmd-k cmd-l"),
action_name: "editor::ConvertToLowerCase",
action_arguments: None,
},
target_keybind_source: KeybindSource::User,
},
r#"
[
{
"context": "Editor",
"bindings": {
"cmd-k cmd-u": "editor::ConvertToUpperCase",
"cmd-[": "pane::GoBack",
}
},
]
"#,
);
}
}

View file

@ -190,6 +190,7 @@ fn replace_value_in_json_text(
}
}
let mut removed_comma = false;
// Look backward for a preceding comma first
let preceding_text = text.get(0..removal_start).unwrap_or("");
if let Some(comma_pos) = preceding_text.rfind(',') {
@ -197,10 +198,12 @@ fn replace_value_in_json_text(
let between_comma_and_key = text.get(comma_pos + 1..removal_start).unwrap_or("");
if between_comma_and_key.trim().is_empty() {
removal_start = comma_pos;
removed_comma = true;
}
}
if let Some(remaining_text) = text.get(existing_value_range.end..) {
if let Some(remaining_text) = text.get(existing_value_range.end..)
&& !removed_comma
{
let mut chars = remaining_text.char_indices();
while let Some((offset, ch)) = chars.next() {
if ch == ',' {