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-06-24 13:55:45 +00:00
|
|
|
inherit (lib.types) enum either listOf package str bool;
|
|
|
|
inherit (lib.nvim.lua) expToLua toLuaObject;
|
|
|
|
inherit (lib.nvim.types) mkGrammarOption diagnostics mkPluginSetupOption;
|
2024-03-24 00:14:39 +00:00
|
|
|
inherit (lib.nvim.languages) diagnosticsToLua;
|
2024-06-24 13:55:45 +00:00
|
|
|
inherit (lib.nvim.dag) entryAnywhere;
|
2023-11-08 02:16:46 +00:00
|
|
|
|
2023-04-17 20:27:27 +00:00
|
|
|
cfg = config.vim.languages.ts;
|
|
|
|
|
2024-09-24 03:20:56 +00:00
|
|
|
defaultServer = "ts_ls";
|
2023-04-17 20:27:27 +00:00
|
|
|
servers = {
|
2024-09-24 03:20:56 +00:00
|
|
|
ts_ls = {
|
2024-07-10 09:44:54 +00:00
|
|
|
package = pkgs.typescript-language-server;
|
2023-04-17 20:27:27 +00:00
|
|
|
lspConfig = ''
|
2024-09-24 03:20:56 +00:00
|
|
|
lspconfig.ts_ls.setup {
|
2023-04-17 20:27:27 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
2024-06-24 13:55:45 +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
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
2024-09-24 03:20:56 +00:00
|
|
|
|
|
|
|
# Here for backwards compatibility. Still consider tsserver a valid
|
|
|
|
# configuration in the enum, but assert if it's set to *properly*
|
|
|
|
# redirect the user to the correct server.
|
|
|
|
tsserver = {
|
|
|
|
package = pkgs.typescript-language-server;
|
|
|
|
lspConfig = ''
|
|
|
|
lspconfig.ts_ls.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-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",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
2024-09-24 03:20:56 +00:00
|
|
|
|
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",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
2024-10-22 20:15:07 +00:00
|
|
|
|
|
|
|
biome = {
|
|
|
|
package = pkgs.biome;
|
|
|
|
nullConfig = ''
|
|
|
|
table.insert(
|
|
|
|
ls_sources,
|
|
|
|
null_ls.builtins.formatting.biome.with({
|
|
|
|
command = "${cfg.format.package}/bin/biome",
|
|
|
|
})
|
|
|
|
)
|
|
|
|
'';
|
|
|
|
};
|
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 = {
|
2024-06-08 16:42:05 +00:00
|
|
|
package = pkgs.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 {
|
2024-09-24 03:20:56 +00:00
|
|
|
_file = ./ts.nix;
|
2023-04-17 20:27:27 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
};
|
2024-06-24 13:55:45 +00:00
|
|
|
|
|
|
|
extensions = {
|
|
|
|
ts-error-translator = {
|
|
|
|
enable = mkEnableOption ''
|
2024-11-25 15:56:47 +00:00
|
|
|
[ts-error-translator.nvim]: https://github.com/dmmulroy/ts-error-translator.nvim
|
|
|
|
|
|
|
|
Typescript error translation with [ts-error-translator.nvim]
|
|
|
|
|
2024-06-24 13:55:45 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
setupOpts = mkPluginSetupOption "ts-error-translator" {
|
|
|
|
# This is the default configuration behaviour.
|
|
|
|
auto_override_publish_diagnostics = mkOption {
|
|
|
|
description = "Automatically override the publish_diagnostics handler";
|
|
|
|
type = bool;
|
|
|
|
default = true;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
|
|
|
};
|
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
|
|
|
};
|
|
|
|
})
|
2024-06-24 13:55:45 +00:00
|
|
|
|
2024-09-24 03:20:56 +00:00
|
|
|
# Extensions
|
2024-06-24 13:55:45 +00:00
|
|
|
(mkIf cfg.extensions."ts-error-translator".enable {
|
|
|
|
vim.startPlugins = ["ts-error-translator"];
|
2024-07-20 08:30:48 +00:00
|
|
|
vim.pluginRC.ts-error-translator = entryAnywhere ''
|
2024-06-24 13:55:45 +00:00
|
|
|
require("ts-error-translator").setup(${toLuaObject cfg.extensions.ts-error-translator.setupOpts})
|
|
|
|
'';
|
|
|
|
})
|
2024-09-24 03:20:56 +00:00
|
|
|
|
|
|
|
# Warn the user if they have set the default server name to tsserver to match upstream (us)
|
|
|
|
# The name "tsserver" has been deprecated in lspconfig, and now should be called ts_ls. This
|
|
|
|
# is a purely cosmetic change, but emits a warning if not accounted for.
|
|
|
|
{
|
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.lsp.enable -> cfg.lsp.server != "tsserver";
|
|
|
|
message = ''
|
2024-09-28 20:21:27 +00:00
|
|
|
As of a recent lspconfig update, the `tsserver` configuration has been renamed
|
2024-09-24 03:20:56 +00:00
|
|
|
to `ts_ls` to match upstream behaviour of `lspconfig`, and the name `tsserver`
|
|
|
|
is no longer considered valid by nvf. Please set `vim.languages.ts.lsp.server`
|
|
|
|
to `"ts_ls"` instead of to `${cfg.lsp.server}`
|
|
|
|
|
|
|
|
Please see <https://github.com/neovim/nvim-lspconfig/pull/3232> for more details
|
|
|
|
about this change.
|
|
|
|
'';
|
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
2023-04-17 20:27:27 +00:00
|
|
|
]);
|
|
|
|
}
|