-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypescript.lua
More file actions
250 lines (243 loc) · 7.96 KB
/
typescript.lua
File metadata and controls
250 lines (243 loc) · 7.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
local function decode_json(filename)
-- Open the file in read mode
local file = io.open(filename, "r")
if not file then
return false -- File doesn't exist or cannot be opened
end
-- Read the contents of the file
local content = file:read "*all"
file:close()
-- Parse the JSON content
local json_parsed, json = pcall(vim.fn.json_decode, content)
if not json_parsed or type(json) ~= "table" then
return false -- Invalid JSON format
end
return json
end
local format_filetypes = { "javascript", "javascriptreact", "typescript", "typescriptreact" }
local function check_json_key_exists(json, ...) return vim.tbl_get(json, ...) ~= nil end
local lsp_rooter, prettierrc_rooter
local has_prettier = function(bufnr)
if type(bufnr) ~= "number" then bufnr = vim.api.nvim_get_current_buf() end
local rooter = require "astrocore.rooter"
if not lsp_rooter then
lsp_rooter = rooter.resolve("lsp", {
ignore = {
servers = function(client)
return not vim.tbl_contains({ "eslint", "ts_ls", "typescript-tools", "volar", "vtsls" }, client.name)
end,
},
})
end
if not prettierrc_rooter then
prettierrc_rooter = rooter.resolve {
".prettierrc",
".prettierrc.json",
".prettierrc.yml",
".prettierrc.yaml",
".prettierrc.json5",
".prettierrc.js",
".prettierrc.cjs",
"prettier.config.js",
".prettierrc.mjs",
"prettier.config.mjs",
"prettier.config.cjs",
".prettierrc.toml",
}
end
local prettier_dependency = false
for _, root in ipairs(require("astrocore").list_insert_unique(lsp_rooter(bufnr), { vim.fn.getcwd() })) do
local package_json = decode_json(root .. "/package.json")
if
package_json
and (
check_json_key_exists(package_json, "dependencies", "prettier")
or check_json_key_exists(package_json, "devDependencies", "prettier")
)
then
prettier_dependency = true
break
end
end
return prettier_dependency or next(prettierrc_rooter(bufnr))
end
local null_ls_formatter = function(params)
if vim.tbl_contains(format_filetypes, params.filetype) then return has_prettier(params.bufnr) end
return true
end
local conform_formatter = function(bufnr) return has_prettier(bufnr) and { "prettierd" } or {} end
return {
{ import = "astrocommunity.lsp.nvim-lsp-file-operations" },
{
"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, { "javascript", "typescript", "tsx", "jsdoc" })
end
end,
},
{
"AstroNvim/astrolsp",
---@type AstroLSPOpts
opts = {
autocmds = {
eslint_fix_on_save = {
cond = function(client) return client.name == "eslint" and vim.fn.exists ":EslintFixAll" > 0 end,
{
event = "BufWritePost",
desc = "Fix all eslint errors",
callback = function(args)
if vim.F.if_nil(vim.b[args.buf].autoformat, vim.g.autoformat, true) then vim.cmd.EslintFixAll() end
end,
},
},
},
---@diagnostic disable: missing-fields
config = {
vtsls = {
settings = {
autoUseWorkspaceTsdk = false,
typescript = {
tsserver = {
maxTsServerMemory = 16980,
},
updateImportsOnFileMove = { enabled = "always" },
inlayHints = {
parameterNames = { enabled = "all" },
parameterTypes = { enabled = true },
variableTypes = { enabled = true },
propertyDeclarationTypes = { enabled = true },
functionLikeReturnTypes = { enabled = true },
enumMemberValues = { enabled = true },
},
},
javascript = {
updateImportsOnFileMove = { enabled = "always" },
inlayHints = {
parameterNames = { enabled = "literals" },
parameterTypes = { enabled = true },
variableTypes = { enabled = true },
propertyDeclarationTypes = { enabled = true },
functionLikeReturnTypes = { enabled = true },
enumMemberValues = { enabled = true },
},
},
vtsls = {
enableMoveToFileCodeAction = true,
},
},
},
},
},
},
{
"williamboman/mason-lspconfig.nvim",
opts = function(_, opts)
opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "vtsls", "eslint" })
end,
},
{
"jay-babu/mason-null-ls.nvim",
optional = true,
opts = function(_, opts)
opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "prettierd" })
if not opts.handlers then opts.handlers = {} end
-- opts.handlers.prettierd = function(source_name, methods)
-- local null_ls = require "null-ls"
-- for _, method in ipairs(methods) do
-- null_ls.register(null_ls.builtins[method][source_name].with { runtime_condition = null_ls_formatter })
-- end
-- end
end,
},
{
"stevearc/conform.nvim",
optional = true,
opts = function(_, opts)
if not opts.formatters_by_ft then opts.formatters_by_ft = {} end
for _, filetype in ipairs(format_filetypes) do
opts.formatters_by_ft[filetype] = conform_formatter
end
end,
},
{
"jay-babu/mason-nvim-dap.nvim",
optional = true,
opts = function(_, opts)
opts.ensure_installed = require("astrocore").list_insert_unique(opts.ensure_installed, { "js" })
end,
},
{
"WhoIsSethDaniel/mason-tool-installer.nvim",
optional = true,
opts = function(_, opts)
opts.ensure_installed = require("astrocore").list_insert_unique(
opts.ensure_installed,
{ "vtsls", "eslint-lsp", "prettierd", "js-debug-adapter" }
)
end,
},
{
"vuki656/package-info.nvim",
dependencies = { "MunifTanjim/nui.nvim" },
opts = {},
event = "BufRead package.json",
},
{
"yioneko/nvim-vtsls",
lazy = true,
dependencies = {
"AstroNvim/astrocore",
opts = {
autocmds = {
nvim_vtsls = {
{
event = "LspAttach",
desc = "Load nvim-vtsls with vtsls",
callback = function(args)
if assert(vim.lsp.get_client_by_id(args.data.client_id)).name == "vtsls" then
require("vtsls")._on_attach(args.data.client_id, args.buf)
vim.api.nvim_del_augroup_by_name "nvim_vtsls"
end
end,
},
},
},
},
},
config = function(_, opts) require("vtsls").config(opts) end,
},
{
"dmmulroy/tsc.nvim",
cmd = "TSC",
opts = {},
},
{
"echasnovski/mini.icons",
optional = true,
opts = {
file = {
[".eslintrc.js"] = { glyph = "", hl = "MiniIconsYellow" },
[".node-version"] = { glyph = "", hl = "MiniIconsGreen" },
[".prettierrc"] = { glyph = "", hl = "MiniIconsPurple" },
[".yarnrc.yml"] = { glyph = "", hl = "MiniIconsBlue" },
["eslint.config.js"] = { glyph = "", hl = "MiniIconsYellow" },
["package.json"] = { glyph = "", hl = "MiniIconsGreen" },
["tsconfig.json"] = { glyph = "", hl = "MiniIconsAzure" },
["tsconfig.build.json"] = { glyph = "", hl = "MiniIconsAzure" },
["yarn.lock"] = { glyph = "", hl = "MiniIconsBlue" },
},
},
},
-- {
-- "nvim-neotest/neotest",
-- optional = true,
-- dependencies = { { "nvim-neotest/neotest-jest", config = function() end } },
-- opts = function(_, opts)
-- if not opts.adapters then opts.adapters = {} end
-- table.insert(opts.adapters, require "neotest-jest"(require("astrocore").plugin_opts "neotest-jest"))
-- end,
-- },
}