summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/core/completion.lua49
-rw-r--r--server/src/vm/vm.lua3
-rw-r--r--server/test/completion/init.lua5
3 files changed, 56 insertions, 1 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua
index b287b89b..f2d2dceb 100644
--- a/server/src/core/completion.lua
+++ b/server/src/core/completion.lua
@@ -233,6 +233,32 @@ local function searchFields(vm, source, word, callback)
end
end
+local function searchIndex(vm, source, word, callback)
+ for _, src in ipairs(vm.sources) do
+ if src:get 'table index' then
+ if matchKey(word, src[1]) then
+ callback(src[1], src, CompletionItemKind.Property)
+ end
+ end
+ end
+end
+
+local function searchCloseGlobal(vm, source, word, callback)
+ local loc = source:bindLocal()
+ local close = loc:close()
+ -- 因为闭包的关系落在局部变量finish到close范围内的source一定能访问到该局部变量
+ for _, src in ipairs(vm.sources) do
+ if src:get 'global'
+ and src.start >= source.finish
+ and src.finish <= close
+ then
+ if matchKey(word, src[1]) then
+ callback(src[1], src, CompletionItemKind.Variable)
+ end
+ end
+ end
+end
+
local KEYS = {'and', 'break', 'do', 'else', 'elseif', 'end', 'false', 'for', 'function', 'goto', 'if', 'in', 'local', 'nil', 'not', 'or', 'repeat', 'return', 'then', 'true', 'until', 'while', 'toclose'}
local function searchKeyWords(vm, source, word, callback)
for _, key in ipairs(KEYS) do
@@ -255,11 +281,34 @@ local function searchAsSuffix(vm, source, word, callback)
searchFields(vm, source, word, callback)
end
+local function searchAsIndex(vm, source, word, callback)
+ searchLocals(vm, source, word, callback)
+ searchIndex(vm, source, word, callback)
+ searchFields(vm, source, word, callback)
+end
+
+local function searchAsLocal(vm, source, word, callback)
+ searchCloseGlobal(vm, source, word, callback)
+
+ -- 特殊支持 local function
+ if matchKey(word, 'function') then
+ callback('function', nil, CompletionItemKind.Keyword)
+ end
+end
+
local function searchSource(vm, source, word, callback)
+ if source:get 'table index' then
+ searchAsIndex(vm, source, word, callback)
+ return
+ end
if source:get 'global' then
searchAsGlobal(vm, source, word, callback)
return
end
+ if source:action() == 'local' then
+ searchAsLocal(vm, source, word, callback)
+ return
+ end
if source:bindLocal() then
searchAsGlobal(vm, source, word, callback)
return
diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua
index aa4c6cc9..0b994011 100644
--- a/server/src/vm/vm.lua
+++ b/server/src/vm/vm.lua
@@ -57,7 +57,7 @@ function mt:buildTable(source)
tbl:setChild(index, value)
else
if key.type == 'name' then
- key.isIndex = true
+ key:set('table index', true)
tbl:setChild(key[1], value)
end
end
@@ -79,6 +79,7 @@ function mt:buildTable(source)
end
-- 处理写了一半的 key = value,把name类的数组元素视为哈希键
if obj.type == 'name' then
+ obj:set('table index', true)
end
end
end
diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua
index cc2c36be..2dba35bc 100644
--- a/server/test/completion/init.lua
+++ b/server/test/completion/init.lua
@@ -361,6 +361,7 @@ local t = {
}
TEST [[
+print(ff2)
local faa
local f@
print(fff)
@@ -375,6 +376,10 @@ print(fff)
kind = CompletionItemKind.Keyword,
},
{
+ label = 'ff2',
+ kind = CompletionItemKind.Text,
+ },
+ {
label = 'faa',
kind = CompletionItemKind.Text,
},