summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-03-06 17:51:35 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-03-06 17:51:35 +0800
commitf9b3e7070d4239687e8db16393b81213c4a701cd (patch)
treec0498f6834e55b2d477eff086b43d4824fd89956 /server
parentca0341d0760a067b6554d5b7aeb8357ec84e564f (diff)
downloadlua-language-server-f9b3e7070d4239687e8db16393b81213c4a701cd.zip
向前找符号
Diffstat (limited to 'server')
-rw-r--r--server/src/core/completion.lua12
-rw-r--r--server/src/vm/vm.lua4
-rw-r--r--server/test/completion/init.lua32
3 files changed, 46 insertions, 2 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua
index a04ffc52..07144255 100644
--- a/server/src/core/completion.lua
+++ b/server/src/core/completion.lua
@@ -155,7 +155,7 @@ end
local function getKind(cata, value)
if value:getType() == 'function' then
local func = value:getFunction()
- if func:getObject() then
+ if func and func:getObject() then
return CompletionItemKind.Method
else
return CompletionItemKind.Function
@@ -233,12 +233,22 @@ local function searchFields(vm, source, word, callback)
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
+ if matchKey(word, key) then
+ callback(key, nil, CompletionItemKind.Keyword)
+ end
+ end
+end
+
local function searchAsGlobal(vm, source, word, callback)
if word == '' then
return
end
searchLocals(vm, source, word, callback)
searchFields(vm, source, word, callback)
+ searchKeyWords(vm, source, word, callback)
end
local function searchAsSuffix(vm, source, word, callback)
diff --git a/server/src/vm/vm.lua b/server/src/vm/vm.lua
index e384eedc..aa4c6cc9 100644
--- a/server/src/vm/vm.lua
+++ b/server/src/vm/vm.lua
@@ -476,7 +476,10 @@ function mt:getSimple(simple, max)
source:bindValue(value, 'get')
elseif source.type == ':' then
object = value
+ source:set('parent', value)
+ source:set('object', object)
elseif source.type == '.' then
+ source:set('parent', value)
end
end
return value
@@ -860,6 +863,7 @@ function mt:doFunction(action)
local value = self:buildFunction(action)
local source = name[#name]
self:instantSource(source)
+ source:set('object', parent)
if source.type == 'index' then
local index = self:getIndex(source[1])
parent:setChild(index, value)
diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua
index 874b0948..cc2c36be 100644
--- a/server/test/completion/init.lua
+++ b/server/test/completion/init.lua
@@ -68,6 +68,20 @@ local function findStartPos(pos, buf)
break
end
end
+ if not res then
+ for i = pos-1, 1, -1 do
+ local c = buf:sub(i, i)
+ if c:find '[%.%:]' then
+ res = i
+ elseif c:find '[%s%c]' then
+ else
+ break
+ end
+ end
+ end
+ if not res then
+ return pos
+ end
return res
end
@@ -93,7 +107,7 @@ function TEST(script)
local vm = buildVM(ast)
assert(vm)
local word = findWord(pos, new_script)
- local startPos = findStartPos(pos, new_script) or pos
+ local startPos = findStartPos(pos, new_script)
local result = core.completion(vm, startPos, word)
if expect then
assert(result)
@@ -262,6 +276,22 @@ t.@
TEST [[
t.a = {}
+t.b = {}
+t. @
+]]
+{
+ {
+ label = 'a',
+ kind = CompletionItemKind.Field,
+ },
+ {
+ label = 'b',
+ kind = CompletionItemKind.Field,
+ },
+}
+
+TEST [[
+t.a = {}
function t:b()
end
t:@