nvim: Add json formatting, auto gather filetypes

Added json to be formatted by prettier like the other javascript-close
filetypes.

Switched the static list of filetypes to automatically format on save to
be replaced by automatically gathering all filetypes set up for
formatter.nvim since I want everything formatted anyway.
This commit is contained in:
Marty Oehme 2022-02-13 20:05:31 +01:00
parent 94cd954df9
commit dee44417a6
Signed by: Marty
GPG key ID: B7538B8F50A1C800

View file

@ -2,22 +2,14 @@ local augroup = require('helpers.augroup')
-- for each filetype autoformat on save -- for each filetype autoformat on save
-- TODO can automatically gather from formatter table keys? -- TODO can automatically gather from formatter table keys?
local filetypes =
'bash,cpp,go,html,javascript,lua,python,rust,sh,typescript,zsh'
augroup({
{
'FileType', filetypes, 'autocmd', 'BufWritePost', '<buffer>',
'FormatWrite'
}
}, 'formatonsave')
local prettierfmt = { local prettierfmt = {
function() function()
local set_quotes = "--single-quote"
if vim.bo.filetype == "json" then set_quotes = "--double-quote" end
return { return {
exe = "prettier", exe = "prettier",
args = { args = {
"--stdin-filepath", vim.api.nvim_buf_get_name(0), "--stdin-filepath", vim.api.nvim_buf_get_name(0), set_quotes
'--single-quote'
}, },
stdin = true stdin = true
} }
@ -27,44 +19,44 @@ local shfmt = {
function() return {exe = "shfmt", args = {"-i 4"}, stdin = true} end function() return {exe = "shfmt", args = {"-i 4"}, stdin = true} end
} }
require('formatter').setup({ local formatters = {
logging = false, bash = shfmt,
filetype = { cpp = {
bash = shfmt, function()
cpp = { return {
function() exe = "clang-format",
return { args = {},
exe = "clang-format", stdin = true,
args = {}, cwd = vim.fn.expand('%:p:h') -- Run clang-format in cwd of the file.
stdin = true, }
cwd = vim.fn.expand('%:p:h') -- Run clang-format in cwd of the file. end
} },
end go = {function() return {exe = "goimports", stdin = true} end},
}, html = prettierfmt,
go = {function() return {exe = "goimports", stdin = true} end}, javascript = prettierfmt,
html = prettierfmt, json = prettierfmt,
javascript = prettierfmt, lua = {
lua = { function()
function() return
return { {exe = "lua-format", args = {"--indent-width", 4}, stdin = true}
exe = "lua-format", end
args = {"--indent-width", 4}, },
stdin = true python = {function() return {exe = "black", args = {"-"}, stdin = true} end},
} rust = {
end function()
}, return {exe = "rustfmt", args = {"--emit=stdout"}, stdin = true}
python = { end
function() },
return {exe = "black", args = {"-"}, stdin = true} sh = shfmt,
end typescript = prettierfmt,
}, zsh = shfmt
rust = { }
function()
return {exe = "rustfmt", args = {"--emit=stdout"}, stdin = true} require('formatter').setup({logging = false, filetype = formatters})
end
}, -- gather filetypes to autocorrect for each activated formatter above
sh = shfmt, local filetype = ""
typescript = prettierfmt, for k, _ in pairs(formatters) do filetype = filetype .. "," .. k end
zsh = shfmt augroup({
} {'FileType', filetype, 'autocmd', 'BufWritePost', '<buffer>', 'FormatWrite'}
}) }, 'formatonsave')