vscode: add options for global and user snippets (#3765)

Co-authored-by: Felix Leitz <felix.leitz92@gmail.com>
This commit is contained in:
Felix Leitz 2023-03-13 18:45:03 +01:00 committed by GitHub
parent 7fe539dfbb
commit 24c1a6335e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 115 additions and 0 deletions

View file

@ -32,6 +32,8 @@ let
tasksFilePath = "${userDir}/tasks.json";
keybindingsFilePath = "${userDir}/keybindings.json";
snippetDir = "${userDir}/snippets";
# TODO: On Darwin where are the extensions?
extensionPath = ".${extensionDir}/extensions";
@ -187,6 +189,34 @@ in {
or by Visual Studio Code.
'';
};
languageSnippets = mkOption {
type = jsonFormat.type;
default = { };
example = {
haskell = {
fixme = {
prefix = [ "fixme" ];
body = [ "$LINE_COMMENT FIXME: $0" ];
description = "Insert a FIXME remark";
};
};
};
description = "Defines user snippets for different languages.";
};
globalSnippets = mkOption {
type = jsonFormat.type;
default = { };
example = {
fixme = {
prefix = [ "fixme" ];
body = [ "$LINE_COMMENT FIXME: $0" ];
description = "Insert a FIXME remark";
};
};
description = "Defines global user snippets.";
};
};
};
@ -243,6 +273,19 @@ in {
};
in "${combinedExtensionsDrv}/${subDir}";
}))
(mkIf (cfg.globalSnippets != { })
(let globalSnippets = "${snippetDir}/global.code-snippets";
in {
"${globalSnippets}".source =
jsonFormat.generate "user-snippet-global.code-snippets"
cfg.globalSnippets;
}))
(lib.mapAttrs' (language: snippet:
lib.nameValuePair "${snippetDir}/${language}.json" {
source = jsonFormat.generate "user-snippet-${language}.json" snippet;
}) cfg.languageSnippets)
];
};
}

View file

@ -2,4 +2,5 @@
vscode-keybindings = ./keybindings.nix;
vscode-tasks = ./tasks.nix;
vscode-update-checks = ./update-checks.nix;
vscode-snippets = ./snippets.nix;
}

View file

@ -0,0 +1,71 @@
{ pkgs, ... }:
let
snippetsDir = if pkgs.stdenv.hostPlatform.isDarwin then
"Library/Application Support/Code/User/snippets"
else
".config/Code/User/snippets";
globalSnippetsPath = "${snippetsDir}/global.code-snippets";
globalSnippetsExpectedContent = pkgs.writeText "global.code-snippet" ''
{
"fixme": {
"body": [
"fixme body in global user snippet"
],
"description": "Insert a FIXME remark",
"prefix": [
"fixme"
]
}
}
'';
haskellSnippetsPath = "${snippetsDir}/haskell.json";
haskellSnippetsExpectedContent = pkgs.writeText "haskell.json" ''
{
"impl": {
"body": [
"impl body in user haskell snippet"
],
"description": "Insert an implementation stub",
"prefix": [
"impl"
]
}
}
'';
in {
programs.vscode = {
enable = true;
package = pkgs.writeScriptBin "vscode" "" // { pname = "vscode"; };
globalSnippets = {
fixme = {
prefix = [ "fixme" ];
body = [ "fixme body in global user snippet" ];
description = "Insert a FIXME remark";
};
};
languageSnippets = {
haskell = {
impl = {
prefix = [ "impl" ];
body = [ "impl body in user haskell snippet" ];
description = "Insert an implementation stub";
};
};
};
};
nmt.script = ''
assertFileExists "home-files/${globalSnippetsPath}"
assertFileContent "home-files/${globalSnippetsPath}" "${globalSnippetsExpectedContent}"
assertFileExists "home-files/${haskellSnippetsPath}"
assertFileContent "home-files/${haskellSnippetsPath}" "${haskellSnippetsExpectedContent}"
'';
}