86 lines
2.5 KiB
Lua
86 lines
2.5 KiB
Lua
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()
|
|
vim.lsp.buf.format({
|
|
bufnr = bufnr,
|
|
async = false,
|
|
})
|
|
end,
|
|
})
|
|
end
|
|
end
|
|
|
|
local configs = {}
|
|
for _, v in ipairs(vim.api.nvim_get_runtime_file('lsp/*', true)) do
|
|
local name = vim.fn.fnamemodify(v, ':t:r')
|
|
configs[name] = true
|
|
end
|
|
vim.lsp.enable(vim.tbl_keys(configs))
|
|
|
|
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
|
|
|
|
if vim.bo.filetype == "lua" or client.name == "jdtls" then
|
|
-- 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
|
|
end,
|
|
})
|
|
|
|
vim.diagnostic.config({
|
|
virtual_lines = true
|
|
})
|
|
|
|
return {
|
|
{
|
|
"williamboman/mason.nvim",
|
|
opts = {
|
|
tools_to_install = {
|
|
"lua-language-server", "vtsls", "ruff", "mypy", "black", "pyright", "tailwindcss-language-server",
|
|
"eslint-lsp", "lemminx", "gopls"
|
|
}
|
|
},
|
|
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",
|
|
config = function(_, opts)
|
|
local null_ls = require("null-ls")
|
|
null_ls.setup({
|
|
on_attach = function(client, bufnr)
|
|
format_on_save(client, bufnr)
|
|
end,
|
|
sources = {
|
|
null_ls.builtins.formatting.prettier,
|
|
},
|
|
})
|
|
end
|
|
}
|
|
|
|
|
|
}
|