From db69592cc100e8c090f89b95dcec1b880b07912b Mon Sep 17 00:00:00 2001 From: Lazar Bulovan Date: Mon, 23 Feb 2026 14:32:21 +0100 Subject: [PATCH] fix: correctly indent IF/ELSIF condition continuations - Add in_condition state to track when inside IF/ELSIF condition (before THEN) - Use condition_indent for continuation lines in conditions - Fixes multiline IF/ELSIF conditions being incorrectly indented --- src/formatter.lua | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/formatter.lua b/src/formatter.lua index 0bf6c28..4a2b96b 100644 --- a/src/formatter.lua +++ b/src/formatter.lua @@ -316,6 +316,8 @@ function M.format_document(content, options) local in_assignment = false local assignment_continuation_indent = "" + local in_condition = false + local condition_indent = "" for line_num, line in ipairs(lines) do local trimmed = line:gsub("^%s+", ""):gsub("%s+$", "") @@ -337,6 +339,21 @@ function M.format_document(content, options) -- Check if this line ends an assignment (has := and ends with ;) local ends_assignment = trimmed:match(":=") and trimmed:match(";%s*$") + -- Check if continuing condition (IF/ELSIF condition before THEN) + if in_condition and not ends_assignment then + local starts_with_operator = is_assignment_continuation(trimmed) + local first_word = trimmed:match("^([%w_]+)") + local is_then = first_word and first_word:upper() == "THEN" + if starts_with_operator or is_then then + table.insert(formatted_lines, condition_indent .. trimmed) + if is_then then + in_condition = false + end + goto continue + end + in_condition = false + end + -- Check if continuing assignment -- Continuation if: in assignment AND (starts with operator OR doesn't start new statement) -- BUT NOT if this line ends an assignment OR starts a new assignment (contains :=) OR starts with block ender @@ -471,6 +488,7 @@ function M.format_document(content, options) end table.insert(formatted_lines, formatted) table.insert(stack, { type = "THEN_CONTENT", indent = #stack, no_indent = true }) + in_condition = false elseif kw == "ELSE" or kw == "ELSIF" then if #stack > 0 and stack[#stack].type == "THEN_CONTENT" then table.remove(stack) @@ -521,6 +539,10 @@ function M.format_document(content, options) local formatted = string.rep(indent_char, current_level) .. trimmed table.insert(formatted_lines, formatted) table.insert(stack, { type = kw, indent = current_level }) + if kw == "IF" or kw == "ELSIF" then + in_condition = true + condition_indent = string.rep(indent_char, current_level) + end goto continue end