diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2018-12-26 17:46:36 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2018-12-26 17:46:36 +0800 |
commit | d1b9237c44bdd9b1fccca5aa1a2b83608027e91d (patch) | |
tree | 16cf93e87017e454c9ed772f30a7ba6a77e61b31 | |
parent | 68bb36273433e7ea764dc1cd583b3a97ccff7a0b (diff) | |
download | lua-language-server-d1b9237c44bdd9b1fccca5aa1a2b83608027e91d.zip |
库函数的可选值支持自动完成
-rw-r--r-- | server/src/matcher/completion.lua | 26 | ||||
-rw-r--r-- | server/src/method/initialize.lua | 6 | ||||
-rw-r--r-- | server/test/completion/init.lua | 46 |
3 files changed, 69 insertions, 9 deletions
diff --git a/server/src/matcher/completion.lua b/server/src/matcher/completion.lua index db34edfb..8f74b7c7 100644 --- a/server/src/matcher/completion.lua +++ b/server/src/matcher/completion.lua @@ -235,11 +235,13 @@ local function searchAsSuffix(result, callback) end local function searchInArg(vm, inCall, inString, callback) - local special = inCall.func.lib and inCall.func.lib.special - if not special then + local lib = inCall.func.lib + if not lib then return end - if special == 'require' then + + -- require列举出可以引用到的文件 + if lib.special == 'require' then if not vm.lsp or not vm.lsp.workspace then return end @@ -253,6 +255,16 @@ local function searchInArg(vm, inCall, inString, callback) end end end + + -- 其他库函数,根据参数位置找枚举值 + if lib.args and lib.enums then + local name = lib.args[inCall.select].name + for _, enum in ipairs(lib.enums) do + if enum.name == name and enum.enum then + callback(enum.enum, CompletionItemKind.EnumMember, nil, enum.description) + end + end + end end local function searchAsIndex(vm, pos, result, callback) @@ -393,7 +405,7 @@ return function (vm, pos) local list = {} local mark = {} - local function callback(var, defualt) + local function callback(var, defualt, detail, documentation) local key if type(var) == 'string' then key = var @@ -408,13 +420,15 @@ return function (vm, pos) list[#list+1] = { label = key, kind = defualt, + detail = detail, + documentation = documentation, } else list[#list+1] = { label = var.key, kind = getKind(var, defualt), - detail = getDetail(var), - documentation = getDocument(var, source), + detail = detail or getDetail(var), + documentation = documentation or getDocument(var, source), } end end diff --git a/server/src/method/initialize.lua b/server/src/method/initialize.lua index 3de9a89e..0f5b840a 100644 --- a/server/src/method/initialize.lua +++ b/server/src/method/initialize.lua @@ -1,10 +1,10 @@ local function allWords() - local str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' + local str = [[abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:('"[]] local list = {} for c in str:gmatch '.' do list[#list+1] = c end - return table.unpack(list) + return list end return function (lsp) @@ -35,7 +35,7 @@ return function (lsp) -- 自动完成 completionProvider = { resolveProvider = false, - triggerCharacters = { '.', ':', allWords() }, + triggerCharacters = allWords(), }, -- 工作目录 workspace = { diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua index 8a477ab2..c64ffcca 100644 --- a/server/test/completion/init.lua +++ b/server/test/completion/init.lua @@ -325,3 +325,49 @@ end kind = CompletionItemKind.Variable, }, } + +TEST [[ +collectgarbage('@') +]] +{ + { + label = 'collect', + kind = CompletionItemKind.EnumMember, + documentation = EXISTS, + }, + { + label = 'stop', + kind = CompletionItemKind.EnumMember, + documentation = EXISTS, + }, + { + label = 'restart', + kind = CompletionItemKind.EnumMember, + documentation = EXISTS, + }, + { + label = 'count', + kind = CompletionItemKind.EnumMember, + documentation = EXISTS, + }, + { + label = 'step', + kind = CompletionItemKind.EnumMember, + documentation = EXISTS, + }, + { + label = 'setpause', + kind = CompletionItemKind.EnumMember, + documentation = EXISTS, + }, + { + label = 'setstepmul', + kind = CompletionItemKind.EnumMember, + documentation = EXISTS, + }, + { + label = 'isrunning', + kind = CompletionItemKind.EnumMember, + documentation = EXISTS, + }, +} |