From 0bdfb3c0bcf8a72b1a14c39642140084248d968e Mon Sep 17 00:00:00 2001 From: winston Date: Thu, 17 Aug 2023 04:25:26 +0200 Subject: [PATCH] fix(nvim): deno/typescript-tools compat --- home/apps/neovim.nix | 3 ++- home/apps/neovim/lua/lsp/webdev.lua | 33 +++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/home/apps/neovim.nix b/home/apps/neovim.nix index 319292e..2b6eb9b 100644 --- a/home/apps/neovim.nix +++ b/home/apps/neovim.nix @@ -53,10 +53,10 @@ nodePackages.graphql nodePackages.graphql-language-service-cli nodePackages.intelephense + nodePackages.typescript nodePackages.typescript-language-server nodePackages.vscode-langservers-extracted nodePackages.yaml-language-server - yarn # rust cargo @@ -85,6 +85,7 @@ # needed for some plugin build steps gnumake unzip + yarn ]; }; diff --git a/home/apps/neovim/lua/lsp/webdev.lua b/home/apps/neovim/lua/lsp/webdev.lua index dfd9030..00aa8d9 100644 --- a/home/apps/neovim/lua/lsp/webdev.lua +++ b/home/apps/neovim/lua/lsp/webdev.lua @@ -31,15 +31,36 @@ M.setup = function(opts) }, opts)) -- attach tsserver only when there's a 'package.json' file in the CWD - require("typescript-tools").setup({ on_attach = opts.on_attach }) + require("typescript-tools").setup({ + single_file_support = false, + root_dir = function(fname) + local root_dir = lspconfig.util.root_pattern("tsconfig.json")(fname) + + -- this is needed to make sure we don't pick up root_dir inside node_modules + local node_modules_index = root_dir + and root_dir:find("node_modules", 1, true) + if node_modules_index and node_modules_index > 0 then + ---@diagnostic disable-next-line: need-check-nil + root_dir = root_dir:sub(1, node_modules_index - 2) + end + + return root_dir + end, + settings = { + expose_as_code_action = { + "add_missing_imports", + "fix_all", + "remove_unused", + }, + -- Nix silliness + -- stylua: ignore + tsserver_path = vim.fn.resolve(vim.fn.exepath("tsserver") .. "/../../lib/node_modules/typescript/bin/tsserver"), + }, + }) -- attach deno only when there's a 'deps.ts' file in the CWD lspconfig.denols.setup(vim.tbl_extend("keep", { - root_dir = lspconfig.util.root_pattern( - "deno.json", - "deno.jsonc", - "deps.ts" - ), + root_dir = lspconfig.util.root_pattern("deno.json", "deno.jsonc"), single_file_support = false, }, opts)) end