plugins/typescript-tools: migrate to mkNeovimPlugin

This commit is contained in:
Austin Horstman 2024-10-22 19:33:07 -05:00
parent 2a40d081d7
commit bb0e3892a2
No known key found for this signature in database
2 changed files with 187 additions and 71 deletions

View file

@ -1,24 +1,26 @@
{ {
lib, lib,
pkgs,
config,
... ...
}: }:
let let
inherit (lib.nixvim) defaultNullOpts; inherit (lib.nixvim) defaultNullOpts;
cfg = config.plugins.typescript-tools;
in in
{ lib.nixvim.neovim-plugin.mkNeovimPlugin {
options.plugins.typescript-tools = { name = "typescript-tools";
enable = lib.mkEnableOption "typescript-tools"; originalName = "typescript-tools.nvim";
package = lib.mkPackageOption pkgs "typescript-tools" { package = "typescript-tools-nvim";
default = [
"vimPlugins" maintainers = [ lib.maintainers.khaneliman ];
"typescript-tools-nvim"
]; # TODO:: introduced 10-22-2024: remove after 24.11
}; optionsRenamedToSettings = [
"onAttach"
"handlers"
];
settingsOptions = {
on_attach = defaultNullOpts.mkLuaFn "__lspOnAttach" "Lua code to run when tsserver attaches to a buffer.";
onAttach = defaultNullOpts.mkLuaFn "__lspOnAttach" "Lua code to run when tsserver attaches to a buffer.";
handlers = lib.mkOption { handlers = lib.mkOption {
type = with lib.types; nullOr (attrsOf strLuaFn); type = with lib.types; nullOr (attrsOf strLuaFn);
default = null; default = null;
@ -34,9 +36,9 @@ in
}; };
settings = { settings = {
separateDiagnosticServer = defaultNullOpts.mkBool true "Spawns an additional tsserver instance to calculate diagnostics"; separate_diagnostic_server = defaultNullOpts.mkBool true "Spawns an additional tsserver instance to calculate diagnostics";
publishDiagnosticOn = publish_diagnostic_on =
defaultNullOpts.mkEnum defaultNullOpts.mkEnum
[ [
"change" "change"
@ -47,7 +49,7 @@ in
Either "change" or "insert_leave". Determines when the client asks the server about diagnostics Either "change" or "insert_leave". Determines when the client asks the server about diagnostics
''; '';
exposeAsCodeAction = lib.mkOption { expose_as_code_action = lib.mkOption {
type = type =
with lib.types; with lib.types;
either (enum [ "all" ]) ( either (enum [ "all" ]) (
@ -64,26 +66,26 @@ in
description = "Specify what to expose as code actions."; description = "Specify what to expose as code actions.";
}; };
tsserverPath = lib.nixvim.mkNullOrStr '' tsserver_path = lib.nixvim.mkNullOrStr ''
Specify a custom path to `tsserver.js` file, if this is nil or file under path Specify a custom path to `tsserver.js` file, if this is nil or file under path
doesn't exist then standard path resolution strategy is applied doesn't exist then standard path resolution strategy is applied
''; '';
tsserverPlugins = tsserver_plugins =
with lib.types; with lib.types;
lib.nixvim.mkNullOrOption (listOf (maybeRaw str)) '' lib.nixvim.mkNullOrOption (listOf (maybeRaw str)) ''
List of plugins for tsserver to load. See this plugins's README List of plugins for tsserver to load. See this plugins's README
at https://github.com/pmizio/typescript-tools.nvim/#-styled-components-support at https://github.com/pmizio/typescript-tools.nvim/#-styled-components-support
''; '';
tsserverMaxMemory = tsserver_max_memory =
lib.nixvim.mkNullOrOption (with lib.types; maybeRaw (either ints.unsigned (enum [ "auto" ]))) lib.nixvim.mkNullOrOption (with lib.types; maybeRaw (either ints.unsigned (enum [ "auto" ])))
'' ''
This value is passed to: https://nodejs.org/api/cli.html#--max-old-space-sizesize-in-megabytes This value is passed to: https://nodejs.org/api/cli.html#--max-old-space-sizesize-in-megabytes
Memory limit in megabytes or "auto"(basically no limit) Memory limit in megabytes or "auto"(basically no limit)
''; '';
tsserverFormatOptions = lib.mkOption { tsserver_format_options = lib.mkOption {
type = with lib.types; nullOr (attrsOf anything); type = with lib.types; nullOr (attrsOf anything);
default = null; default = null;
description = "Configuration options that well be passed to the tsserver instance. Find available options [here](https://github.com/microsoft/TypeScript/blob/v5.0.4/src/server/protocol.ts#L3418)"; description = "Configuration options that well be passed to the tsserver instance. Find available options [here](https://github.com/microsoft/TypeScript/blob/v5.0.4/src/server/protocol.ts#L3418)";
@ -99,7 +101,7 @@ in
}; };
}; };
tsserverFilePreferences = lib.mkOption { tsserver_file_references = lib.mkOption {
type = with lib.types; nullOr (attrsOf anything); type = with lib.types; nullOr (attrsOf anything);
default = null; default = null;
description = "Configuration options that well be passed to the tsserver instance. Find available options [here](https://github.com/microsoft/TypeScript/blob/v5.0.4/src/server/protocol.ts#L3439)"; description = "Configuration options that well be passed to the tsserver instance. Find available options [here](https://github.com/microsoft/TypeScript/blob/v5.0.4/src/server/protocol.ts#L3439)";
@ -113,15 +115,15 @@ in
}; };
}; };
tsserverLocale = defaultNullOpts.mkStr "en" '' tsserver_locale = defaultNullOpts.mkStr "en" ''
Locale of all tsserver messages. Supported locales here: https://github.com/microsoft/TypeScript/blob/3c221fc086be52b19801f6e8d82596d04607ede6/src/compiler/utilitiesPublic.ts#L620 Locale of all tsserver messages. Supported locales here: https://github.com/microsoft/TypeScript/blob/3c221fc086be52b19801f6e8d82596d04607ede6/src/compiler/utilitiesPublic.ts#L620
''; '';
completeFunctionCalls = defaultNullOpts.mkBool false '' complete_function_calls = defaultNullOpts.mkBool false ''
Mirror of VSCode's `typescript.suggest.completeFunctionCalls` Mirror of VSCode's `typescript.suggest.completeFunctionCalls`
''; '';
includeCompletionsWithInsertText = defaultNullOpts.mkBool true '' include_completions_with_insert_text = defaultNullOpts.mkBool true ''
Mirror of VSCode's `typescript.suggest.completeFunctionCalls` Mirror of VSCode's `typescript.suggest.completeFunctionCalls`
''; '';
@ -136,11 +138,11 @@ in
"off" "off"
"WARNING: Experimental feature also in VSCode, disabled by default because it might impact server performance."; "WARNING: Experimental feature also in VSCode, disabled by default because it might impact server performance.";
disableMemberCodeLens = defaultNullOpts.mkBool true '' disable_member_code_lens = defaultNullOpts.mkBool true ''
By default code lenses are displayed on all referenceable values. Display less by removing member references from lenses. By default code lenses are displayed on all referenceable values. Display less by removing member references from lenses.
''; '';
jsxCloseTag = { jsx_close_tag = {
enable = defaultNullOpts.mkBool false '' enable = defaultNullOpts.mkBool false ''
Functions similarly to `nvim-ts-autotag`. This is disabled by default to avoid conflicts. Functions similarly to `nvim-ts-autotag`. This is disabled by default to avoid conflicts.
''; '';
@ -156,38 +158,115 @@ in
}; };
}; };
}; };
config = lib.mkIf cfg.enable {
extraPlugins = [ cfg.package ];
plugins.lsp.postConfig = settingsExample = {
with cfg; handlers = {
"textDocument/publishDiagnostics" = ''
api.filter_diagnostics(
{ 80006 }
)
'';
};
settings = {
tsserver_plugins = [
"@styled/typescript-styled-plugin"
];
tsserver_file_preferences.__raw = ''
function(ft)
return {
includeInlayParameterNameHints = "all",
includeCompletionsForModuleExports = true,
quotePreference = "auto",
}
end
'';
tsserver_format_options.__raw = ''
function(ft)
return {
allowIncompleteCompletions = false,
allowRenameOfImportPath = false,
}
end
'';
};
};
# NOTE: call setup in lsp config
callSetup = false;
extraConfig =
cfg:
let let
options = { definedOpts = lib.filter (opt: lib.hasAttrByPath opt cfg.settings) [
inherit handlers; [ "separateDiagnosticServer" ]
on_attach = onAttach; [ "publishDiagnosticOn" ]
[ "exposeAsCodeAction" ]
[ "tsserverPath" ]
[ "tsserverPlugins" ]
[ "tsserverMaxMemory" ]
[ "tsserverFormatOptions" ]
[ "tsserverFilePreferences" ]
[ "tsserverLocale" ]
[ "completeFunctionCalls" ]
[ "includeCompletionsWithInsertText" ]
[ "codeLens" ]
[ "disableMemberCodeLens" ]
[
"jsxCloseTag"
"enable"
]
[
"jsxCloseTag"
"filetypes"
]
];
in
{
plugins.lsp.postConfig =
let
# TODO:: introduced 10-22-2024: remove after 24.11
renamedSettings = lib.foldl' (
acc: optPath:
let
snakeCasePath = builtins.map lib.nixvim.toSnakeCase optPath;
optValue = lib.getAttrFromPath optPath cfg.settings;
in
lib.recursiveUpdate acc (lib.setAttrByPath snakeCasePath optValue)
) { } definedOpts;
settings = with settings; { # Based on lib.filterAttrsRecursive
separate_diagnostic_server = separateDiagnosticServer; # TODO: Maybe move to nixvim's or upstream's lib?
publish_diagnostic_on = publishDiagnosticOn; filterAttrsRecursivePath =
expose_as_code_action = exposeAsCodeAction; predicate: set: path:
tsserver_path = tsserverPath; lib.listToAttrs (
tsserver_plugins = tsserverPlugins; lib.concatMap (
tsserver_max_memory = tsserverMaxMemory; name:
tsserver_format_options = tsserverFormatOptions; let
tsserver_file_preferences = tsserverFilePreferences; path' = path ++ [ name ];
tsserver_locale = tsserverLocale; v = set.${name};
complete_function_calls = completeFunctionCalls; in
include_completions_with_insert_text = includeCompletionsWithInsertText; lib.optional (predicate path' v) {
code_lens = codeLens; inherit name;
disable_member_code_lens = disableMemberCodeLens; value = if lib.isAttrs v then filterAttrsRecursivePath predicate v path' else v;
jsx_close_tag = with jsxCloseTag; { }
inherit enable filetypes; ) (lib.attrNames set)
}; );
};
setupOptions =
filterAttrsRecursivePath (path: value: !builtins.elem path definedOpts) cfg.settings [ ]
// {
settings = lib.recursiveUpdate cfg.settings.settings renamedSettings;
}; };
in in
'' ''
require('typescript-tools').setup(${lib.nixvim.toLuaObject options}) require('typescript-tools').setup(${lib.nixvim.toLuaObject setupOptions})
'';
# TODO:: introduced 10-22-2024: remove after 24.11
# Nested settings can't have normal mkRenamedOptionModule functionality so we can only
# alert the user that they are using the old values
warnings = lib.optional (definedOpts != [ ]) ''
Nixvim(plugins.typescript-tools): The following settings have moved under `plugins.typescript-tools.settings.settings` with snake_case:
${lib.concatMapStringsSep "\n" (opt: " - ${lib.showOption (lib.toList opt)}") definedOpts}
''; '';
}; };
} }

View file

@ -7,20 +7,20 @@
plugins.typescript-tools = { plugins.typescript-tools = {
enable = true; enable = true;
settings = { settings = {
separateDiagnosticServer = true; separate_diagnostic_server = true;
publishDiagnosticOn = "insert_leave"; publish_diagnostic_on = "insert_leave";
exposeAsCodeAction = null; expose_as_code_action = null;
tsserverPath = null; tsserver_path = null;
tsserverPlugins = null; tsserver_plugins = null;
tsserverMaxMemory = "auto"; tsserver_max_memory = "auto";
tsserverFormatOptions = null; tsserver_format_options = null;
tsserverFilePreferences = null; tsserver_file_preferences = null;
tsserverLocale = "en"; tsserver_locale = "en";
completeFunctionCalls = false; complete_function_calls = false;
includeCompletionsWithInsertText = true; include_completions_with_insert_text = true;
codeLens = "off"; code_lens = "off";
disableMemberCodeLens = true; disable_member_code_lens = true;
jsxCloseTag = { jsx_close_tag = {
enable = false; enable = false;
filetypes = [ filetypes = [
"javascriptreact" "javascriptreact"
@ -30,4 +30,41 @@
}; };
}; };
}; };
example = {
plugins.typescript-tools = {
enable = true;
settings = {
handlers = {
"textDocument/publishDiagnostics" = ''
api.filter_diagnostics(
{ 80006 }
)
'';
};
settings = {
tsserver_plugins = [
"@styled/typescript-styled-plugin"
];
tsserver_file_preferences.__raw = ''
function(ft)
return {
includeInlayParameterNameHints = "all",
includeCompletionsForModuleExports = true,
quotePreference = "auto",
}
end
'';
tsserver_format_options.__raw = ''
function(ft)
return {
allowIncompleteCompletions = false,
allowRenameOfImportPath = false,
}
end
'';
};
};
};
};
} }