diff --git a/src/diagnostics.lua b/src/diagnostics.lua index 506e913..7d14974 100644 --- a/src/diagnostics.lua +++ b/src/diagnostics.lua @@ -95,11 +95,22 @@ function M.validate_diagnostics(content, workspace_types, root_dir) var_section_start = nil 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 local hash_vars = {} for var in line:gmatch("#[%w_]+") do 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 for var_name, _ in pairs(hash_vars) do diff --git a/src/formatter.lua b/src/formatter.lua index b0bbc30..77438b0 100644 --- a/src/formatter.lua +++ b/src/formatter.lua @@ -175,7 +175,23 @@ function M.format_document(content, options) if not (trimmed:match("^#?%w+") or trimmed:match('^"')) then return false 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 local assign_pos = trimmed:find(":=") 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 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 for _, l in ipairs(fb_lines) do table.insert(result, base_indent .. l) @@ -412,7 +428,7 @@ function M.format_document(content, options) in_fb_call = true fb_call_lines = { trimmed } fb_base_indent = get_indent() - local fb_name = trimmed:match("^(#?[%w_]+)") + local fb_name = trimmed:match("^(#?.-)%s*%(") or trimmed:match("^(#?.-)$") if fb_name then fb_param_indent = fb_base_indent .. string.rep(" ", #fb_name + 1) else