From a07c958b56aaa48ce2d9ad7b764df596692bedfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=80=E8=90=8C=E5=B0=8F=E6=B1=90?= Date: Mon, 9 Dec 2019 19:52:01 +0800 Subject: =?UTF-8?q?=E5=A6=82=E6=9E=9C=E8=A1=A8=E4=B8=AD=E6=89=80=E6=9C=89?= =?UTF-8?q?=E7=9A=84=E5=80=BC=E9=83=BD=E6=98=AF=E7=A1=AE=E5=AE=9A=E7=9A=84?= =?UTF-8?q?=E6=95=B4=E6=95=B0=EF=BC=8C=E5=88=99=E6=8C=89=E7=85=A7=E6=95=B4?= =?UTF-8?q?=E6=95=B0=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script-beta/core/hover/table.lua | 65 ++++++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 13 deletions(-) (limited to 'script-beta/core') diff --git a/script-beta/core/hover/table.lua b/script-beta/core/hover/table.lua index 72af66e7..c6d93d03 100644 --- a/script-beta/core/hover/table.lua +++ b/script-beta/core/hover/table.lua @@ -60,25 +60,37 @@ local function mergeLiteral(a, b) return b end -return function (source) - local literals = {} - local classes = {} - vm.eachField(source, function (info) - local key, class, literal = getField(info) - classes[key] = vm.mergeTypeViews(class, classes[key]) - literals[key] = mergeLiteral(literal, literals[key]) - end) - if classes['[any]'] == 'any' then - classes['[any]'] = nil +local function buildAsHash(classes, literals) + local keys = {} + for k in pairs(classes) do + keys[#keys+1] = k end - if not next(classes) then - return '{}' + table.sort(keys) + local lines = {} + lines[#lines+1] = '{' + for _, key in ipairs(keys) do + local class = classes[key] + local literal = literals[key] + if literal then + table.sort(literal) + local view = table.concat(literal, '|') + lines[#lines+1] = (' %s: %s = %s,'):format(key, class, view) + else + lines[#lines+1] = (' %s: %s,'):format(key, class) + end end + lines[#lines+1] = '}' + return table.concat(lines, '\n') +end + +local function buildAsConst(classes, literals) local keys = {} for k in pairs(classes) do keys[#keys+1] = k end - table.sort(keys) + table.sort(keys, function (a, b) + return literals[a][1] < literals[b][1] + end) local lines = {} lines[#lines+1] = '{' for _, key in ipairs(keys) do @@ -95,3 +107,30 @@ return function (source) lines[#lines+1] = '}' return table.concat(lines, '\n') end + +return function (source) + local literals = {} + local classes = {} + local intValue = true + vm.eachField(source, function (info) + local key, class, literal = getField(info) + classes[key] = vm.mergeTypeViews(class, classes[key]) + literals[key] = mergeLiteral(literal, literals[key]) + if class ~= 'integer' + or not literals[key] + or #literals[key] ~= 1 then + intValue = false + end + end) + if classes['[any]'] == 'any' then + classes['[any]'] = nil + end + if not next(classes) then + return '{}' + end + if intValue then + return buildAsConst(classes, literals) + else + return buildAsHash(classes, literals) + end +end -- cgit v1.2.3