fix: prevent attribute block lines from being treated as assignments
The formatter was incorrectly treating multi-line attribute blocks as
assignments, causing AUTHOR/VERSION lines to get weird indentation.
Lines like:
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
Were being processed incorrectly because the attribute block line
contains := but no semicolon, causing the formatter to treat the
next line as a continuation.
Fix by tracking in_attr_block state and skipping assignment detection
for lines inside or exiting attribute blocks.
This commit is contained in:
+20
-1
@@ -359,6 +359,7 @@ function M.format_document(content, options)
|
||||
local assignment_continuation_indent = ""
|
||||
local in_condition = false
|
||||
local condition_indent = ""
|
||||
local in_attr_block = false
|
||||
|
||||
for line_num, line in ipairs(lines) do
|
||||
local trimmed = line:gsub("^%s+", ""):gsub("%s+$", "")
|
||||
@@ -377,6 +378,18 @@ function M.format_document(content, options)
|
||||
|
||||
trimmed = collapse_attribute_block(trimmed)
|
||||
|
||||
-- Track if we're entering or exiting an attribute block
|
||||
local has_open_brace = trimmed:find("{")
|
||||
local has_close_brace = trimmed:find("}")
|
||||
local exiting_attr_block = false
|
||||
|
||||
if has_open_brace and not has_close_brace then
|
||||
in_attr_block = true
|
||||
elseif has_close_brace and not has_open_brace then
|
||||
in_attr_block = false
|
||||
exiting_attr_block = true
|
||||
end
|
||||
|
||||
-- Check if this line ends an assignment (has := and ends with ;)
|
||||
local ends_assignment = trimmed:match(":=") and trimmed:match(";%s*$")
|
||||
|
||||
@@ -602,7 +615,8 @@ function M.format_document(content, options)
|
||||
end
|
||||
|
||||
-- Regular line - check for assignment start
|
||||
if trimmed:match(":=") then
|
||||
-- But not if its inside an attribute block, exiting one, or a single-line attribute block
|
||||
if not in_attr_block and not exiting_attr_block and trimmed:match(":=") and not trimmed:match("^{.*}$") then
|
||||
local base_indent = get_indent()
|
||||
local assign_pos = trimmed:find(":=")
|
||||
if assign_pos then
|
||||
@@ -620,6 +634,11 @@ function M.format_document(content, options)
|
||||
in_assignment = false
|
||||
end
|
||||
|
||||
-- Also reset assignment state when in attribute block
|
||||
if in_attr_block then
|
||||
in_assignment = false
|
||||
end
|
||||
|
||||
local formatted = get_indent() .. trimmed
|
||||
if NEEDS_SEMICOLON[kw] and trimmed:sub(-1) ~= ";" then
|
||||
formatted = formatted .. ";"
|
||||
|
||||
Reference in New Issue
Block a user