nvim-config11/lua/plugins/lsp.lua

116 lines
4 KiB
Lua
Raw Normal View History

2025-03-27 08:18:20 +00:00
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
local format_on_save = 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()
-- prevents format from being included in the undo history
-- this way the cursor doesn't jump around the file when
-- undoing
vim.cmd("silent! undojoin")
2025-03-27 08:18:20 +00:00
vim.lsp.buf.format({
bufnr = bufnr,
async = false,
})
end,
})
end
end
vim.api.nvim_create_autocmd('LspAttach', {
callback = function(args)
local client = vim.lsp.get_client_by_id(args.data.client_id)
if not client then return end
2025-03-29 20:01:39 +00:00
local LSP_CLIENT_NAMES_FORMAT_ON_SAVE = { "lua_ls", "jdtls", "gopls", "taplo", "yamlls" }
2025-03-27 12:48:32 +00:00
for _, client_name_autoformat in ipairs(LSP_CLIENT_NAMES_FORMAT_ON_SAVE) do
if client.name == client_name_autoformat and client.server_capabilities.documentFormattingProvider then
2025-03-27 12:48:32 +00:00
-- Format the current buffer on save
vim.api.nvim_create_autocmd('BufWritePre', {
buffer = args.buf,
callback = function()
vim.lsp.buf.format({ bufnr = args.buf, id = client.id })
end,
})
end
2025-03-27 08:18:20 +00:00
end
end,
})
vim.diagnostic.config({
virtual_lines = {
current_line = true
}
2025-03-27 08:18:20 +00:00
})
return {
2025-03-31 11:19:48 +01:00
{
"neovim/nvim-lspconfig",
config = function()
local lspconfig = require('lspconfig')
2025-04-04 14:59:29 +01:00
local capabilities = vim.lsp.protocol.make_client_capabilities()
capabilities.textDocument.foldingRange = {
dynamicRegistration = false,
lineFoldingOnly = true
}
2025-03-31 11:19:48 +01:00
for _, v in ipairs(vim.api.nvim_get_runtime_file('lua/lsp/*', true)) do
local name = vim.fn.fnamemodify(v, ':t:r')
local cfg = require("lsp/" .. name)
2025-04-04 14:59:29 +01:00
lspconfig[name].setup(vim.tbl_deep_extend("force", cfg, { capabilities }))
2025-03-31 11:19:48 +01:00
end
end
},
2025-03-27 08:18:20 +00:00
{
"williamboman/mason.nvim",
2025-03-27 08:18:20 +00:00
opts = {
tools_to_install = {
"lua-language-server", "vtsls", "ruff", "mypy", "black",
2025-07-16 20:14:33 +01:00
"pyright", "emmet-language-server", "tailwindcss-language-server",
2025-03-27 12:48:32 +00:00
"eslint-lsp", "lemminx", "gopls", "prettierd", "dotenv-linter",
2025-04-01 18:05:47 +01:00
"editorconfig-checker", "rust-analyzer", "taplo",
"json-lsp", "harper-ls", "proselint", "alex"
2025-03-27 08:18:20 +00:00
}
},
config = function(_, opts)
require("mason").setup()
local mason_registry = require("mason-registry")
mason_registry.refresh()
for _, tool in pairs(opts.tools_to_install) do
local package = mason_registry.get_package(tool)
if not package:is_installed() then
package:install()
end
end
end
},
{
"nvimtools/none-ls.nvim",
dependencies = { "nvimtools/none-ls-extras.nvim" },
config = function()
2025-03-27 08:18:20 +00:00
local null_ls = require("null-ls")
null_ls.setup({
on_attach = function(client, bufnr)
format_on_save(client, bufnr)
end,
sources = {
2025-03-27 12:48:32 +00:00
null_ls.builtins.formatting.prettierd,
null_ls.builtins.diagnostics.dotenv_linter,
null_ls.builtins.diagnostics.editorconfig_checker,
2025-04-01 18:05:47 +01:00
null_ls.builtins.diagnostics.proselint,
null_ls.builtins.diagnostics.alex
2025-03-27 08:18:20 +00:00
},
})
end
}
}