summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-12-10 19:43:12 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-12-10 19:43:12 +0800
commite87c7b5567053de3d94f06c823a0d0962756b237 (patch)
tree52da8ca37f50b6b570864bb83a4d3141e4199524
parent244d00ba67e30d28b9c835fc04a09ca3e98e9a20 (diff)
downloadlua-language-server-e87c7b5567053de3d94f06c823a0d0962756b237.zip
支持 field
-rw-r--r--script-beta/core/completion.lua40
-rw-r--r--script-beta/log.lua6
-rw-r--r--test-beta/completion/init.lua28
3 files changed, 41 insertions, 33 deletions
diff --git a/script-beta/core/completion.lua b/script-beta/core/completion.lua
index 92148f28..099060de 100644
--- a/script-beta/core/completion.lua
+++ b/script-beta/core/completion.lua
@@ -113,26 +113,29 @@ local function isSameSource(source, pos)
return source.start <= pos and source.finish >= pos
end
-local function checkField(ast, text, word, offset, results)
- local myStart = offset - #word + 1
- local parent, oop = findParent(ast, text, myStart - 1)
- if not parent then
- parent = guide.getLocal(ast.ast, '_ENV', offset)
- if not parent then
- return
- end
- end
+local function checkField(word, start, parent, oop, results)
local used = {}
vm.eachField(parent, function (info)
local key = info.key
if key
and key:sub(1, 1) == 's'
- and not isSameSource(info.source, myStart) then
+ and not isSameSource(info.source, start) then
local name = key:sub(3)
if not used[name] and matchKey(word, name) then
+ local kind = ckind.Field
+ local literal = vm.getLiteral(info.source)
+ if literal ~= nil then
+ kind = ckind.Enum
+ end
results[#results+1] = {
label = name,
- kind = ckind.Field,
+ kind = kind,
+ id = stack(function ()
+ return {
+ detail = getLabel(info.source),
+ description = info.source.description,
+ }
+ end)
}
end
used[name] = true
@@ -168,7 +171,7 @@ local function buildFunction(results, source, oop, data)
end
end
-local function checkLibrary(ast, text, word, offset, results)
+local function checkLibrary(word, results)
for name, lib in pairs(library.global) do
if matchKey(word, name) then
if lib.type == 'function' then
@@ -218,10 +221,17 @@ local function tryWord(ast, text, offset, results)
if not word then
return nil
end
+ local start = offset - #word + 1
if not isInString(ast, offset) then
- checkLocal(ast, word, offset, results)
- checkField(ast, text, word, offset, results)
- checkLibrary(ast, text, word, offset, results)
+ local parent, oop = findParent(ast, text, start - 1)
+ if parent then
+ checkField(word, start, parent, oop, results)
+ else
+ checkLocal(ast, word, start, results)
+ local env = guide.getLocal(ast.ast, '_ENV', start)
+ checkField(word, start, env, false, results)
+ checkLibrary(word, results)
+ end
end
checkCommon(word, text, results)
end
diff --git a/script-beta/log.lua b/script-beta/log.lua
index 1688f79d..c3b08198 100644
--- a/script-beta/log.lua
+++ b/script-beta/log.lua
@@ -80,7 +80,7 @@ function m.warn(...)
end
function m.error(...)
- pushLog('error', ...)
+ return pushLog('error', ...)
end
function m.raw(thd, level, msg, source, currentline, clock)
@@ -89,7 +89,7 @@ function m.raw(thd, level, msg, source, currentline, clock)
end
init_log_file()
if not m.file then
- return
+ return ''
end
local sec, ms = mathModf(m.startTime + clock)
local timestr = osDate('%H:%M:%S', sec)
@@ -108,7 +108,7 @@ function m.raw(thd, level, msg, source, currentline, clock)
if m.size > m.maxSize then
m.file:write('[REACH MAX SIZE]')
end
- return
+ return buf
end
function m.init(root, path)
diff --git a/test-beta/completion/init.lua b/test-beta/completion/init.lua
index 3a47f6db..bebe481b 100644
--- a/test-beta/completion/init.lua
+++ b/test-beta/completion/init.lua
@@ -15,22 +15,14 @@ local function eq(a, b)
if tp1 == 'table' then
local mark = {}
for k in pairs(a) do
- if type(k) == 'number'
- or k == 'label'
- or k == 'kind' then
- if not eq(a[k], b[k]) then
- return false
- end
- mark[k] = true
+ if not eq(a[k], b[k]) then
+ return false
end
+ mark[k] = true
end
for k in pairs(b) do
- if type(k) == 'number'
- or k == 'label'
- or k == 'kind' then
- if not mark[k] then
- return false
- end
+ if not mark[k] then
+ return false
end
end
return true
@@ -48,6 +40,13 @@ function TEST(script)
files.setText('', new_script)
local result = core.completion('', pos)
+ for _, item in ipairs(result) do
+ for k in pairs(item) do
+ if k ~= 'label' and k ~= 'kind' then
+ item[k] = nil
+ end
+ end
+ end
if expect then
assert(result)
assert(eq(expect, result))
@@ -167,13 +166,12 @@ TEST [[
local t = {
abc = 1,
}
-t.a$
+t.ab$
]]
{
{
label = 'abc',
kind = CompletionItemKind.Enum,
- detail = '(number) = 1',
}
}