dotfiles/nvim/.config/nvim/lua/plug/_format.lua
Marty Oehme b33ae09a33
nvim: Disable automatic formatting on save
Had to disable automatic formatting since it was messing with my
contributions to other git projects, if they did either not have a
formatter enabled (most of the time) or had different formatters or
those set up differently than this setup (fixable, but I don't think
it's worth the time).

Instead, formatting can be invoked with `<localleader>f`/`F` to format
or format and save respectively.
2022-12-08 13:28:00 +01:00

65 lines
2.0 KiB
Lua

-- for each filetype autoformat on save
-- TODO can automatically gather from formatter table keys?
local prettierfmt = {
function()
local set_quotes = "--single-quote"
if vim.bo.filetype == "json" then set_quotes = "--double-quote" end
return {
exe = "prettier",
args = {
"--stdin-filepath", vim.api.nvim_buf_get_name(0), set_quotes
},
stdin = true
}
end
}
local shfmt = {
function() return { exe = "shfmt", args = { "-i 4" }, stdin = true } end
}
local formatters = {
bash = shfmt,
cpp = {
function()
return {
exe = "clang-format",
args = {},
stdin = true,
cwd = vim.fn.expand('%:p:h') -- Run clang-format in cwd of the file.
}
end
},
go = { function() return { exe = "goimports", stdin = true } end },
html = prettierfmt,
javascript = prettierfmt,
json = prettierfmt,
lua = {
function()
return { exe = "lua-format", args = { "--indent-width", 4 }, stdin = true }
end
},
python = { function() return { exe = "black", args = { "-" }, stdin = true } end },
rust = {
function()
return { exe = "rustfmt", args = { "--emit=stdout" }, stdin = true }
end
},
sh = shfmt,
typescript = prettierfmt,
zsh = shfmt
}
require('formatter').setup({ logging = false, filetype = formatters })
-- Format on save:
-- DISABLED FOR NOW, due to messing with git contributions if they
-- do not use a formatter. Instead, formatting with key mapping used.
-- gather filetypes to autocorrect for each activated formatter above
-- for k, _ in pairs(formatters) do
-- vim.api.nvim_create_autocmd({"Filetype " .. k}, {
-- command = "autocmd BufWritePost <buffer> FormatWrite",
-- desc = "Automatically format on write",
-- group = vim.api.nvim_create_augroup('formatonsave', {clear = true})
-- })
-- end