2023-04-17 20:27:27 +00:00
|
|
|
{
|
|
|
|
config,
|
2024-03-09 05:49:22 +00:00
|
|
|
pkgs,
|
2023-04-17 20:27:27 +00:00
|
|
|
lib,
|
|
|
|
...
|
2023-11-08 02:16:46 +00:00
|
|
|
}: let
|
2023-11-14 07:18:32 +00:00
|
|
|
inherit (builtins) attrNames;
|
2024-03-09 05:49:22 +00:00
|
|
|
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;
|
2024-03-09 05:49:22 +00:00
|
|
|
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;
|
2023-11-08 02:16:46 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
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,
|
2023-09-18 22:40:09 +00:00
|
|
|
cmd = ${
|
|
|
|
if isList cfg.lsp.package
|
2024-03-09 05:49:22 +00:00
|
|
|
then expToLua cfg.lsp.package
|
2023-09-18 22:40:09 +00:00
|
|
|
else ''{"${cfg.lsp.package}/bin/typescript-language-server", "--stdio"}''
|
|
|
|
}
|
2023-04-17 20:27:27 +00:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
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,
|
2023-09-18 22:40:09 +00:00
|
|
|
cmd = ${
|
|
|
|
if isList cfg.lsp.package
|
2024-03-09 05:49:22 +00:00
|
|
|
then expToLua cfg.lsp.package
|
2023-09-18 22:40:09 +00:00
|
|
|
else ''{"${cfg.lsp.package}/bin/deno", "lsp"}''
|
|
|
|
}
|
2023-07-29 20:34:21 +00:00
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
2023-04-17 20:27:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# TODO: specify packages
|
2023-07-13 20:19:15 +00:00
|
|
|
defaultFormat = "prettier";
|
2023-04-17 20:27:27 +00:00
|
|
|
formats = {
|
|
|
|
prettier = {
|
|
|
|
package = pkgs.nodePackages.prettier;
|
|
|
|
nullConfig = ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
|
|
|
null_ls.builtins.formatting.prettier.with({
|
|
|
|
command = "${cfg.format.package}/bin/prettier",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
2023-07-11 19:54:22 +00:00
|
|
|
prettierd = {
|
|
|
|
package = pkgs.prettierd;
|
|
|
|
nullConfig = ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
|
|
|
null_ls.builtins.formatting.prettier.with({
|
|
|
|
command = "${cfg.format.package}/bin/prettierd",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
2023-04-17 20:27:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
# TODO: specify packages
|
2024-03-24 00:14:39 +00:00
|
|
|
defaultDiagnosticsProvider = ["eslint_d"];
|
|
|
|
diagnosticsProviders = {
|
2023-05-12 18:30:37 +00:00
|
|
|
eslint_d = {
|
|
|
|
package = pkgs.nodePackages.eslint_d;
|
2023-04-17 20:27:27 +00:00
|
|
|
nullConfig = pkg: ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
2023-05-12 18:30:37 +00:00
|
|
|
null_ls.builtins.diagnostics.eslint_d.with({
|
2024-03-24 00:14:39 +00:00
|
|
|
command = "${getExe pkg}",
|
2023-04-17 20:27:27 +00:00
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
in {
|
|
|
|
options.vim.languages.ts = {
|
2023-04-18 17:11:06 +00:00
|
|
|
enable = mkEnableOption "Typescript/Javascript language support";
|
2023-04-17 20:27:27 +00:00
|
|
|
|
|
|
|
treesitter = {
|
2023-10-21 17:15:36 +00:00
|
|
|
enable = mkEnableOption "Typescript/Javascript treesitter" // {default = config.vim.languages.enableTreesitter;};
|
2024-03-09 05:49:22 +00:00
|
|
|
tsPackage = mkGrammarOption pkgs "tsx";
|
|
|
|
jsPackage = mkGrammarOption pkgs "javascript";
|
2023-04-17 20:27:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
lsp = {
|
2023-10-21 17:15:36 +00:00
|
|
|
enable = mkEnableOption "Typescript/Javascript LSP support" // {default = config.vim.languages.enableLSP;};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
server = mkOption {
|
|
|
|
description = "Typescript/Javascript LSP server to use";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = enum (attrNames servers);
|
2023-04-17 20:27:27 +00:00
|
|
|
default = defaultServer;
|
|
|
|
};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
package = mkOption {
|
2023-09-22 22:28:39 +00:00
|
|
|
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"]'';
|
2024-03-09 05:49:22 +00:00
|
|
|
type = either package (listOf str);
|
2023-04-17 20:27:27 +00:00
|
|
|
default = servers.${cfg.lsp.server}.package;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
format = {
|
2023-10-21 17:15:36 +00:00
|
|
|
enable = mkEnableOption "Typescript/Javascript formatting" // {default = config.vim.languages.enableFormat;};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
type = mkOption {
|
|
|
|
description = "Typescript/Javascript formatter to use";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = enum (attrNames formats);
|
2023-04-17 20:27:27 +00:00
|
|
|
default = defaultFormat;
|
|
|
|
};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
package = mkOption {
|
|
|
|
description = "Typescript/Javascript formatter package";
|
2024-03-09 05:49:22 +00:00
|
|
|
type = package;
|
2023-04-17 20:27:27 +00:00
|
|
|
default = formats.${cfg.format.type}.package;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
extraDiagnostics = {
|
2023-10-21 17:15:36 +00:00
|
|
|
enable = mkEnableOption "extra Typescript/Javascript diagnostics" // {default = config.vim.languages.enableExtraDiagnostics;};
|
2023-06-05 20:10:25 +00:00
|
|
|
|
2024-03-24 00:14:39 +00:00
|
|
|
types = diagnostics {
|
2023-04-17 20:27:27 +00:00
|
|
|
langDesc = "Typescript/Javascript";
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit diagnosticsProviders;
|
|
|
|
inherit defaultDiagnosticsProvider;
|
2023-04-17 20:27:27 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
2023-04-17 20:27:27 +00:00
|
|
|
lang = "ts";
|
|
|
|
config = cfg.extraDiagnostics.types;
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit diagnosticsProviders;
|
2023-04-17 20:27:27 +00:00
|
|
|
};
|
|
|
|
})
|
|
|
|
]);
|
|
|
|
}
|