aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSteven Arcangeli <stevearc@stevearc.com>2023-09-29 12:46:24 -0700
committerSteven Arcangeli <stevearc@stevearc.com>2023-09-29 12:47:15 -0700
commit470d41988e83913df428c9e832c15b8bb84301ad (patch)
tree99f35f7c60f8e63f386cc691c0cbeadc3d16e547
parentf3b02b5031fef6525caf208fa6c006b0fb10b81c (diff)
fix: injected formatter preserves indentation of code blocks
-rw-r--r--lua/conform/formatters/injected.lua37
1 files changed, 37 insertions, 0 deletions
diff --git a/lua/conform/formatters/injected.lua b/lua/conform/formatters/injected.lua
index c25f2f9..4990e05 100644
--- a/lua/conform/formatters/injected.lua
+++ b/lua/conform/formatters/injected.lua
@@ -6,6 +6,41 @@ local function in_range(range, start_lnum, end_lnum)
return not range or (start_lnum <= range["end"][1] and range["start"][1] <= end_lnum)
end
+---@param lines string[]
+---@return string?
+local function get_indent(lines)
+ local indent = nil
+ for _, line in ipairs(lines) do
+ if line ~= "" then
+ local whitespace = line:match("^%s*")
+ if whitespace == "" then
+ return nil
+ elseif not indent or whitespace:len() < indent:len() then
+ indent = whitespace
+ end
+ end
+ end
+ return indent
+end
+
+---@param original_lines string[]
+---@param new_lines? string[]
+local function apply_indent(original_lines, new_lines)
+ if not new_lines then
+ return
+ end
+ local indent = get_indent(original_lines)
+ local already_indented = get_indent(new_lines)
+ if not indent or already_indented then
+ return
+ end
+ for i, line in ipairs(new_lines) do
+ if line ~= "" then
+ new_lines[i] = indent .. line
+ end
+ end
+end
+
---@type conform.FileLuaFormatterConfig
return {
meta = {
@@ -90,6 +125,8 @@ return {
local format_opts = { async = true, bufnr = ctx.buf, quiet = true }
local idx = num_format
conform.format_lines(formatter_names, input_lines, format_opts, function(err, new_lines)
+ -- Preserve indentation in case the code block is indented
+ apply_indent(input_lines, new_lines)
formatter_cb(err, idx, start_lnum, end_lnum, new_lines)
end)
end