diff --git a/src/main.lua b/src/main.lua index 2467e78..f689aa9 100644 --- a/src/main.lua +++ b/src/main.lua @@ -568,8 +568,14 @@ function handlers.textDocument_semanticTokensFull(params) for line in doc.content:gmatch("([^\n]*)\n") do table.insert(lines, line) end + + local total_lines = #lines local function add_token(line, startChar, length, tokenType) + -- Validate line number is within bounds + if line < 0 or line >= total_lines then + return + end local deltaLine = line local deltaStart = startChar if #tokens >= 4 then @@ -595,7 +601,8 @@ function handlers.textDocument_semanticTokensFull(params) if doc.variables then for var_name, info in pairs(doc.variables) do - if info.line == lineNum then + -- Validate variable line number is within bounds + if info.line and info.line >= 0 and info.line < total_lines and info.line == lineNum then local startChar = line:find(var_name, 1, true) if startChar then add_token(lineNum, startChar - 1, #var_name, "variable") @@ -673,11 +680,14 @@ function handlers.textDocument_inlayHint(params) table.insert(lines, line) end + local total_lines = #lines + for i, line in ipairs(lines) do local lineNum = i - 1 if doc.variables then for var_name, info in pairs(doc.variables) do - if info.line == lineNum and info.data_type and info.data_type ~= "unknown" then + -- Validate line number is within bounds + if info.line and info.line >= 0 and info.line < total_lines and info.line == lineNum and info.data_type and info.data_type ~= "unknown" then local colonPos = line:find(":", 1, true) if colonPos then table.insert(hints, {