diff --git a/.config/nvim/after/plugin/null-ls.rc.lua b/.config/nvim/after/plugin/null-ls.rc.lua new file mode 100644 index 0000000..549834c --- /dev/null +++ b/.config/nvim/after/plugin/null-ls.rc.lua @@ -0,0 +1,42 @@ +local status, null_ls = pcall(require, "null-ls") +if not status then + return +end + +local augroup = vim.api.nvim_create_augroup("LspFormatting", {}) + +local lsp_formatting = function(bufnr) + vim.lsp.buf.format({ + filter = function(client) + return client.name == "null-ls" + end, + bufnr = bufnr, + }) +end + +null_ls.setup({ + sources = { + null_ls.builtins.formatting.prettierd, + null_ls.builtins.formatting.autopep8, + null_ls.builtins.formatting.stylua, + }, + 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() + lsp_formatting(bufnr) + end, + }) + end + end, +}) + +vim.api.nvim_create_user_command("DisableLspFormatting", function() + vim.api.nvim_clear_autocmds({ group = augroup, buffer = 0 }) +end, { nargs = 0 })