fix: Attribute block expand/collapse, TIME literal diagnostics, and formatter bugs

- SCLFormat: Remove clear_buffer_state() call that was preventing
       expand after formatting (attr_toggle state now preserved)
     - Diagnostics: Fix false 'Undefined variable' for IEC literals
       like T#2s, D#1d, S5T#1s (TIME, DATE, S5TIME types)
     - Formatter: Fix block comments (* ... *) being misidentified as
       FB calls in VAR_IN_OUT/VAR_INPUT/VAR_OUTPUT sections
     - Formatter: Fix array-based FB calls like instTimers[idx].TON()
       not being properly formatted with aligned parameters
This commit is contained in:
2026-02-24 15:27:58 +01:00
parent 37f8d475f0
commit 0880df4ef5
2 changed files with 31 additions and 4 deletions
+12 -1
View File
@@ -95,11 +95,22 @@ function M.validate_diagnostics(content, workspace_types, root_dir)
var_section_start = nil var_section_start = nil
end end
-- Check if a hash-prefixed match is part of an IEC literal (TIME, S5TIME, etc.)
local function is_iec_literal_at(line, match_start)
if not line or not match_start then return false end
if match_start <= 1 then return false end
local prefix = line:sub(match_start - 1, match_start - 1):upper()
return prefix == "T" or prefix == "D" or prefix == "S"
end
if not in_var_section then if not in_var_section then
local hash_vars = {} local hash_vars = {}
for var in line:gmatch("#[%w_]+") do for var in line:gmatch("#[%w_]+") do
local var_name = var:sub(2) local var_name = var:sub(2)
hash_vars[var_name] = true local match_start = line:find(var, 1, true)
if not is_iec_literal_at(line, match_start) then
hash_vars[var_name] = true
end
end end
for var_name, _ in pairs(hash_vars) do for var_name, _ in pairs(hash_vars) do
+19 -3
View File
@@ -175,7 +175,23 @@ function M.format_document(content, options)
if not (trimmed:match("^#?%w+") or trimmed:match('^"')) then if not (trimmed:match("^#?%w+") or trimmed:match('^"')) then
return false return false
end end
local paren_pos = trimmed:find("%(")
-- Find the first ( that is not inside a block comment (* ... *)
local paren_pos = nil
local comment_depth = 0
for i = 1, #trimmed do
local c = trimmed:sub(i, i)
local next_c = trimmed:sub(i+1, i+1)
if c == "(" and next_c == "*" then
comment_depth = comment_depth + 1
elseif c == "*" and next_c == ")" and comment_depth > 0 then
comment_depth = comment_depth - 1
elseif c == "(" and comment_depth == 0 then
paren_pos = i
break
end
end
if not paren_pos then return false end if not paren_pos then return false end
local assign_pos = trimmed:find(":=") local assign_pos = trimmed:find(":=")
if assign_pos and assign_pos < paren_pos then return false end if assign_pos and assign_pos < paren_pos then return false end
@@ -196,7 +212,7 @@ function M.format_document(content, options)
local result = {} local result = {}
local all_text = table.concat(fb_lines, " ") local all_text = table.concat(fb_lines, " ")
local fb_name = all_text:match("^(#?[%w_]+)%s*%(") local fb_name = all_text:match("^(#?.-)%s*%(")
if not fb_name then if not fb_name then
for _, l in ipairs(fb_lines) do for _, l in ipairs(fb_lines) do
table.insert(result, base_indent .. l) table.insert(result, base_indent .. l)
@@ -412,7 +428,7 @@ function M.format_document(content, options)
in_fb_call = true in_fb_call = true
fb_call_lines = { trimmed } fb_call_lines = { trimmed }
fb_base_indent = get_indent() fb_base_indent = get_indent()
local fb_name = trimmed:match("^(#?[%w_]+)") local fb_name = trimmed:match("^(#?.-)%s*%(") or trimmed:match("^(#?.-)$")
if fb_name then if fb_name then
fb_param_indent = fb_base_indent .. string.rep(" ", #fb_name + 1) fb_param_indent = fb_base_indent .. string.rep(" ", #fb_name + 1)
else else