2022-12-08 22:32:32 +00:00
|
|
|
------------------------------------------------------
|
|
|
|
-------------------- Built-in LSP --------------------
|
|
|
|
------------------------------------------------------
|
2022-11-25 16:30:22 +00:00
|
|
|
local nix_vars = require("nix-vars")
|
|
|
|
local nvim_lsp = require('lspconfig')
|
|
|
|
|
|
|
|
local on_attach = function(client, bufnr)
|
2022-12-03 03:31:37 +00:00
|
|
|
local bufopts = {noremap = true, silent = true, buffer = bufnr}
|
2024-03-06 00:30:19 +00:00
|
|
|
|
|
|
|
if client.supports_method("textDocument/formatting") then
|
|
|
|
vim.api.nvim_clear_autocmds({group = augroup, buffer = bufnr})
|
|
|
|
vim.api.nvim_create_autocmd("BufWritePre", {
|
|
|
|
group = augroup,
|
|
|
|
buffer = bufnr,
|
2024-04-16 00:16:23 +00:00
|
|
|
callback = function()
|
|
|
|
vim.lsp.buf.format({async = false, timeout_ms = 2000})
|
|
|
|
end
|
2024-03-06 00:30:19 +00:00
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2022-12-03 03:31:37 +00:00
|
|
|
vim.keymap.set('n', 'K', vim.lsp.buf.hover, bufopts)
|
|
|
|
vim.keymap.set('n', '<C-k>', vim.lsp.buf.signature_help, bufopts)
|
2022-12-04 19:32:16 +00:00
|
|
|
vim.keymap.set('n', '<leader>lD', vim.lsp.buf.declaration, bufopts)
|
|
|
|
vim.keymap.set('n', '<leader>ld', vim.lsp.buf.definition, bufopts)
|
|
|
|
vim.keymap.set('n', '<leader>li', vim.lsp.buf.implementation, bufopts)
|
|
|
|
vim.keymap.set('n', '<leader>ln', vim.lsp.buf.rename, bufopts)
|
|
|
|
vim.keymap.set('n', '<leader>lr', vim.lsp.buf.references, bufopts)
|
|
|
|
vim.keymap.set('n', '<leader>lt', vim.lsp.buf.type_definition, bufopts)
|
2024-04-16 00:16:23 +00:00
|
|
|
vim.keymap.set('n', '<leader>lf', function()
|
|
|
|
vim.lsp.buf.format {async = true, timeout_ms = 2000}
|
|
|
|
end, bufopts)
|
2022-11-25 16:30:22 +00:00
|
|
|
end
|
|
|
|
|
2022-12-08 22:32:32 +00:00
|
|
|
local on_attach_no_formatting = function(client, bufnr)
|
2024-03-06 00:30:19 +00:00
|
|
|
-- Disable Formatting
|
2022-12-08 22:32:32 +00:00
|
|
|
client.server_capabilities.documentFormattingProvider = false
|
|
|
|
client.server_capabilities.documentRangeFormattingProvider = false
|
|
|
|
on_attach(client, bufnr)
|
|
|
|
end
|
|
|
|
|
2024-03-06 00:30:19 +00:00
|
|
|
local organize_go_imports = function()
|
|
|
|
local encoding = vim.lsp.util._get_offset_encoding()
|
|
|
|
local params = vim.lsp.util.make_range_params(nil, encoding)
|
|
|
|
params.context = {only = {"source.organizeImports"}}
|
|
|
|
|
|
|
|
local result = vim.lsp.buf_request_sync(0, "textDocument/codeAction",
|
|
|
|
params, 3000)
|
|
|
|
for _, res in pairs(result or {}) do
|
|
|
|
for _, r in pairs(res.result or {}) do
|
|
|
|
if r.edit then
|
|
|
|
vim.lsp.util.apply_workspace_edit(r.edit, encoding)
|
|
|
|
else
|
|
|
|
vim.lsp.buf.execute_command(r.command)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-01 22:00:33 +00:00
|
|
|
-- Define LSP Flags & Capabilities
|
2022-12-03 03:31:37 +00:00
|
|
|
local lsp_flags = {debounce_text_changes = 150}
|
2022-11-25 16:30:22 +00:00
|
|
|
local capabilities = require('cmp_nvim_lsp').default_capabilities()
|
2022-12-01 22:00:33 +00:00
|
|
|
|
|
|
|
-- Python LSP Configuration
|
2022-12-03 03:31:37 +00:00
|
|
|
nvim_lsp.pyright.setup {
|
2022-11-25 16:30:22 +00:00
|
|
|
on_attach = on_attach,
|
|
|
|
flags = lsp_flags,
|
2022-12-03 03:31:37 +00:00
|
|
|
capabilities = capabilities
|
2022-11-25 16:30:22 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 22:00:33 +00:00
|
|
|
-- HTML LSP Configuration
|
2022-12-03 03:31:37 +00:00
|
|
|
nvim_lsp.html.setup {
|
2022-12-08 22:32:32 +00:00
|
|
|
on_attach = on_attach_no_formatting,
|
|
|
|
flags = lsp_flags,
|
|
|
|
capabilities = capabilities,
|
2024-03-05 22:52:30 +00:00
|
|
|
cmd = {nix_vars.vscls .. "/bin/vscode-html-language-server", "--stdio"}
|
2022-12-08 22:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-- JSON LSP Configuration
|
|
|
|
nvim_lsp.jsonls.setup {
|
|
|
|
on_attach = on_attach_no_formatting,
|
|
|
|
flags = lsp_flags,
|
|
|
|
capabilities = capabilities,
|
2024-03-05 22:52:30 +00:00
|
|
|
cmd = {nix_vars.vscls .. "/bin/vscode-html-language-server", "--stdio"}
|
2022-12-08 22:32:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
-- CSS LSP Configuration
|
|
|
|
nvim_lsp.cssls.setup {
|
|
|
|
on_attach = on_attach_no_formatting,
|
2022-11-25 16:30:22 +00:00
|
|
|
flags = lsp_flags,
|
|
|
|
capabilities = capabilities,
|
2024-03-05 22:52:30 +00:00
|
|
|
cmd = {nix_vars.vscls .. "/bin/vscode-html-language-server", "--stdio"}
|
2022-11-25 16:30:22 +00:00
|
|
|
}
|
|
|
|
|
2022-12-01 22:00:33 +00:00
|
|
|
-- Typescript / Javascript LSP Configuration
|
2022-12-03 03:31:37 +00:00
|
|
|
nvim_lsp.tsserver.setup {
|
2022-12-08 22:32:32 +00:00
|
|
|
on_attach = on_attach_no_formatting,
|
2022-11-25 16:30:22 +00:00
|
|
|
flags = lsp_flags,
|
|
|
|
capabilities = capabilities,
|
2024-02-11 16:13:00 +00:00
|
|
|
cmd = {nix_vars.tsls, "--stdio"}
|
2022-11-25 16:30:22 +00:00
|
|
|
}
|
2022-12-08 22:32:32 +00:00
|
|
|
|
2024-02-11 16:13:00 +00:00
|
|
|
-- Svelte LSP Configuration
|
2024-02-17 16:05:12 +00:00
|
|
|
nvim_lsp.svelte.setup {
|
|
|
|
on_attach = on_attach_no_formatting,
|
|
|
|
flags = lsp_flags,
|
|
|
|
capabilities = capabilities,
|
|
|
|
cmd = {nix_vars.sveltels, "--stdio"}
|
|
|
|
}
|
2024-02-11 16:13:00 +00:00
|
|
|
|
2023-08-31 13:03:45 +00:00
|
|
|
-- Go LSP Configuration
|
|
|
|
nvim_lsp.gopls.setup {
|
2024-03-06 00:30:19 +00:00
|
|
|
on_attach = function(client, bufnr)
|
|
|
|
on_attach(client, bufnr)
|
2024-03-12 05:19:51 +00:00
|
|
|
vim.api.nvim_create_autocmd("BufWritePre", {
|
|
|
|
group = augroup,
|
|
|
|
buffer = bufnr,
|
|
|
|
callback = organize_go_imports
|
|
|
|
})
|
2024-03-06 00:30:19 +00:00
|
|
|
end,
|
2023-08-31 13:03:45 +00:00
|
|
|
flags = lsp_flags,
|
|
|
|
capabilities = capabilities,
|
2024-02-11 16:13:00 +00:00
|
|
|
cmd = {nix_vars.gopls}
|
2023-08-31 13:03:45 +00:00
|
|
|
}
|
|
|
|
|
2024-03-13 06:26:11 +00:00
|
|
|
-- Go LSP Linting
|
|
|
|
nvim_lsp.golangci_lint_ls.setup {
|
|
|
|
on_attach = on_attach_no_formatting,
|
|
|
|
flags = lsp_flags,
|
|
|
|
capabilities = capabilities,
|
|
|
|
cmd = {nix_vars.golintls},
|
|
|
|
init_options = {
|
|
|
|
command = {
|
2024-03-14 18:05:02 +00:00
|
|
|
"golangci-lint", "run", "--out-format", "json",
|
|
|
|
"--issues-exit-code=1"
|
2024-03-13 06:26:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-08 22:32:32 +00:00
|
|
|
------------------------------------------------------
|
|
|
|
--------------------- Null-LS LSP --------------------
|
|
|
|
------------------------------------------------------
|
|
|
|
local null_ls = require("null-ls")
|
|
|
|
|
2024-04-16 00:16:23 +00:00
|
|
|
local eslintFiles = {
|
|
|
|
'.eslintrc', '.eslintrc.js', '.eslintrc.cjs', '.eslintrc.yaml',
|
|
|
|
'.eslintrc.yml', '.eslintrc.json', 'eslint.config.js', 'eslint.config.mjs',
|
|
|
|
'eslint.config.cjs', 'eslint.config.ts', 'eslint.config.mts',
|
|
|
|
'eslint.config.cts'
|
|
|
|
}
|
2024-03-05 22:52:30 +00:00
|
|
|
|
2024-04-16 00:16:23 +00:00
|
|
|
has_eslint_in_parents = function(fname)
|
|
|
|
root_file = nvim_lsp.util.insert_package_json(eslintFiles, 'eslintConfig',
|
|
|
|
fname)
|
|
|
|
return nvim_lsp.util.root_pattern(unpack(root_file))(fname)
|
2024-02-06 14:17:44 +00:00
|
|
|
|
2024-04-16 00:16:23 +00:00
|
|
|
end, null_ls.setup({
|
2022-12-08 22:32:32 +00:00
|
|
|
sources = {
|
2024-03-05 22:52:30 +00:00
|
|
|
-- Prettier Formatting
|
2024-02-06 14:17:44 +00:00
|
|
|
null_ls.builtins.formatting.prettier.with({
|
|
|
|
condition = function(utils)
|
2024-04-16 00:16:23 +00:00
|
|
|
return not has_eslint_in_parents(vim.fn.getcwd())
|
2024-03-05 22:52:30 +00:00
|
|
|
end
|
|
|
|
}), -- ESLint Diagnostics & Formatting
|
|
|
|
null_ls.builtins.diagnostics.eslint_d.with({
|
|
|
|
condition = function(utils)
|
2024-04-16 00:16:23 +00:00
|
|
|
return has_eslint_in_parents(vim.fn.getcwd())
|
2024-02-06 14:17:44 +00:00
|
|
|
end
|
2024-03-05 22:52:30 +00:00
|
|
|
}), null_ls.builtins.formatting.eslint_d.with({
|
2024-02-06 14:17:44 +00:00
|
|
|
condition = function(utils)
|
2024-04-16 00:16:23 +00:00
|
|
|
return has_eslint_in_parents(vim.fn.getcwd())
|
2024-02-06 14:17:44 +00:00
|
|
|
end
|
|
|
|
}), null_ls.builtins.formatting.djlint.with({filetypes = {"template"}}),
|
2022-12-08 22:32:32 +00:00
|
|
|
null_ls.builtins.completion.spell,
|
|
|
|
null_ls.builtins.formatting.nixpkgs_fmt,
|
|
|
|
null_ls.builtins.formatting.lua_format,
|
2023-09-08 12:41:16 +00:00
|
|
|
null_ls.builtins.diagnostics.sqlfluff,
|
2022-12-08 22:32:32 +00:00
|
|
|
null_ls.builtins.formatting.sqlfluff
|
|
|
|
},
|
|
|
|
on_attach = function(client, bufnr)
|
|
|
|
if client.supports_method("textDocument/formatting") then
|
|
|
|
vim.api.nvim_clear_autocmds({group = augroup, buffer = bufnr})
|
|
|
|
vim.api.nvim_create_autocmd("BufWritePre", {
|
|
|
|
group = augroup,
|
|
|
|
buffer = bufnr,
|
|
|
|
callback = function()
|
2024-04-16 00:16:23 +00:00
|
|
|
vim.lsp.buf.format({async = false, timeout_ms = 2000})
|
2022-12-08 22:32:32 +00:00
|
|
|
end
|
|
|
|
})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
})
|