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:
+28
-2
@@ -67,6 +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
|
||||
|
||||
-- FB call continuation tracking
|
||||
local in_fb_call = false
|
||||
@@ -300,6 +301,11 @@ function M.format_document(content, options)
|
||||
indent_level = indent_level + 1
|
||||
-- Check for same-level keywords (THEN, ELSE, DO, OF on their own line)
|
||||
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
|
||||
if (kw == "ELSIF" or kw == "ELSE") and if_base_level then
|
||||
indent_level = if_base_level
|
||||
@@ -311,12 +317,28 @@ function M.format_document(content, options)
|
||||
end
|
||||
-- Check for block starters
|
||||
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
|
||||
-- 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
|
||||
table.insert(formatted_lines, formatted_line)
|
||||
indent_level = indent_level + 1
|
||||
-- Regular line
|
||||
else
|
||||
if needs_semicolon(trimmed) and not in_fb_call then
|
||||
@@ -327,6 +349,10 @@ 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