Skip to content

Commit

Permalink
feat(pack): modularization of all typescript packs
Browse files Browse the repository at this point in the history
  • Loading branch information
owittek committed Apr 10, 2023
1 parent 26f79eb commit 2ccd558
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 251 deletions.
90 changes: 1 addition & 89 deletions lua/astrocommunity/pack/typescript-denols/denols.lua
Original file line number Diff line number Diff line change
@@ -1,89 +1 @@
local utils = require "astrocommunity.utils"

return {
{ import = "astrocommunity.pack.json" },
{
"nvim-treesitter/nvim-treesitter",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table or string "all".
if not opts.ensure_installed then
opts.ensure_installed = {}
elseif opts.ensure_installed == "all" then
return
end
-- Add the required file types to opts.ensure_installed.
utils.list_insert_unique(opts.ensure_installed, { "javascript", "typescript", "tsx" })
end,
},
{
"williamboman/mason-lspconfig.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add tsserver to opts.ensure_installed using vim.list_extend.
utils.list_insert_unique(opts.ensure_installed, "denols")
end,
},
{
"jay-babu/mason-nvim-dap.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add to opts.ensure_installed using table.insert.
utils.list_insert_unique(opts.ensure_installed, "js")
end,
},
{
"mfussenegger/nvim-dap",
ft = { "ts", "js", "tsx", "jsx" },
enabled = true,
dependencies = {
{
"mxsdev/nvim-dap-vscode-js",
opts = { debugger_cmd = { "js-debug-adapter" }, adapters = { "pwa-node" } },
},
{ "theHamsta/nvim-dap-virtual-text", config = true },
{ "rcarriga/nvim-dap-ui", config = true },
},
config = function()
local dap = require "dap"
for _, language in ipairs { "typescript", "typescriptreact", "javascript", "javascriptreact" } do
if not dap.configurations[language] then dap.configurations[language] = {} end

utils.list_insert_unique(dap.configurations[language], {
{
request = "launch",
name = "(Deno) Launch current file",
type = "pwa-node",
program = "${file}",
cwd = vim.fn.getcwd(),
runtimeExecutable = "deno",
runtimeArgs = { "run", "--inspect-wait", "--allow-all" },
attachSimplePort = 9229,
},
{
request = "launch",
name = "(Deno) Launch main.ts",
type = "pwa-node",
program = "${workspaceFolder}/main.ts",
cwd = vim.fn.getcwd(),
runtimeExecutable = "deno",
runtimeArgs = { "run", "--inspect-wait", "--allow-all" },
attachSimplePort = 9229,
},
})
end
end,
},
{
"sigmasd/deno-nvim",
init = function() utils.list_insert_unique(astronvim.lsp.skip_setup, "denols") end,
ft = {
"typescript",
"typescriptreact",
"javascript",
"javascriptreact",
},
opts = function() return { server = require("astronvim.utils.lsp").config "denols" } end,
},
}
return require("astrocommunity.pack.typescript-tsserver-and-denols.denols").get_config()
59 changes: 59 additions & 0 deletions lua/astrocommunity/pack/typescript-tsserver-and-denols/denols.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
local utils = require "astrocommunity.utils"
local ts_config = require "astrocommunity.pack.typescript-tsserver-and-denols.ts-config"

local Deno = {
dap_config = {
{
request = "launch",
name = "(Deno) Launch current file",
type = "pwa-node",
program = "${file}",
cwd = vim.fn.getcwd(),
runtimeExecutable = "deno",
runtimeArgs = { "run", "--inspect-wait", "--allow-all" },
attachSimplePort = 9229,
},
{
request = "launch",
name = "(Deno) Launch main.ts",
type = "pwa-node",
program = "${workspaceFolder}/main.ts",
cwd = vim.fn.getcwd(),
runtimeExecutable = "deno",
runtimeArgs = { "run", "--inspect-wait", "--allow-all" },
attachSimplePort = 9229,
},
},
}

function Deno.get_base_config()
local base_config = ts_config.get_base_config "denols"

table.insert(base_config, {
"sigmasd/deno-nvim",
init = function() utils.list_insert_unique(astronvim.lsp.skip_setup, "denols") end,
ft = ts_config.filetypes,
opts = function() return { server = require("astronvim.utils.lsp").config "denols" } end,
})
return base_config
end

function Deno.get_dap_config()
local base_dap_config = ts_config.get_base_dap_config

base_dap_config.config = function()
local dap = require "dap"
for _, language in ts_config.filetypes do
dap.configurations[language] = Deno.dap_config
end
end
return base_dap_config
end

function Deno.get_config()
local base_config = Deno.get_base_config()
table.insert(base_config, Deno.get_dap_config())
return base_config
end

return Deno
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
local utils = require "astrocommunity.utils"

local filetypes = { "javascript", "typescript", "javascriptreact", "typescriptreact" }

local M = {
filetypes = filetypes,
get_base_dap_config = {
"mfussenegger/nvim-dap",
ft = filetypes,
enabled = true,
dependencies = {
{
"mxsdev/nvim-dap-vscode-js",
opts = { debugger_cmd = { "js-debug-adapter" }, adapters = { "pwa-node" } },
},
{ "theHamsta/nvim-dap-virtual-text", config = true },
{ "rcarriga/nvim-dap-ui", config = true },
},
},
}

function M.get_base_config(lsp)
if lsp ~= "tsserver" and lsp ~= "denols" then error("Invalid lsp: " .. lsp) end
return {
{ import = "astrocommunity.pack.json" },
{
"nvim-treesitter/nvim-treesitter",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table or string "all".
if not opts.ensure_installed then
opts.ensure_installed = {}
elseif opts.ensure_installed == "all" then
return
end
-- Add the required file types to opts.ensure_installed.
utils.list_insert_unique(opts.ensure_installed, { "javascript", "typescript", "tsx" })
end,
},
{
"williamboman/mason-lspconfig.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add tsserver to opts.ensure_installed using vim.list_extend.
utils.list_insert_unique(opts.ensure_installed, lsp)
end,
},
{
"jay-babu/mason-nvim-dap.nvim",
opts = function(_, opts)
-- Ensure that opts.ensure_installed exists and is a table.
if not opts.ensure_installed then opts.ensure_installed = {} end
-- Add to opts.ensure_installed using table.insert.
utils.list_insert_unique(opts.ensure_installed, "js")
end,
},
}
end

return M
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
return {
{ import = "astrocommunity.pack.typescript" },
{ import = "astrocommunity.pack.typescript-denols" },
local config = {
{ import = "astrocommunity.pack.typescript-tsserver-and-denols.tsserver" },
{ import = "astrocommunity.pack.typescript-tsserver-and-denols.denols" },
{
"sigmasd/deno-nvim",
opts = function(_, opts)
Expand Down Expand Up @@ -43,7 +43,22 @@ return {
end,
})
end,
eslint_d = function()
require("null-ls").register(require("null-ls").builtins.diagnostics.eslint_d.with {
condition = function(util)
return util.root_has_file "package.json"
or util.root_has_file ".eslintrc.json"
or util.root_has_file ".eslintrc.js"
end,
})
end,
},
},
},
}

-- Only the denols dap config is being added to the config table
-- as it's being loaded after the tsserver config
table.insert(config, require("astrocommunity.pack.typescript-tsserver-and-denols.tsserver").get_dap_config())

return config
131 changes: 131 additions & 0 deletions lua/astrocommunity/pack/typescript-tsserver-and-denols/tsserver.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
local utils = require "astrocommunity.utils"
local ts_config = require "astrocommunity.pack.typescript-tsserver-and-denols.ts-config"

local TSServer = {
dap_attach_config = {
request = "attach",
name = "Attach to ...",
type = "pwa-node",
cwd = "${workspaceFolder}",
processId = function(...) return require("dap.utils").pick_process(...) end,
},
dap_javascript_config = {
{
request = "launch",
name = "Launch file",
type = "pwa-node",
cwd = "${workspaceFolder}",
program = "${file}",
},
},
dap_typescript_config = {
{
type = "pwa-node",
request = "launch",
name = "(TS-Server) Launch current file",
program = "${file}",
cwd = "${workspaceFolder}",
runtimeExecutable = "ts-node",
sourceMaps = true,
protocol = "inspector",
console = "integratedTerminal",
resolveSourceMapLocations = {
"${workspaceFolder}/dist/**/*.js",
"${workspaceFolder}/**",
"!**/node_modules/**",
},
},
},
}

function TSServer.get_base_config()
local base_config = ts_config.get_base_config "tsserver"

local events = require "neo-tree.events"
local function on_file_remove(args)
local ts_clients = vim.lsp.get_active_clients { name = "tsserver" }
for _, ts_client in ipairs(ts_clients) do
ts_client.request("workspace/executeCommand", {
command = "_typescript.applyRenameFile",
arguments = {
{
sourceUri = vim.uri_from_fname(args.source),
targetUri = vim.uri_from_fname(args.destination),
},
},
})
end
end

table.insert(base_config, {
"jay-babu/mason-null-ls.nvim",
opts = function(_, opts)
if not opts.ensure_installed then opts.ensure_installed = {} end

utils.list_insert_unique(opts.ensure_installed, "prettierd")
utils.list_insert_unique(opts.ensure_installed, "eslint_d")
end,
})

table.insert(base_config, {
"jose-elias-alvarez/typescript.nvim",
init = function() utils.list_insert_unique(astronvim.lsp.skip_setup, "tsserver") end,
ft = ts_config.filetypes,
opts = function() return { server = require("astronvim.utils.lsp").config "tsserver" } end,
})

table.insert(base_config, {
"nvim-neo-tree/neo-tree.nvim",
opts = {
event_handlers = {
{
event = events.FILE_MOVED,
handler = on_file_remove,
},
{
event = events.FILE_RENAMED,
handler = on_file_remove,
},
},
},
})

table.insert(base_config, {
"vuki656/package-info.nvim",
requires = "MunifTanjim/nui.nvim",
config = true,
event = "BufRead package.json",
})

return base_config
end

function TSServer.get_dap_config()
local base_dap_config = ts_config.get_base_dap_config
base_dap_config.config = function()
local dap = require "dap"

for _, language in ipairs { "typescript", "typescriptreact" } do
if not dap.configurations[language] then dap.configurations[language] = {} end

utils.list_insert_unique(dap.configurations[language], TSServer.dap_attach_config)
utils.list_insert_unique(dap.configurations[language], TSServer.dap_typescript_config)
end

for _, language in ipairs { "javascript", "javascriptreact" } do
if not dap.configurations[language] then dap.configurations[language] = {} end

utils.list_insert_unique(dap.configurations[language], TSServer.dap_attach_config)
utils.list_insert_unique(dap.configurations[language], TSServer.dap_javascript_config)
end
end
return base_dap_config
end

function TSServer.get_config()
local base_config = TSServer.get_base_config()
table.insert(base_config, TSServer.get_dap_config())
return base_config
end

return TSServer
Loading

0 comments on commit 2ccd558

Please sign in to comment.