Skip to content

Commit 99b7167

Browse files
authored
Ability to update JSON arrays (#38087)
Closes #ISSUE Adds the ability to our JSON updating code to update arrays within other objects. Previously updating of arrays was limited to just top level arrays (i.e. `keymap.json`) however this PR makes it so nested arrays are supported as well using `#{index}` syntax as a key. This PR also fixes an issue with the array updating code that meant that updating empty json values `""` or an empty `keymap.json` file in the case of the Keymap Editor would fail instead of creating a new array. Release Notes: - Fixed an issue where keybindings would fail to save in the Keymap Editor if the `keymap.json` file was completely empty
1 parent 1c27a6d commit 99b7167

File tree

2 files changed

+898
-86
lines changed

2 files changed

+898
-86
lines changed

crates/settings/src/keymap_file.rs

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -678,8 +678,7 @@ impl KeymapFile {
678678
None,
679679
index,
680680
tab_size,
681-
)
682-
.context("Failed to remove keybinding")?;
681+
);
683682
keymap_contents.replace_range(replace_range, &replace_value);
684683
return Ok(keymap_contents);
685684
}
@@ -699,16 +698,14 @@ impl KeymapFile {
699698
// if we are only changing the keybinding (common case)
700699
// not the context, etc. Then just update the binding in place
701700

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")?;
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+
);
712709
keymap_contents.replace_range(replace_range, &replace_value);
713710

714711
return Ok(keymap_contents);
@@ -721,28 +718,24 @@ impl KeymapFile {
721718
// just update the section in place, updating the context
722719
// and the binding
723720

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")?;
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+
);
734729
keymap_contents.replace_range(replace_range, &replace_value);
735730

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")?;
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+
);
746739
keymap_contents.replace_range(replace_range, &replace_value);
747740
return Ok(keymap_contents);
748741
} else {
@@ -751,16 +744,14 @@ impl KeymapFile {
751744
// section, then treat this operation as an add operation of the
752745
// new binding with the updated context.
753746

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")?;
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+
);
764755
keymap_contents.replace_range(replace_range, &replace_value);
765756
operation = KeybindUpdateOperation::Add {
766757
source,
@@ -811,7 +802,7 @@ impl KeymapFile {
811802
&keymap_contents,
812803
&value.into(),
813804
tab_size,
814-
)?;
805+
);
815806
keymap_contents.replace_range(replace_range, &replace_value);
816807
}
817808
return Ok(keymap_contents);

0 commit comments

Comments
 (0)