nixos-configs/home-manager/neovim/none-ls.lua
Pim Kunis 3b9b7d3c01 replace efm with none-ls
enable black formatter for black
enable stylua formatter for lua
update nix flake inputs
format lua files
2023-11-30 16:59:49 +01:00

51 lines
1.2 KiB
Lua

-- renamed to none-ls
local null_ls_status_ok, null_ls = pcall(require, "null-ls")
if not null_ls_status_ok then
return
end
local formatting = null_ls.builtins.formatting
local diagnostics = null_ls.builtins.diagnostics
local code_actions = null_ls.builtins.code_actions
-- to setup format on save
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
require("null-ls").setup({
sources = {
formatting.stylua,
formatting.black,
},
-- configure format on save
on_attach = function(current_client, bufnr)
if current_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({
filter = function(client)
-- only use null-ls for formatting instead of lsp server
return client.name == "null-ls"
end,
bufnr = bufnr,
})
end,
})
end
end,
})
-- formatting command
vim.api.nvim_create_user_command("Format", function()
vim.lsp.buf.format(nil, 10000)
end, {})
vim.keymap.set(
"n",
"<leader>fm",
":Format<CR>",
{ desc = "Format current buffer (also done on save)", noremap = true, silent = true }
)