From 4bccd6591d90cc5defe6355d634be4b0c562d832 Mon Sep 17 00:00:00 2001 From: Jose Angel Mendez Santiago Date: Thu, 25 Apr 2024 09:30:52 -0600 Subject: [PATCH] fix(pack): update typescript-all-in-one disable typescript-tools to denols (#876) * feat(pack): update typescript-all-in-one disable tsserver and typescript-tools to denols * refactor(pack): clean up `typescript-all-in-one` and `typescript-deno` implementation * fix(pack): add a bunch of missing `optional` keys --------- Co-authored-by: Micah Halter --- .../lsp/actions-preview-nvim/init.lua | 1 + .../pack/elixir-phoenix/init.lua | 1 + lua/astrocommunity/pack/elm/init.lua | 3 + lua/astrocommunity/pack/fish/init.lua | 1 + lua/astrocommunity/pack/gleam/init.lua | 2 + lua/astrocommunity/pack/just/init.lua | 1 + lua/astrocommunity/pack/nim/init.lua | 2 + lua/astrocommunity/pack/pkl/init.lua | 1 + lua/astrocommunity/pack/templ/init.lua | 2 + .../pack/typescript-all-in-one/init.lua | 71 ++++++++++++------- .../pack/typescript-deno/init.lua | 10 +-- 11 files changed, 59 insertions(+), 36 deletions(-) diff --git a/lua/astrocommunity/lsp/actions-preview-nvim/init.lua b/lua/astrocommunity/lsp/actions-preview-nvim/init.lua index e9b405869..9a355c489 100644 --- a/lua/astrocommunity/lsp/actions-preview-nvim/init.lua +++ b/lua/astrocommunity/lsp/actions-preview-nvim/init.lua @@ -6,6 +6,7 @@ return { "nvim-telescope/telescope.nvim", { "AstroNvim/astrolsp", + optional = true, ---@type AstroLSPOpts opts = { mappings = { diff --git a/lua/astrocommunity/pack/elixir-phoenix/init.lua b/lua/astrocommunity/pack/elixir-phoenix/init.lua index afb9a3d98..9e6b0f299 100644 --- a/lua/astrocommunity/pack/elixir-phoenix/init.lua +++ b/lua/astrocommunity/pack/elixir-phoenix/init.lua @@ -18,6 +18,7 @@ return { }, { "AstroNvim/astrolsp", + optional = true, opts = { config = { tailwindcss = { diff --git a/lua/astrocommunity/pack/elm/init.lua b/lua/astrocommunity/pack/elm/init.lua index 40d3c18a1..1cccd16e3 100644 --- a/lua/astrocommunity/pack/elm/init.lua +++ b/lua/astrocommunity/pack/elm/init.lua @@ -1,6 +1,7 @@ return { { "nvim-treesitter/nvim-treesitter", + optional = true, opts = function(_, opts) if opts.ensure_installed ~= "all" then opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "elm" }) @@ -9,12 +10,14 @@ return { }, { "williamboman/mason-lspconfig.nvim", + optional = true, opts = function(_, opts) opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "elmls" }) end, }, { "jay-babu/mason-null-ls.nvim", + optional = true, opts = function(_, opts) opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "elm_format" }) end, diff --git a/lua/astrocommunity/pack/fish/init.lua b/lua/astrocommunity/pack/fish/init.lua index b1db94d83..633d170a9 100644 --- a/lua/astrocommunity/pack/fish/init.lua +++ b/lua/astrocommunity/pack/fish/init.lua @@ -1,6 +1,7 @@ return { { "nvim-treesitter/nvim-treesitter", + optional = true, opts = function(_, opts) if opts.ensure_installed ~= "all" then opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "fish" }) diff --git a/lua/astrocommunity/pack/gleam/init.lua b/lua/astrocommunity/pack/gleam/init.lua index 0337b30fc..b9864a836 100644 --- a/lua/astrocommunity/pack/gleam/init.lua +++ b/lua/astrocommunity/pack/gleam/init.lua @@ -1,6 +1,7 @@ return { { "nvim-treesitter/nvim-treesitter", + optional = true, opts = function(_, opts) if opts.ensure_installed ~= "all" then opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "gleam" }) @@ -9,6 +10,7 @@ return { }, { "AstroNvim/astrolsp", + optional = true, opts = function(_, opts) if vim.fn.executable "gleam" == 1 then opts.servers = require("astrocore").list_insert_unique(opts.servers, { "gleam" }) diff --git a/lua/astrocommunity/pack/just/init.lua b/lua/astrocommunity/pack/just/init.lua index 00aef3d46..49bae8ddd 100644 --- a/lua/astrocommunity/pack/just/init.lua +++ b/lua/astrocommunity/pack/just/init.lua @@ -26,6 +26,7 @@ return { }, { "nvim-treesitter/nvim-treesitter", + optional = true, opts = function(_, opts) local parser_config = require("nvim-treesitter.parsers").get_parser_configs() parser_config.just = { diff --git a/lua/astrocommunity/pack/nim/init.lua b/lua/astrocommunity/pack/nim/init.lua index 11f6af565..f423fc0a4 100644 --- a/lua/astrocommunity/pack/nim/init.lua +++ b/lua/astrocommunity/pack/nim/init.lua @@ -1,12 +1,14 @@ return { { "williamboman/mason-lspconfig.nvim", + optional = true, opts = function(_, opts) opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "nim_langserver" }) end, }, { "nvim-treesitter/nvim-treesitter", + optional = true, opts = function(_, opts) if opts.ensure_installed ~= "all" then opts.ensure_installed = diff --git a/lua/astrocommunity/pack/pkl/init.lua b/lua/astrocommunity/pack/pkl/init.lua index 1658c54d5..5a2124566 100644 --- a/lua/astrocommunity/pack/pkl/init.lua +++ b/lua/astrocommunity/pack/pkl/init.lua @@ -14,6 +14,7 @@ return { dependencies = { { "nvim-treesitter/nvim-treesitter", + optional = true, opts = function(_, opts) local parser_config = require("nvim-treesitter.parsers").get_parser_configs() diff --git a/lua/astrocommunity/pack/templ/init.lua b/lua/astrocommunity/pack/templ/init.lua index a8ba9ad04..f1ce374f3 100644 --- a/lua/astrocommunity/pack/templ/init.lua +++ b/lua/astrocommunity/pack/templ/init.lua @@ -4,6 +4,7 @@ local utils = require "astronvim.utils" return { { "nvim-treesitter/nvim-treesitter", + optional = true, opts = function(_, opts) -- Ensure that opts.ensure_installed exists and is a table or string "all". if opts.ensure_installed ~= "all" then @@ -13,6 +14,7 @@ return { }, { "williamboman/mason-lspconfig.nvim", + optional = true, opts = function(_, opts) opts.ensure_installed = utils.list_insert_unique(opts.ensure_installed, "templ") end, }, } diff --git a/lua/astrocommunity/pack/typescript-all-in-one/init.lua b/lua/astrocommunity/pack/typescript-all-in-one/init.lua index 9fb093ae0..8a7a9dfa1 100644 --- a/lua/astrocommunity/pack/typescript-all-in-one/init.lua +++ b/lua/astrocommunity/pack/typescript-all-in-one/init.lua @@ -2,33 +2,50 @@ return { { import = "astrocommunity.pack.typescript" }, { import = "astrocommunity.pack.typescript-deno" }, { - "sigmasd/deno-nvim", - -- HACK: This disables tsserver if denols is attached. - -- A solution that only enables the required lsp should replace it. - opts = function(_, opts) - vim.api.nvim_create_autocmd("LspAttach", { - callback = function(args) - local bufnr = args.buf - local curr_client = vim.lsp.get_client_by_id(args.data.client_id) - -- if deno attached, stop all typescript servers - if curr_client.name == "denols" then - vim.lsp.for_each_buffer_client(bufnr, function(client, client_id) - if client.name == "tsserver" then vim.lsp.stop_client(client_id, true) end - end) - -- if tsserver attached, stop it if there is a denols server attached - elseif curr_client.name == "tsserver" then - for _, client in ipairs(vim.lsp.get_clients { bufnr = bufnr }) do - if client.name == "denols" then - vim.lsp.stop_client(curr_client.id, true) - break + "AstroNvim/astrolsp", + optional = true, + opts = { + config = { + denols = { + -- adjust deno ls root directory detection + root_dir = function(...) return require("lspconfig.util").root_pattern("deno.json", "deno.jsonc")(...) end, + }, + }, + }, + }, + { + "AstroNvim/astrocore", + ---@type AstroCoreOpts + opts = { + autocmds = { + -- set up autocommand to choose the correct language server + typescript_deno_switch = { + { + event = "LspAttach", + callback = function(args) + local bufnr = args.buf + local curr_client = vim.lsp.get_client_by_id(args.data.client_id) + + if curr_client and curr_client.name == "denols" then + local clients = (vim.lsp.get_clients or vim.lsp.get_active_clients) { + bufnr = bufnr, + name = "typescript-tools", + } + for _, client in ipairs(clients) do + vim.lsp.stop_client(client.id, true) + end + end + + -- if tsserver attached, stop it if there is a denols server attached + if curr_client and curr_client.name == "typescript-tools" then + if next((vim.lsp.get_clients or vim.lsp.get_active_clients) { bufnr = bufnr, name = "denols" }) then + vim.lsp.stop_client(curr_client.id, true) + end end - end - end - end, - }) - local astrolsp_avail, astrolsp = pcall(require, "astrolsp") - opts.server = astrolsp_avail and astrolsp.lsp_opts "denols" - opts.server.root_dir = require("lspconfig.util").root_pattern("deno.json", "deno.jsonc") - end, + end, + }, + }, + }, + }, }, } diff --git a/lua/astrocommunity/pack/typescript-deno/init.lua b/lua/astrocommunity/pack/typescript-deno/init.lua index 6808d2bd4..54de59db6 100644 --- a/lua/astrocommunity/pack/typescript-deno/init.lua +++ b/lua/astrocommunity/pack/typescript-deno/init.lua @@ -1,13 +1,4 @@ return { - { - "AstroNvim/astrolsp", - optional = true, - ---@type AstroLSPOpts - opts = { - ---@diagnostic disable: missing-fields - handlers = { denols = false }, - }, - }, { import = "astrocommunity.pack.json" }, { "nvim-treesitter/nvim-treesitter", @@ -44,6 +35,7 @@ return { { "sigmasd/deno-nvim", ft = { "javascript", "typescript", "javascriptreact", "typescriptreact" }, + dependencies = { { "AstroNvim/astrolsp", optional = true, opts = { handlers = { denols = false } } } }, opts = function(_, opts) local astrolsp_avail, astrolsp = pcall(require, "astrolsp") if astrolsp_avail then opts.server = astrolsp.lsp_opts "denols" end