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
|
local files = require 'files'
local searcher = require 'core.searcher'
local vm = require 'vm'
local getLabel = require 'core.hover.label'
local getDesc = require 'core.hover.description'
local util = require 'utility'
local findSource = require 'core.find-source'
local lang = require 'language'
local markdown = require 'provider.markdown'
local infer = require 'core.infer'
local function eachFunctionAndOverload(value, callback)
callback(value)
if not value.bindDocs then
return
end
for _, doc in ipairs(value.bindDocs) do
if doc.type == 'doc.overload' then
callback(doc.overload)
end
end
end
local function getHoverAsValue(source)
local label = getLabel(source)
local desc = getDesc(source)
if not desc then
local values = vm.getDefs(source)
for _, def in ipairs(values) do
desc = getDesc(def)
if desc then
break
end
end
end
return {
label = label,
source = source,
description = desc,
}
end
local function getHoverAsFunction(source)
local values = vm.getDefs(source)
local desc = getDesc(source)
local labels = {}
local defs = 0
local protos = 0
local other = 0
local mark = {}
for _, def in ipairs(values) do
def = searcher.getObjectValue(def) or def
if def.type == 'function'
or def.type == 'doc.type.function' then
eachFunctionAndOverload(def, function (value)
if mark[value] then
return
end
mark[value] =true
local label = getLabel(value)
if label then
defs = defs + 1
labels[label] = (labels[label] or 0) + 1
if labels[label] == 1 then
protos = protos + 1
end
end
desc = desc or getDesc(value)
end)
elseif def.type == 'table'
or def.type == 'boolean'
or def.type == 'string'
or def.type == 'number' then
other = other + 1
desc = desc or getDesc(def)
end
end
if defs == 0 then
return getHoverAsValue(source)
end
if defs == 1 and other == 0 then
return {
label = next(labels),
source = source,
description = desc,
}
end
local lines = {}
if defs > 1 then
lines[#lines+1] = lang.script('HOVER_MULTI_DEF_PROTO', defs, protos)
end
if other > 0 then
lines[#lines+1] = lang.script('HOVER_MULTI_PROTO_NOT_FUNC', other)
end
if defs > 1 then
for label, count in util.sortPairs(labels) do
lines[#lines+1] = ('(%d) %s'):format(count, label)
end
else
lines[#lines+1] = next(labels)
end
local label = table.concat(lines, '\n')
return {
label = label,
source = source,
description = desc,
}
end
local function getHoverAsDocName(source)
local label = getLabel(source)
local desc = getDesc(source)
return {
label = label,
source = source,
description = desc,
}
end
local function getHover(source)
if source.type == 'doc.type.name' then
return getHoverAsDocName(source)
end
local isFunction = infer.hasType(source, 'function')
if isFunction then
return getHoverAsFunction(source)
else
return getHoverAsValue(source)
end
end
local accept = {
['local'] = true,
['setlocal'] = true,
['getlocal'] = true,
['setglobal'] = true,
['getglobal'] = true,
['field'] = true,
['method'] = true,
['string'] = true,
['number'] = true,
['doc.type.name'] = true,
['function'] = true,
}
local function getHoverByUri(uri, offset)
local ast = files.getState(uri)
if not ast then
return nil
end
local source = findSource(ast, offset, accept)
if not source then
return nil
end
local hover = getHover(source)
if SHOWSOURCE then
hover.description = ('%s\n---\n\n```lua\n%s\n```'):format(
hover.description or '',
util.dump(source, {
deep = 1,
})
)
end
return hover
end
return {
get = getHover,
byUri = getHoverByUri,
}
|