diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-06-26 16:42:19 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-06-26 16:42:19 +0800 |
commit | f8407a7ed2faa1892ab63fbaed5539420af90180 (patch) | |
tree | 68466bb0b0ccc6512bd52cfe45b4059ea4b2bd66 | |
parent | e98245ae3492ce132ddfe9153b1a5a80f3a3e3e4 (diff) | |
download | lua-language-server-f8407a7ed2faa1892ab63fbaed5539420af90180.zip |
自动完成对多原型函数的支持
-rw-r--r-- | server/main.lua | 2 | ||||
-rw-r--r-- | server/src/core/completion.lua | 29 | ||||
-rw-r--r-- | server/test/completion/init.lua | 69 | ||||
-rw-r--r-- | server/test/crossfile/completion.lua | 10 |
4 files changed, 94 insertions, 16 deletions
diff --git a/server/main.lua b/server/main.lua index 9ba8fc92..61f17f2f 100644 --- a/server/main.lua +++ b/server/main.lua @@ -22,7 +22,7 @@ log.info('Lua Lsp startup, root: ', ROOT) log.debug('ROOT:', ROOT:string()) ac = {} -xpcall(dofile, log.debug, rootPath .. 'debugger.lua') +--xpcall(dofile, log.debug, rootPath .. 'debugger.lua') require 'utility' require 'global_protect' local service = require 'service' diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index a938d83c..34eecb0e 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -82,25 +82,40 @@ end local function getDetail(value) local literal = value:getLiteral() local tp = type(literal) + local detals = {} + if value:getType() ~= 'any' then + detals[#detals+1] = ('(%s)'):format(value:getType()) + end if tp == 'boolean' then - return ('= %q'):format(literal) + detals[#detals+1] = (' = %q'):format(literal) elseif tp == 'string' then - return ('= %q'):format(literal) + detals[#detals+1] = (' = %q'):format(literal) elseif tp == 'number' then if math.type(literal) == 'integer' then - return ('= %q'):format(literal) + detals[#detals+1] = (' = %q'):format(literal) else - local str = ('= %.16f'):format(literal) + local str = (' = %.16f'):format(literal) local dot = str:find('.', 1, true) local suffix = str:find('[0]+$', dot + 2) if suffix then - return str:sub(1, suffix - 1) + detals[#detals+1] = str:sub(1, suffix - 1) else - return str + detals[#detals+1] = str end end end - return nil + if value:getType() == 'function' then + ---@type emmyFunction + local func = value:getFunction() + local overLoads = func and func:getEmmyOverLoads() + if overLoads then + detals[#detals+1] = ('(%d 个原型)'):format(#overLoads + 1) + end + end + if #detals == 0 then + return nil + end + return table.concat(detals) end local function getKind(cata, value) diff --git a/server/test/completion/init.lua b/server/test/completion/init.lua index 16928d6c..2b63358e 100644 --- a/server/test/completion/init.lua +++ b/server/test/completion/init.lua @@ -155,6 +155,7 @@ ass$ label = 'assert', kind = CompletionItemKind.Function, documentation = EXISTS, + detail = '(function)', } } @@ -166,7 +167,7 @@ z$ { label = 'zabc', kind = CompletionItemKind.Variable, - detail = '= 1', + detail = '(number) = 1', } } @@ -178,7 +179,7 @@ z$ { label = 'zabc', kind = CompletionItemKind.Variable, - detail = '= 1.0', + detail = '(number) = 1.0', } } @@ -192,7 +193,7 @@ t.a$ { label = 'abc', kind = CompletionItemKind.Enum, - detail = '= 1', + detail = '(number) = 1', } } @@ -208,6 +209,7 @@ mt:g$ label = 'get', kind = CompletionItemKind.Method, documentation = EXISTS, + detail = EXISTS, } } @@ -219,6 +221,7 @@ loc$ label = 'collectgarbage', kind = CompletionItemKind.Function, documentation = EXISTS, + detail = EXISTS, }, { label = 'local', @@ -235,10 +238,12 @@ t.$ { label = 'a', kind = CompletionItemKind.Field, + detail = EXISTS, }, { label = 'b', kind = CompletionItemKind.Field, + detail = EXISTS, }, } @@ -251,10 +256,12 @@ t. $ { label = 'a', kind = CompletionItemKind.Field, + detail = EXISTS, }, { label = 'b', kind = CompletionItemKind.Field, + detail = EXISTS, }, } @@ -269,6 +276,7 @@ t:$ label = 'b', kind = CompletionItemKind.Method, documentation = EXISTS, + detail = EXISTS, }, } @@ -283,11 +291,13 @@ xxx() { label = 'a', kind = CompletionItemKind.Field, + detail = EXISTS, }, { label = 'xxx', kind = CompletionItemKind.Function, documentation = EXISTS, + detail = EXISTS, }, } @@ -315,6 +325,7 @@ local t = { { label = 'xxxx', kind = CompletionItemKind.Variable, + detail = EXISTS, }, { label = 'xxyy', @@ -328,11 +339,13 @@ local t = { label = 'next', kind = CompletionItemKind.Function, documentation = EXISTS, + detail = EXISTS, }, { label = 'xpcall', kind = CompletionItemKind.Function, documentation = EXISTS, + detail = EXISTS, } } @@ -511,6 +524,7 @@ self.results.list[#self.re$] { label = 'results', kind = CompletionItemKind.Field, + detail = EXISTS, }, } @@ -530,6 +544,7 @@ fff[#ff$] { label = 'fff', kind = CompletionItemKind.Field, + detail = EXISTS, } } @@ -648,10 +663,12 @@ end { label = 'a', kind = CompletionItemKind.Field, + detail = EXISTS, }, { label = 'b', kind = CompletionItemKind.Field, + detail = EXISTS, }, } @@ -724,6 +741,7 @@ t.$ { label = '__index', kind = CompletionItemKind.Field, + detail = EXISTS, } } @@ -747,11 +765,13 @@ else$ label = 'select', kind = CompletionItemKind.Function, documentation = EXISTS, + detail = EXISTS, }, { label = 'setmetatable', kind = CompletionItemKind.Function, documentation = EXISTS, + detail = EXISTS, }, { label = 'else', @@ -776,6 +796,7 @@ xpcal$ label = 'xpcall', kind = CompletionItemKind.Function, documentation = EXISTS, + detail = EXISTS, } } @@ -1039,10 +1060,12 @@ mt.$ { label = 'id', kind = CompletionItemKind.Field, + detail = EXISTS, }, { label = 'name', kind = CompletionItemKind.Field, + detail = EXISTS, }, } @@ -1057,11 +1080,13 @@ end label = 'select', kind = CompletionItemKind.Function, documentation = EXISTS, + detail = EXISTS, }, { label = 'setmetatable', kind = CompletionItemKind.Function, documentation = EXISTS, + detail = EXISTS, }, { label = 'else', @@ -1215,6 +1240,7 @@ t.$ { label = 'a.b.c', kind = CompletionItemKind.Field, + detail = EXISTS, textEdit = { start = 37, finish = 36, @@ -1239,6 +1265,7 @@ z$ { label = 'z.b.c', kind = CompletionItemKind.Field, + detail = EXISTS, textEdit = { start = 22, finish = 21, @@ -1258,3 +1285,39 @@ TEST [[ io.close(1, $) ]] (nil) + +TEST [[ +--- JustTest +---@overload fun(list:table):string +---@overload fun(list:table, sep:string):string +---@overload fun(list:table, sep:string, i:number):string +---@param list table +---@param sep string +---@param i number +---@param j number +---@return string +local function zzzzz(list, sep, i, j) end + +zzz$ +]] +{ + { + label = 'zzzzz', + kind = CompletionItemKind.Function, + detail = '(function)(4 个原型)', + documentation = { + kind = 'markdown', + value = [[ +```lua +function zzzzz(list: table, sep: string, i: number, j: number) + -> string +``` +JustTest +```lua + +``` + +]] + }, + } +} diff --git a/server/test/crossfile/completion.lua b/server/test/crossfile/completion.lua index adfd3cfd..cb9f2f65 100644 --- a/server/test/crossfile/completion.lua +++ b/server/test/crossfile/completion.lua @@ -445,17 +445,17 @@ TEST { { label = 'a', kind = CompletionItemKind.Enum, - detail = '= 1', + detail = '(number) = 1', }, { label = 'b', kind = CompletionItemKind.Enum, - detail = '= 2', + detail = '(number) = 2', }, { label = 'c', kind = CompletionItemKind.Enum, - detail = '= 3', + detail = '(number) = 3', }, } } @@ -490,7 +490,7 @@ TEST { { label = 'zabcdef', kind = CompletionItemKind.Enum, - detail = '= 1', + detail = '(number) = 1', }, } } @@ -525,7 +525,7 @@ TEST { { label = 'zabcdef', kind = CompletionItemKind.Enum, - detail = '= 1', + detail = '(number) = 1', }, } } |