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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
|
local findLib = require 'core.find_lib'
local getFunctionHover = require 'core.hover.function'
local getFunctionHoverAsLib = require 'core.hover.lib_function'
local getFunctionHoverAsEmmy = require 'core.hover.emmy_function'
local buildValueName = require 'core.hover.name'
local OriginTypes = {
['any'] = true,
['nil'] = true,
['integer'] = true,
['number'] = true,
['boolean'] = true,
['string'] = true,
['thread'] = true,
['userdata'] = true,
['table'] = true,
['function'] = true,
}
local function findClass(value)
-- 检查是否有emmy
local emmy = value:getEmmy()
if emmy then
return emmy:getType()
end
-- 检查对象元表
local metaValue = value:getMetaTable()
if not metaValue then
return nil
end
-- 检查元表中的 __name
local metaName = metaValue:rawGet('__name')
if metaName and type(metaName:getLiteral()) == 'string' then
return metaName:getLiteral()
end
-- 检查元表的 __index
local indexValue = metaValue:rawGet('__index')
if not indexValue then
return nil
end
-- 查找index方法中的以下字段: type name class
-- 允许多重继承
return indexValue:eachChild(function (k, v)
-- 键值类型必须均为字符串
if type(k) ~= 'string' then
return
end
if type(v:getLiteral()) ~= 'string' then
return
end
local lKey = k:lower()
if lKey == 'type'
or lKey == 'name'
or lKey == 'class'
then
-- 必须只有过一次赋值
local hasSet = false
local ok = v:eachInfo(function (info)
if info.type == 'set' then
if hasSet then
return false
else
hasSet = true
end
end
end)
if ok == false then
return false
end
return v:getLiteral()
end
end)
end
local function formatKey(key)
local kType = type(key)
if kType == 'table' then
key = ('[*%s]'):format(key:getType())
elseif math.type(key) == 'integer' then
key = ('[%03d]'):format(key)
elseif kType == 'string' then
if key:find '^%d' or key:find '[^%w_]' then
key = ('[%q]'):format(key)
end
elseif key == '' then
key = '[*any]'
else
key = ('[%s]'):format(key)
end
return key
end
local function unpackTable(value)
local lines = {}
value:eachChild(function (key, child)
key = formatKey(key)
local vType = type(child:getLiteral())
if vType == 'boolean'
or vType == 'integer'
or vType == 'number'
or vType == 'string'
then
lines[#lines+1] = ('%s: %s = %q'):format(key, child:getType(), child:getLiteral())
else
lines[#lines+1] = ('%s: %s'):format(key, child:getType())
end
end)
local emmy = value:getEmmy()
if emmy then
if emmy.type == 'emmy.arrayType' then
lines[#lines+1] = ('[*integer]: %s'):format(emmy:getName())
elseif emmy.type == 'emmy.tableType' then
lines[#lines+1] = ('[*%s]: %s'):format(emmy:getKeyType():getType(), emmy:getValueType():getType())
end
end
if #lines == 0 then
return '{}'
end
-- 整理一下表
local cleaned = {}
local used = {}
for _, line in ipairs(lines) do
if used[line] then
goto CONTINUE
end
used[line] = true
if line == '[*any]: any' then
goto CONTINUE
end
cleaned[#cleaned+1] = ' ' .. line .. ','
:: CONTINUE ::
end
table.sort(cleaned)
table.insert(cleaned, 1, '{')
cleaned[#cleaned+1] = '}'
return table.concat(cleaned, '\r\n')
end
local function getValueHover(source, name, value, lib)
local valueType = value:getType()
local class = findClass(value)
if class then
valueType = class
lib = nil
end
if not OriginTypes[valueType] then
valueType = '*' .. valueType
end
local tip
local literal
if lib then
literal = lib.code or (lib.value and ('%q'):format(lib.value))
tip = lib.description
else
literal = value:getLiteral() and ('%q'):format(value:getLiteral())
end
local tp
if source:bindLocal() then
tp = 'local'
elseif source:get 'global' then
tp = 'global'
elseif source:get 'simple' then
local simple = source:get 'simple'
if simple[1]:get 'global' then
tp = 'global'
else
tp = 'field'
end
else
tp = 'field'
end
local text
if valueType == 'table' then
text = ('%s %s: %s'):format(tp, name, unpackTable(value))
else
if literal == nil then
if class and not OriginTypes[class] then
text = ('%s %s: %s %s'):format(tp, name, valueType, unpackTable(value))
else
text = ('%s %s: %s'):format(tp, name, valueType)
end
else
text = ('%s %s: %s = %s'):format(tp, name, valueType, literal)
end
end
return {
label = text,
description = tip,
}
end
local function hoverAsValue(source, lsp, select)
local lib, fullkey = findLib(source)
---@type value
local value = source:findValue()
local name = fullkey or buildValueName(source)
local hover
if value:getType() == 'function' then
local object = source:get 'object'
if lib then
hover = getFunctionHoverAsLib(name, lib, object, select)
else
local emmy = value:getEmmy()
if emmy and emmy.type == 'emmy.functionType' then
hover = getFunctionHoverAsEmmy(name, emmy, object, select)
else
local func = value:getFunction()
hover = getFunctionHover(name, func, object, select)
end
end
else
hover = getValueHover(source, name, value, lib)
end
if not hover then
return nil
end
hover.name = name
return hover
end
local function hoverAsTargetUri(source, lsp)
local uri = source:get 'target uri'
if not lsp or not lsp.workspace then
return nil
end
local path = lsp.workspace:relativePathByUri(uri)
return {
description = ('[%s](%s)'):format(path:string(), uri),
}
end
return function (source, lsp, select)
if not source then
return nil
end
if source:get 'target uri' then
return hoverAsTargetUri(source, lsp)
end
if source.type == 'name' and source:bindValue() then
return hoverAsValue(source, lsp, select)
end
if source.type == 'simple' then
source = source[#source]
if source.type == 'name' and source:bindValue() then
return hoverAsValue(source, lsp, select)
end
end
return nil
end
|