summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-12-19 13:55:54 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-12-19 13:55:54 +0800
commit316fac0c3c4981444db27b18c2caaef29c32418e (patch)
treee6151046c5407fee4fa7ec8b28d1506266b5da9a
parent3400b54cca23a5440502897fd848dd01684d021b (diff)
downloadlua-language-server-316fac0c3c4981444db27b18c2caaef29c32418e.zip
支持找全局变量
-rw-r--r--server/src/matcher/completion.lua48
-rw-r--r--server/test/completion/init.lua49
2 files changed, 79 insertions, 18 deletions
diff --git a/server/src/matcher/completion.lua b/server/src/matcher/completion.lua
index 910173f0..a5bc6eb5 100644
--- a/server/src/matcher/completion.lua
+++ b/server/src/matcher/completion.lua
@@ -84,13 +84,13 @@ local function matchKey(me, other)
end
return false
end
- -- 5. 找到下一个可用的字,如果超出长度则失败
+ -- 5. 找到下一个可用的字,如果超出长度就算成功
::NEXT::
repeat
cur = cur + 1
until not used[cur]
if cur > #lOther then
- return false
+ break
end
end
return true
@@ -103,27 +103,61 @@ local function searchLocals(vm, pos, name, callback)
end
if loc.source.start <= pos and loc.close >= pos then
if matchKey(name, loc.key) then
- callback(loc.key)
+ callback(loc)
end
end
::CONTINUE::
end
end
+local function searchFields(name, parent, callback)
+ for key, field in pairs(parent.value.child) do
+ if type(key) ~= 'string' then
+ goto CONTINUE
+ end
+ if matchKey(name, key ) then
+ callback(field)
+ end
+ ::CONTINUE::
+ end
+end
+
return function (vm, pos)
local result = findResult(vm, pos)
if not result then
return nil
end
local list = {}
- local source = result.source
- if source.type == 'name' then
- searchLocals(vm, pos, result.key, function (label)
+ if result.type == 'local' then
+ searchLocals(vm, pos, result.key, function (loc)
list[#list+1] = {
- label = label,
+ label = loc.key,
kind = CompletionItemKind.Variable,
}
end)
+ -- 也尝试搜索全局变量
+ searchFields(result.key, vm.results.locals[1], function (field)
+ list[#list+1] = {
+ label = field.key,
+ kind = CompletionItemKind.Field,
+ }
+ end)
+ elseif result.type == 'field' then
+ if result.parent.value.ENV == true then
+ -- 全局变量也搜索是不是local
+ searchLocals(vm, pos, result.key, function (loc)
+ list[#list+1] = {
+ label = loc.key,
+ kind = CompletionItemKind.Variable,
+ }
+ end)
+ end
+ searchFields(result.key, result.parent, function (field)
+ list[#list+1] = {
+ label = field.key,
+ kind = CompletionItemKind.Field,
+ }
+ end)
end
return list
end
diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua
index da633a86..218a6a46 100644
--- a/server/test/completion/init.lua
+++ b/server/test/completion/init.lua
@@ -68,36 +68,63 @@ function TEST(script)
end
TEST [[
-local abcde
-a@
+local zabcde
+za@
]]
{
{
- label = 'abcde',
+ label = 'zabcde',
kind = CompletionItemKind.Variable,
}
}
TEST [[
-local abcdefg
-local abcde
-abcde@
+local zabcdefg
+local zabcde
+zabcde@
]]
{
{
- label = 'abcdefg',
+ label = 'zabcdefg',
kind = CompletionItemKind.Variable,
}
}
TEST [[
-local abcdefg
-a@
-local abcde
+local zabcdefg
+za@
+local zabcde
]]
{
{
- label = 'abcdefg',
+ label = 'zabcdefg',
kind = CompletionItemKind.Variable,
}
}
+
+TEST [[
+local zabcde
+zace@
+]]
+{
+ {
+ label = 'zabcde',
+ kind = CompletionItemKind.Variable,
+ }
+}
+
+TEST [[
+ZABC
+local zabc
+zac@
+]]
+{
+ {
+ label = 'zabc',
+ kind = CompletionItemKind.Variable,
+ },
+ {
+ label = 'ZABC',
+ kind = CompletionItemKind.Field,
+ },
+}