zed-editor: survive if previous files are not JSON5 (#7351)

This commit is contained in:
Jairo Llopis
2025-07-03 13:44:20 +01:00
committed by GitHub
parent 426b405d97
commit b182e64c01
4 changed files with 145 additions and 2 deletions

View File

@@ -22,9 +22,9 @@ let
# No file? Create it
echo ${lib.escapeShellArg empty} > ${lib.escapeShellArg path}
fi
dynamic="$(${lib.getExe json5} --as-json ${lib.escapeShellArg path})"
dynamic="$(${lib.getExe json5} --as-json ${lib.escapeShellArg path} 2>/dev/null || echo ${lib.escapeShellArg empty})"
static="$(cat ${lib.escapeShellArg staticSettings})"
config="$(${lib.getExe pkgs.jq} -s ${lib.escapeShellArg jqOperation} --argjson dynamic "$dynamic" --argjson static "$static")"
config="$(${lib.getExe pkgs.jq} -n ${lib.escapeShellArg jqOperation} --argjson dynamic "$dynamic" --argjson static "$static")"
printf '%s\n' "$config" > ${lib.escapeShellArg path}
unset config
'';

View File

@@ -2,6 +2,8 @@
zed-extensions = ./extensions.nix;
zed-install-remote-server = ./install-remote-server.nix;
zed-keymap = ./keymap.nix;
zed-keymap-empty = ./keymap-empty.nix;
zed-settings = ./settings.nix;
zed-settings-empty = ./settings-empty.nix;
zed-themes = ./themes;
}

View File

@@ -0,0 +1,74 @@
{
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
# For some reason, the preexisting keymaps is an empty file
preexistingKeymaps = builtins.toFile "preexisting.json" "";
expectedContent = builtins.toFile "expected.json" ''
[
{
"bindings": {
"up": "menu::SelectPrev"
}
},
{
"bindings": {
"escape": "editor::Cancel"
},
"context": "Editor"
}
]
'';
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}"
'';
}

View File

@@ -0,0 +1,67 @@
# Test custom keymap functionality
{
config,
lib,
pkgs,
...
}:
{
programs.zed-editor = {
enable = true;
package = config.lib.test.mkStubPackage { };
userSettings = {
theme = "XY-Zed";
features = {
copilot = false;
};
vim_mode = false;
ui_font_size = 16;
buffer_font_size = 16;
};
};
home.homeDirectory = lib.mkForce "/@TMPDIR@/hm-user";
nmt.script =
let
# For some reason, the preexisting settings is an empty file
preexistingSettings = builtins.toFile "preexisting.json" "";
expectedContent = builtins.toFile "expected.json" ''
{
"buffer_font_size": 16,
"features": {
"copilot": false
},
"theme": "XY-Zed",
"ui_font_size": 16,
"vim_mode": false
}
'';
settingsPath = ".config/zed/settings.json";
activationScript = pkgs.writeScript "activation" config.home.activation.zedSettingsActivation.data;
in
''
export HOME=$TMPDIR/hm-user
# Simulate preexisting settings
mkdir -p $HOME/.config/zed
cat ${preexistingSettings} > $HOME/${settingsPath}
# Run the activation script
substitute ${activationScript} $TMPDIR/activate --subst-var TMPDIR
chmod +x $TMPDIR/activate
$TMPDIR/activate
# Validate the merged settings
assertFileExists "$HOME/${settingsPath}"
assertFileContent "$HOME/${settingsPath}" "${expectedContent}"
# Test idempotency
$TMPDIR/activate
assertFileExists "$HOME/${settingsPath}"
assertFileContent "$HOME/${settingsPath}" "${expectedContent}"
'';
}