diff --git a/lua/conform/formatters/djlint.lua b/lua/conform/formatters/djlint.lua index f02cb0a2..13c966d2 100644 --- a/lua/conform/formatters/djlint.lua +++ b/lua/conform/formatters/djlint.lua @@ -7,12 +7,7 @@ return { }, command = "djlint", args = function(_, ctx) - local bo = vim.bo[ctx.buf] - local indent_size = bo.shiftwidth - if indent_size == 0 or not indent_size then - indent_size = bo.tabstop or 4 -- default is 4 - end - return { "--reformat", "--indent", indent_size, "-" } + return { "--reformat", "--indent", ctx.shiftwidth, "-" } end, cwd = util.root_file({ ".djlintrc", diff --git a/lua/conform/formatters/markdown-toc.lua b/lua/conform/formatters/markdown-toc.lua index 7f9705e3..624e9ce5 100644 --- a/lua/conform/formatters/markdown-toc.lua +++ b/lua/conform/formatters/markdown-toc.lua @@ -9,15 +9,7 @@ return { args = function(_, ctx) -- use the indentation set in the current buffer, effectively allowing us to -- use values from .editorconfig - local indent = "\t" - local bo = vim.bo[ctx.buf] - if bo.expandtab then - local indent_size = bo.shiftwidth - if indent_size == 0 or not indent_size then - indent_size = bo.tabstop or 4 -- default is 4 - end - indent = (" "):rep(indent_size) - end + local indent = vim.bo[ctx.buf].expandtab and "\t" or (" "):rep(ctx.shiftwidth) return { "--indent=" .. indent, "-i", "$FILENAME" } end, } diff --git a/lua/conform/formatters/shfmt.lua b/lua/conform/formatters/shfmt.lua index f8bb2c63..6a75ff91 100644 --- a/lua/conform/formatters/shfmt.lua +++ b/lua/conform/formatters/shfmt.lua @@ -7,13 +7,8 @@ return { command = "shfmt", args = function(_, ctx) local args = { "-filename", "$FILENAME" } - local bo = vim.bo[ctx.buf] - if bo.expandtab then - local indent_size = bo.shiftwidth - if indent_size == 0 or not indent_size then - indent_size = bo.tabstop or 2 - end - vim.list_extend(args, { "-i", tostring(indent_size) }) + if vim.bo[ctx.buf].expandtab then + vim.list_extend(args, { "-i", ctx.shiftwidth }) end return args end, diff --git a/lua/conform/runner.lua b/lua/conform/runner.lua index 38311983..c0a19cd3 100644 --- a/lua/conform/runner.lua +++ b/lua/conform/runner.lua @@ -450,6 +450,11 @@ M.build_context = function(bufnr, config, range) end local filename = vim.api.nvim_buf_get_name(bufnr) + local shiftwidth = vim.bo[bufnr].shiftwidth + if shiftwidth == 0 then + shiftwidth = vim.bo[bufnr].tabstop + end + -- Hack around checkhealth. For buffers that are not files, we need to fabricate a filename if vim.bo[bufnr].buftype ~= "" then filename = "" @@ -482,6 +487,7 @@ M.build_context = function(bufnr, config, range) filename = filename, dirname = dirname, range = range, + shiftwidth = shiftwidth, } end diff --git a/lua/conform/types.lua b/lua/conform/types.lua index b00c3f0b..05a4e4f6 100644 --- a/lua/conform/types.lua +++ b/lua/conform/types.lua @@ -49,6 +49,7 @@ ---@field filename string ---@field dirname string ---@field range? conform.Range +---@field shiftwidth integer ---@class (exact) conform.RangeContext : conform.Context ---@field range conform.Range diff --git a/tests/runner_spec.lua b/tests/runner_spec.lua index 8c0dadfb..41446402 100644 --- a/tests/runner_spec.lua +++ b/tests/runner_spec.lua @@ -59,11 +59,36 @@ describe("runner", function() local config = assert(conform.get_formatter_config("test")) local ctx = runner.build_context(0, config) local filename = vim.api.nvim_buf_get_name(bufnr) - assert.are.same({ - buf = bufnr, - filename = filename, - dirname = vim.fs.dirname(filename), - }, ctx) + assert.equal(bufnr, ctx.buf) + assert.equal(filename, ctx.filename) + assert.equal(vim.fs.dirname(filename), ctx.dirname) + end) + + it("sets the shiftwidth to shiftwidth", function() + vim.cmd.edit({ args = { "README.md" } }) + local bufnr = vim.api.nvim_get_current_buf() + vim.bo[bufnr].shiftwidth = 7 + conform.formatters.test = { + meta = { url = "", description = "" }, + command = "echo", + } + local config = assert(conform.get_formatter_config("test")) + local ctx = runner.build_context(0, config) + assert.equal(7, ctx.shiftwidth) + end) + + it("sets the shiftwidth to tabstop when necessary", function() + vim.cmd.edit({ args = { "README.md" } }) + local bufnr = vim.api.nvim_get_current_buf() + vim.bo[bufnr].shiftwidth = 0 + vim.bo[bufnr].tabstop = 3 + conform.formatters.test = { + meta = { url = "", description = "" }, + command = "echo", + } + local config = assert(conform.get_formatter_config("test")) + local ctx = runner.build_context(0, config) + assert.equal(3, ctx.shiftwidth) end) it("sets temp file when stdin = false", function()