kickstart.nvim/lua/utils/formatting.lua

33 lines
874 B
Lua

local M = {}
---filter passed to vim.lsp.buf.format
---always selects null-ls if it's available and caches the value per buffer
---@param client table client attached to a buffer
---@return boolean if client matches
function M.format_filter(client)
local filetype = vim.bo.filetype
local n = require('null-ls')
local s = require('null-ls.sources')
local method = n.methods.FORMATTING
local available_formatters = s.get_available(filetype, method)
if #available_formatters > 0 then
return client.name == 'null-ls'
elseif client.supports_method('textDocument/formatting') then
return true
else
return false
end
end
---Simple wrapper for vim.lsp.buf.format() to provide defaults
---@param opts table|nil
function M.format(opts)
opts = opts or {}
opts.filter = opts.filter or M.format_filter
return vim.lsp.buf.format(opts)
end
return M