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
This commit is contained in:
@@ -316,6 +316,8 @@ function M.format_document(content, options)
|
|||||||
|
|
||||||
local in_assignment = false
|
local in_assignment = false
|
||||||
local assignment_continuation_indent = ""
|
local assignment_continuation_indent = ""
|
||||||
|
local in_condition = false
|
||||||
|
local condition_indent = ""
|
||||||
|
|
||||||
for line_num, line in ipairs(lines) do
|
for line_num, line in ipairs(lines) do
|
||||||
local trimmed = line:gsub("^%s+", ""):gsub("%s+$", "")
|
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 ;)
|
-- Check if this line ends an assignment (has := and ends with ;)
|
||||||
local ends_assignment = trimmed:match(":=") and trimmed:match(";%s*$")
|
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
|
-- Check if continuing assignment
|
||||||
-- Continuation if: in assignment AND (starts with operator OR doesn't start new statement)
|
-- 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
|
-- 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
|
end
|
||||||
table.insert(formatted_lines, formatted)
|
table.insert(formatted_lines, formatted)
|
||||||
table.insert(stack, { type = "THEN_CONTENT", indent = #stack, no_indent = true })
|
table.insert(stack, { type = "THEN_CONTENT", indent = #stack, no_indent = true })
|
||||||
|
in_condition = false
|
||||||
elseif kw == "ELSE" or kw == "ELSIF" then
|
elseif kw == "ELSE" or kw == "ELSIF" then
|
||||||
if #stack > 0 and stack[#stack].type == "THEN_CONTENT" then
|
if #stack > 0 and stack[#stack].type == "THEN_CONTENT" then
|
||||||
table.remove(stack)
|
table.remove(stack)
|
||||||
@@ -521,6 +539,10 @@ function M.format_document(content, options)
|
|||||||
local formatted = string.rep(indent_char, current_level) .. trimmed
|
local formatted = string.rep(indent_char, current_level) .. trimmed
|
||||||
table.insert(formatted_lines, formatted)
|
table.insert(formatted_lines, formatted)
|
||||||
table.insert(stack, { type = kw, indent = current_level })
|
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
|
goto continue
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user