diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-08-27 20:54:07 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-08-27 20:54:07 +0800 |
commit | 972e6458ed5c256cb3d1de05b585bc3fc55f82ad (patch) | |
tree | 6f8a8eb5adfff5f0410f60348a8ee3940a7ec5c3 /server | |
parent | a828a593e119ec222e4ef9e6a737234cabe00245 (diff) | |
download | lua-language-server-972e6458ed5c256cb3d1de05b585bc3fc55f82ad.zip |
分离函数调用与函数对象
Diffstat (limited to 'server')
-rw-r--r-- | server/src/core/completion.lua | 87 | ||||
-rw-r--r-- | server/src/method/textDocument/completion.lua | 1 | ||||
-rw-r--r-- | server/test/completion/init.lua | 103 |
3 files changed, 140 insertions, 51 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index a3994833..b9191568 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -153,11 +153,45 @@ local function getKind(cata, value) return nil end +local function buildSnipArgs(args) + local t = {} + for i, name in ipairs(args) do + t[i] = ('${%d:%s}'):format(i, name) + end + return table.concat(t, ', ') +end + +local function getFunctionSnip(name, value) + if value:getType() ~= 'function' then + return + end + local lib = value:getLib() + local hover + if lib then + hover = getFunctionHoverAsLib(name, lib) + else + local emmy = value:getEmmy() + if emmy and emmy.type == 'emmy.functionType' then + hover = getFunctionHoverAsEmmy(name, emmy) + else + hover = getFunctionHover(name, value:getFunction()) + end + end + if not hover then + return ('%s()'):format(name) + end + if not hover.args then + return ('%s()'):format(name) + end + return ('%s(%s)'):format(name, buildSnipArgs(hover.args)) +end + local function getValueData(cata, name, value, pos, source) local data = { documentation = getDucumentation(name, value), detail = getDetail(value), kind = getKind(cata, value), + snip = getFunctionSnip(name, value), } if cata == 'field' then if not parser:grammar(name, 'Name') then @@ -869,49 +903,6 @@ local function searchSpecial(vm, source, word, callback, pos, text) searchSpecialHashSign(vm, pos, text, callback) end -local function buildSnipArgs(args) - local t = {} - for i, name in ipairs(args) do - t[i] = ('${%d:%s}'):format(i, name) - end - return table.concat(t, ', ') -end - -local function makeFunctionSnippet(src, data) - if not src then - return - end - local value = src:findValue() - if not value then - return - end - local func = value:getFunction() - if not func then - return - end - local name = data.label - local lib = value:getLib() - local hover - if lib then - hover = getFunctionHoverAsLib(name, lib) - else - local emmy = value:getEmmy() - if emmy and emmy.type == 'emmy.functionType' then - hover = getFunctionHoverAsEmmy(name, emmy) - else - hover = getFunctionHover(name, value:getFunction()) - end - end - if not hover then - return - end - if not hover.args then - return - end - data.insertTextFormat = 2 - data.insertText = ('%s(%s)'):format(data.label, buildSnipArgs(hover.args)) -end - local function makeList(source, pos, word) local list = {} local mark = {} @@ -937,8 +928,16 @@ local function makeList(source, pos, word) if not data.kind then data.kind = kind end - makeFunctionSnippet(src, data) list[#list+1] = data + if data.snip then + local snipData = table.deepCopy(data) + snipData.insertText = data.snip + snipData.kind = CompletionItemKind.Snippet + snipData.label = snipData.label .. '()' + snipData.snip = nil + data.snip = nil + list[#list+1] = snipData + end end, list end diff --git a/server/src/method/textDocument/completion.lua b/server/src/method/textDocument/completion.lua index 1b9aa73f..eb33b3ce 100644 --- a/server/src/method/textDocument/completion.lua +++ b/server/src/method/textDocument/completion.lua @@ -80,6 +80,7 @@ return function (lsp, params) for i, item in ipairs(items) do item.sortText = ('%04d'):format(i) + item.insertTextFormat = 2 if item.textEdit then item.textEdit.range = posToRange(lines, item.textEdit.start, item.textEdit.finish) item.textEdit.start = nil diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua index b3cc273f..dd17b463 100644 --- a/server/test/completion/init.lua +++ b/server/test/completion/init.lua @@ -30,7 +30,7 @@ local CompletionItemKind = { TypeParameter = 25, } -local EXISTS = {} +local EXISTS = {'EXISTS'} local function eq(a, b) if a == EXISTS and b ~= nil then @@ -156,7 +156,14 @@ ass$ kind = CompletionItemKind.Function, documentation = EXISTS, detail = '(function)', - } + }, + { + label = 'assert()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + insertText = EXISTS, + detail = '(function)', + }, } TEST [[ @@ -210,7 +217,14 @@ mt:g$ kind = CompletionItemKind.Method, documentation = EXISTS, detail = EXISTS, - } + }, + { + label = 'get()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + insertText = EXISTS, + detail = EXISTS, + }, } TEST [[ @@ -224,6 +238,13 @@ loc$ detail = EXISTS, }, { + label = 'collectgarbage()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + detail = EXISTS, + insertText = EXISTS, + }, + { label = 'local', kind = CompletionItemKind.Keyword, }, @@ -284,6 +305,13 @@ t:$ documentation = EXISTS, detail = EXISTS, }, + { + label = 'b()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + detail = EXISTS, + insertText = EXISTS, + }, } TEST [[ @@ -305,6 +333,13 @@ xxx() documentation = EXISTS, detail = EXISTS, }, + { + label = 'xxx()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + detail = EXISTS, + insertText = EXISTS, + }, } TEST [[ @@ -348,11 +383,25 @@ local t = { detail = EXISTS, }, { + label = 'next()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + detail = EXISTS, + insertText = EXISTS, + }, + { label = 'xpcall', kind = CompletionItemKind.Function, documentation = EXISTS, detail = EXISTS, - } + }, + { + label = 'xpcall()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + detail = EXISTS, + insertText = EXISTS, + }, } TEST [[ @@ -774,12 +823,26 @@ else$ detail = EXISTS, }, { + label = 'select()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + detail = EXISTS, + insertText = EXISTS, + }, + { label = 'setmetatable', kind = CompletionItemKind.Function, documentation = EXISTS, detail = EXISTS, }, { + label = 'setmetatable()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + detail = EXISTS, + insertText = EXISTS, + }, + { label = 'else', kind = CompletionItemKind.Keyword, }, @@ -809,7 +872,14 @@ xpcal$ kind = CompletionItemKind.Function, documentation = EXISTS, detail = EXISTS, - } + }, + { + label = 'xpcall()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + detail = EXISTS, + insertText = EXISTS, + }, } TEST [[ @@ -1095,12 +1165,26 @@ end detail = EXISTS, }, { + label = 'select()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + detail = EXISTS, + insertText = EXISTS, + }, + { label = 'setmetatable', kind = CompletionItemKind.Function, documentation = EXISTS, detail = EXISTS, }, { + label = 'setmetatable()', + kind = CompletionItemKind.Snippet, + documentation = EXISTS, + detail = EXISTS, + insertText = EXISTS, + }, + { label = 'else', kind = CompletionItemKind.Keyword, }, @@ -1323,8 +1407,6 @@ zzz$ label = 'zzzzz', kind = CompletionItemKind.Function, detail = '(function)(4 prototypes)', - insertText = EXISTS, - insertTextFormat = 2, documentation = { kind = 'markdown', value = [[ @@ -1339,6 +1421,13 @@ JustTest ]] }, + }, + { + label = 'zzzzz()', + kind = CompletionItemKind.Snippet, + detail = '(function)(4 prototypes)', + insertText = EXISTS, + documentation = EXISTS, } } |