Skip to content

Commit

Permalink
refactor(pack): clean up typescript-all-in-one and `typescript-deno…
Browse files Browse the repository at this point in the history
…` implementation
  • Loading branch information
mehalter committed Apr 25, 2024
1 parent 3566558 commit 582696a
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 50 deletions.
81 changes: 40 additions & 41 deletions lua/astrocommunity/pack/typescript-all-in-one/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,49 @@ 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 curr_client and curr_client.name == "denols" then
local clients = vim.lsp.get_clients { bufnr = bufnr }
for _, client in ipairs(clients) do
if client.name == "tsserver" or client.name == "typescript-tools" then
vim.lsp.stop_client(client.id, true)
end
end
end
"AstroNvim/astrolsp",
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 tsserver attached, stop it if there is a denols server attached
if curr_client and curr_client.name == "tsserver" then
local clients = vim.lsp.get_clients { bufnr = bufnr }
for _, client in ipairs(clients) do
if client.name == "denols" then
vim.lsp.stop_client(curr_client.id, true)
break
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
end
end

-- if typescript-tools attached, stop it if there is a denols server attached
if curr_client and curr_client.name == "typescript-tools" then
local clients = vim.lsp.get_clients { bufnr = bufnr }
for _, client in ipairs(clients) do
if client.name == "denols" then
vim.lsp.stop_client(curr_client.id, true)
break
-- 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,
},
},
},
},
},
}
20 changes: 11 additions & 9 deletions lua/astrocommunity/pack/typescript-deno/init.lua
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -44,6 +35,17 @@ return {
{
"sigmasd/deno-nvim",
ft = { "javascript", "typescript", "javascriptreact", "typescriptreact" },
dependencies = {
{
"AstroNvim/astrolsp",
optional = true,
---@type AstroLSPOpts
opts = {
---@diagnostic disable: missing-fields
handlers = { denols = false },
},
},
},
opts = function(_, opts)
local astrolsp_avail, astrolsp = pcall(require, "astrolsp")
if astrolsp_avail then opts.server = astrolsp.lsp_opts "denols" end
Expand Down

0 comments on commit 582696a

Please sign in to comment.