nix-config/home/features/coding/nvim/plugin/none-ls.lua
2024-03-03 13:08:33 +01:00

41 lines
1,010 B
Lua

local null_ls = require("null-ls")
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
local opts = {
sources = {
-- Lua
null_ls.builtins.formatting.stylua,
-- Python
null_ls.builtins.formatting.black,
null_ls.builtins.diagnostics.ruff,
-- Javascript
null_ls.builtins.diagnostics.eslint_d,
null_ls.builtins.diagnostics.jsonlint,
-- C/C++
null_ls.builtins.formatting.clang_format,
-- Nix
null_ls.builtins.formatting.alejandra,
-- Rust
null_ls.builtins.formatting.rustfmt,
-- YAML
null_ls.builtins.formatting.yamlfmt,
},
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()
vim.lsp.buf.format({ bufnr = bufnr })
end,
})
end
end,
}
null_ls.setup(opts)
vim.keymap.set("n", "<leader>gf", vim.lsp.buf.format, {})