summaryrefslogtreecommitdiff
path: root/script
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-03-17 11:18:19 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-03-17 11:18:19 +0800
commit55380a91e9a6d7fe4bbaeddc4b649822183a0db8 (patch)
tree1993e903e557a318005206ad3b558e89d7b58ab4 /script
parent268cfa9dd50292269c4c3bcc40093b54c6727bfa (diff)
downloadlua-language-server-55380a91e9a6d7fe4bbaeddc4b649822183a0db8.zip
completion: field in table
Diffstat (limited to 'script')
-rw-r--r--script/core/completion.lua41
1 files changed, 37 insertions, 4 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua
index 02724626..99c152f8 100644
--- a/script/core/completion.lua
+++ b/script/core/completion.lua
@@ -1286,10 +1286,13 @@ end
local function checkTableLiteralField(ast, text, offset, tbl, fields, results)
local mark = {}
- for _, field in ipairs(vm.getDefFields(tbl, 0)) do
- local name = guide.getKeyName(field)
- if name then
- mark[name] = true
+ for _, field in ipairs(tbl) do
+ if field.type == 'tablefield'
+ or field.type == 'tableindex' then
+ local name = guide.getKeyName(field)
+ if name then
+ mark[name] = true
+ end
end
end
table.sort(fields, function (a, b)
@@ -1326,6 +1329,9 @@ end
local function checkTableLiteralFieldByCall(ast, text, offset, call, funcs, index, results)
local source = findNearestSource(ast, offset)
+ if not source then
+ return
+ end
if source.type ~= 'table'
and (not source.parent or source.parent.type ~= 'table') then
return
@@ -1384,6 +1390,32 @@ local function tryCallArg(ast, text, offset, results)
checkTableLiteralFieldByCall(ast, text, offset, call, defs, argIndex, results)
end
+local function tryTable(ast, text, offset, results)
+ local source = findNearestSource(ast, offset)
+ if not source then
+ return
+ end
+ if source.type ~= 'table'
+ and (not source.parent or source.parent.type ~= 'table') then
+ return
+ end
+ local mark = {}
+ local fields = {}
+ local tbl = source
+ if source.type ~= 'table' then
+ tbl = source.parent
+ end
+ local defs = vm.getDefFields(tbl, 0)
+ for _, field in ipairs(defs) do
+ local name = guide.getKeyName(field)
+ if name and not mark[name] then
+ mark[name] = true
+ fields[#fields+1] = field
+ end
+ end
+ checkTableLiteralField(ast, text, offset, tbl, fields, results)
+end
+
local function getComment(ast, offset)
for _, comm in ipairs(ast.comms) do
if offset >= comm.start - 2 and offset <= comm.finish then
@@ -1843,6 +1875,7 @@ local function completion(uri, offset)
else
trySpecial(ast, text, offset, results)
tryCallArg(ast, text, offset, results)
+ tryTable(ast, text, offset, results)
tryWord(ast, text, offset, results)
tryIndex(ast, text, offset, results)
trySymbol(ast, text, offset, results)