From 1ed8e7ef98db46af84e6be02f6538250f2ef259f Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Mon, 22 Jun 2020 20:48:22 +0200 Subject: [PATCH] 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 --- modules/programs/vscode.nix | 52 ++++++++++++++++-- tests/default.nix | 1 + tests/modules/programs/vscode/default.nix | 1 + tests/modules/programs/vscode/keybindings.nix | 53 +++++++++++++++++++ 4 files changed, 104 insertions(+), 3 deletions(-) create mode 100644 tests/modules/programs/vscode/default.nix create mode 100644 tests/modules/programs/vscode/keybindings.nix diff --git a/modules/programs/vscode.nix b/modules/programs/vscode.nix index 8e8fba777..099760c83 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 + keybindings.json. + ''; + }; + 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 45b0d83e6..02afd5a25 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 000000000..70f6d2e70 --- /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 000000000..420b212dc --- /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}" + ''; + }; +}