81 lines
2.2 KiB
Lua
81 lines
2.2 KiB
Lua
local shfmt = function()
|
|
local indent = 0 -- Assume tabs initially.
|
|
if vim.opt.expandtab:get() then
|
|
local shiftwidth = vim.opt.shiftwidth:get()
|
|
if shiftwidth == 0 then
|
|
indent = vim.opt.tabstop:get()
|
|
else
|
|
indent = shiftwidth
|
|
end
|
|
end
|
|
|
|
return {
|
|
exe = "shfmt",
|
|
-- stylua: ignore start
|
|
args = {
|
|
"--indent", indent, -- 0 for tabs, >0 for number of spaces.
|
|
"--keep-padding", -- Keep column alignment paddings.
|
|
},
|
|
-- stylua: ignore end
|
|
stdin = true,
|
|
}
|
|
end
|
|
|
|
local toggle_format_on_write = (function()
|
|
local augroup_id = nil
|
|
return function()
|
|
if augroup_id then
|
|
vim.api.nvim_del_augroup_by_id(augroup_id)
|
|
augroup_id = nil
|
|
vim.notify("Format on write DISABLED", vim.log.levels.WARN)
|
|
else
|
|
augroup_id = vim.api.nvim_create_augroup("fschauen.format_on_write", { clear = true })
|
|
vim.api.nvim_create_autocmd("BufWritePost", {
|
|
desc = "Format files on write.",
|
|
group = augroup_id,
|
|
pattern = "*",
|
|
command = ":FormatWrite",
|
|
})
|
|
vim.notify("Format on write enabled", vim.log.levels.INFO)
|
|
end
|
|
end
|
|
end)()
|
|
|
|
return {
|
|
"mhartington/formatter.nvim",
|
|
|
|
cmd = {
|
|
"Format",
|
|
"FormatLock",
|
|
"FormatWrite",
|
|
"FormatWriteLock",
|
|
},
|
|
|
|
keys = {
|
|
-- stylua: ignore start
|
|
{ "<leader>FT", toggle_format_on_write, desc = " [F]ormat on write [T]oggle" },
|
|
{ "<leader>FF", "<cmd>Format<cr>", desc = " [F]ormat [F]ile" },
|
|
{ "<c-f>", "<cmd>Format<cr>", mode = "i", desc = " [f]ormat file" },
|
|
-- stylua: ignore end
|
|
},
|
|
|
|
opts = function()
|
|
local builtin = require("formatter.filetypes")
|
|
return {
|
|
filetype = {
|
|
-- stylua: ignore start
|
|
c = { builtin.c.clangformat },
|
|
cmake = { builtin.cmake.cmakeformat },
|
|
cpp = { builtin.cpp.clangformat },
|
|
cs = { builtin.cs.clangformat },
|
|
json = { builtin.cs.prettier },
|
|
lua = { builtin.lua.stylua },
|
|
markdown = { builtin.markdown.prettier },
|
|
python = {}, -- TODO: pick one
|
|
sh = { shfmt() },
|
|
zsh = { builtin.zsh.beautysh },
|
|
-- stylua: ignore end
|
|
},
|
|
}
|
|
end,
|
|
}
|