nvf/modules/plugins/languages/svelte.nix

160 lines
4.2 KiB
Nix
Raw Permalink Normal View History

2023-06-03 14:18:26 +00:00
{
config,
pkgs,
2023-06-03 14:18:26 +00:00
lib,
...
}: let
inherit (builtins) attrNames;
inherit (lib.options) mkEnableOption mkOption;
inherit (lib.modules) mkIf mkMerge;
inherit (lib.lists) isList;
2024-03-24 00:14:39 +00:00
inherit (lib.meta) getExe;
inherit (lib.types) enum either listOf package str;
inherit (lib.nvim.lua) expToLua;
2024-03-24 00:14:39 +00:00
inherit (lib.nvim.languages) diagnosticsToLua;
inherit (lib.nvim.types) mkGrammarOption diagnostics;
2023-06-03 14:18:26 +00:00
cfg = config.vim.languages.svelte;
defaultServer = "svelte";
servers = {
svelte = {
package = pkgs.nodePackages.svelte-language-server;
lspConfig = ''
lspconfig.svelte.setup {
capabilities = capabilities;
on_attach = attach_keymaps,
2023-09-18 23:40:56 +00:00
cmd = ${
if isList cfg.lsp.package
then expToLua cfg.lsp.package
2023-09-18 23:40:56 +00:00
else ''{"${cfg.lsp.package}/bin/svelteserver", "--stdio"}''
}
2023-06-03 14:18:26 +00:00
}
'';
};
};
# TODO: specify packages
defaultFormat = "prettier";
formats = {
prettier = {
package = pkgs.nodePackages.prettier;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.prettier.with({
command = "${cfg.format.package}/bin/prettier",
})
)
'';
};
biome = {
package = pkgs.biome;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.biome.with({
command = "${cfg.format.package}/bin/biome",
})
)
'';
};
2023-06-03 14:18:26 +00:00
};
# TODO: specify packages
2024-03-24 00:14:39 +00:00
defaultDiagnosticsProvider = ["eslint_d"];
diagnosticsProviders = {
2023-06-03 14:18:26 +00:00
eslint_d = {
2024-06-08 16:42:05 +00:00
package = pkgs.eslint_d;
2023-06-03 14:18:26 +00:00
nullConfig = pkg: ''
table.insert(
ls_sources,
null_ls.builtins.diagnostics.eslint_d.with({
2024-03-24 00:14:39 +00:00
command = "${getExe pkg}",
2023-06-03 14:18:26 +00:00
})
)
'';
};
};
in {
options.vim.languages.svelte = {
enable = mkEnableOption "Svelte language support";
treesitter = {
enable = mkEnableOption "Svelte treesitter" // {default = config.vim.languages.enableTreesitter;};
sveltePackage = mkGrammarOption pkgs "svelte";
2023-06-03 14:18:26 +00:00
};
lsp = {
enable = mkEnableOption "Svelte LSP support" // {default = config.vim.languages.enableLSP;};
2023-06-03 14:18:26 +00:00
server = mkOption {
description = "Svelte LSP server to use";
type = enum (attrNames servers);
2023-06-03 14:18:26 +00:00
default = defaultServer;
};
2023-06-03 14:18:26 +00:00
package = mkOption {
description = "Svelte LSP server package, or the command to run as a list of strings";
example = ''[lib.getExe pkgs.jdt-language-server "-data" "~/.cache/jdtls/workspace"]'';
type = either package (listOf str);
2023-06-03 14:18:26 +00:00
default = servers.${cfg.lsp.server}.package;
};
};
format = {
enable = mkEnableOption "Svelte formatting" // {default = config.vim.languages.enableFormat;};
2023-06-03 14:18:26 +00:00
type = mkOption {
description = "Svelte formatter to use";
type = enum (attrNames formats);
2023-06-03 14:18:26 +00:00
default = defaultFormat;
};
2023-06-03 14:18:26 +00:00
package = mkOption {
description = "Svelte formatter package";
type = package;
2023-06-03 14:18:26 +00:00
default = formats.${cfg.format.type}.package;
};
};
extraDiagnostics = {
enable = mkEnableOption "extra Svelte diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;};
2024-03-24 00:14:39 +00:00
types = diagnostics {
2023-06-03 14:18:26 +00:00
langDesc = "Svelte";
2024-03-24 00:14:39 +00:00
inherit diagnosticsProviders;
inherit defaultDiagnosticsProvider;
2023-06-03 14:18:26 +00:00
};
};
};
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.sveltePackage];
})
(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
vim.lsp.lspconfig.sources.svelte-lsp = servers.${cfg.lsp.server}.lspConfig;
})
(mkIf cfg.format.enable {
vim.lsp.null-ls.enable = true;
vim.lsp.null-ls.sources.svelte-format = formats.${cfg.format.type}.nullConfig;
})
(mkIf cfg.extraDiagnostics.enable {
vim.lsp.null-ls.enable = true;
2024-03-24 00:14:39 +00:00
vim.lsp.null-ls.sources = diagnosticsToLua {
2023-06-03 14:18:26 +00:00
lang = "svelte";
config = cfg.extraDiagnostics.types;
2024-03-24 00:14:39 +00:00
inherit diagnosticsProviders;
2023-06-03 14:18:26 +00:00
};
})
]);
}