From 470d41988e83913df428c9e832c15b8bb84301ad Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Fri, 29 Sep 2023 12:46:24 -0700 Subject: fix: injected formatter preserves indentation of code blocks --- lua/conform/formatters/injected.lua | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'lua/conform') 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 -- cgit v1.2.3-70-g09d2