summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--server/src/core/completion.lua22
-rw-r--r--server/src/core/document_symbol.lua4
-rw-r--r--server/src/core/hover.lua18
-rw-r--r--server/src/core/hover_function.lua2
-rw-r--r--server/src/core/value.lua17
-rw-r--r--server/src/core/vm.lua16
-rw-r--r--server/test/type_inference/init.lua2
7 files changed, 46 insertions, 35 deletions
diff --git a/server/src/core/completion.lua b/server/src/core/completion.lua
index db9189f0..cbabd9ff 100644
--- a/server/src/core/completion.lua
+++ b/server/src/core/completion.lua
@@ -121,7 +121,7 @@ local function searchFields(name, source, parent, object, callback)
goto CONTINUE
end
if object then
- if not field.value or field.value.type ~= 'function' then
+ if not field.value or field.value:getType() ~= 'function' then
goto CONTINUE
end
end
@@ -147,16 +147,16 @@ end
local function getKind(var, default)
local value = var.value
if default == CompletionItemKind.Variable then
- if value.type == 'function' then
+ if value:getType() == 'function' then
return CompletionItemKind.Function
end
end
if default == CompletionItemKind.Field then
- local tp = type(value.value)
+ local tp = type(value:getValue())
if tp == 'number' or tp == 'integer' or tp == 'string' then
return CompletionItemKind.Enum
end
- if value.type == 'function' then
+ if value:getType() == 'function' then
if var.parent and var.parent.value and var.parent.value.ENV ~= true then
return CompletionItemKind.Method
else
@@ -168,14 +168,14 @@ local function getKind(var, default)
end
local function getDetail(var)
- local tp = type(var.value.value)
+ local tp = type(var.value:getValue())
if tp == 'boolean' then
- return ('= %q'):format(var.value.value)
+ return ('= %q'):format(var.value:getValue())
elseif tp == 'number' then
- if math.type(var.value.value) == 'integer' then
- return ('= %q'):format(var.value.value)
+ if math.type(var.value:getValue()) == 'integer' then
+ return ('= %q'):format(var.value:getValue())
else
- local str = ('= %.10f'):format(var.value.value)
+ local str = ('= %.10f'):format(var.value:getValue())
local dot = str:find('.', 1, true)
local suffix = str:find('[0]+$', dot+2)
if suffix then
@@ -185,7 +185,7 @@ local function getDetail(var)
end
end
elseif tp == 'string' then
- return ('= %q'):format(var.value.value)
+ return ('= %q'):format(var.value:getValue())
end
return nil
end
@@ -194,7 +194,7 @@ local function getDocument(var, source)
if not source then
return nil
end
- if var.value.type == 'function' then
+ if var.value:getType() == 'function' then
local hvr = hover(var, source)
if not hvr then
return nil
diff --git a/server/src/core/document_symbol.lua b/server/src/core/document_symbol.lua
index 6044c697..4ede0654 100644
--- a/server/src/core/document_symbol.lua
+++ b/server/src/core/document_symbol.lua
@@ -77,7 +77,7 @@ local function buildFunction(vm, func)
end
local function isLocalTable(var)
- if not var.value or var.value.type ~= 'table' then
+ if not var.value or var.value:getType() ~= 'table' then
return false
end
if var.value.source.start == 0 then
@@ -96,7 +96,7 @@ local function buildVar(vm, var)
if var.source.start == 0 then
return nil
end
- if var.value and var.value.type == 'function' and var.value.uri == vm.uri then
+ if var.value and var.value:getType() == 'function' and var.value.uri == vm.uri then
return nil
end
if var.hide then
diff --git a/server/src/core/hover.lua b/server/src/core/hover.lua
index 7c3623fd..d3beeaad 100644
--- a/server/src/core/hover.lua
+++ b/server/src/core/hover.lua
@@ -218,8 +218,8 @@ local function findClass(result)
-- 查找meta表的 __name 字段
local name = metatable:rawGetField('__name')
-- 值必须是字符串
- if name and name.value and type(name.value.value) == 'string' then
- return name.value.value
+ if name and name.value and type(name.value:getValue()) == 'string' then
+ return name.value:getValue()
end
-- 查找meta表 __index 里的字段
local index = metatable:rawGetField('__index')
@@ -229,7 +229,7 @@ local function findClass(result)
if type(key) ~= 'string' then
goto CONTINUE
end
- if not field.value or type(field.value.value) ~= 'string' then
+ if not field.value or type(field.value:getValue()) ~= 'string' then
goto CONTINUE
end
local lKey = key:lower()
@@ -244,7 +244,7 @@ local function findClass(result)
hasSet = true
end
end
- return field.value.value
+ return field.value:getValue()
end
::CONTINUE::
end)
@@ -274,11 +274,11 @@ local function unpackTable(result)
goto CONTINUE
end
- local vType = type(value.value)
+ local vType = type(value:getValue())
if vType == 'boolean' or vType == 'integer' or vType == 'number' or vType == 'string' then
- lines[#lines+1] = (' %s: %s = %q,'):format(key, value.type, value.value)
+ lines[#lines+1] = (' %s: %s = %q,'):format(key, value:getType(), value:getValue())
else
- lines[#lines+1] = (' %s: %s,'):format(key, value.type)
+ lines[#lines+1] = (' %s: %s,'):format(key, value:getType())
end
::CONTINUE::
end
@@ -308,7 +308,7 @@ local function getValueHover(name, valueType, result, lib)
value = lib.code or (lib.value and ('%q'):format(lib.value))
tip = lib.description
else
- value = result.value.value and ('%q'):format(result.value.value)
+ value = result.value:getValue() and ('%q'):format(result.value:getValue())
end
local tp = result.type
@@ -371,7 +371,7 @@ return function (result, source, lsp, select)
valueType = valueType[1]
end
else
- valueType = result.value.type or 'nil'
+ valueType = result.value:getType() or 'nil'
end
local name = fullKey or buildValueName(result, source)
local hover
diff --git a/server/src/core/hover_function.lua b/server/src/core/hover_function.lua
index 42a5f766..8c0908d2 100644
--- a/server/src/core/hover_function.lua
+++ b/server/src/core/hover_function.lua
@@ -8,7 +8,7 @@ local function buildValueArgs(func, oo, select)
end
if func.argValues then
for i, value in ipairs(func.argValues) do
- values[i] = value.type
+ values[i] = value:getType()
end
end
local strs = {}
diff --git a/server/src/core/value.lua b/server/src/core/value.lua
index f4aed441..1641ea5e 100644
--- a/server/src/core/value.lua
+++ b/server/src/core/value.lua
@@ -3,20 +3,29 @@ local DefaultSource = { start = 0, finish = 0 }
local mt = {}
mt.__index = mt
mt.type = 'value'
+mt._type = 'any'
function mt:setValue(source, value)
- self.value = value
+ self._value = value
+end
+
+function mt:getValue()
+ return self._value
end
function mt:inference(tp)
if tp == '...' then
error('Value type cant be ...')
end
- if self.type == 'any' and tp ~= 'nil' then
- self.type = tp
+ if self._type == 'any' and tp ~= 'nil' then
+ self._type = tp
end
end
+function mt:getType()
+ return self._type
+end
+
function mt:createField(name, source)
local field = {
type = 'field',
@@ -54,6 +63,7 @@ function mt:rawGetField(name, source)
end
end
end
+ return field
end
function mt:getField(name, source)
@@ -118,6 +128,7 @@ return function (tp, source, value)
end
local self = setmetatable({
source = source or DefaultSource,
+ _type = tp,
}, mt)
if value ~= nil then
self:setValue(source, value)
diff --git a/server/src/core/vm.lua b/server/src/core/vm.lua
index f2d80b4c..4eeb43c1 100644
--- a/server/src/core/vm.lua
+++ b/server/src/core/vm.lua
@@ -336,9 +336,9 @@ function mt:setValue(var, value, source)
value:addInfo('set', source)
end
if var.value then
- if value.type == 'any' then
+ if value:getType() == 'any' then
self:mergeChild(var.value, value)
- elseif value.type == 'nil' then
+ elseif value:getType() == 'nil' then
self:mergeValue(var.value, value)
elseif var.value.uri == self.uri then
var.value = value
@@ -480,8 +480,8 @@ function mt:setFunctionArg(func, values)
if not func.argValues[i] then
func.argValues[i] = values[i]
end
- values[i]:inference(func.argValues[i].type)
- func.argValues[i]:inference(values[i].type)
+ values[i]:inference(func.argValues[i]:getType())
+ func.argValues[i]:inference(values[i]:getType())
end
self:updateFunctionArgs(func)
@@ -691,10 +691,10 @@ function mt:mergeFunctionReturn(func, index, value)
func.returns[index] = value
return
end
- if value.type == 'nil' then
+ if value:getType() == 'nil' then
return
end
- if value == 'any' and func.returns[index] ~= 'nil' then
+ if value:getType() == 'any' and func.returns[index] ~= 'nil' then
return
end
func.returns[index] = value
@@ -1004,10 +1004,10 @@ function mt:getSimple(simple, mode)
end
function mt:isTrue(v)
- if v.type == 'nil' then
+ if v:getType() == 'nil' then
return false
end
- if v.type == 'boolean' and not v.value then
+ if v:getType() == 'boolean' and not v:getValue() then
return false
end
return true
diff --git a/server/test/type_inference/init.lua b/server/test/type_inference/init.lua
index 47e94760..0962c20d 100644
--- a/server/test/type_inference/init.lua
+++ b/server/test/type_inference/init.lua
@@ -14,7 +14,7 @@ function TEST(res)
assert(vm)
local result = core.findResult(vm, pos)
assert(result)
- assert(res == result.value.type)
+ assert(res == result.value:getType())
end
end