Iterating on null-ls formatting

Had to disable the format on save since it was fighting with some other
linter.
This commit is contained in:
Tyler Hallada 2022-10-13 16:55:34 -04:00
parent 7b6f148302
commit c532e5f2f0
2 changed files with 51 additions and 3 deletions

View File

@ -22,6 +22,7 @@ local on_attach = function(_, bufnr)
vim.keymap.set('v', '<leader>a', vim.lsp.buf.code_action, bufopts) vim.keymap.set('v', '<leader>a', vim.lsp.buf.code_action, bufopts)
vim.keymap.set('n', 'ga', vim.lsp.buf.code_action, bufopts) vim.keymap.set('n', 'ga', vim.lsp.buf.code_action, bufopts)
vim.keymap.set('n', '<leader>f', function() vim.lsp.buf.format { async = true } end, bufopts) vim.keymap.set('n', '<leader>f', function() vim.lsp.buf.format { async = true } end, bufopts)
vim.keymap.set('n', '<leader>F', function() vim.lsp.buf.format { async = true } end, bufopts)
-- vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts) -- vim.keymap.set('n', 'gr', vim.lsp.buf.references, bufopts)
-- vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts) -- vim.keymap.set('n', 'gd', vim.lsp.buf.definition, bufopts)
-- vim.keymap.set('n', '<leader>D', vim.lsp.buf.type_definition, bufopts) -- vim.keymap.set('n', '<leader>D', vim.lsp.buf.type_definition, bufopts)

View File

@ -1,10 +1,57 @@
local null_ls = require('null-ls') local null_ls = require("null-ls")
-- From: https://github.com/jose-elias-alvarez/null-ls.nvim/wiki/Formatting-on-save
local async_formatting = function(bufnr)
bufnr = bufnr or vim.api.nvim_get_current_buf()
vim.lsp.buf_request(
bufnr,
"textDocument/formatting",
vim.lsp.util.make_formatting_params({}),
function(err, res, ctx)
if err then
local err_msg = type(err) == "string" and err or err.message
-- you can modify the log message / level (or ignore it completely)
vim.notify("formatting: " .. err_msg, vim.log.levels.WARN)
return
end
-- don't apply results if buffer is unloaded or has been modified
if not vim.api.nvim_buf_is_loaded(bufnr) or vim.api.nvim_buf_get_option(bufnr, "modified") then
return
end
if res then
local client = vim.lsp.get_client_by_id(ctx.client_id)
vim.lsp.util.apply_text_edits(res, bufnr, client and client.offset_encoding or "utf-16")
vim.api.nvim_buf_call(bufnr, function()
vim.cmd("silent noautocmd update")
end)
end
end
)
end
local augroup = vim.api.nvim_create_augroup("LspFormatting", {})
null_ls.setup({ null_ls.setup({
-- thallada: broken
-- 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("BufWritePost", {
-- group = augroup,
-- buffer = bufnr,
-- callback = function()
-- async_formatting(bufnr)
-- end,
-- })
-- end
-- end,
sources = { sources = {
-- Linter -- Linter
-- null_ls.builtins.diagnostics.eslint_d, -- null_ls.builtins.diagnostics.eslint_d,
null_ls.builtins.diagnostics.jsonlint, -- null_ls.builtins.diagnostics.jsonlint,
null_ls.builtins.diagnostics.markdownlint, null_ls.builtins.diagnostics.markdownlint,
null_ls.builtins.diagnostics.stylelint, null_ls.builtins.diagnostics.stylelint,
null_ls.builtins.diagnostics.tidy, null_ls.builtins.diagnostics.tidy,
@ -15,7 +62,7 @@ null_ls.setup({
-- Formatter -- Formatter
null_ls.builtins.formatting.eslint_d, null_ls.builtins.formatting.eslint_d,
null_ls.builtins.formatting.markdownlint, null_ls.builtins.formatting.markdownlint,
null_ls.builtins.formatting.prettierd, -- null_ls.builtins.formatting.prettierd,
null_ls.builtins.formatting.sqlfluff.with({ null_ls.builtins.formatting.sqlfluff.with({
extra_args = { "--dialect", "postgres" }, extra_args = { "--dialect", "postgres" },
}), }),