fix(formatter): Handle THEN on separate line and multi-line conditions

- Track when IF has THEN on separate line
- Don't increase indent after IF until THEN is seen
- Increase indent after THEN for content
- Continuation lines in conditions stay at same level as IF
- END_IF stays at same level as content (TIA Portal style)
This commit is contained in:
2026-02-18 15:26:05 +01:00
parent 5bc6bb0750
commit 25c306efee
+29 -3
View File
@@ -67,6 +67,7 @@ function M.format_document(content, options)
local in_var_section = false local in_var_section = false
local in_code_section = false local in_code_section = false
local prev_line_keyword = nil local prev_line_keyword = nil
local prev_line_had_then = true -- Track if previous line had THEN
-- FB call continuation tracking -- FB call continuation tracking
local in_fb_call = false local in_fb_call = false
@@ -300,6 +301,11 @@ function M.format_document(content, options)
indent_level = indent_level + 1 indent_level = indent_level + 1
-- Check for same-level keywords (THEN, ELSE, DO, OF on their own line) -- Check for same-level keywords (THEN, ELSE, DO, OF on their own line)
elseif same_level_keywords[kw] then elseif same_level_keywords[kw] then
-- For THEN on its own line (after IF without THEN on same line), increase indent for content
if kw == "THEN" then
-- Increase indent after THEN for content
indent_level = indent_level + 1
end
-- For ELSIF and ELSE, reset to IF base level -- For ELSIF and ELSE, reset to IF base level
if (kw == "ELSIF" or kw == "ELSE") and if_base_level then if (kw == "ELSIF" or kw == "ELSE") and if_base_level then
indent_level = if_base_level indent_level = if_base_level
@@ -311,12 +317,28 @@ function M.format_document(content, options)
end end
-- Check for block starters -- Check for block starters
elseif is_block_starter(kw) then elseif is_block_starter(kw) then
-- Save base level for IF statements -- Save base level for IF statements AFTER checking for THEN
if kw == "IF" then if kw == "IF" then
if_base_level = indent_level -- Check if THEN is on the same line
if trimmed:match("THEN%s*$") then
if_base_level = indent_level
prev_line_had_then = true
indent_level = indent_level + 1
else
-- THEN on separate line, don't increase yet
if_base_level = indent_level
prev_line_had_then = false
end
elseif kw == "THEN" then
-- THEN on its own line, increase for content
prev_line_had_then = true
indent_level = indent_level + 1
else
-- For other block starters (FOR, WHILE, REGION, CASE), always increase
prev_line_had_then = true
indent_level = indent_level + 1
end end
table.insert(formatted_lines, formatted_line) table.insert(formatted_lines, formatted_line)
indent_level = indent_level + 1
-- Regular line -- Regular line
else else
if needs_semicolon(trimmed) and not in_fb_call then if needs_semicolon(trimmed) and not in_fb_call then
@@ -327,6 +349,10 @@ function M.format_document(content, options)
end end
prev_line_keyword = kw prev_line_keyword = kw
-- Reset THEN tracking for next iteration (unless current line has THEN)
if kw ~= "THEN" then
prev_line_had_then = true
end
end end
local result = table.concat(formatted_lines, "\n") local result = table.concat(formatted_lines, "\n")