1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-11-21 17:59:39 +01:00
home-manager/tests/modules/programs/zed-editor/keymap.nix
Jairo Llopis ff31a4677c
fix(zed): support preexisting JSON5 settings (#7317)
Zed uses JSON5 for settings files. JQ doesn't understand that format and
fails if found, when merging with preexisting settings.

Here I add a conversion step that converts JSON5 to JSON before handling
the contents to JQ.

Besides, I changed the arguments in the jq function, so instead of using
`[0]` and `[1]`, we now use `$dynamic` and `$static` respectively. This
should make scripts more readable.

Fixes https://github.com/nix-community/home-manager/issues/7247
Fixes https://github.com/nix-community/home-manager/issues/7226
2025-06-24 15:48:03 -05:00

104 lines
2.3 KiB
Nix

{
config,
lib,
pkgs,
...
}:
{
programs.zed-editor = {
enable = true;
package = config.lib.test.mkStubPackage { };
userKeymaps = [
{
bindings = {
up = "menu::SelectPrev";
};
}
{
context = "Editor";
bindings = {
escape = "editor::Cancel";
};
}
];
};
home.homeDirectory = lib.mkForce "/@TMPDIR@/hm-user";
nmt.script =
let
preexistingKeymaps = builtins.toFile "preexisting.json" ''
[
// Things changed interactively
{
"bindings": {
"down": "menu::SelectNext"
}
},
{
"bindings": {
"down": "select"
},
"context": "Terminal"
},
/* Manually changed */
{
"bindings": {
"enter": "newline"
},
"context": "Editor",
},
]
'';
expectedContent = builtins.toFile "expected.json" ''
[
{
"bindings": {
"down": "menu::SelectNext",
"up": "menu::SelectPrev"
}
},
{
"bindings": {
"enter": "newline",
"escape": "editor::Cancel"
},
"context": "Editor"
},
{
"bindings": {
"down": "select"
},
"context": "Terminal"
}
]
'';
keymapPath = ".config/zed/keymap.json";
activationScript = pkgs.writeScript "activation" config.home.activation.zedKeymapActivation.data;
in
''
export HOME=$TMPDIR/hm-user
# Simulate preexisting keymaps
mkdir -p $HOME/.config/zed
cat ${preexistingKeymaps} > $HOME/${keymapPath}
# Run the activation script
substitute ${activationScript} $TMPDIR/activate --subst-var TMPDIR
chmod +x $TMPDIR/activate
$TMPDIR/activate
# Validate the merged keymaps
assertFileExists "$HOME/${keymapPath}"
assertFileContent "$HOME/${keymapPath}" "${expectedContent}"
# Test idempotency
$TMPDIR/activate
assertFileExists "$HOME/${keymapPath}"
assertFileContent "$HOME/${keymapPath}" "${expectedContent}"
'';
}