53 lines
1.2 KiB
Lua
53 lines
1.2 KiB
Lua
-- https://github.com/LazyVim/LazyVim/blob/main/lua/lazyvim/util/format.lua
|
|
|
|
---@class lib.lazyvim.format
|
|
local M = {}
|
|
|
|
---@param buf? number The buffer to enable for
|
|
function M.enabled(buf)
|
|
buf = (buf == nil or buf == 0) and vim.api.nvim_get_current_buf() or buf
|
|
local gaf = vim.g.autoformat
|
|
local baf = vim.b[buf].autoformat
|
|
|
|
-- If the buffer has a local value, use that.
|
|
if baf ~= nil then
|
|
return baf
|
|
end
|
|
|
|
-- Otherwise use the global value if set, or true by default.
|
|
return gaf == nil or gaf
|
|
end
|
|
|
|
---@param enable? boolean Whether to enable or disable
|
|
---@param buf? boolean Whether to enable for current buffer only
|
|
function M.enable(enable, buf)
|
|
if enable == nil then
|
|
enable = true
|
|
end
|
|
|
|
if buf then
|
|
vim.b.autoformat = enable
|
|
else
|
|
vim.g.autoformat = enable
|
|
vim.b.autoformat = nil
|
|
end
|
|
end
|
|
|
|
---@param buf? boolean Whether to toggle for current buffer only
|
|
function M.snacks_toggle(buf)
|
|
return Snacks.toggle({
|
|
name = 'auto format (' .. (buf and 'buffer' or 'global') .. ')',
|
|
get = function()
|
|
if not buf then
|
|
return vim.g.autoformat == nil or vim.g.autoformat
|
|
end
|
|
|
|
return M.enabled()
|
|
end,
|
|
set = function(state)
|
|
M.enable(state, buf)
|
|
end,
|
|
})
|
|
end
|
|
|
|
return M
|