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_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
|
local in_multi_line_condition = false -- Track if we're in multi-line IF condition
|
||||||
|
|
||||||
-- FB call continuation tracking
|
-- FB call continuation tracking
|
||||||
local in_fb_call = false
|
local in_fb_call = false
|
||||||
@@ -286,11 +286,7 @@ function M.format_document(content, options)
|
|||||||
if_base_level = nil
|
if_base_level = nil
|
||||||
end
|
end
|
||||||
in_fb_call = false -- Reset FB call state
|
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)
|
indent_level = math.max(0, indent_level - 1)
|
||||||
-- 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
|
|
||||||
if needs_semicolon(trimmed) then
|
if needs_semicolon(trimmed) then
|
||||||
formatted_line = formatted_line .. ";"
|
formatted_line = formatted_line .. ";"
|
||||||
end
|
end
|
||||||
@@ -322,25 +318,40 @@ function M.format_document(content, options)
|
|||||||
-- Check if THEN is on the same line
|
-- Check if THEN is on the same line
|
||||||
if trimmed:match("THEN%s*$") then
|
if trimmed:match("THEN%s*$") then
|
||||||
if_base_level = indent_level
|
if_base_level = indent_level
|
||||||
prev_line_had_then = true
|
in_multi_line_condition = false
|
||||||
indent_level = indent_level + 1
|
indent_level = indent_level + 1
|
||||||
else
|
else
|
||||||
-- THEN on separate line, don't increase yet
|
-- THEN on separate line, don't increase yet
|
||||||
if_base_level = indent_level
|
if_base_level = indent_level
|
||||||
prev_line_had_then = false
|
in_multi_line_condition = true
|
||||||
end
|
end
|
||||||
elseif kw == "THEN" then
|
elseif kw == "THEN" then
|
||||||
-- THEN on its own line, increase for content
|
-- THEN on its own line, increase for content and exit multi-line condition
|
||||||
prev_line_had_then = true
|
in_multi_line_condition = false
|
||||||
indent_level = indent_level + 1
|
indent_level = indent_level + 1
|
||||||
else
|
else
|
||||||
-- For other block starters (FOR, WHILE, REGION, CASE), always increase
|
-- 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
|
indent_level = indent_level + 1
|
||||||
end
|
end
|
||||||
table.insert(formatted_lines, formatted_line)
|
table.insert(formatted_lines, formatted_line)
|
||||||
-- Regular line
|
-- Regular line
|
||||||
else
|
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
|
if needs_semicolon(trimmed) and not in_fb_call then
|
||||||
formatted_line = formatted_line .. ";"
|
formatted_line = formatted_line .. ";"
|
||||||
end
|
end
|
||||||
@@ -349,10 +360,6 @@ 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")
|
||||||
|
|||||||
Reference in New Issue
Block a user