summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-12-05 17:52:03 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-12-05 17:52:03 +0800
commit14f38cf2671ce044dd2ee752b3d66dbbaf7b83aa (patch)
treef56f2d7c1d86557a6be502aa3118e00697b73dc1
parent51185264ab76b43571689dd717b3e685d4ff9c73 (diff)
downloadlua-language-server-14f38cf2671ce044dd2ee752b3d66dbbaf7b83aa.zip
更新hover
-rw-r--r--script-beta/core/hover/table.lua35
-rw-r--r--script-beta/parser/guide.lua3
-rw-r--r--script-beta/vm/getValue.lua89
-rw-r--r--test-beta/hover/init.lua22
4 files changed, 62 insertions, 87 deletions
diff --git a/script-beta/core/hover/table.lua b/script-beta/core/hover/table.lua
index f625d973..dba70860 100644
--- a/script-beta/core/hover/table.lua
+++ b/script-beta/core/hover/table.lua
@@ -3,22 +3,42 @@ local util = require 'utility'
local getClass = require 'core.hover.class'
local function getKey(info)
- if not info.key then
- return 'any'
+ if not info.key or #info.key <= 2 then
+ local source = info.source
+ if not source.index then
+ return '[any]'
+ end
+ local class = getClass(source.index)
+ if class then
+ return ('[%s]'):format(class)
+ end
+ local tp = vm.getType(source.index)
+ if tp then
+ return ('[%s]'):format(tp)
+ end
+ return '[any]'
end
local ktype = info.key:sub(1, 2)
+ local key = info.key:sub(3)
if ktype == 's|' then
- return info.key:sub(3)
+ if key:match '^[%a_][%w_]*$' then
+ return key
+ else
+ return ('[%s]'):format(util.viewLiteral(key))
+ end
end
- return ('[%s]'):format(info.key:sub(3))
+ return ('[%s]'):format(key)
end
local function getField(info)
- local type = vm.getType(info.source)
+ local tp = vm.getType(info.source)
local class = getClass(info.source)
local literal = vm.getLiteral(info.source)
local key = getKey(info)
- return key, class or type, literal
+ if type(literal) == 'string' and #literal >= 50 then
+ literal = literal:sub(1, 47) .. '...'
+ end
+ return key, class or tp, literal
end
local function mergeLiteral(a, b)
@@ -45,6 +65,9 @@ return function (source)
classes[key] = vm.mergeTypeViews(class, classes[key])
literals[key] = mergeLiteral(literal, literals[key])
end)
+ if classes['[any]'] == 'any' then
+ classes['[any]'] = nil
+ end
if not next(classes) then
return '{}'
end
diff --git a/script-beta/parser/guide.lua b/script-beta/parser/guide.lua
index af511555..2e26a3da 100644
--- a/script-beta/parser/guide.lua
+++ b/script-beta/parser/guide.lua
@@ -2,6 +2,7 @@ local error = error
local type = type
local next = next
local tostring = tostring
+local util = require 'utility'
_ENV = nil
@@ -510,7 +511,7 @@ function m.getKeyName(obj)
elseif tp == 'number' then
local n = obj[1]
if n then
- return ('n|%q'):format(obj[1])
+ return ('n|%s'):format(util.viewLiteral(obj[1]))
else
return 'n'
end
diff --git a/script-beta/vm/getValue.lua b/script-beta/vm/getValue.lua
index f4b1ff68..f6b3ff53 100644
--- a/script-beta/vm/getValue.lua
+++ b/script-beta/vm/getValue.lua
@@ -168,6 +168,16 @@ local function checkUnary(source)
end
end
+local function mathCheck(a, b)
+ local v1 = vm.getLiteral(a, 'integer') or vm.getLiteral(a, 'number')
+ local v2 = vm.getLiteral(b, 'integer') or vm.getLiteral(a, 'number')
+ local int = vm.hasType(a, 'integer')
+ and vm.hasType(b, 'integer')
+ and not vm.hasType(a, 'number')
+ and not vm.hasType(b, 'number')
+ return int and 'integer' or 'number', v1, v2
+end
+
local function checkBinary(source)
if source.type ~= 'binary' then
return
@@ -383,87 +393,37 @@ local function checkBinary(source)
}
-- 其他数学运算根据2侧的值决定,当2侧的值均为整数时返回整数
elseif op.type == '+' then
- local v1 = vm.getLiteral(source[1], 'integer')
- local v2 = vm.getLiteral(source[2], 'integer')
- if v1 and v2 then
- return alloc {
- type = 'integer',
- value = v1 + v2,
- source = source,
- }
- end
- v1 = v1 or vm.getLiteral(source[1], 'number')
- v2 = v2 or vm.getLiteral(source[1], 'number')
+ local int, v1, v2 = mathCheck(source[1], source[2])
return alloc {
- type = 'number',
+ type = int,
value = (v1 and v2) and (v1 + v2) or nil,
source = source,
}
elseif op.type == '-' then
- local v1 = vm.getLiteral(source[1], 'integer')
- local v2 = vm.getLiteral(source[2], 'integer')
- if v1 and v2 then
- return alloc {
- type = 'integer',
- value = v1 - v2,
- source = source,
- }
- end
- v1 = v1 or vm.getLiteral(source[1], 'number')
- v2 = v2 or vm.getLiteral(source[1], 'number')
+ local int, v1, v2 = mathCheck(source[1], source[2])
return alloc {
- type = 'number',
+ type = int,
value = (v1 and v2) and (v1 - v2) or nil,
source = source,
}
elseif op.type == '*' then
- local v1 = vm.getLiteral(source[1], 'integer')
- local v2 = vm.getLiteral(source[2], 'integer')
- if v1 and v2 then
- return alloc {
- type = 'integer',
- value = v1 * v2,
- source = source,
- }
- end
- v1 = v1 or vm.getLiteral(source[1], 'number')
- v2 = v2 or vm.getLiteral(source[1], 'number')
+ local int, v1, v2 = mathCheck(source[1], source[2])
return alloc {
- type = 'number',
+ type = int,
value = (v1 and v2) and (v1 * v2) or nil,
source = source,
}
elseif op.type == '%' then
- local v1 = vm.getLiteral(source[1], 'integer')
- local v2 = vm.getLiteral(source[2], 'integer')
- if v1 and v2 then
- return alloc {
- type = 'integer',
- value = v1 % v2,
- source = source,
- }
- end
- v1 = v1 or vm.getLiteral(source[1], 'number')
- v2 = v2 or vm.getLiteral(source[1], 'number')
+ local int, v1, v2 = mathCheck(source[1], source[2])
return alloc {
- type = 'number',
+ type = int,
value = (v1 and v2) and (v1 % v2) or nil,
source = source,
}
elseif op.type == '//' then
- local v1 = vm.getLiteral(source[1], 'integer')
- local v2 = vm.getLiteral(source[2], 'integer')
- if v1 and v2 then
- return alloc {
- type = 'integer',
- value = v1 // v2,
- source = source,
- }
- end
- v1 = v1 or vm.getLiteral(source[1], 'number')
- v2 = v2 or vm.getLiteral(source[1], 'number')
+ local int, v1, v2 = mathCheck(source[1], source[2])
return alloc {
- type = 'number',
+ type = int,
value = (v1 and v2) and (v1 // v2) or nil,
source = source,
}
@@ -479,15 +439,6 @@ local function checkValue(source)
end
end
-local function hasTypeInResults(results, type)
- for i = 1, #results do
- if results[i].type == type then
- return true
- end
- end
- return false
-end
-
local function inferByCall(results, source)
if #results ~= 0 then
return
diff --git a/test-beta/hover/init.lua b/test-beta/hover/init.lua
index a8bdd3d3..787cb251 100644
--- a/test-beta/hover/init.lua
+++ b/test-beta/hover/init.lua
@@ -393,10 +393,10 @@ local <?t?> = {
[[
local t: {
["012"]: integer = 8,
- [*function]: integer = 6,
- [*table]: integer = 5,
- [001]: integer = 2,
+ [1]: integer = 2,
[5.5]: integer = 4,
+ [function]: integer = 6,
+ [table]: integer = 5,
[true]: integer = 3,
a: integer = 1,
b: integer = 7,
@@ -413,7 +413,7 @@ t[any] = any
]]
[[
local t: {
- [*number]: number = 1,
+ [integer]: integer = 1,
}
]]
@@ -423,7 +423,7 @@ local y = x
print(<?y?>)
]]
[[
-local y: number = 1
+local y: integer = 1
]]
TEST[[
@@ -435,9 +435,9 @@ local <?obj?> = setmetatable({}, {__index = mt})
]]
[[
local obj: {
- a: number = 1,
- b: number = 2,
- c: number = 3,
+ a: integer = 1,
+ b: integer = 2,
+ c: integer = 3,
}
]]
@@ -466,10 +466,10 @@ local <?self?> = setmetatable({
}, mt)
]]
[[
-local self: *obj {
- __index: table,
+local self: obj {
+ __index: obj,
__name: string = "obj",
- id: number = 1,
+ id: integer = 1,
remove: function,
}
]]