From 24fe53f1cc7b2f35040efe94984906a17f53b197 Mon Sep 17 00:00:00 2001 From: Fernando Schauenburg Date: Fri, 19 Jul 2024 22:39:44 +0200 Subject: [PATCH] nvim: fix handling of tabs/spaces for `shfmt` formatter --- .../nvim/lua/fschauen/plugins/formatter.lua | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/config/nvim/lua/fschauen/plugins/formatter.lua b/config/nvim/lua/fschauen/plugins/formatter.lua index ccd4a0c..ee0eb63 100644 --- a/config/nvim/lua/fschauen/plugins/formatter.lua +++ b/config/nvim/lua/fschauen/plugins/formatter.lua @@ -1,3 +1,26 @@ +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 + return { "mhartington/formatter.nvim", @@ -28,7 +51,7 @@ return { lua = { builtin.lua.stylua }, markdown = { builtin.markdown.prettier }, python = {}, -- TODO: pick one - sh = { builtin.sh.shfmt }, + sh = { shfmt() }, zsh = { builtin.zsh.beautysh }, -- stylua: ignore end },