summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2021-02-25 15:30:58 +0800
committer最萌小汐 <sumneko@hotmail.com>2021-02-25 15:30:58 +0800
commitcd6ac072254b8583a4b8d7f8a973977b7f054766 (patch)
tree723b1da2ce276235df949ee23a988c3cc669f700
parentbf9828d40bd2c7803866138a925b9326ddc2cef3 (diff)
downloadlua-language-server-cd6ac072254b8583a4b8d7f8a973977b7f054766.zip
improve hover of `table<K, V>`
-rw-r--r--script/core/hover/label.lua4
-rw-r--r--script/parser/guide.lua10
-rw-r--r--test/hover/init.lua15
-rw-r--r--test/type_inference/init.lua47
4 files changed, 71 insertions, 5 deletions
diff --git a/script/core/hover/label.lua b/script/core/hover/label.lua
index 68af973e..a54abd09 100644
--- a/script/core/hover/label.lua
+++ b/script/core/hover/label.lua
@@ -48,7 +48,9 @@ local function asValue(source, title)
local class = vm.getClass(source, 0)
local literal = vm.getInferLiteral(source, 0)
local cont
- if not vm.hasInferType(source, 'string', 0) and not type:find('%[%]$') then
+ if not vm.hasInferType(source, 'string', 0)
+ and not type:find('%[%]$')
+ and not type:find('%w%<') then
if #vm.getFields(source, 0) > 0
or vm.hasInferType(source, 'table', 0) then
cont = buildTable(source)
diff --git a/script/parser/guide.lua b/script/parser/guide.lua
index 03e88561..abe46fda 100644
--- a/script/parser/guide.lua
+++ b/script/parser/guide.lua
@@ -3122,6 +3122,7 @@ function m.viewInferType(infers)
local mark = {}
local types = {}
local hasDoc
+ local hasDocTable
for i = 1, #infers do
local infer = infers[i]
local src = infer.source
@@ -3132,7 +3133,10 @@ function m.viewInferType(infers)
or src.type == 'doc.type.table' then
if infer.type ~= 'any' then
hasDoc = true
- break
+ end
+ if src.type == 'doc.type.array'
+ or src.type == 'doc.type.table' then
+ hasDocTable = true
end
end
end
@@ -3148,11 +3152,15 @@ function m.viewInferType(infers)
or src.type == 'doc.type.enum'
or src.type == 'doc.resume' then
local tp = infer.type or 'any'
+ if hasDocTable and tp == 'table' then
+ goto CONTINUE
+ end
if not mark[tp] then
types[#types+1] = tp
end
mark[tp] = true
end
+ ::CONTINUE::
end
else
for i = 1, #infers do
diff --git a/test/hover/init.lua b/test/hover/init.lua
index 0cfb501b..52d31416 100644
--- a/test/hover/init.lua
+++ b/test/hover/init.lua
@@ -1538,3 +1538,18 @@ local u: number {
x: any,
}
]]
+
+TEST [[
+---@generic K, V
+---@param t table<K, V>
+---@return K
+---@return V
+local function next(t) end
+
+---@type table<string, boolean>
+local t
+local k, v = next(<?t?>)
+]]
+[[
+local t: table<string, boolean>
+]]
diff --git a/test/type_inference/init.lua b/test/type_inference/init.lua
index 9182731b..c53f1c47 100644
--- a/test/type_inference/init.lua
+++ b/test/type_inference/init.lua
@@ -391,17 +391,58 @@ for <?i?> in ipairs() do
end
]]
--- TODO
+TEST 'table<string, boolean>' [[
+---@generic K, V
+---@param t table<K, V>
+---@return K
+---@return V
+local function next(t) end
+
+---@type table<string, boolean>
+local t
+local k, v = next(<?t?>)
+]]
+
+--TEST 'string' [[
+-----@generic K, V
+-----@param t table<K, V>
+-----@return K
+-----@return V
+--local function next(t) end
+--
+-----@type table<string, boolean>
+--local t
+--local <?k?>, v = next(t)
+--]]
+--
+--TEST 'boolean' [[
+-----@generic K, V
+-----@param t table<K, V>
+-----@return K
+-----@return V
+--local function next(t) end
+--
+-----@type table<string, boolean>
+--local t
+--local k, <?v?> = next(t)
+--]]
+--
+--TEST 'boolean' [[
+-----@type table<string, boolean>
+--local t
+--local k, <?v?> = next(t)
+--]]
+--
--TEST 'string' [[
-----@type table<string, boolean>
--local t
---for <?k?>, v in ipairs(t) do
+--for <?k?>, v in pairs(t) do
--end
--]]
--
--TEST 'boolean' [[
-----@type table<string, boolean>
--local t
---for k, <?v?> in ipairs(t) do
+--for k, <?v?> in pairs(t) do
--end
--]]