vscode: add options for keybindings

Adds a new `keybindings` option to the `vscode` configuration.

It contains a list of key bindings, which will be written to
`%vscode-dir%/User/keybindings.json`.

PR #1351
This commit is contained in:
Christoph Herzog 2020-06-22 20:48:22 +02:00 committed by Robert Helgesson
parent e6e49ad73c
commit 1ed8e7ef98
No known key found for this signature in database
GPG key ID: 36BDAA14C2797E89
4 changed files with 104 additions and 3 deletions

View file

@ -20,11 +20,14 @@ let
"vscodium" = "vscode-oss"; "vscodium" = "vscode-oss";
}.${vscodePname}; }.${vscodePname};
configFilePath = userDir =
if pkgs.stdenv.hostPlatform.isDarwin then if pkgs.stdenv.hostPlatform.isDarwin then
"Library/Application Support/${configDir}/User/settings.json" "Library/Application Support/${configDir}/User"
else else
"${config.xdg.configHome}/${configDir}/User/settings.json"; "${config.xdg.configHome}/${configDir}/User";
configFilePath = "${userDir}/settings.json";
keybindingsFilePath = "${userDir}/keybindings.json";
# TODO: On Darwin where are the extensions? # TODO: On Darwin where are the extensions?
extensionPath = ".${extensionDir}/extensions"; extensionPath = ".${extensionDir}/extensions";
@ -59,6 +62,45 @@ in
''; '';
}; };
keybindings = mkOption {
type = types.listOf (types.submodule {
options = {
key = mkOption {
type = types.str;
example = "ctrl+c";
description = "The key or key-combination to bind.";
};
command = mkOption {
type = types.str;
example = "editor.action.clipboardCopyAction";
description = "The VS Code command to execute.";
};
when = mkOption {
type = types.str;
default = "";
example = "textInputFocus";
description = "Optional context filter.";
};
};
});
default = [];
example = literalExample ''
[
{
key = "ctrl+c";
command = "editor.action.clipboardCopyAction";
when = "textInputFocus";
}
]
'';
description = ''
Keybindings written to Visual Studio Code's
<filename>keybindings.json</filename>.
'';
};
extensions = mkOption { extensions = mkOption {
type = types.listOf types.package; type = types.listOf types.package;
default = []; default = [];
@ -93,6 +135,10 @@ in
mkIf (cfg.userSettings != {}) { mkIf (cfg.userSettings != {}) {
text = builtins.toJSON cfg.userSettings; text = builtins.toJSON cfg.userSettings;
}; };
"${keybindingsFilePath}" =
mkIf (cfg.keybindings != []) {
text = builtins.toJSON cfg.keybindings;
};
} }
toSymlink; toSymlink;
}; };

View file

@ -65,6 +65,7 @@ import nmt {
./modules/programs/starship ./modules/programs/starship
./modules/programs/texlive ./modules/programs/texlive
./modules/programs/tmux ./modules/programs/tmux
./modules/programs/vscode
./modules/programs/zplug ./modules/programs/zplug
./modules/programs/zsh ./modules/programs/zsh
./modules/xresources ./modules/xresources

View file

@ -0,0 +1 @@
{ vscode-keybindings = ./keybindings.nix; }

View file

@ -0,0 +1,53 @@
# Test that keybdinings.json is created correctly.
{ config, lib, pkgs, ... }:
with 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";
}
];
targetPath = if pkgs.stdenv.hostPlatform.isDarwin then
"Library/Application Support/Code/User/keybindings.json"
else
".config/Code/User/keybindings.json";
expectedJson = pkgs.writeText "expected.json" (builtins.toJSON bindings);
in {
config = {
programs.vscode = {
enable = true;
keybindings = bindings;
};
nixpkgs.overlays = [
(self: super: {
vscode = pkgs.runCommandLocal "vscode" { pname = "vscode"; } ''
mkdir -p $out/bin
touch $out/bin/code
chmod +x $out/bin/code;
'';
})
];
nmt.script = ''
assertFileExists "home-files/${targetPath}"
assertFileContent "home-files/${targetPath}" "${expectedJson}"
'';
};
}