diff options
-rw-r--r-- | script/core/completion.lua | 64 | ||||
-rw-r--r-- | test/completion/init.lua | 138 |
2 files changed, 70 insertions, 132 deletions
diff --git a/script/core/completion.lua b/script/core/completion.lua index 6f1a2952..bab0f452 100644 --- a/script/core/completion.lua +++ b/script/core/completion.lua @@ -252,6 +252,26 @@ local function isSameSource(ast, source, pos) return source.start <= pos and source.finish >= pos end +local function getParams(func, oop) + if not func.args then + return '' + end + local args = {} + for _, arg in ipairs(func.args) do + if arg.type == '...' then + args[#args+1] = '...' + elseif arg.type == 'doc.type.arg' then + args[#args+1] = arg.name[1] + else + args[#args+1] = arg[1] + end + end + if oop and args[1] ~= '...' then + table.remove(args, 1) + end + return '(' .. table.concat(args, ', ') .. ')' +end + local function checkLocal(ast, word, offset, results) local locals = guide.getVisibleLocals(ast.ast, offset) for name, source in pairs(locals) do @@ -262,16 +282,22 @@ local function checkLocal(ast, word, offset, results) goto CONTINUE end if vm.hasType(source, 'function') then - buildFunction(results, source, false, { - label = name, - kind = define.CompletionItemKind.Function, - id = stack(function () - return { - detail = buildDetail(source), - description = buildDesc(source), - } - end), - }) + for _, def in ipairs(vm.getDefs(source, 0)) do + if def.type == 'function' + or def.type == 'doc.type.function' then + local funcLabel = name .. getParams(def, false) + buildFunction(results, source, false, { + label = funcLabel, + kind = define.CompletionItemKind.Function, + id = stack(function () + return { + detail = buildDetail(source), + description = buildDesc(source), + } + end), + }) + end + end else results[#results+1] = { label = name, @@ -453,20 +479,6 @@ local function checkFieldThen(name, src, word, start, offset, parent, oop, resul } end -local function getParams(func) - local args = {} - for _, arg in ipairs(func.args) do - if arg.type == '...' then - args[#args+1] = '...' - elseif arg.type == 'doc.type.arg' then - args[#args+1] = arg.name[1] - else - args[#args+1] = arg[1] - end - end - return '(' .. table.concat(args, ', ') .. ')' -end - local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, results, locals, isGlobal) local fields = {} local count = 0 @@ -489,7 +501,7 @@ local function checkFieldOfRefs(refs, ast, word, start, offset, parent, oop, res local value = guide.getObjectValue(src) or src if value.type == 'function' or value.type == 'doc.type.function' then - funcLabel = name .. getParams(value) + funcLabel = name .. getParams(value, oop) fields[funcLabel] = src fields[name] = false count = count + 1 @@ -572,7 +584,7 @@ local function checkCommon(myUri, word, text, offset, results) results.enableCommon = true local used = {} for _, result in ipairs(results) do - used[result.label] = true + used[result.label:match '^[^(]*'] = true end for _, data in ipairs(keyWordMap) do used[data[1]] = true diff --git a/test/completion/init.lua b/test/completion/init.lua index 1344d3af..ebdac49a 100644 --- a/test/completion/init.lua +++ b/test/completion/init.lua @@ -60,6 +60,8 @@ local Cared = { ['deprecated'] = true, } +local IgnoreFunction = false + function TEST(script) return function (expect) files.removeAll() @@ -86,6 +88,16 @@ function TEST(script) end end end + if IgnoreFunction then + for i = #result, 1, -1 do + local item = result[i] + if item.label:find '%(' + and not item.label:find 'function' then + result[i] = result[#result] + result[#result] = nil + end + end + end assert(result) if expect.include then expect.include = nil @@ -217,11 +229,11 @@ ff$ ]] { { - label = 'assert(a, b)', + label = 'ffff(a, b)', kind = define.CompletionItemKind.Function, }, { - label = 'assert(a, b)', + label = 'ffff(a, b)', kind = define.CompletionItemKind.Snippet, } } @@ -285,11 +297,11 @@ mt:g$ ]] { { - label = 'get', + label = 'get(a, b)', kind = define.CompletionItemKind.Method, }, { - label = 'get()', + label = 'get(a, b)', kind = define.CompletionItemKind.Snippet, }, { @@ -311,15 +323,16 @@ loc$ kind = define.CompletionItemKind.Snippet, }, { - label = 'collectgarbage', + label = 'collectgarbage(opt, ...)', kind = define.CompletionItemKind.Function, }, { - label = 'collectgarbage()', + label = 'collectgarbage(opt, ...)', kind = define.CompletionItemKind.Snippet, }, } +IgnoreFunction = true TEST [[ do$ ]] @@ -332,50 +345,6 @@ do$ label = 'do .. end', kind = define.CompletionItemKind.Snippet, }, - { - label = 'dofile', - kind = define.CompletionItemKind.Function, - }, - { - label = 'dofile()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'load', - kind = define.CompletionItemKind.Function, - }, - { - label = 'load()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'loadfile', - kind = define.CompletionItemKind.Function, - }, - { - label = 'loadfile()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'loadstring', - kind = define.CompletionItemKind.Function, - deprecated = true, - }, - { - label = 'loadstring()', - kind = define.CompletionItemKind.Snippet, - deprecated = true, - }, - { - label = 'module', - kind = define.CompletionItemKind.Function, - deprecated = true, - }, - { - label = 'module()', - kind = define.CompletionItemKind.Snippet, - deprecated = true, - }, } TEST [[ @@ -454,6 +423,7 @@ t. $ }, } +IgnoreFunction = false TEST [[ t.a = {} function t:b() @@ -462,7 +432,7 @@ t:$ ]] { { - label = 'b', + label = 'b()', kind = define.CompletionItemKind.Method, }, { @@ -507,6 +477,7 @@ TEST 'local s = "a:$"' (nil) TEST 'debug.$' (EXISTS) +IgnoreFunction = true TEST [[ local xxxx = { xxyy = 1, @@ -530,22 +501,6 @@ local t = { label = 'xxzz', kind = define.CompletionItemKind.Property, }, - { - label = 'next', - kind = define.CompletionItemKind.Function, - }, - { - label = 'next()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'xpcall', - kind = define.CompletionItemKind.Function, - }, - { - label = 'xpcall()', - kind = define.CompletionItemKind.Snippet, - }, } TEST [[ @@ -1030,25 +985,10 @@ else$ label = 'ELSE', kind = define.CompletionItemKind.Enum, }, - { - label = 'select', - kind = define.CompletionItemKind.Function, - }, - { - label = 'select()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'setmetatable', - kind = define.CompletionItemKind.Function, - }, - { - label = 'setmetatable()', - kind = define.CompletionItemKind.Snippet, - }, } Cared['insertText'] = true +IgnoreFunction = false TEST [[ local xpcal xpcal$ @@ -1059,11 +999,11 @@ xpcal$ kind = define.CompletionItemKind.Variable, }, { - label = 'xpcall', + label = 'xpcall(f, msgh, arg1, ...)', kind = define.CompletionItemKind.Function, }, { - label = 'xpcall()', + label = 'xpcall(f, msgh, arg1, ...)', kind = define.CompletionItemKind.Snippet, insertText = EXISTS, }, @@ -1077,11 +1017,11 @@ mt:f$ ]] { { - label = 'f', + label = 'f(a, b, c)', kind = define.CompletionItemKind.Method, }, { - label = 'f()', + label = 'f(a, b, c)', kind = define.CompletionItemKind.Snippet, insertText = 'f(${1:a: any}, ${2:b: any}, ${3:c: any})', }, @@ -1123,6 +1063,7 @@ end", }, } Cared['insertText'] = false +IgnoreFunction = true TEST [[ local function f() @@ -1143,22 +1084,6 @@ end label = 'elseif .. then', kind = define.CompletionItemKind.Snippet, }, - { - label = 'select', - kind = define.CompletionItemKind.Function, - }, - { - label = 'select()', - kind = define.CompletionItemKind.Snippet, - }, - { - label = 'setmetatable', - kind = define.CompletionItemKind.Function, - }, - { - label = 'setmetatable()', - kind = define.CompletionItemKind.Snippet, - }, } TEST [[ @@ -1259,17 +1184,18 @@ io$ ]] (EXISTS) +IgnoreFunction = false TEST [[ loadstring$ ]] { { - label = 'loadstring', + label = 'loadstring(text, chunkname)', kind = define.CompletionItemKind.Function, deprecated = true, }, { - label = 'loadstring()', + label = 'loadstring(text, chunkname)', kind = define.CompletionItemKind.Snippet, deprecated = true, }, @@ -1293,7 +1219,7 @@ loadstring$ ]] { { - label = 'loadstring', + label = 'loadstring()', kind = define.CompletionItemKind.Function, }, { |