From dee44417a61b9989402990e85d04fc3e6512ecef Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sun, 13 Feb 2022 20:05:31 +0100 Subject: [PATCH] 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. --- nvim/.config/nvim/lua/plug/_format.lua | 96 ++++++++++++-------------- 1 file changed, 44 insertions(+), 52 deletions(-) diff --git a/nvim/.config/nvim/lua/plug/_format.lua b/nvim/.config/nvim/lua/plug/_format.lua index abc2630..815e837 100644 --- a/nvim/.config/nvim/lua/plug/_format.lua +++ b/nvim/.config/nvim/lua/plug/_format.lua @@ -2,22 +2,14 @@ local augroup = require('helpers.augroup') -- for each filetype autoformat on save -- 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', '', - 'FormatWrite' - } -}, 'formatonsave') - 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), - '--single-quote' + "--stdin-filepath", vim.api.nvim_buf_get_name(0), set_quotes }, stdin = true } @@ -27,44 +19,44 @@ local shfmt = { function() return {exe = "shfmt", args = {"-i 4"}, stdin = true} end } -require('formatter').setup({ - logging = false, - filetype = { - 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, - 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 - } -}) +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}) + +-- gather filetypes to autocorrect for each activated formatter above +local filetype = "" +for k, _ in pairs(formatters) do filetype = filetype .. "," .. k end +augroup({ + {'FileType', filetype, 'autocmd', 'BufWritePost', '', 'FormatWrite'} +}, 'formatonsave')