aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristoph Herzog <chris@theduke.at>2020-06-22 20:48:22 +0200
committerRobert Helgesson <robert@rycee.net>2020-09-04 14:14:52 +0200
commit1ed8e7ef98db46af84e6be02f6538250f2ef259f (patch)
tree8ac9f1f9d0f8cd6476e472b880a7055b59a80c0c
parente6e49ad73c7d8cbd9f6622f0a3330ce7b71c8a86 (diff)
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
-rw-r--r--modules/programs/vscode.nix52
-rw-r--r--tests/default.nix1
-rw-r--r--tests/modules/programs/vscode/default.nix1
-rw-r--r--tests/modules/programs/vscode/keybindings.nix53
4 files changed, 104 insertions, 3 deletions
diff --git a/modules/programs/vscode.nix b/modules/programs/vscode.nix
index 8e8fba777ce..099760c834a 100644
--- a/modules/programs/vscode.nix
+++ b/modules/programs/vscode.nix
@@ -20,11 +20,14 @@ let
"vscodium" = "vscode-oss";
}.${vscodePname};
- configFilePath =
+ userDir =
if pkgs.stdenv.hostPlatform.isDarwin then
- "Library/Application Support/${configDir}/User/settings.json"
+ "Library/Application Support/${configDir}/User"
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?
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 {
type = types.listOf types.package;
default = [];
@@ -93,6 +135,10 @@ in
mkIf (cfg.userSettings != {}) {
text = builtins.toJSON cfg.userSettings;
};
+ "${keybindingsFilePath}" =
+ mkIf (cfg.keybindings != []) {
+ text = builtins.toJSON cfg.keybindings;
+ };
}
toSymlink;
};
diff --git a/tests/default.nix b/tests/default.nix
index 45b0d83e6b8..02afd5a2518 100644
--- a/tests/default.nix
+++ b/tests/default.nix
@@ -65,6 +65,7 @@ import nmt {
./modules/programs/starship
./modules/programs/texlive
./modules/programs/tmux
+ ./modules/programs/vscode
./modules/programs/zplug
./modules/programs/zsh
./modules/xresources
diff --git a/tests/modules/programs/vscode/default.nix b/tests/modules/programs/vscode/default.nix
new file mode 100644
index 00000000000..70f6d2e7060
--- /dev/null
+++ b/tests/modules/programs/vscode/default.nix
@@ -0,0 +1 @@
+{ vscode-keybindings = ./keybindings.nix; }
diff --git a/tests/modules/programs/vscode/keybindings.nix b/tests/modules/programs/vscode/keybindings.nix
new file mode 100644
index 00000000000..420b212dce9
--- /dev/null
+++ b/tests/modules/programs/vscode/keybindings.nix
@@ -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}"
+ '';
+ };
+}