neovim: add formatting module

This commit is contained in:
2026-03-28 16:44:15 +03:00
parent cf9d5110fd
commit b92750688e
4 changed files with 89 additions and 1 deletions

View File

@@ -24,6 +24,10 @@ null_ls.setup({
pattern = "*",
group = group,
callback = function()
if not require("formatting").is_enabled(vim.api.nvim_get_current_buf()) then
return
end
vim.lsp.buf.format({
filter = function(client)
return not vim.tbl_contains(servers_with_annoying_formatters, client.name)

View File

@@ -1,5 +1,6 @@
local lualine = require("lualine")
local devicons = require("nvim-web-devicons")
local formatting = require("formatting")
local mode = {
"mode",
@@ -22,6 +23,17 @@ local relative_filename = {
path = 1,
}
local formatting_status = {
function()
local is_formatting_enabled = formatting.is_enabled(vim.api.nvim_get_current_buf())
if is_formatting_enabled then
return "[f+]"
else
return "[f-]"
end
end,
}
local searchcount = {
"searchcount",
}
@@ -128,7 +140,7 @@ lualine.setup({
lualine_a = { mode },
lualine_b = { branch },
lualine_c = { diagnostics, relative_filename },
lualine_x = { macro_recording, searchcount, location, tabstop, fileformat },
lualine_x = { macro_recording, searchcount, formatting_status, location, tabstop, fileformat },
lualine_y = { filetype },
lualine_z = {},
},

View File

@@ -62,3 +62,5 @@ pack.add({
require("catppuccin").setup({})
pack.register_user_commands()
require("formatting").register_user_commands()

View File

@@ -0,0 +1,70 @@
--[[
-- this file breaks my brain with all of these booleans, but it seems to work
--]]
local M = {
-- false = formatting enabled for all buffers
-- true = formatting disabled for all buffers
-- number[] = formatting disabled for only these buffers
---@type true | false | number[]
_disabled_buffers = false,
}
---@param buf number
---@return boolean, boolean
--- returns 2 booleans: enabled for all buffers, enabled for this buffer
function M._is_enabled(buf)
if M._disabled_buffers == true then
return false, false
end
if type(M._disabled_buffers) == "table" then
local enabled = not vim.tbl_contains(M._disabled_buffers, buf)
return true, enabled
end
return true, true
end
---@param buf number
function M.is_enabled(buf)
local _, enabled = M._is_enabled(buf)
return enabled
end
function M.register_user_commands()
vim.api.nvim_create_user_command("FormattingEnable", function()
M._disabled_buffers = false
end, {})
vim.api.nvim_create_user_command("FormattingEnableBuf", function()
local current_buf = vim.api.nvim_get_current_buf()
if type(M._disabled_buffers) == "table" then
local new_table = {}
for _, b in ipairs(M._disabled_buffers) do
if b ~= current_buf then
table.insert(new_table, b)
end
end
M._disabled_buffers = new_table
else
M._disabled_buffers = false
end
end, {})
vim.api.nvim_create_user_command("FormattingDisable", function()
M._disabled_buffers = true
end, {})
vim.api.nvim_create_user_command("FormattingDisableBuf", function()
local current_buf = vim.api.nvim_get_current_buf()
if type(M._disabled_buffers) == "table" then
table.insert(M._disabled_buffers, current_buf)
else
M._disabled_buffers = { current_buf }
end
end, {})
end
return M