-
Notifications
You must be signed in to change notification settings - Fork 17
/
lsp.lua
410 lines (384 loc) · 10.6 KB
/
lsp.lua
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
-- for debugging
-- :lua require('vim.lsp.log').set_level("debug")
-- :lua print(vim.inspect(vim.lsp.buf_get_clients()))
-- :lua print(vim.lsp.get_log_path())
-- :lua print(vim.inspect(vim.tbl_keys(vim.lsp.callbacks)))
local has_lsp, nvim_lsp = pcall(require, "lspconfig")
if not has_lsp then
return
end
local has_lspsaga, lspsaga = pcall(require, "lspsaga")
local has_extensions = pcall(require, "lsp_extensions")
local utils = require "_.utils"
local map_opts = {noremap = true, silent = true}
lspsaga.init_lsp_saga()
require "_.completion".setup()
utils.augroup(
"COMPLETION",
function()
if has_extensions then
vim.api.nvim_command(
"au CursorMoved,InsertLeave,BufEnter,BufWinEnter,TabEnter,BufWritePost * lua require'lsp_extensions'.inlay_hints()"
)
end
end
)
vim.fn.sign_define(
"LspDiagnosticsSignError",
{
text = utils.get_icon("error"),
texthl = "LspDiagnosticsDefaultError",
linehl = "",
numhl = ""
}
)
vim.fn.sign_define(
"LspDiagnosticsSignWarning",
{
text = utils.get_icon("warn"),
texthl = "LspDiagnosticsDefaultWarning",
linehl = "",
numhl = ""
}
)
vim.fn.sign_define(
"LspDiagnosticsSignInformation",
{
text = utils.get_icon("info"),
texthl = "LspDiagnosticsDefaultInformation",
linehl = "",
numhl = ""
}
)
vim.fn.sign_define(
"LspDiagnosticsSignHint",
{
text = utils.get_icon("hint"),
texthl = "LspDiagnosticsDefaultHint",
linehl = "",
numhl = ""
}
)
vim.api.nvim_set_option("omnifunc", "v:lua.vim.lsp.omnifunc")
local default_mappings = {
["<leader>a"] = {"<Cmd>lua vim.lsp.buf.code_action()<CR>"},
["<leader>f"] = {"<cmd>lua vim.lsp.buf.references()<CR>"},
["<leader>r"] = {"<cmd>lua vim.lsp.buf.rename()<CR>"},
["K"] = {"<Cmd>lua vim.lsp.buf.hover()<CR>"},
["<leader>ld"] = {"<cmd>lua vim.lsp.diagnostic.show_line_diagnostics()<CR>"},
["[d"] = {"<cmd>lua vim.lsp.diagnostic.goto_next()<cr>"},
["]d"] = {"<cmd>lua vim.lsp.diagnostic.goto_prev()<CR>"},
["<C-]>"] = {"<Cmd>lua vim.lsp.buf.definition()<CR>"},
["<leader>D"] = {"<Cmd>lua vim.lsp.buf.declaration()<CR>"},
["<leader>i"] = {"<cmd>lua vim.lsp.buf.implementation()<CR>"}
}
local lspsaga_mappings = {
["<leader>d"] = {
"<Cmd>lua require'lspsaga.provider'.preview_definition()<CR>"
},
["<leader>a"] = {
"<Cmd>lua require'lspsaga.codeaction'.code_action()<CR>",
"<Cmd>'<,'>lua require'lspsaga.codeaction'.range_code_action()<CR>"
},
["<leader>f"] = {"<cmd>lua require'lspsaga.provider'.lsp_finder()<CR>"},
["<leader>s"] = {
"<cmd>lua require'lspsaga.signaturehelp'.signature_help()<CR>"
},
["<leader>r"] = {"<cmd>lua require'lspsaga.rename'.rename()<CR>"},
["<leader>ld"] = {
"<cmd>lua require'lspsaga.diagnostic'.show_line_diagnostics()<CR>"
},
["[d"] = {
"<cmd>lua require'lspsaga.diagnostic'.lsp_jump_diagnostic_prev()<CR>"
},
["]d"] = {
"<cmd>lua require'lspsaga.diagnostic'.lsp_jump_diagnostic_next()<CR>"
},
["K"] = {"<cmd>lua require('lspsaga.hover').render_hover_doc()<cr>"},
["<C-f>"] = {"<cmd>lua require('lspsaga.hover').smart_scroll_hover(1)<cr>"},
["<C-b>"] = {"<cmd>lua require('lspsaga.hover').smart_scroll_hover(-1)<CR>"}
}
local mappings =
vim.tbl_extend(
"force",
default_mappings,
has_lspsaga and lspsaga_mappings or {}
)
local on_attach = function(client)
client.config.flags.allow_incremental_sync = true
require "lsp_signature".on_attach()
for lhs, rhs in pairs(mappings) do
if lhs == "K" then
if vim.api.nvim_buf_get_option(0, "filetype") ~= "vim" then
utils.bmap("n", lhs, rhs[1], map_opts)
end
else
utils.bmap("n", lhs, rhs[1], map_opts)
if #rhs == 2 then
utils.bmap("v", lhs, rhs[2], map_opts)
end
end
end
if client.resolved_capabilities.document_highlight then
vim.api.nvim_exec(
[[
hi! link LspReferenceRead SpecialKey
hi! link LspReferenceText SpecialKey
hi! link LspReferenceWrite SpecialKey
]],
false
)
end
utils.augroup(
"LSP",
function()
vim.api.nvim_command(
"autocmd CursorHold <buffer> lua vim.lsp.diagnostic.show_line_diagnostics()"
)
if client.resolved_capabilities.document_highlight then
vim.api.nvim_command(
"autocmd CursorHold <buffer> lua vim.lsp.buf.document_highlight()"
)
vim.api.nvim_command(
"autocmd CursorHoldI <buffer> lua vim.lsp.buf.document_highlight()"
)
vim.api.nvim_command(
"autocmd CursorMoved <buffer> lua vim.lsp.buf.clear_references()"
)
end
end
)
end
-- https://github.com/nvim-lua/diagnostic-nvim/issues/73
vim.lsp.handlers["textDocument/publishDiagnostics"] =
vim.lsp.with(
vim.lsp.diagnostic.on_publish_diagnostics,
{
virtual_text = false,
-- virtual_text = {
-- spacing = 4,
-- prefix = "~"
-- },
underline = false,
signs = true,
update_in_insert = false
}
)
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.completion.completionItem.snippetSupport = true
capabilities.textDocument.completion.completionItem.resolveSupport = {
properties = {
"documentation",
"detail",
"additionalTextEdits"
}
}
require("nlua.lsp.nvim").setup(
nvim_lsp,
{
on_attach = on_attach,
globals = {"vim", "spoon", "hs"}
}
)
local configs = require "lspconfig/configs"
local server_name = "tailwindlsp"
configs[server_name] = {
default_config = {
cmd = {server_name},
filetypes = {
-- html
"aspnetcorerazor",
"blade",
"django-html",
"edge",
"ejs",
"eruby",
"gohtml",
"haml",
"handlebars",
"hbs",
"html",
"html-eex",
"jade",
"leaf",
"liquid",
"markdown",
"mdx",
"mustache",
"njk",
"nunjucks",
"php",
"razor",
"slim",
"twig",
-- css
"css",
"less",
"postcss",
"sass",
"scss",
"stylus",
"sugarss",
-- js
"javascript",
"javascript.jsx",
"javascriptreact",
"reason",
"rescript",
"typescript",
"typescript.tsx",
"typescriptreact",
-- mixed
"vue",
"svelte"
},
init_options = {
userLanguages = {
eruby = "html",
["javascript.jsx"] = "javascriptreact",
["typescript.tsx"] = "typescriptreact"
}
},
root_dir = function(fname)
return nvim_lsp.util.root_pattern(
"tailwind.config.js",
"tailwind.config.ts"
)(fname) or
nvim_lsp.util.root_pattern("postcss.config.js", "postcss.config.ts")(
fname
) or
nvim_lsp.util.find_package_json_ancestor(fname) or
nvim_lsp.util.find_node_modules_ancestor(fname) or
nvim_lsp.util.find_git_ancestor(fname)
end,
handlers = {
["tailwindcss/getConfiguration"] = function(_, _, params, _, bufnr, _)
-- tailwindcss lang server waits for this repsonse before providing hover
vim.lsp.buf_notify(
bufnr,
"tailwindcss/getConfigurationResponse",
{_id = params._id}
)
end
}
},
docs = {
description = [[ ]],
default_config = {
root_dir = [[root_pattern("package.json", ".git")]]
}
}
}
local servers = {
cssls = {},
bashls = {},
vimls = {},
pyright = {},
dockerls = {},
rust_analyzer = {
capabilities = capabilities
},
gopls = {
cmd = {"gopls", "serve"},
root_dir = function(fname)
return nvim_lsp.util.root_pattern("go.mod", ".git")(fname) or
vim.fn.getcwd()
end
},
tsserver = {
root_dir = function(fname)
return nvim_lsp.util.root_pattern("tsconfig.json")(fname) or
nvim_lsp.util.root_pattern("package.json", "jsconfig.json", ".git")(
fname
) or
vim.fn.getcwd()
end
},
denols = {
root_dir = function(fname)
return nvim_lsp.util.root_pattern("deps.ts")(fname) or
nvim_lsp.util.root_pattern("mod.ts")(fname)
end
},
tailwindlsp = {},
-- rnix = {},
jsonls = {
filetypes = {"json", "jsonc"},
settings = {
json = {
-- Schemas https://www.schemastore.org
schemas = {
{
fileMatch = {"package.json"},
url = "https://json.schemastore.org/package.json"
},
{
fileMatch = {"tsconfig*.json"},
url = "https://json.schemastore.org/tsconfig.json"
},
{
fileMatch = {
".prettierrc",
".prettierrc.json",
"prettier.config.json"
},
url = "https://json.schemastore.org/prettierrc.json"
},
{
fileMatch = {".eslintrc", ".eslintrc.json"},
url = "https://json.schemastore.org/eslintrc.json"
},
{
fileMatch = {".babelrc", ".babelrc.json", "babel.config.json"},
url = "https://json.schemastore.org/babelrc.json"
},
{
fileMatch = {"lerna.json"},
url = "https://json.schemastore.org/lerna.json"
},
{
fileMatch = {"now.json", "vercel.json"},
url = "https://json.schemastore.org/now.json"
},
{
fileMatch = {
".stylelintrc",
".stylelintrc.json",
"stylelint.config.json"
},
url = "http://json.schemastore.org/stylelintrc.json"
}
}
}
}
},
yamlls = {
settings = {
yaml = {
-- Schemas https://www.schemastore.org
schemas = {
["http://json.schemastore.org/gitlab-ci.json"] = {".gitlab-ci.yml"},
["https://json.schemastore.org/bamboo-spec.json"] = {
"bamboo-specs/*.{yml,yaml}"
},
["https://raw.githubusercontent.com/compose-spec/compose-spec/master/schema/compose-spec.json"] = {
"docker-compose*.{yml,yaml}"
},
["http://json.schemastore.org/github-workflow.json"] = ".github/workflows/*.{yml,yaml}",
["http://json.schemastore.org/github-action.json"] = ".github/action.{yml,yaml}",
["http://json.schemastore.org/prettierrc.json"] = ".prettierrc.{yml,yaml}",
["http://json.schemastore.org/stylelintrc.json"] = ".stylelintrc.{yml,yaml}",
["http://json.schemastore.org/circleciconfig"] = ".circleci/**/*.{yml,yaml}"
}
}
}
}
}
for server, config in pairs(servers) do
local server_disabled = (config.disabled ~= nil and config.disabled) or false
if not server_disabled then
nvim_lsp[server].setup(
vim.tbl_deep_extend("force", {on_attach = on_attach}, config)
)
end
end