nvf/modules/languages/ts.nix

174 lines
4.8 KiB
Nix
Raw Normal View History

{
config,
pkgs,
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.types) mkGrammarOption diagnostics;
inherit (lib.nvim.languages) diagnosticsToLua;
cfg = config.vim.languages.ts;
defaultServer = "tsserver";
servers = {
tsserver = {
package = pkgs.nodePackages.typescript-language-server;
lspConfig = ''
lspconfig.tsserver.setup {
capabilities = capabilities;
on_attach = attach_keymaps,
cmd = ${
if isList cfg.lsp.package
then expToLua cfg.lsp.package
else ''{"${cfg.lsp.package}/bin/typescript-language-server", "--stdio"}''
}
}
'';
};
2023-07-29 20:34:21 +00:00
denols = {
package = pkgs.deno;
lspConfig = ''
vim.g.markdown_fenced_languages = { "ts=typescript" }
lspconfig.denols.setup {
capabilities = capabilities;
on_attach = attach_keymaps,
cmd = ${
if isList cfg.lsp.package
then expToLua cfg.lsp.package
else ''{"${cfg.lsp.package}/bin/deno", "lsp"}''
}
2023-07-29 20:34:21 +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",
})
)
'';
};
prettierd = {
package = pkgs.prettierd;
nullConfig = ''
table.insert(
ls_sources,
null_ls.builtins.formatting.prettier.with({
command = "${cfg.format.package}/bin/prettierd",
})
)
'';
};
};
# TODO: specify packages
2024-03-24 00:14:39 +00:00
defaultDiagnosticsProvider = ["eslint_d"];
diagnosticsProviders = {
eslint_d = {
package = pkgs.nodePackages.eslint_d;
nullConfig = pkg: ''
table.insert(
ls_sources,
null_ls.builtins.diagnostics.eslint_d.with({
2024-03-24 00:14:39 +00:00
command = "${getExe pkg}",
})
)
'';
};
};
in {
options.vim.languages.ts = {
enable = mkEnableOption "Typescript/Javascript language support";
treesitter = {
enable = mkEnableOption "Typescript/Javascript treesitter" // {default = config.vim.languages.enableTreesitter;};
tsPackage = mkGrammarOption pkgs "tsx";
jsPackage = mkGrammarOption pkgs "javascript";
};
lsp = {
enable = mkEnableOption "Typescript/Javascript LSP support" // {default = config.vim.languages.enableLSP;};
server = mkOption {
description = "Typescript/Javascript LSP server to use";
type = enum (attrNames servers);
default = defaultServer;
};
package = mkOption {
description = "Typescript/Javascript 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);
default = servers.${cfg.lsp.server}.package;
};
};
format = {
enable = mkEnableOption "Typescript/Javascript formatting" // {default = config.vim.languages.enableFormat;};
type = mkOption {
description = "Typescript/Javascript formatter to use";
type = enum (attrNames formats);
default = defaultFormat;
};
package = mkOption {
description = "Typescript/Javascript formatter package";
type = package;
default = formats.${cfg.format.type}.package;
};
};
extraDiagnostics = {
enable = mkEnableOption "extra Typescript/Javascript diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;};
2024-03-24 00:14:39 +00:00
types = diagnostics {
langDesc = "Typescript/Javascript";
2024-03-24 00:14:39 +00:00
inherit diagnosticsProviders;
inherit defaultDiagnosticsProvider;
};
};
};
config = mkIf cfg.enable (mkMerge [
(mkIf cfg.treesitter.enable {
vim.treesitter.enable = true;
vim.treesitter.grammars = [cfg.treesitter.tsPackage cfg.treesitter.jsPackage];
})
(mkIf cfg.lsp.enable {
vim.lsp.lspconfig.enable = true;
vim.lsp.lspconfig.sources.ts-lsp = servers.${cfg.lsp.server}.lspConfig;
})
(mkIf cfg.format.enable {
vim.lsp.null-ls.enable = true;
vim.lsp.null-ls.sources.ts-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 {
lang = "ts";
config = cfg.extraDiagnostics.types;
2024-03-24 00:14:39 +00:00
inherit diagnosticsProviders;
};
})
]);
}