aboutsummaryrefslogtreecommitdiffstats
path: root/lua
diff options
context:
space:
mode:
authorMicah Halter <micah@mehalter.com>2024-07-01 13:58:55 -0400
committerGitHub <noreply@github.com>2024-07-01 10:58:55 -0700
commitcd75be867f2331b22905f47d28c0c270a69466aa (patch)
tree026bb2bd83bacad4e12e590b67073d4472197733 /lua
parentfc19dfc0e7d0f5e9f01cd42a4c518e030b3790f0 (diff)
feat(shfmt): add automatic indentation detection (#481)
* feat(shfmt): automatically detect indentation * fix(markdown-toc): improve correctness of indentation size calculation * fix(djlint): improve correctness of indentation size calculation * feat: add effective `shiftwidth` to `conform.Context` this also refactors formatters that automatically set indentation level to use the new shiftwidth context
Diffstat (limited to 'lua')
-rw-r--r--lua/conform/formatters/djlint.lua3
-rw-r--r--lua/conform/formatters/markdown-toc.lua4
-rw-r--r--lua/conform/formatters/shfmt.lua8
-rw-r--r--lua/conform/runner.lua6
-rw-r--r--lua/conform/types.lua1
5 files changed, 17 insertions, 5 deletions
diff --git a/lua/conform/formatters/djlint.lua b/lua/conform/formatters/djlint.lua
index b7a177b..13c966d 100644
--- a/lua/conform/formatters/djlint.lua
+++ b/lua/conform/formatters/djlint.lua
@@ -7,8 +7,7 @@ return {
},
command = "djlint",
args = function(_, ctx)
- local indent = vim.bo[ctx.buf].tabstop or 4 -- default is 4
- return { "--reformat", "--indent", indent, "-" }
+ 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 a7a9694..5e1d80a 100644
--- a/lua/conform/formatters/markdown-toc.lua
+++ b/lua/conform/formatters/markdown-toc.lua
@@ -6,10 +6,10 @@ return {
},
command = "markdown-toc",
stdin = false,
- args = function(self, ctx)
+ args = function(_, ctx)
-- use the indentation set in the current buffer, effectively allowing us to
-- use values from .editorconfig
- local indent = vim.bo[ctx.buf].expandtab and (" "):rep(vim.bo[ctx.buf].tabstop) or "\t"
+ local indent = vim.bo[ctx.buf].expandtab and (" "):rep(ctx.shiftwidth) or "\t"
return { "--indent=" .. indent, "-i", "$FILENAME" }
end,
}
diff --git a/lua/conform/formatters/shfmt.lua b/lua/conform/formatters/shfmt.lua
index 29b8615..6a75ff9 100644
--- a/lua/conform/formatters/shfmt.lua
+++ b/lua/conform/formatters/shfmt.lua
@@ -5,5 +5,11 @@ return {
description = "A shell parser, formatter, and interpreter with `bash` support.",
},
command = "shfmt",
- args = { "-filename", "$FILENAME" },
+ args = function(_, ctx)
+ local args = { "-filename", "$FILENAME" }
+ 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 3831198..c0a19cd 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 b00c3f0..05a4e4f 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