1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
local vm = require 'vm'
local util = require 'utility'
local config = require 'config'
local await = require 'await'
local guide = require 'parser.guide'
---@param uri uri
---@param keys string[]
---@param nodeMap table<string, vm.node>
---@param reachMax integer
local function buildAsHash(uri, keys, nodeMap, reachMax)
local lines = {}
lines[#lines+1] = '{'
for _, key in ipairs(keys) do
local node = nodeMap[key]
local isOptional = node:isOptional()
if isOptional then
node = node:copy()
node:removeOptional()
end
local ifr = vm.getInfer(node)
local typeView = ifr:view(uri, 'unknown')
local literalView = ifr:viewLiterals()
if literalView then
lines[#lines+1] = (' %s%s: %s = %s,'):format(
key,
isOptional and '?' or '',
typeView,
literalView
)
else
lines[#lines+1] = (' %s%s: %s,'):format(
key,
isOptional and '?' or '',
typeView
)
end
end
if reachMax > 0 then
lines[#lines+1] = (' ...(+%d)'):format(reachMax)
end
lines[#lines+1] = '}'
return table.concat(lines, '\n')
end
---@param uri uri
---@param keys string[]
---@param nodeMap table<string, vm.node>
---@param reachMax integer
local function buildAsConst(uri, keys, nodeMap, reachMax)
local literalMap = {}
for _, key in ipairs(keys) do
literalMap[key] = vm.getInfer(nodeMap[key]):viewLiterals()
end
table.sort(keys, function (a, b)
return tonumber(literalMap[a]) < tonumber(literalMap[b])
end)
local lines = {}
lines[#lines+1] = '{'
for _, key in ipairs(keys) do
local node = nodeMap[key]
local isOptional = node:isOptional()
if isOptional then
node = node:copy()
node:removeOptional()
end
local typeView = vm.getInfer(node):view(uri, 'unknown')
local literalView = literalMap[key]
if literalView then
lines[#lines+1] = (' %s%s: %s = %s,'):format(
key,
isOptional and '?' or '',
typeView,
literalView
)
else
lines[#lines+1] = (' %s%s: %s,'):format(
key,
isOptional and '?' or '',
typeView
)
end
end
if reachMax > 0 then
lines[#lines+1] = (' ...(+%d)'):format(reachMax)
end
lines[#lines+1] = '}'
return table.concat(lines, '\n')
end
---@param source parser.object
---@param fields parser.object[]
local function getVisibleKeyMap(source, fields)
local uri = guide.getUri(source)
local keys = {}
local map = {}
local ignored = {}
for _, field in ipairs(fields) do
local key = vm.viewKey(field, uri)
local rawKey = guide.getKeyName(field)
if rawKey and rawKey ~= key then
ignored[rawKey] = true
map[rawKey] = nil
end
if not ignored[key]
and vm.isVisible(source, field) then
if key and not map[key] then
map[key] = true
end
end
end
for key in pairs(map) do
keys[#keys+1] = key
end
table.sort(keys, function (a, b)
if a == b then
return false
end
local s1 = 0
local s2 = 0
if a:sub(1, 1) == '_' then
s1 = s1 + 10
end
if b:sub(1, 1) == '_' then
s2 = s2 + 10
end
if a:sub(1, 1) == '[' then
s1 = s1 + 1
end
if b:sub(1, 1) == '[' then
s2 = s2 + 1
end
if s1 == s2 then
return a < b
end
return s1 < s2
end)
return keys, map
end
---@async
local function getNodeMap(uri, fields, keyMap)
---@type table<string, vm.node>
local nodeMap = {}
for _, field in ipairs(fields) do
local key = vm.viewKey(field, uri)
if not key or not keyMap[key] then
goto CONTINUE
end
await.delay()
local node = vm.compileNode(field)
if nodeMap[key] then
nodeMap[key]:merge(node)
else
nodeMap[key] = node:copy()
end
::CONTINUE::
end
return nodeMap
end
---@async
---@return string?
return function (source)
local uri = guide.getUri(source)
local maxFields = config.get(uri, 'Lua.hover.previewFields')
if maxFields <= 0 then
return nil
end
local node = vm.compileNode(source)
for n in node:eachObject() do
if n.type == 'global' and n.cate == 'type' then
if n.name == 'string'
or (n.name ~= 'unknown' and n.name ~= 'any' and vm.isSubType(uri, n.name, 'string')) then
return nil
end
elseif n.type == 'doc.type.string'
or n.type == 'string' then
return nil
elseif n.type == 'doc.type.sign' then
return nil
end
end
local fields = vm.getFields(source)
local keys, map = getVisibleKeyMap(source, fields)
if #keys == 0 then
return nil
end
local reachMax = #keys - maxFields
if #keys > maxFields then
for i = maxFields + 1, #keys do
map[keys[i]] = nil
keys[i] = nil
end
end
local nodeMap = getNodeMap(uri, fields, map)
local isConsts = true
for i = 1, #keys do
await.delay()
local key = keys[i]
local literal = vm.getInfer(nodeMap[key]):viewLiterals()
if not tonumber(literal) then
isConsts = false
end
end
local result
if isConsts then
result = buildAsConst(uri, keys, nodeMap, reachMax)
else
result = buildAsHash(uri, keys, nodeMap, reachMax)
end
--if timeUp then
-- result = ('\n--%s\n%s'):format(lang.script.HOVER_TABLE_TIME_UP, result)
--end
return result
end
|