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
|
local emmyFunction = require 'core.hover.emmy_function'
local function buildValueArgs(func, object, select)
if not func then
return '', nil
end
local names = {}
local values = {}
local options = {}
if func.argValues then
for i, value in ipairs(func.argValues) do
values[i] = value:getType()
end
end
if func.args then
for i, arg in ipairs(func.args) do
names[#names+1] = arg:getName()
local param = func:findEmmyParamByName(arg:getName())
if param then
values[i] = param:getType()
options[i] = param:getOption()
end
end
end
local strs = {}
local start = 1
if object then
start = 2
end
local max
if func:getSource() then
max = #names
else
max = math.max(#names, #values)
end
local args = {}
for i = start, max do
local name = names[i]
local value = values[i] or 'any'
local option = options[i]
if option and option.optional then
if i > start then
strs[#strs+1] = ' ['
else
strs[#strs+1] = '['
end
end
if i > start then
strs[#strs+1] = ', '
end
if i == select then
strs[#strs+1] = '@ARG'
end
if name then
strs[#strs+1] = name .. ': ' .. value
else
strs[#strs+1] = value
end
args[#args+1] = strs[#strs]
if i == select then
strs[#strs+1] = '@ARG'
end
if option and option.optional == 'self' then
strs[#strs+1] = ']'
end
end
if func:hasDots() then
if max > 0 then
strs[#strs+1] = ', '
end
strs[#strs+1] = '...'
end
if options then
for _, option in pairs(options) do
if option.optional == 'after' then
strs[#strs+1] = ']'
end
end
end
local text = table.concat(strs)
local argLabel = {}
for i = 1, 2 do
local pos = text:find('@ARG', 1, true)
if pos then
if i == 1 then
argLabel[i] = pos
else
argLabel[i] = pos - 1
end
text = text:sub(1, pos-1) .. text:sub(pos+4)
end
end
if #argLabel == 0 then
argLabel = nil
end
return text, argLabel, args
end
local function buildValueReturns(func)
if not func then
return '\n -> any'
end
if not func:get 'hasReturn' then
return ''
end
local strs = {}
local emmys = {}
local n = 0
func:eachEmmyReturn(function (emmy)
n = n + 1
emmys[n] = emmy
end)
if func.returns then
for i, rtn in ipairs(func.returns) do
local emmy = emmys[i]
local option = emmy and emmy.option
if option and option.optional then
if i > 1 then
strs[#strs+1] = ' ['
else
strs[#strs+1] = '['
end
end
if i > 1 then
strs[#strs+1] = '\n , '
end
if emmy and emmy.name then
strs[#strs+1] = ('%s: '):format(emmy.name)
elseif option and option.name then
strs[#strs+1] = ('%s: '):format(option.name)
end
strs[#strs+1] = rtn:getType()
if option and option.optional == 'self' then
strs[#strs+1] = ']'
end
end
for i = 1, #func.returns do
local emmy = emmys[i]
if emmy and emmy.option and emmy.option.optional == 'after' then
strs[#strs+1] = ']'
end
end
end
if #strs == 0 then
strs[1] = 'any'
end
return '\n -> ' .. table.concat(strs)
end
---@param func emmyFunction
local function buildEnum(func)
if not func then
return nil
end
local params = func:getEmmyParams()
if not params then
return nil
end
local strs = {}
local raw = {}
for _, param in ipairs(params) do
local first = true
local name = param:getName()
raw[name] = {}
param:eachEnum(function (enum)
if first then
first = false
strs[#strs+1] = ('\n%s: %s'):format(param:getName(), param:getType())
end
if enum.default then
strs[#strs+1] = ('\n |>%s'):format(enum[1])
else
strs[#strs+1] = ('\n | %s'):format(enum[1])
end
if enum.comment then
strs[#strs+1] = ' -- ' .. enum.comment
end
raw[name][#raw[name]+1] = enum[1]
end)
end
if #strs == 0 then
return nil
end
return table.concat(strs), raw
end
local function getComment(func)
if not func then
return nil
end
local comments = {}
local params = func:getEmmyParams()
if params then
for _, param in ipairs(params) do
local option = param:getOption()
if option and option.comment then
comments[#comments+1] = ('+ `%s`*(%s)*: %s'):format(param:getName(), param:getType(), option.comment)
end
end
end
comments[#comments+1] = func:getComment()
if #comments == 0 then
return nil
end
return table.concat(comments, '\n\n')
end
local function getOverLoads(name, func, object, select)
local overloads = func and func:getEmmyOverLoads()
if not overloads then
return nil
end
local list = {}
for _, ol in ipairs(overloads) do
local hover = emmyFunction(name, ol, object, select)
list[#list+1] = hover.label
end
return table.concat(list, '\n')
end
return function (name, func, object, select)
local argStr, argLabel, args = buildValueArgs(func, object, select)
local returns = buildValueReturns(func)
local enum, rawEnum = buildEnum(func)
local comment = getComment(func)
local overloads = getOverLoads(name, func, object, select)
return {
label = ('function %s(%s)%s'):format(name, argStr, returns),
name = name,
argStr = argStr,
returns = returns,
description = comment,
enum = enum,
rawEnum = rawEnum,
argLabel = argLabel,
overloads = overloads,
args = args,
}
end
|