diff options
author | 最萌小汐 <sumneko@hotmail.com> | 2021-05-15 17:30:22 +0800 |
---|---|---|
committer | 最萌小汐 <sumneko@hotmail.com> | 2021-05-15 17:30:22 +0800 |
commit | 91c8da46833320274f874250d21b71da05cf9689 (patch) | |
tree | 4e6fde10bb427e03f4cf17a9b195ac8cfa41b857 /script/core | |
parent | ed85e835a9ee6b6e4e36a8bcf8772a92c165558b (diff) | |
download | lua-language-server-91c8da46833320274f874250d21b71da05cf9689.zip |
update generic
Diffstat (limited to 'script/core')
-rw-r--r-- | script/core/generic.lua | 14 | ||||
-rw-r--r-- | script/core/infer.lua | 22 | ||||
-rw-r--r-- | script/core/linker.lua | 30 |
3 files changed, 60 insertions, 6 deletions
diff --git a/script/core/generic.lua b/script/core/generic.lua index 16840b9c..a7baf090 100644 --- a/script/core/generic.lua +++ b/script/core/generic.lua @@ -160,10 +160,12 @@ local function buildValues(closure) if doc.type == 'doc.param' then local extends = doc.extends local index = extends.paramIndex - local param = params and params[index] - closure.params[index] = param and createValue(closure, extends, function (road, key, proto) - buildValue(road, key, proto, param, upvalues) - end) or extends + if index then + local param = params and params[index] + closure.params[index] = param and createValue(closure, extends, function (road, key, proto) + buildValue(road, key, proto, param, upvalues) + end) or extends + end end end for _, doc in ipairs(protoFunction.bindDocs) do @@ -177,7 +179,7 @@ local function buildValues(closure) if protoFunction.type == 'doc.type.function' then for index, arg in ipairs(protoFunction.args) do local extends = arg.extends - local param = params[index] + local param = params and params[index] closure.params[index] = param and createValue(closure, extends, function (road, key, proto) buildValue(road, key, proto, param, upvalues) end) or extends @@ -195,6 +197,8 @@ function m.createClosure(proto, call) local protoFunction, parentClosure if proto.type == 'function' then protoFunction = proto + elseif proto.type == 'doc.type.function' then + protoFunction = proto elseif proto.type == 'generic.value' then protoFunction = proto.proto parentClosure = proto.closure diff --git a/script/core/infer.lua b/script/core/infer.lua index 01cd7aaf..c46f26cf 100644 --- a/script/core/infer.lua +++ b/script/core/infer.lua @@ -8,6 +8,18 @@ local BE_CONNACT = {'BE_CONNACT'} local CLASS = {'CLASS'} local TABLE = {'TABLE'} +local TypeSort = { + ['boolean'] = 1, + ['string'] = 2, + ['integer'] = 3, + ['number'] = 4, + ['table'] = 5, + ['function'] = 6, + ['true'] = 101, + ['false'] = 102, + ['nil'] = 999, +} + local m = {} local function mergeTable(a, b) @@ -240,7 +252,15 @@ function m.viewInfers(infers) infers[0] = 'any' return 'any' end - table.sort(infers) + table.sort(result, function (a, b) + local sa = TypeSort[a] or 100 + local sb = TypeSort[b] or 100 + if sa == sb then + return a < b + else + return sa < sb + end + end) infers[0] = table.concat(result, '|') return infers[0] end diff --git a/script/core/linker.lua b/script/core/linker.lua index 1bd47d3c..b1dc8476 100644 --- a/script/core/linker.lua +++ b/script/core/linker.lua @@ -346,6 +346,26 @@ m.PARAM_INDEX = PARAM_INDEX m.TABLE_KEY = TABLE_KEY m.ANY_FIELD = ANY_FIELD +--- 寻找doc的主体 +---@param obj parser.guide.object +---@return parser.guide.object +local function getDocStateWithoutCrossFunction(obj) + for _ = 1, 1000 do + local parent = obj.parent + if not parent then + return obj + end + if parent.type == 'doc' then + return obj + end + if parent.type == 'doc.type.function' then + return nil + end + obj = parent + end + error('guide.getDocState overstack') +end + ---添加关联单元 ---@param source parser.guide.object function m.pushSource(source) @@ -538,6 +558,16 @@ function m.compileLink(source) pushForward(returnID, getID(rtn)) end end + -- @type fun(x: T):T 的情况 + local docType = getDocStateWithoutCrossFunction(source) + if docType and docType.type == 'doc.type' then + guide.eachSourceType(source, 'doc.type.name', function (typeName) + if typeName.typeGeneric then + source.isGeneric = true + return false + end + end) + end end if source.type == 'doc.type.table' then if source.tkey then |