From cee44b0414ba5614991f9e355f66db48758531e5 Mon Sep 17 00:00:00 2001 From: cracyc Date: Sat, 30 Oct 2021 12:04:54 -0500 Subject: [PATCH] plugins/console: fix crash --- plugins/console/init.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/plugins/console/init.lua b/plugins/console/init.lua index cec818cc329..6b8643fe168 100644 --- a/plugins/console/init.lua +++ b/plugins/console/init.lua @@ -72,7 +72,7 @@ function console.startplugin() -- Main completion function. It evaluates the current sub-expression -- to determine its type. Currently supports tables fields, global -- 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) value = tostring(value) if value:match("^" .. word) then @@ -92,7 +92,7 @@ function console.startplugin() end if expr and expr ~= "" then - local v = load("return " .. expr) + local v = load("local STRING = {'" .. table.concat(strs,"','") .. "'} return " .. expr) if v then err, v = pcall(v) if (not err) or (not v) then @@ -158,6 +158,7 @@ function console.startplugin() -- separator item ( '.', ':', '[', '(' ) and the current string in case -- of an unfinished string literal. local function simplify_expression(expr, word) + local strs = {} -- Replace annoying sequences \' and \" inside literal strings expr = expr:gsub("\\(['\"])", function (c) return string.format("\\%03d", string.byte(c)) @@ -177,7 +178,10 @@ function console.startplugin() idx, startpat, endpat = idx2, sign, sign end 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 expr = expr:gsub(startpat .. "(.*)", function (str) curstring = str @@ -194,7 +198,7 @@ function console.startplugin() expr = expr:gsub("(%w)%s+(%w)","%1|%2") expr = expr:gsub("%s+", "") -- Remove now useless spaces -- 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 local function get_completions(line, endpos) @@ -210,8 +214,8 @@ function console.startplugin() word = word or "" end - local str, expr, sep = simplify_expression(line, word) - contextual_list(expr, sep, str, word) + local str, strs, expr, sep = simplify_expression(line, word) + contextual_list(expr, sep, str, word, strs) if #matches > 1 then print("\n") for k, v in pairs(matches) do