diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-08-27 20:28:01 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-08-27 20:28:01 +0800 |
commit | a828a593e119ec222e4ef9e6a737234cabe00245 (patch) | |
tree | 8c39fb4a4738bdaa1d6d81173e42ba50903cb368 /server/src | |
parent | f188a3001de879387fafbb3e875ceafa48e74ab9 (diff) | |
download | lua-language-server-a828a593e119ec222e4ef9e6a737234cabe00245.zip |
函数调用的代码片段
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/core/completion.lua | 44 | ||||
-rw-r--r-- | server/src/core/hover/emmy_function.lua | 9 | ||||
-rw-r--r-- | server/src/core/hover/function.lua | 9 | ||||
-rw-r--r-- | server/src/core/hover/lib_function.lua | 17 | ||||
-rw-r--r-- | server/src/core/snippet.lua | 27 |
5 files changed, 76 insertions, 30 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua index 5a403a55..a3994833 100644 --- a/server/src/core/completion.lua +++ b/server/src/core/completion.lua @@ -869,6 +869,49 @@ 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 = {} @@ -894,6 +937,7 @@ local function makeList(source, pos, word) if not data.kind then data.kind = kind end + makeFunctionSnippet(src, data) list[#list+1] = data end, list end diff --git a/server/src/core/hover/emmy_function.lua b/server/src/core/hover/emmy_function.lua index 8c36ea2c..e38e54a4 100644 --- a/server/src/core/hover/emmy_function.lua +++ b/server/src/core/hover/emmy_function.lua @@ -7,6 +7,7 @@ local function buildEmmyArgs(emmy, object, select) start = 1 end local strs = {} + local args = {} local i = 0 emmy:eachParam(function (name, typeObj) i = i + 1 @@ -20,6 +21,7 @@ local function buildEmmyArgs(emmy, object, select) strs[#strs+1] = '@ARG' end strs[#strs+1] = name .. ': ' .. typeObj:getType() + args[#args+1] = strs[#strs] if i == select then strs[#strs+1] = '@ARG' end @@ -40,7 +42,7 @@ local function buildEmmyArgs(emmy, object, select) if #argLabel == 0 then argLabel = nil end - return text, argLabel + return text, argLabel, args end local function buildEmmyReturns(emmy) @@ -115,12 +117,12 @@ local function buildEnum(lib) end return function (name, emmy, object, select) - local args, argLabel = buildEmmyArgs(emmy, object, select) + local argStr, argLabel, args = buildEmmyArgs(emmy, object, select) local returns = buildEmmyReturns(emmy) local enum = buildEnum(emmy) local tip = emmy.description local headLen = #('function %s('):format(name) - local title = ('function %s(%s)%s'):format(name, args, returns) + local title = ('function %s(%s)%s'):format(name, argStr, returns) if argLabel then argLabel[1] = argLabel[1] + headLen argLabel[2] = argLabel[2] + headLen @@ -130,5 +132,6 @@ return function (name, emmy, object, select) description = tip, enum = enum, argLabel = argLabel, + args = args, } end diff --git a/server/src/core/hover/function.lua b/server/src/core/hover/function.lua index 42d6bf6d..eab1ff59 100644 --- a/server/src/core/hover/function.lua +++ b/server/src/core/hover/function.lua @@ -33,6 +33,7 @@ local function buildValueArgs(func, object, select) else max = math.max(#names, #values) end + local args = {} for i = start, max do local name = names[i] local value = values[i] or 'any' @@ -56,6 +57,7 @@ local function buildValueArgs(func, object, select) else strs[#strs+1] = value end + args[#args+1] = strs[#strs] if i == select then strs[#strs+1] = '@ARG' end @@ -95,7 +97,7 @@ local function buildValueArgs(func, object, select) if #argLabel == 0 then argLabel = nil end - return text, argLabel + return text, argLabel, args end local function buildValueReturns(func) @@ -215,13 +217,13 @@ local function getOverLoads(name, func, object, select) end return function (name, func, object, select) - local args, argLabel = buildValueArgs(func, object, select) + local argStr, argLabel, args = buildValueArgs(func, object, select) local returns = buildValueReturns(func) local enum = buildEnum(func) local comment = getComment(func) local overloads = getOverLoads(name, func, object, select) local headLen = #('function %s('):format(name) - local title = ('function %s(%s)%s'):format(name, args, returns) + local title = ('function %s(%s)%s'):format(name, argStr, returns) if argLabel then argLabel[1] = argLabel[1] + headLen argLabel[2] = argLabel[2] + headLen @@ -232,5 +234,6 @@ return function (name, func, object, select) enum = enum, argLabel = argLabel, overloads = overloads, + args = args, } end diff --git a/server/src/core/hover/lib_function.lua b/server/src/core/hover/lib_function.lua index 3aa0cc5a..75e70064 100644 --- a/server/src/core/hover/lib_function.lua +++ b/server/src/core/hover/lib_function.lua @@ -11,6 +11,7 @@ local function buildLibArgs(lib, object, select) start = 1 end local strs = {} + local args = {} for i = start, #lib.args do local arg = lib.args[i] if arg.optional then @@ -28,14 +29,17 @@ local function buildLibArgs(lib, object, select) if i == select then argStr[#argStr+1] = '@ARG' end + local name = '' if arg.name then - argStr[#argStr+1] = ('%s: '):format(arg.name) + name = ('%s: '):format(arg.name) end if type(arg.type) == 'table' then - argStr[#argStr+1] = table.concat(arg.type, '/') + name = name .. table.concat(arg.type, '/') else - argStr[#argStr+1] = arg.type or 'any' + name = name .. (arg.type or 'any') end + argStr[#argStr+1] = name + args[#args+1] = name if arg.default then argStr[#argStr+1] = ('(%q)'):format(arg.default) end @@ -71,7 +75,7 @@ local function buildLibArgs(lib, object, select) if #argLabel == 0 then argLabel = nil end - return text, argLabel + return text, argLabel, args end local function buildLibReturns(lib) @@ -192,13 +196,13 @@ local function buildDoc(lib) end return function (name, lib, object, select) - local args, argLabel = buildLibArgs(lib, object, select) + local argStr, argLabel, args = buildLibArgs(lib, object, select) local returns = buildLibReturns(lib) local enum = buildEnum(lib) local tip = lib.description local doc = buildDoc(lib) local headLen = #('function %s('):format(name) - local title = ('function %s(%s)%s'):format(name, args, returns) + local title = ('function %s(%s)%s'):format(name, argStr, returns) if argLabel then argLabel[1] = argLabel[1] + headLen argLabel[2] = argLabel[2] + headLen @@ -209,5 +213,6 @@ return function (name, lib, object, select) enum = enum, argLabel = argLabel, doc = doc, + args = args, } end diff --git a/server/src/core/snippet.lua b/server/src/core/snippet.lua index a098d6a0..a4458788 100644 --- a/server/src/core/snippet.lua +++ b/server/src/core/snippet.lua @@ -18,8 +18,7 @@ end add('key', 'do', 'do .. end') [[ do $0 -end -]] +end]] add('key', 'elseif', 'elseif .. then') [[elseif ${1:true} then]] @@ -27,50 +26,42 @@ add('key', 'elseif', 'elseif .. then') add('key', 'for', 'for .. in') [[ for ${1:key, value} in ${2:pairs(t)} do $0 -end -]] +end]] add('key', 'for', 'for i = ..') [[ for ${1:i} = ${2:1}, ${3:10, 2} do $0 -end -]] +end]] add('key', 'function', 'function name()') [[ function ${1:name}(${2:arg1, arg2, arg3}) $0 -end -]] +end]] add('key', 'function', 'function ()') [[ function (${1:arg1, arg2, arg3}) $0 -end -]] +end]] add('key', 'local', 'local function') [[ local function ${1:name}(${2:arg1, arg2, arg3}) $0 -end -]] +end]] add('key', 'if', 'if .. then') [[ if ${1:true} then $0 -end -]] +end]] add('key', 'repeat', 'repeat .. until') [[ repeat $0 -until ${1:true} -]] +until ${1:true}]] add('key', 'while', 'while .. do') [[ while ${1:true} do $0 -end -]] +end]] add('key', 'return', 'do return end') [[do return ${1:true} end]] |