summaryrefslogtreecommitdiff
path: root/script-beta/core
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-12-09 19:52:01 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-12-09 19:52:01 +0800
commita07c958b56aaa48ce2d9ad7b764df596692bedfb (patch)
tree59e5aa114966b2a6b380a28325ef0ce2d5169d42 /script-beta/core
parent610bd3b1db2d7e27dde61228697a3479474a7bb7 (diff)
downloadlua-language-server-a07c958b56aaa48ce2d9ad7b764df596692bedfb.zip
如果表中所有的值都是确定的整数,则按照整数排序
Diffstat (limited to 'script-beta/core')
-rw-r--r--script-beta/core/hover/table.lua65
1 files changed, 52 insertions, 13 deletions
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