feat: add builtin types/functions to diagnostics and completions
Files Modified/Created: - src/builtin_instructions.lua - NEW: Contains all TIA Portal built-in types, FBs, and functions - src/diagnostics.lua - Added missing data types + builtin instruction checking - src/main.lua - Added completion support for built-in types/F Bs/functions - src/plc_json.lua - Added .db file parsing + BOM handling fixes - lua/scl/blink_cmp_source.lua - Extended completion with all built-in types/F Bs/functions - src/node-types.json - Added node types for parser - AGENTS.md - Updated documentation Features Added: 1. Diagnostics - No more false "Unknown data type" warnings for: - All elementary types (USINT, SINT, UINT, UDINT, LINT, ULINT, TOD, DTL, etc.) - Timer/counter types (IEC_TON, TOF, TP, CTU, CTD, CTUD) - TIA Portal built-in FBs and functions 2. Auto-completion - Shows in VAR section after :: - 28 elementary data types - 23 built-in function blocks (TON, TOF, TP, CTU, CTD, CTUD, R_TRIG, F_TRIG, etc.) - 80+ built-in functions (ADD, SUB, MUL, DIV, SIN, COS, SQRT, etc.)
This commit is contained in:
+70
-1
@@ -26,6 +26,7 @@ local treesitter = dofile(script_path .. "/treesitter.lua")
|
||||
local plc_json = dofile(script_path .. "/plc_json.lua")
|
||||
local diagnostics = dofile(script_path .. "/diagnostics.lua")
|
||||
local formatter = dofile(script_path .. "/formatter.lua")
|
||||
local builtin = dofile(script_path .. "/builtin_instructions.lua")
|
||||
|
||||
local documents = {}
|
||||
|
||||
@@ -57,7 +58,7 @@ local semanticTokenTypes = {
|
||||
local capabilities = {
|
||||
hoverProvider = true,
|
||||
completionProvider = {
|
||||
triggerCharacters = { ".", "#", "(" },
|
||||
triggerCharacters = { ".", "#", "(", " ", ":", ";", "\n" },
|
||||
resolveProvider = false,
|
||||
},
|
||||
definitionProvider = true,
|
||||
@@ -385,6 +386,27 @@ function handlers.textDocument_completion(params)
|
||||
local line = lines[line_idx]
|
||||
local col = params.position.character
|
||||
local trigger = col > 0 and line:sub(col, col) or ""
|
||||
local line_before = col > 1 and line:sub(1, col - 1) or ""
|
||||
|
||||
-- Get word prefix being typed (for filtering)
|
||||
local prefix_start = col - 1
|
||||
while prefix_start > 0 and line:sub(prefix_start, prefix_start):match("[%w_]") do
|
||||
prefix_start = prefix_start - 1
|
||||
end
|
||||
local word_prefix = line:sub(prefix_start + 1, col - 1)
|
||||
|
||||
-- Check if we're in VAR section
|
||||
local in_var_section = false
|
||||
for i = line_idx - 1, 1, -1 do
|
||||
local prev_line = lines[i]:gsub("^%s+", ""):gsub("%s+$", "")
|
||||
if prev_line:match("^END_VAR") or prev_line:match("^VAR_TEMP") or prev_line:match("^VAR_IN_OUT") or prev_line:match("^VAR_OUTPUT") or prev_line:match("^VAR_INPUT") then
|
||||
break
|
||||
end
|
||||
if prev_line:match("^VAR%s*$") or prev_line:match("^VAR$") then
|
||||
in_var_section = true
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if trigger == "#" then
|
||||
if doc.variables then
|
||||
@@ -450,6 +472,53 @@ function handlers.textDocument_completion(params)
|
||||
})
|
||||
end
|
||||
end
|
||||
else
|
||||
-- Check context before cursor
|
||||
local is_var_decl = line_before:match(":%s*$")
|
||||
|
||||
-- Helper to add item with prefix filtering
|
||||
local function add_item(name, kind_val, detail_text)
|
||||
if word_prefix == "" or name:upper():find("^" .. word_prefix:upper()) then
|
||||
table.insert(items, {
|
||||
label = name,
|
||||
kind = kind_val,
|
||||
detail = detail_text,
|
||||
insertText = name,
|
||||
insertTextFormat = 1,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
||||
if in_var_section or is_var_decl then
|
||||
-- In VAR section: show data types and function blocks
|
||||
for type_name, _ in pairs(builtin.DATA_TYPES) do
|
||||
add_item(type_name, 7, "Data type")
|
||||
end
|
||||
|
||||
for fb_name, _ in pairs(builtin.FUNCTION_BLOCKS) do
|
||||
add_item(fb_name, 6, "Function block")
|
||||
end
|
||||
|
||||
-- Also add workspace types if available
|
||||
if doc.types then
|
||||
for type_name, type_info in pairs(doc.types) do
|
||||
add_item(type_name, 7, type_info.kind or "Type")
|
||||
end
|
||||
end
|
||||
else
|
||||
-- General context: show all
|
||||
for type_name, _ in pairs(builtin.DATA_TYPES) do
|
||||
add_item(type_name, 7, "Data type")
|
||||
end
|
||||
|
||||
for fb_name, _ in pairs(builtin.FUNCTION_BLOCKS) do
|
||||
add_item(fb_name, 6, "Function block")
|
||||
end
|
||||
|
||||
for func_name, _ in pairs(builtin.FUNCTIONS) do
|
||||
add_item(func_name, 3, "Function")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user