Skip to content

Commit

Permalink
feat: add effective shiftwidth to conform.Context
Browse files Browse the repository at this point in the history
this also refactors formatters that automatically set indentation level
to use the new shiftwidth context
  • Loading branch information
mehalter committed Jun 28, 2024
1 parent 57dc139 commit 6d457e2
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 27 deletions.
7 changes: 1 addition & 6 deletions lua/conform/formatters/djlint.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 1 addition & 9 deletions lua/conform/formatters/markdown-toc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
9 changes: 2 additions & 7 deletions lua/conform/formatters/shfmt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions lua/conform/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down Expand Up @@ -482,6 +487,7 @@ M.build_context = function(bufnr, config, range)
filename = filename,
dirname = dirname,
range = range,
shiftwidth = shiftwidth,
}
end

Expand Down
1 change: 1 addition & 0 deletions lua/conform/types.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 30 additions & 5 deletions tests/runner_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 6d457e2

Please sign in to comment.