diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2019-11-29 17:41:07 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2019-11-29 17:41:07 +0800 |
commit | b5d452f3cc7315940e860a21c9bb15673e544d9c (patch) | |
tree | e41be9b3e923eef40adb4a62b77e03b4797099b0 | |
parent | cbf18dc54d2d2315ae3bace888fc428550149c7a (diff) | |
download | lua-language-server-b5d452f3cc7315940e860a21c9bb15673e544d9c.zip |
支持 lib 函数的 arg 与 return
-rw-r--r-- | script-beta/core/hover/arg.lua | 22 | ||||
-rw-r--r-- | script-beta/core/hover/init.lua | 4 | ||||
-rw-r--r-- | script-beta/core/hover/label.lua | 10 | ||||
-rw-r--r-- | script-beta/core/hover/name.lua | 44 | ||||
-rw-r--r-- | script-beta/vm/getValue.lua | 15 | ||||
-rw-r--r-- | test-beta/hover/init.lua | 11 |
6 files changed, 82 insertions, 24 deletions
diff --git a/script-beta/core/hover/arg.lua b/script-beta/core/hover/arg.lua index 70285e6b..268ceecf 100644 --- a/script-beta/core/hover/arg.lua +++ b/script-beta/core/hover/arg.lua @@ -1,7 +1,7 @@ local guide = require 'parser.guide' local vm = require 'vm' -local function asFunction(source) +local function asFunction(source, caller) if not source.args then return '' end @@ -15,11 +15,25 @@ local function asFunction(source) args[i] = ('%s'):format(vm.getType(arg)) end end - return table.concat(args, ', ') + local methodDef, methodCall + local parent = source.parent + if parent and parent.type == 'setmethod' then + methodDef = true + end + if caller.type == 'method' + or caller.type == 'getmethod' + or caller.type == 'setmethod' then + methodCall = true + end + if not methodDef and methodCall then + return table.concat(args, ', ', 2) + else + return table.concat(args, ', ') + end end -return function (source) +return function (source, caller) if source.type == 'function' then - return asFunction(source) + return asFunction(source, caller) end end diff --git a/script-beta/core/hover/init.lua b/script-beta/core/hover/init.lua index b99c14b2..ae4df936 100644 --- a/script-beta/core/hover/init.lua +++ b/script-beta/core/hover/init.lua @@ -8,7 +8,7 @@ local function getHoverAsFunction(source) local labels = {} for _, value in ipairs(values) do if value.type == 'function' then - labels[#labels+1] = getLabel(value.source) + labels[#labels+1] = getLabel(value.source, source) end end @@ -20,7 +20,7 @@ local function getHoverAsFunction(source) end local function getHoverAsValue(source) - local label = getLabel(source) + local label = getLabel(source, source) return { label = label, source = source, diff --git a/script-beta/core/hover/label.lua b/script-beta/core/hover/label.lua index f5d49bb0..e9b987e1 100644 --- a/script-beta/core/hover/label.lua +++ b/script-beta/core/hover/label.lua @@ -6,9 +6,9 @@ local getClass = require 'core.hover.class' local vm = require 'vm' local util = require 'utility' -local function asFunction(source) - local name = buildName(source) - local arg = buildArg(source) +local function asFunction(source, caller) + local name = buildName(source, caller) + local arg = buildArg(source, caller) local rtn = buildReturn(source) local lines = {} lines[1] = ('function %s(%s)'):format(name, arg) @@ -89,9 +89,9 @@ local function asLibrary(source) end end -return function (source) +return function (source, caller) if source.type == 'function' then - return asFunction(source) + return asFunction(source, caller) elseif source.type == 'local' or source.type == 'getlocal' or source.type == 'setlocal' then diff --git a/script-beta/core/hover/name.lua b/script-beta/core/hover/name.lua index 3d855ee5..7b8cf449 100644 --- a/script-beta/core/hover/name.lua +++ b/script-beta/core/hover/name.lua @@ -5,14 +5,14 @@ local function asLocal(source) return guide.getName(source) end -local function asMethod(source) +local function asMethod(source, caller) local class = getClass(source.node) local node = class or guide.getName(source.node) or '?' local method = guide.getName(source) return ('%s:%s'):format(node, method) end -local function asField(source) +local function asField(source, caller) local class = getClass(source.node) local node = class or guide.getName(source.node) or '?' local method = guide.getName(source) @@ -27,13 +27,39 @@ local function asGlobal(source) return guide.getName(source) end -local function asLibrary(source) - return source.doc or source.name +local function asLibrary(source, caller) + local p + if caller.type == 'method' + or caller.type == 'getmethod' + or caller.type == 'setmethod' then + if source.parent then + for _, parent in ipairs(source.parent) do + if parent.type == 'object' then + p = parent.name .. ':' + break + end + end + end + else + if source.parent then + for _, parent in ipairs(source.parent) do + if parent.type == 'global' then + p = parent.name .. '.' + break + end + end + end + end + if p then + return ('%s%s'):format(p, source.name) + else + return source.name + end end -local function buildName(source) +local function buildName(source, caller) if source.library then - return asLibrary(source) or '' + return asLibrary(source, caller) or '' end if source.type == 'local' or source.type == 'getlocal' @@ -46,18 +72,18 @@ local function buildName(source) end if source.type == 'setmethod' or source.type == 'getmethod' then - return asMethod(source) or '' + return asMethod(source, caller) or '' end if source.type == 'setfield' or source.type == 'getfield' then - return asField(source) or '' + return asField(source, caller) or '' end if source.type == 'tablefield' then return asTableField(source) or '' end local parent = source.parent if parent then - return buildName(parent) + return buildName(parent, caller) end return '' end diff --git a/script-beta/vm/getValue.lua b/script-beta/vm/getValue.lua index ef3a5504..67954360 100644 --- a/script-beta/vm/getValue.lua +++ b/script-beta/vm/getValue.lua @@ -538,6 +538,20 @@ local function checkDef(results, source) end) end +local function checkLibraryTypes(source) + if type(source.type) ~= 'table' then + return nil + end + local results = {} + for i = 1, #source.type do + insert(results, { + type = source.type[i], + source = source, + }) + end + return results +end + local function checkLibrary(source) local lib = vm.getLibrary(source) if not lib then @@ -730,6 +744,7 @@ local function getValue(source) or checkValue(source) or checkUnary(source) or checkBinary(source) + or checkLibraryTypes(source) or checkLibrary(source) or checkLibraryReturn(source) or checkLibraryArg(source) diff --git a/test-beta/hover/init.lua b/test-beta/hover/init.lua index 6d268cc8..28f5a0d4 100644 --- a/test-beta/hover/init.lua +++ b/test-beta/hover/init.lua @@ -242,7 +242,7 @@ TEST[[ ('xx'):<?sub?>() ]] [[function string:sub(i: integer, j: integer) - -> any]] + -> string]] TEST [[ local <?v?> = collectgarbage() @@ -255,12 +255,14 @@ w2l:get_default()[<?type?>] ]] "local type: any" +-- TODO 可选参数(或多原型) TEST [[ <?load?>() ]] [=[ -function load(chunk: string/function [, chunkname: string [, mode: string [, env: table]]]) - -> function, error_message: string +function load(chunk: string|function, chunkname: string, mode: string, env: table) + -> function + 2. error_message: string ]=] TEST [[ @@ -271,6 +273,7 @@ function string.lower(string) -> string ]] +-- TODO 不根据传入值推测参数类型 TEST [[ local function x(a, ...) end @@ -278,7 +281,7 @@ end <?x?>(1, 2, 3, 4, 5, 6, 7) ]] [[ -function x(a: number, ...) +function x(a: any, ...) ]] TEST [[ |