diff --git a/docs/manual/release-notes/rl-0.9.md b/docs/manual/release-notes/rl-0.9.md index ce9af830..a283160b 100644 --- a/docs/manual/release-notes/rl-0.9.md +++ b/docs/manual/release-notes/rl-0.9.md @@ -65,6 +65,8 @@ - Moved `vim.lsp.harper-ls` to `vim.lsp.presets.haper`. +- Renamed `ts_ls` to `typescript-language-server`. + - Renamed `denols` to `deno`. - Renamed `tsgo` to `typescript-go`. diff --git a/modules/plugins/lsp/presets/default.nix b/modules/plugins/lsp/presets/default.nix index 5a403992..b529a57a 100644 --- a/modules/plugins/lsp/presets/default.nix +++ b/modules/plugins/lsp/presets/default.nix @@ -6,5 +6,6 @@ ./tinymist.nix ./twig-language-server.nix ./typescript-go.nix + ./typescript-language-server.nix ]; } diff --git a/modules/plugins/lsp/presets/typescript-language-server.nix b/modules/plugins/lsp/presets/typescript-language-server.nix new file mode 100644 index 00000000..bf1dc1f4 --- /dev/null +++ b/modules/plugins/lsp/presets/typescript-language-server.nix @@ -0,0 +1,60 @@ +{ + config, + lib, + pkgs, + ... +}: let + inherit (lib.meta) getExe; + inherit (lib.modules) mkIf; + inherit (lib.options) mkEnableOption; + inherit (lib.generators) mkLuaInline; + + cfg = config.vim.lsp.presets.typescript-language-server; +in { + options.vim.lsp.presets.typescript-language-server = { + enable = mkEnableOption "the Typescript Language Server"; + }; + + config = mkIf cfg.enable { + vim.lsp.servers.typescript-language-server = { + enable = true; + cmd = [(getExe pkgs.typescript-language-server) "--stdio"]; + root_markers = [".git" "package.json"]; + init_options = {hostInfo = "neovim";}; + handlers = { + # handle rename request for certain code actions like extracting functions / types + "_typescript.rename" = mkLuaInline '' + function(_, result, ctx) + local client = assert(vim.lsp.get_client_by_id(ctx.client_id)) + vim.lsp.util.show_document({ + uri = result.textDocument.uri, + range = { + start = result.position, + ['end'] = result.position, + }, + }, client.offset_encoding) + vim.lsp.buf.rename() + return vim.NIL + end + ''; + }; + on_attach = mkLuaInline '' + function(client, bufnr) + -- ts_ls provides `source.*` code actions that apply to the whole file. These only appear in + -- `vim.lsp.buf.code_action()` if specified in `context.only`. + vim.api.nvim_buf_create_user_command(0, 'LspTypescriptSourceAction', function() + local source_actions = vim.tbl_filter(function(action) + return vim.startswith(action, 'source.') + end, client.server_capabilities.codeActionProvider.codeActionKinds) + + vim.lsp.buf.code_action({ + context = { + only = source_actions, + }, + }) + end, {}) + end + ''; + }; + }; +}