1
0
Fork 0
mirror of https://github.com/nix-community/home-manager.git synced 2025-12-06 17:11:03 +01:00
home-manager/tests/modules/programs/vscode/keybindings.nix
Jonathan Bouchard b022c9e3b8
vscode: allow specifying paths for VSCode JSON settings (#7055)
Enables users to provide paths to JSON files for VS Code settings,
tasks, and keybindings. This allows for more flexible configuration
management and reuse of existing configuration files instead of using inline configurations.
2025-05-15 13:32:46 -05:00

144 lines
3.4 KiB
Nix

# Test that keybindings.json is created correctly.
{ pkgs, lib, ... }:
let
bindings = [
{
key = "ctrl+c";
command = "editor.action.clipboardCopyAction";
when = "textInputFocus && false";
}
{
key = "ctrl+c";
command = "deleteFile";
when = "";
}
{
key = "d";
command = "deleteFile";
when = "explorerViewletVisible";
}
{
key = "ctrl+r";
command = "run";
args = {
command = "echo file";
};
}
];
keybindingsPath =
name:
if pkgs.stdenv.hostPlatform.isDarwin then
"Library/Application Support/Code/User/${
lib.optionalString (name != "default") "profiles/${name}/"
}keybindings.json"
else
".config/Code/User/${lib.optionalString (name != "default") "profiles/${name}/"}keybindings.json";
settingsPath =
name:
if pkgs.stdenv.hostPlatform.isDarwin then
"Library/Application Support/Code/User/${
lib.optionalString (name != "default") "profiles/${name}/"
}settings.json"
else
".config/Code/User/${lib.optionalString (name != "default") "profiles/${name}/"}settings.json";
content = ''
[
// Order doesn't change
{
"command": "deleteFile",
"key": "ctrl+c",
"when": ""
},
{
"command": "deleteFile",
"key": "ctrl+c",
"when": ""
},
{
"args": {
"command": "echo file"
},
"command": "run",
"key": "ctrl+r"
},
// Comments should be preserved
{
"command": "editor.action.clipboardCopyAction",
"key": "ctrl+c",
"when": "textInputFocus && false"
},
{
"command": "deleteFile",
"key": "d",
"when": "explorerViewletVisible"
}
]
'';
customBindingsPath = pkgs.writeText "custom.json" content;
expectedKeybindings = pkgs.writeText "expected.json" ''
[
{
"command": "editor.action.clipboardCopyAction",
"key": "ctrl+c",
"when": "textInputFocus && false"
},
{
"command": "deleteFile",
"key": "ctrl+c",
"when": ""
},
{
"command": "deleteFile",
"key": "d",
"when": "explorerViewletVisible"
},
{
"args": {
"command": "echo file"
},
"command": "run",
"key": "ctrl+r"
}
]
'';
expectedCustomKeybindings = pkgs.writeText "custom-expected.json" content;
in
{
programs.vscode = {
enable = true;
profiles = {
default.keybindings = bindings;
test.keybindings = bindings;
custom.keybindings = customBindingsPath;
};
package = pkgs.writeScriptBin "vscode" "" // {
pname = "vscode";
version = "1.75.0";
};
};
nmt.script = ''
assertFileExists "home-files/${keybindingsPath "default"}"
assertFileContent "home-files/${keybindingsPath "default"}" "${expectedKeybindings}"
assertPathNotExists "home-files/${settingsPath "default"}"
assertFileExists "home-files/${keybindingsPath "test"}"
assertFileContent "home-files/${keybindingsPath "test"}" "${expectedKeybindings}"
assertPathNotExists "home-files/${settingsPath "test"}"
assertFileExists "home-files/${keybindingsPath "custom"}"
assertFileContent "home-files/${keybindingsPath "custom"}" "${expectedCustomKeybindings}"
assertPathNotExists "home-files/${settingsPath "custom"}"
'';
}