plugins/console: fix crash

This commit is contained in:
cracyc 2021-10-30 12:04:54 -05:00
parent 88cfb2afbd
commit cee44b0414

View File

@ -72,7 +72,7 @@ function console.startplugin()
-- Main completion function. It evaluates the current sub-expression -- Main completion function. It evaluates the current sub-expression
-- to determine its type. Currently supports tables fields, global -- to determine its type. Currently supports tables fields, global
-- variables and function prototype completion. -- variables and function prototype completion.
local function contextual_list(expr, sep, str, word) local function contextual_list(expr, sep, str, word, strs)
local function add(value) local function add(value)
value = tostring(value) value = tostring(value)
if value:match("^" .. word) then if value:match("^" .. word) then
@ -92,7 +92,7 @@ function console.startplugin()
end end
if expr and expr ~= "" then if expr and expr ~= "" then
local v = load("return " .. expr) local v = load("local STRING = {'" .. table.concat(strs,"','") .. "'} return " .. expr)
if v then if v then
err, v = pcall(v) err, v = pcall(v)
if (not err) or (not v) then if (not err) or (not v) then
@ -158,6 +158,7 @@ function console.startplugin()
-- separator item ( '.', ':', '[', '(' ) and the current string in case -- separator item ( '.', ':', '[', '(' ) and the current string in case
-- of an unfinished string literal. -- of an unfinished string literal.
local function simplify_expression(expr, word) local function simplify_expression(expr, word)
local strs = {}
-- Replace annoying sequences \' and \" inside literal strings -- Replace annoying sequences \' and \" inside literal strings
expr = expr:gsub("\\(['\"])", function (c) expr = expr:gsub("\\(['\"])", function (c)
return string.format("\\%03d", string.byte(c)) return string.format("\\%03d", string.byte(c))
@ -177,7 +178,10 @@ function console.startplugin()
idx, startpat, endpat = idx2, sign, sign idx, startpat, endpat = idx2, sign, sign
end end
if expr:sub(idx):find("^" .. startpat .. ".-" .. endpat) then if expr:sub(idx):find("^" .. startpat .. ".-" .. endpat) then
expr = expr:gsub(startpat .. "(.-)" .. endpat, " STRING ") expr = expr:gsub(startpat .. "(.-)" .. endpat, function (str)
strs[#strs + 1] = str
return " STRING[" .. #strs .. "] "
end)
else else
expr = expr:gsub(startpat .. "(.*)", function (str) expr = expr:gsub(startpat .. "(.*)", function (str)
curstring = str curstring = str
@ -194,7 +198,7 @@ function console.startplugin()
expr = expr:gsub("(%w)%s+(%w)","%1|%2") expr = expr:gsub("(%w)%s+(%w)","%1|%2")
expr = expr:gsub("%s+", "") -- Remove now useless spaces expr = expr:gsub("%s+", "") -- Remove now useless spaces
-- This main regular expression looks for table indexes and function calls. -- This main regular expression looks for table indexes and function calls.
return curstring, expr:match("([%.:%w%(%)%[%]_]-)([:%.%[%(])" .. word .. "$") return curstring, strs, expr:match("([%.:%w%(%)%[%]_]-)([:%.%[%(])" .. word .. "$")
end end
local function get_completions(line, endpos) local function get_completions(line, endpos)
@ -210,8 +214,8 @@ function console.startplugin()
word = word or "" word = word or ""
end end
local str, expr, sep = simplify_expression(line, word) local str, strs, expr, sep = simplify_expression(line, word)
contextual_list(expr, sep, str, word) contextual_list(expr, sep, str, word, strs)
if #matches > 1 then if #matches > 1 then
print("\n") print("\n")
for k, v in pairs(matches) do for k, v in pairs(matches) do