fix(diagnostics): correctly validate array element types
Extract actual element type for array declarations like 'Array[0..3] OF IEC_TIMER' instead of incorrectly flagging 'Array' as unknown.
This commit is contained in:
+19
-5
@@ -116,14 +116,20 @@ function M.validate_diagnostics(content, workspace_types, root_dir)
|
|||||||
|
|
||||||
local type_part = nil
|
local type_part = nil
|
||||||
local brace_depth = 0
|
local brace_depth = 0
|
||||||
|
local comment_depth = 0
|
||||||
local last_colon = nil
|
local last_colon = nil
|
||||||
for i = 1, #line do
|
for i = 1, #line do
|
||||||
local c = line:sub(i, i)
|
local c = line:sub(i, i)
|
||||||
if c == "{" then
|
local next_c = line: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
|
||||||
brace_depth = brace_depth + 1
|
brace_depth = brace_depth + 1
|
||||||
elseif c == "}" then
|
elseif c == "}" and comment_depth == 0 then
|
||||||
brace_depth = brace_depth - 1
|
brace_depth = brace_depth - 1
|
||||||
elseif c == ":" and brace_depth == 0 then
|
elseif c == ":" and brace_depth == 0 and comment_depth == 0 then
|
||||||
last_colon = i
|
last_colon = i
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -131,11 +137,16 @@ function M.validate_diagnostics(content, workspace_types, root_dir)
|
|||||||
type_part = line:sub(last_colon + 1)
|
type_part = line:sub(last_colon + 1)
|
||||||
end
|
end
|
||||||
if type_part then
|
if type_part then
|
||||||
|
type_part = type_part:gsub("%s*%(%*.-%*%)", "") -- Remove block comments (* ... *) with leading whitespace
|
||||||
type_part = type_part:gsub("%s*:=.*$", "")
|
type_part = type_part:gsub("%s*:=.*$", "")
|
||||||
|
type_part = type_part:gsub("%s*;.*$", "") -- Remove trailing semicolon
|
||||||
type_part = type_part:gsub("%s*$", "")
|
type_part = type_part:gsub("%s*$", "")
|
||||||
type_part = type_part:gsub("^%s+", "")
|
type_part = type_part:gsub("^%s+", "")
|
||||||
|
|
||||||
local base_type = type_part:match("^(%w+)")
|
local base_type = type_part:match("^([%w_]+)")
|
||||||
|
if type_part:match("%s+OF%s+") then
|
||||||
|
base_type = type_part:match(".*%s+OF%s+([%w_]+)")
|
||||||
|
end
|
||||||
local upper_type = base_type and base_type:upper() or nil
|
local upper_type = base_type and base_type:upper() or nil
|
||||||
if base_type and not all_types[base_type] and
|
if base_type and not all_types[base_type] and
|
||||||
upper_type ~= "BOOL" and upper_type ~= "INT" and upper_type ~= "DINT" and
|
upper_type ~= "BOOL" and upper_type ~= "INT" and upper_type ~= "DINT" and
|
||||||
@@ -144,7 +155,10 @@ function M.validate_diagnostics(content, workspace_types, root_dir)
|
|||||||
upper_type ~= "CHAR" and upper_type ~= "STRING" and upper_type ~= "WCHAR" and
|
upper_type ~= "CHAR" and upper_type ~= "STRING" and upper_type ~= "WCHAR" and
|
||||||
upper_type ~= "WSTRING" and upper_type ~= "TIME" and upper_type ~= "DATE" and
|
upper_type ~= "WSTRING" and upper_type ~= "TIME" and upper_type ~= "DATE" and
|
||||||
upper_type ~= "TIME_OF_DAY" and upper_type ~= "DATE_AND_TIME" and
|
upper_type ~= "TIME_OF_DAY" and upper_type ~= "DATE_AND_TIME" and
|
||||||
upper_type ~= "S5TIME" and upper_type ~= "LTIME" then
|
upper_type ~= "S5TIME" and upper_type ~= "LTIME" and
|
||||||
|
upper_type ~= "IEC_TIMER" and upper_type ~= "TON_TIME" and
|
||||||
|
upper_type ~= "TOF_TIME" and upper_type ~= "TONR_TIME" and
|
||||||
|
upper_type ~= "TP_TIME" then
|
||||||
table.insert(diagnostics, {
|
table.insert(diagnostics, {
|
||||||
range = range_to_lsp(line_num, 0, line_num, #line),
|
range = range_to_lsp(line_num, 0, line_num, #line),
|
||||||
severity = DiagnosticSeverity.Warning,
|
severity = DiagnosticSeverity.Warning,
|
||||||
|
|||||||
Reference in New Issue
Block a user