summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2018-12-19 18:45:21 +0800
committer最萌小汐 <sumneko@hotmail.com>2018-12-19 18:45:21 +0800
commit89f47f8671b5b41a97850efd309f642d880aad97 (patch)
treee2818e7746e779be4648b4818c1ac47857bf3571 /server
parentabaccdbdd6f8858e594ff8a64925d3d4b0024b8b (diff)
downloadlua-language-server-89f47f8671b5b41a97850efd309f642d880aad97.zip
不要和下一行连起来
Diffstat (limited to 'server')
-rw-r--r--server/src/matcher/completion.lua2
-rw-r--r--server/src/matcher/find_result.lua70
-rw-r--r--server/test/completion/init.lua19
3 files changed, 85 insertions, 6 deletions
diff --git a/server/src/matcher/completion.lua b/server/src/matcher/completion.lua
index 0f0b0ced..acbb367e 100644
--- a/server/src/matcher/completion.lua
+++ b/server/src/matcher/completion.lua
@@ -218,7 +218,7 @@ local function searchAsSuffix(result, callback)
end
return function (vm, pos)
- local result, source = findResult(vm, pos)
+ local result, source = findResult(vm, pos, true)
if not result then
return nil
end
diff --git a/server/src/matcher/find_result.lua b/server/src/matcher/find_result.lua
index cbbe49d7..2b7c1b98 100644
--- a/server/src/matcher/find_result.lua
+++ b/server/src/matcher/find_result.lua
@@ -1,17 +1,20 @@
local function isContainPos(obj, pos)
- return obj.start <= pos and obj.finish + 1 >= pos
+ if obj.start <= pos and obj.finish + 1 >= pos then
+ return true
+ end
+ return false, pos - (obj.finish + 1)
end
-return function (vm, pos)
- local results = vm.results
- for source, object in pairs(results.sources) do
- if source.type == 'multi-source' then
+local function findAtPos(results, pos)
+ for sources, object in pairs(results.sources) do
+ if sources.type == 'multi-source' then
for _, source in ipairs(source) do
if source.type ~= 'simple' and isContainPos(source, pos) then
return object, source
end
end
else
+ local source = sources
if source.type ~= 'simple' and isContainPos(source, pos) then
return object, source
end
@@ -19,3 +22,60 @@ return function (vm, pos)
end
return nil
end
+
+local function findClosePos(results, pos)
+ local curDis = math.maxinteger
+ local parent = nil
+ for sources, object in pairs(results.sources) do
+ if sources.type == 'multi-source' then
+ for _, source in ipairs(source) do
+ if source.type ~= 'simple' then
+ local inside, dis = isContainPos(source, pos)
+ if inside then
+ return object, source
+ elseif dis > 0 and dis < curDis then
+ curDis = dis
+ parent = object
+ end
+ end
+ end
+ else
+ local source = sources
+ if source.type ~= 'simple' then
+ local inside, dis = isContainPos(source, pos)
+ if inside then
+ return object, source
+ elseif dis > 0 and dis < curDis then
+ curDis = dis
+ parent = object
+ end
+ end
+ end
+ end
+ if parent then
+ -- 造个假的 DirtyName
+ local source = {
+ type = 'name',
+ start = pos,
+ finish = pos,
+ [1] = '',
+ }
+ local result = {
+ type = 'field',
+ parent = parent,
+ key = '',
+ source = source,
+ }
+ return result, source
+ end
+ return nil
+end
+
+return function (vm, pos, close)
+ local results = vm.results
+ if close then
+ return findClosePos(results, pos)
+ else
+ return findAtPos(results, pos)
+ end
+end
diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua
index 95f66d33..f672bbac 100644
--- a/server/test/completion/init.lua
+++ b/server/test/completion/init.lua
@@ -237,3 +237,22 @@ t:@
documentation = EXISTS,
}
}
+
+TEST [[
+local t = {
+ a = {},
+}
+t.@
+xxx()
+]]
+{
+ {
+ label = 'a',
+ kind = CompletionItemKind.Field,
+ },
+ {
+ label = 'xxx',
+ kind = CompletionItemKind.Method,
+ documentation = EXISTS,
+ },
+}