Skip to content

Commit 48c53a3

Browse files
committed
resolve #1065 show detail for doc.type.table
1 parent 55166a4 commit 48c53a3

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

changelog.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
# changelog
22

33
## 3.5.0
4-
* `CHG` `---@diagnostic disable: <ERR_NAME>` can suppress syntax errors
5-
* `CHG` `completion.callSnippet` no longer generate parameter types
4+
* `CHG` diagnostic: `---@diagnostic disable: <ERR_NAME>` can suppress syntax errors
5+
* `CHG` completion: `completion.callSnippet` no longer generate parameter types
6+
* `CHG` hover: show `---@type {x: number, y: number}` as detail instead of `table`
67
* `FIX` [#1278](https://github.com/sumneko/lua-language-server/issues/1278)
78

89
## 3.4.1

script/core/hover/label.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ local function asValue(source, title)
5555
and ( type == 'table'
5656
or type == 'any'
5757
or type == 'unknown'
58-
or type == 'nil') then
58+
or type == 'nil'
59+
or type:sub(1, 1) == '{') then
5960
else
6061
pack[#pack+1] = type
6162
end

script/vm/infer.lua

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ local vm = require 'vm.vm'
88
---@field views table<string, boolean>
99
---@field cachedView? string
1010
---@field node? vm.node
11+
---@field _drop table
1112
local mt = {}
1213
mt.__index = mt
1314
mt._hasNumber = false
@@ -122,11 +123,45 @@ local viewNodeSwitch = util.switch()
122123
for i, sign in ipairs(source.signs) do
123124
buf[i] = vm.getInfer(sign):view(uri)
124125
end
126+
if infer._drop then
127+
local node = vm.compileNode(source)
128+
for c in node:eachObject() do
129+
if guide.isLiteral(c) then
130+
infer._drop[c] = true
131+
end
132+
end
133+
end
125134
return ('%s<%s>'):format(source.node[1], table.concat(buf, ', '))
126135
end)
127136
: case 'doc.type.table'
128-
: call(function (source, infer)
129-
infer._hasTable = true
137+
: call(function (source, infer, uri)
138+
if #source.fields == 0 then
139+
infer._hasTable = true
140+
return
141+
end
142+
if infer._drop and infer._drop[source] then
143+
infer._hasTable = true
144+
return
145+
end
146+
infer._hasClass = true
147+
local buf = {}
148+
buf[#buf+1] = '{ '
149+
for i, field in ipairs(source.fields) do
150+
if i > 1 then
151+
buf[#buf+1] = ', '
152+
end
153+
local key = field.name
154+
if key.type == 'doc.type' then
155+
buf[#buf+1] = ('[%s]: '):format(vm.getInfer(key):view(uri))
156+
elseif type(key[1]) == 'string' then
157+
buf[#buf+1] = key[1] .. ': '
158+
else
159+
buf[#buf+1] = ('[%q]: '):format(key[1])
160+
end
161+
buf[#buf+1] = vm.getInfer(field.extends):view(uri)
162+
end
163+
buf[#buf+1] = ' }'
164+
return table.concat(buf)
130165
end)
131166
: case 'doc.type.string'
132167
: call(function (source, infer)
@@ -209,6 +244,7 @@ function vm.getInfer(source)
209244
end
210245
local infer = setmetatable({
211246
node = node,
247+
_drop = {},
212248
}, mt)
213249
node.lastInfer = infer
214250

test/type_inference/init.lua

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ TEST 'fun(a: string, b: any, c?: boolean, ...any):c, d?, ...unknown' [[
520520
local <?x?>
521521
]]
522522

523-
TEST 'table' [[
523+
TEST '{ [string]: string }' [[
524524
---@type { [string]: string }
525525
local <?x?>
526526
]]
@@ -2439,14 +2439,14 @@ TEST 'table' [[
24392439
local <?x?>
24402440
]]
24412441

2442-
TEST 'table' [[
2442+
TEST '{ name: boolean }' [[
24432443
---@alias tp {name: boolean}
24442444
24452445
---@type tp
24462446
local <?x?>
24472447
]]
24482448

2449-
TEST 'boolean|table' [[
2449+
TEST 'boolean|{ name: boolean }' [[
24502450
---@alias tp boolean | {name: boolean}
24512451
24522452
---@type tp
@@ -3376,3 +3376,8 @@ TEST 'A' [[
33763376
---@diagnostic disable
33773377
local <?t?>
33783378
]]
3379+
3380+
TEST '{ [string]: number, [true]: string, [1]: boolean, tag: integer }' [[
3381+
---@type {[string]: number, [true]: string, [1]: boolean, tag: integer}
3382+
local <?t?>
3383+
]]

0 commit comments

Comments
 (0)