fix(formatter): Fix multi-line condition and END_IF indentation
- Multi-line IF conditions: continuation lines at +1 from IF - END_IF now decreases indent (reverted previous wrong change) - Track multi-line condition state separately from THEN tracking - Now matches TIA Portal expected format
This commit is contained in:
+22
-15
@@ -67,7 +67,7 @@ function M.format_document(content, options)
|
||||
local in_var_section = false
|
||||
local in_code_section = false
|
||||
local prev_line_keyword = nil
|
||||
local prev_line_had_then = true -- Track if previous line had THEN
|
||||
local in_multi_line_condition = false -- Track if we're in multi-line IF condition
|
||||
|
||||
-- FB call continuation tracking
|
||||
local in_fb_call = false
|
||||
@@ -286,11 +286,7 @@ function M.format_document(content, options)
|
||||
if_base_level = nil
|
||||
end
|
||||
in_fb_call = false -- Reset FB call state
|
||||
-- Don't decrease indent for END_IF - it stays at same level as content (per TIA Portal)
|
||||
-- Only decrease for other enders like END_REGION, END_CASE, END_FOR, etc.
|
||||
if kw ~= "END_IF" then
|
||||
indent_level = math.max(0, indent_level - 1)
|
||||
end
|
||||
indent_level = math.max(0, indent_level - 1)
|
||||
if needs_semicolon(trimmed) then
|
||||
formatted_line = formatted_line .. ";"
|
||||
end
|
||||
@@ -322,25 +318,40 @@ function M.format_document(content, options)
|
||||
-- Check if THEN is on the same line
|
||||
if trimmed:match("THEN%s*$") then
|
||||
if_base_level = indent_level
|
||||
prev_line_had_then = true
|
||||
in_multi_line_condition = false
|
||||
indent_level = indent_level + 1
|
||||
else
|
||||
-- THEN on separate line, don't increase yet
|
||||
if_base_level = indent_level
|
||||
prev_line_had_then = false
|
||||
in_multi_line_condition = true
|
||||
end
|
||||
elseif kw == "THEN" then
|
||||
-- THEN on its own line, increase for content
|
||||
prev_line_had_then = true
|
||||
-- THEN on its own line, increase for content and exit multi-line condition
|
||||
in_multi_line_condition = false
|
||||
indent_level = indent_level + 1
|
||||
else
|
||||
-- For other block starters (FOR, WHILE, REGION, CASE), always increase
|
||||
prev_line_had_then = true
|
||||
in_multi_line_condition = false
|
||||
indent_level = indent_level + 1
|
||||
end
|
||||
table.insert(formatted_lines, formatted_line)
|
||||
-- Regular line
|
||||
else
|
||||
-- Handle multi-line condition continuation
|
||||
local use_multi_line_indent = false
|
||||
if in_multi_line_condition and kw ~= "THEN" then
|
||||
-- This is a continuation of a multi-line IF condition
|
||||
-- Use +1 indent from IF level
|
||||
use_multi_line_indent = true
|
||||
in_multi_line_condition = false -- Exit multi-line mode after first continuation
|
||||
end
|
||||
|
||||
if use_multi_line_indent then
|
||||
formatted_line = string.rep(indent_char, (indent_level + 1) * indent_size) .. trimmed
|
||||
else
|
||||
formatted_line = get_indent() .. trimmed
|
||||
end
|
||||
|
||||
if needs_semicolon(trimmed) and not in_fb_call then
|
||||
formatted_line = formatted_line .. ";"
|
||||
end
|
||||
@@ -349,10 +360,6 @@ function M.format_document(content, options)
|
||||
end
|
||||
|
||||
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
|
||||
|
||||
local result = table.concat(formatted_lines, "\n")
|
||||
|
||||
Reference in New Issue
Block a user