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
#[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");
}
}
}
}