dotfiles/nvim/.config/nvim/lua/plugins/formatting.lua
Marty Oehme c98fa26e91
nvim: Create single source of truth for language features
All additional languages features (LSPs, treesitter parsers, linters and
formatters) are now defined in a single place in 'core/languages'.

This file simply sets up a big table which contains all the enabled
programs and parsers, divided by type. They adhere to the structure
given by the respective plugin.

HACK: We are still cheating a bit currently for treesitter parsers since
I have not had the heart to go through all of them to
activate/deactivate what I could need. Most of them are simply still
loaded, not connected to a specific language. Will have to be sorted out
at some point but it is good enough for now.
2025-03-15 20:08:47 +01:00

88 lines
2 KiB
Lua

local formatters = {}
for _, lang in pairs(Languages) do
if not lang.format then
goto continue
end
for ft, val in pairs(lang.format) do
formatters[ft] = val
end
::continue::
end
return {
-- formatting setup
{
"zapling/mason-conform.nvim",
dependencies = {
{ "williamboman/mason.nvim" },
{
"stevearc/conform.nvim",
config = function()
require("conform").setup({
formatters_by_ft = formatters,
lsp_format = "fallback",
format_after_save = function(bufnr)
if vim.g.disable_autoformat or vim.b[bufnr].disable_autoformat then
return
end
return { lsp_fallback = true }
end,
})
require("conform").formatters.prettier = {
options = {
ext_parsers = {
qmd = "markdown",
},
},
}
vim.api.nvim_create_user_command("FormatDisable", function(args)
if args.bang then
vim.g.disable_autoformat = true
else
-- FormatDisable! will disable formatting globally
vim.b.disable_autoformat = true
end
end, {
desc = "Disable formatting on save",
bang = true,
})
vim.api.nvim_create_user_command("FormatEnable", function()
vim.b.disable_autoformat = false
vim.g.disable_autoformat = false
end, {
desc = "Enable formatting on save",
})
end,
cmd = { "ConformInfo" },
keys = {
{
"<localleader>ll",
function()
require("conform").format({ async = true, lsp_fallback = true })
end,
mode = { "n", "v" },
desc = "Format buffer",
},
{
"<localleader>lL",
function()
vim.g.disable_autoformat = not vim.g.disable_autoformat
end,
desc = "Toggle AutoFormat",
},
{
"<leader>vf",
":ConformInfo<cr>",
desc = "ConformInfo",
},
},
init = function()
-- If you want the formatexpr, here is the place to set it
vim.o.formatexpr = "v:lua.require'conform'.formatexpr()"
end,
},
},
event = { "BufWritePre" },
opts = {},
},
}