summaryrefslogtreecommitdiff
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
parent1ad4e66d410ac0bbca7ebdd4ab1ec5f7dc47af1c (diff)
downloadlua-language-server-eb822b74afdec570e500b8e6778f8d6d30a8d98a.zip
整理代码
-rw-r--r--script-beta/core/hover/label.lua12
-rw-r--r--script-beta/core/hover/table.lua59
-rw-r--r--script-beta/parser/guide.lua25
-rw-r--r--script-beta/vm/getInfer.lua3
-rw-r--r--script-beta/vm/vm.lua18
5 files changed, 52 insertions, 65 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
diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua
index 824c7c6d..5ef81a18 100644
--- a/script-beta/parser/guide.lua
+++ b/script-beta/parser/guide.lua
@@ -1862,24 +1862,23 @@ function m.allocInfer(o)
end
end
-function m.mergeTypes(infers)
- local types = {}
+function m.mergeTypes(types)
+ local results = {}
local mark = {}
- for i = 1, #infers do
- for tp in infers[i]:gmatch '[^|]+' do
- if not mark[tp] and tp ~= 'any' then
- mark[tp] = true
- types[#types+1] = tp
- end
+ for i = 1, #types do
+ local tp = types[i]
+ if not mark[tp] and tp ~= 'any' then
+ mark[tp] = true
+ results[#results+1] = tp
end
end
- if #types == 0 then
+ if #results == 0 then
return 'any'
end
- if #types == 1 then
- return types[1]
+ if #results == 1 then
+ return results[1]
end
- tableSort(types, function (a, b)
+ tableSort(results, function (a, b)
local sa = TypeSort[a]
local sb = TypeSort[b]
if sa and sb then
@@ -1896,7 +1895,7 @@ function m.mergeTypes(infers)
end
return false
end)
- return tableConcat(types, '|')
+ return tableConcat(results, '|')
end
function m.viewInferType(infers)
diff --git a/script-beta/vm/getInfer.lua b/script-beta/vm/getInfer.lua
index c27fc6d2..194f78b4 100644
--- a/script-beta/vm/getInfer.lua
+++ b/script-beta/vm/getInfer.lua
@@ -32,6 +32,9 @@ function vm.getLiteral(source)
literals[#literals+1] = util.viewLiteral(value)
end
end
+ if #literals == 0 then
+ return nil
+ end
table.sort(literals)
return table.concat(literals, '|')
end
diff --git a/script-beta/vm/vm.lua b/script-beta/vm/vm.lua
index d5028bdc..907532d8 100644
--- a/script-beta/vm/vm.lua
+++ b/script-beta/vm/vm.lua
@@ -33,24 +33,6 @@ function m.lock(tp, source)
end
end
---- 获取link的uri
-function m.getLinkUris(call)
- local workspace = require 'workspace'
- local func = call.node
- local name = func.special
- if name == 'require' then
- local args = call.args
- if not args[1] then
- return nil
- end
- local literal = guide.getLiteral(args[1])
- if type(literal) ~= 'string' then
- return nil
- end
- return workspace.findUrisByRequirePath(literal, true)
- end
-end
-
function m.isSet(src)
local tp = src.type
if tp == 'setglobal'