summaryrefslogtreecommitdiff
path: root/script-beta/core/hover
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2020-08-17 18:29:47 +0800
committer最萌小汐 <sumneko@hotmail.com>2020-08-17 18:29:47 +0800
commiteb822b74afdec570e500b8e6778f8d6d30a8d98a (patch)
tree0f5f96155033110c4f8952035ab6df2e269afbef /script-beta/core/hover
parent1ad4e66d410ac0bbca7ebdd4ab1ec5f7dc47af1c (diff)
downloadlua-language-server-eb822b74afdec570e500b8e6778f8d6d30a8d98a.zip
整理代码
Diffstat (limited to 'script-beta/core/hover')
-rw-r--r--script-beta/core/hover/label.lua12
-rw-r--r--script-beta/core/hover/table.lua59
2 files changed, 37 insertions, 34 deletions
diff --git a/script-beta/core/hover/label.lua b/script-beta/core/hover/label.lua
index 57b2ee8e..4f896bc7 100644
--- a/script-beta/core/hover/label.lua
+++ b/script-beta/core/hover/label.lua
@@ -18,9 +18,9 @@ end
local function asValue(source, title)
local name = buildName(source)
- local class = 'any'
- local infers = vm.getInfers(source)
+ local infers = vm.getInfers(source)
local type = vm.getType(source)
+ local class = vm.getClass(source)
local literal = vm.getLiteral(source)
local cont
if vm.hasType(source, 'table') then
@@ -34,12 +34,12 @@ local function asValue(source, title)
if cont then
type = nil
end
- if class == 'any' then
- pack[#pack+1] = type
- else
+ if class then
pack[#pack+1] = class
+ else
+ pack[#pack+1] = type
end
- if literal ~= '' then
+ if literal then
pack[#pack+1] = '='
pack[#pack+1] = literal
end
diff --git a/script-beta/core/hover/table.lua b/script-beta/core/hover/table.lua
index 5b086cd6..97209a4f 100644
--- a/script-beta/core/hover/table.lua
+++ b/script-beta/core/hover/table.lua
@@ -46,25 +46,6 @@ local function getField(src)
return key, class or tp, literal
end
-local function mergeLiteral(a, b)
- if not a then
- return b
- end
- local view = util.viewLiteral(a)
- if not view then
- return b
- end
- if not b then
- return { view, [view] = true }
- end
- if b[view] then
- return b
- end
- b[view] = true
- b[#b+1] = view
- return b
-end
-
local function buildAsHash(classes, literals)
local keys = {}
for k in pairs(classes) do
@@ -77,9 +58,7 @@ local function buildAsHash(classes, literals)
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)
+ lines[#lines+1] = (' %s: %s = %s,'):format(key, class, literal)
else
lines[#lines+1] = (' %s: %s,'):format(key, class)
end
@@ -94,7 +73,7 @@ local function buildAsConst(classes, literals)
keys[#keys+1] = k
end
table.sort(keys, function (a, b)
- return tonumber(literals[a][1]) < tonumber(literals[b][1])
+ return tonumber(literals[a]) < tonumber(literals[b])
end)
local lines = {}
lines[#lines+1] = '{'
@@ -102,9 +81,7 @@ local function buildAsConst(classes, literals)
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)
+ lines[#lines+1] = (' %s: %s = %s,'):format(key, class, literal)
else
lines[#lines+1] = (' %s: %s,'):format(key, class)
end
@@ -113,20 +90,46 @@ local function buildAsConst(classes, literals)
return table.concat(lines, '\n')
end
+local function mergeLiteral(literals)
+ local results = {}
+ local mark = {}
+ for _, value in ipairs(literals) do
+ if not mark[value] then
+ mark[value] = true
+ results[#results+1] = value
+ end
+ end
+ if #results == 0 then
+ return nil
+ end
+ table.sort(results)
+ return table.concat(results, '|')
+end
+
return function (source)
local literals = {}
local classes = {}
local intValue = true
vm.eachField(source, function (src)
local key, class, literal = getField(src)
- classes[key] = guide.mergeTypes {class, classes[key]}
- literals[key] = mergeLiteral(literal, literals[key])
+ if not classes[key] then
+ classes[key] = {}
+ end
+ if not literals[key] then
+ literals[key] = {}
+ end
+ classes[key][#classes[key]+1] = class
+ literals[key][#literals[key]+1] = literal
if class ~= 'integer'
or not literals[key]
or #literals[key] ~= 1 then
intValue = false
end
end)
+ for key, class in pairs(classes) do
+ classes[key] = guide.mergeTypes(class)
+ literals[key] = mergeLiteral(literals[key])
+ end
if classes['[any]'] == 'any' then
classes['[any]'] = nil
end