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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#[cfg(test)]
mod tests {
use lsp_types::GotoDefinitionResponse;
use crate::handlers::test_lib::ProviderVirtualWorkspace;
#[test]
fn test_basic_definition() {
let mut ws = ProviderVirtualWorkspace::new();
ws.check_definition(
r#"
---@generic T
---@param name `T`
---@return T
local function new(name)
return name
end
---@class Ability
local a = new("<??>Ability")
"#,
);
}
#[test]
fn test_table_field_definition_1() {
let mut ws = ProviderVirtualWorkspace::new();
ws.check_definition(
r#"
---@class T
---@field func fun(self:string)
---@type T
local t = {
f<??>unc = function(self)
end
}
"#,
);
}
#[test]
fn test_table_field_definition_2() {
let mut ws = ProviderVirtualWorkspace::new();
ws.check_definition(
r#"
---@class T
---@field func fun(self: T) 注释注释
---@type T
local t = {
func = function(self)
end,
a = 1,
}
t:func<??>()
"#,
);
}
#[test]
fn test_goto_field() {
let mut ws = ProviderVirtualWorkspace::new();
ws.check_definition(
r#"
local t = {}
function t:test(a)
self.abc = a
end
print(t.abc<??>)
"#,
);
}
#[test]
fn test_goto_overload() {
let mut ws = ProviderVirtualWorkspace::new();
ws.def(
r#"
---@class Goto1
---@class Goto2
---@class Goto3
---@class T
---@field func fun(a:Goto1) # 1
---@field func fun(a:Goto2) # 2
---@field func fun(a:Goto3) # 3
local T = {}
function T:func(a)
end
"#,
);
{
let result = ws
.check_definition(
r#"
---@type Goto2
local Goto2
---@type T
local t
t.fu<??>nc(Goto2)
"#,
)
.unwrap();
match result {
GotoDefinitionResponse::Array(array) => {
assert_eq!(array.len(), 2);
}
_ => {
panic!("expect array");
}
}
}
{
let result = ws
.check_definition(
r#"
---@type T
local t
t.fu<??>nc()
"#,
)
.unwrap();
match result {
GotoDefinitionResponse::Array(array) => {
assert_eq!(array.len(), 4);
}
_ => {
panic!("expect array");
}
}
}
}
#[test]
fn test_goto_return_field() {
let mut ws = ProviderVirtualWorkspace::new();
ws.def_file(
"test.lua",
r#"
local function test()
end
return {
test = test,
}
"#,
);
let result = ws
.check_definition(
r#"
local t = require("test")
local test = t.test
te<??>st()
"#,
)
.unwrap();
match result {
GotoDefinitionResponse::Array(locations) => {
assert_eq!(locations.len(), 1);
assert_eq!(locations[0].range.start.line, 1);
}
_ => {
panic!("expect scalar");
}
}
}
#[test]
fn test_goto_return_field_2() {
let mut ws = ProviderVirtualWorkspace::new_with_init_std_lib();
ws.def_file(
"test.lua",
r#"
---@export
---@class Export
local export = {}
---@generic T
---@param name `T`|T
---@param tbl? table
---@return T
local function new(name, tbl)
end
export.new = new
return export
"#,
);
let result = ws
.check_definition(
r#"
local new = require("test").new
new<??>("A")
"#,
)
.unwrap();
match result {
GotoDefinitionResponse::Array(locations) => {
assert_eq!(locations.len(), 1);
assert_eq!(locations[0].range.start.line, 8);
}
_ => {
panic!("expect scalar");
}
}
}
#[test]
fn test_goto_generic_type() {
let mut ws = ProviderVirtualWorkspace::new();
ws.def_file(
"1.lua",
r#"
---@generic T
---@param name `T`|T
---@return T
function new(name)
end
"#,
);
ws.def_file(
"2.lua",
r#"
---@namespace AAA
---@class BBB<T>
"#,
);
let result = ws
.check_definition(
r#"
new("AAA.BBB<??>")
"#,
)
.unwrap();
match result {
GotoDefinitionResponse::Array(array) => {
assert_eq!(array.len(), 1);
let location = &array[0];
assert_eq!(location.uri.path().as_str().ends_with("2.lua"), true);
}
_ => {
panic!("expect array");
}
}
}
#[test]
fn test_goto_export_function() {
let mut ws = ProviderVirtualWorkspace::new();
ws.def_file(
"a.lua",
r#"
local function create()
end
return create
"#,
);
let result = ws
.check_definition(
r#"
local create = require('a')
create<??>()
"#,
)
.unwrap();
match result {
GotoDefinitionResponse::Scalar(location) => {
assert_eq!(location.uri.path().as_str().ends_with("a.lua"), true);
}
_ => {
panic!("expect array");
}
}
}
#[test]
fn test_goto_export_function_2() {
let mut ws = ProviderVirtualWorkspace::new();
ws.def_file(
"a.lua",
r#"
local function testA()
end
local function create()
end
return create
"#,
);
ws.def_file(
"b.lua",
r#"
local Rxlua = {}
local create = require('a')
Rxlua.create = create
return Rxlua
"#,
);
let result = ws
.check_definition(
r#"
local create = require('b').create
create<??>()
"#,
)
.unwrap();
match result {
GotoDefinitionResponse::Array(array) => {
assert_eq!(array.len(), 1);
let location = &array[0];
assert_eq!(location.uri.path().as_str().ends_with("a.lua"), true);
}
_ => {
panic!("expect array");
}
}
}
}