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
|
package provider
import (
"testing"
"github.com/grindlemire/go-tui/internal/tuigen"
)
func TestNewCompletionProvider_Constructor(t *testing.T) {
cp := NewCompletionProvider(newStubIndex(), &nilGoplsProxy{}, &nilVirtualFiles{})
if cp == nil {
t.Fatal("expected non-nil provider")
}
content := `<div class="fle`
doc := &Document{URI: "file:///test.gsx", Content: content, Version: 1}
ctx := &CursorContext{
Document: doc,
Position: Position{Line: 0, Character: len(content)},
Offset: len(content),
Word: "fle",
InClassAttr: true,
Scope: &Scope{},
}
result, err := cp.Complete(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result == nil || len(result.Items) == 0 {
t.Fatal("expected tailwind completion items")
}
foundFlex := false
for _, item := range result.Items {
if item.Label == "flex" {
foundFlex = true
break
}
}
if !foundFlex {
t.Errorf("expected flex completion among items")
}
}
func TestFindLetBindingInNodes_Recursion(t *testing.T) {
binding := &tuigen.LetBinding{Name: "header", IsShortForm: true, Position: tuigen.Position{Line: 5, Column: 2}}
type tc struct {
nodes []tuigen.Node
found bool
}
tests := map[string]tc{
"inside element": {
nodes: []tuigen.Node{&tuigen.Element{Tag: "div", Children: []tuigen.Node{binding}}},
found: true,
},
"inside for loop": {
nodes: []tuigen.Node{&tuigen.ForLoop{Body: []tuigen.Node{binding}}},
found: true,
},
"inside if then": {
nodes: []tuigen.Node{&tuigen.IfStmt{Then: []tuigen.Node{binding}}},
found: true,
},
"inside if else": {
nodes: []tuigen.Node{&tuigen.IfStmt{Else: []tuigen.Node{binding}}},
found: true,
},
"inside component call": {
nodes: []tuigen.Node{&tuigen.ComponentCall{Name: "Card", Children: []tuigen.Node{binding}}},
found: true,
},
"name mismatch": {
nodes: []tuigen.Node{&tuigen.LetBinding{Name: "other", IsShortForm: true}},
found: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := findLetBindingInNodes(tt.nodes, "header")
if tt.found && got != binding {
t.Errorf("expected to find binding, got %+v", got)
}
if !tt.found && got != nil {
t.Errorf("expected nil, got %+v", got)
}
})
}
}
func TestFindForLoopWithVariable_Recursion(t *testing.T) {
loop := &tuigen.ForLoop{Index: "i", Value: "item", Position: tuigen.Position{Line: 5, Column: 2}}
type tc struct {
nodes []tuigen.Node
varName string
found bool
}
tests := map[string]tc{
"index variable direct": {
nodes: []tuigen.Node{loop},
varName: "i",
found: true,
},
"nested loop in body": {
nodes: []tuigen.Node{&tuigen.ForLoop{Index: "x", Value: "y", Body: []tuigen.Node{loop}}},
varName: "item",
found: true,
},
"inside element": {
nodes: []tuigen.Node{&tuigen.Element{Tag: "div", Children: []tuigen.Node{loop}}},
varName: "item",
found: true,
},
"inside if then": {
nodes: []tuigen.Node{&tuigen.IfStmt{Then: []tuigen.Node{loop}}},
varName: "item",
found: true,
},
"inside if else": {
nodes: []tuigen.Node{&tuigen.IfStmt{Else: []tuigen.Node{loop}}},
varName: "item",
found: true,
},
"inside component call": {
nodes: []tuigen.Node{&tuigen.ComponentCall{Name: "Card", Children: []tuigen.Node{loop}}},
varName: "item",
found: true,
},
"no match": {
nodes: []tuigen.Node{loop},
varName: "missing",
found: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := findForLoopWithVariable(tt.nodes, tt.varName)
if tt.found && got != loop {
t.Errorf("expected to find loop, got %+v", got)
}
if !tt.found && got != nil {
t.Errorf("expected nil, got %+v", got)
}
})
}
}
func TestFindGoCodeWithVariable_Recursion(t *testing.T) {
goCode := &tuigen.GoCode{Code: "msg := tui.NewRef()", Position: tuigen.Position{Line: 5, Column: 2}}
type tc struct {
nodes []tuigen.Node
found bool
}
tests := map[string]tc{
"inside element": {
nodes: []tuigen.Node{&tuigen.Element{Tag: "div", Children: []tuigen.Node{goCode}}},
found: true,
},
"inside for loop": {
nodes: []tuigen.Node{&tuigen.ForLoop{Body: []tuigen.Node{goCode}}},
found: true,
},
"inside if then": {
nodes: []tuigen.Node{&tuigen.IfStmt{Then: []tuigen.Node{goCode}}},
found: true,
},
"inside if else": {
nodes: []tuigen.Node{&tuigen.IfStmt{Else: []tuigen.Node{goCode}}},
found: true,
},
"inside component call": {
nodes: []tuigen.Node{&tuigen.ComponentCall{Name: "Card", Children: []tuigen.Node{goCode}}},
found: true,
},
"inside let binding element": {
nodes: []tuigen.Node{&tuigen.LetBinding{Element: &tuigen.Element{
Tag: "div",
Children: []tuigen.Node{goCode},
}}},
found: true,
},
"no declaration": {
nodes: []tuigen.Node{&tuigen.GoCode{Code: "fmt.Println(msg)"}},
found: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := findGoCodeWithVariable(tt.nodes, "msg")
if tt.found && got != goCode {
t.Errorf("expected to find GoCode, got %+v", got)
}
if !tt.found && got != nil {
t.Errorf("expected nil, got %+v", got)
}
})
}
}
func TestFindVariableUsagesInNodesExcluding_Recursion(t *testing.T) {
uri := "file:///test.gsx"
nodes := []tuigen.Node{
&tuigen.GoCode{Code: "msg := transform(msg)", Position: tuigen.Position{Line: 10, Column: 2}},
&tuigen.RawGoExpr{Code: "msg", Position: tuigen.Position{Line: 11, Column: 7}},
&tuigen.Element{
Tag: "div",
Attributes: []*tuigen.Attribute{
{Name: "id", Value: &tuigen.GoExpr{Code: "msg", Position: tuigen.Position{Line: 12, Column: 10}}},
},
Children: []tuigen.Node{
&tuigen.GoExpr{Code: "msg", Position: tuigen.Position{Line: 13, Column: 7}},
},
},
&tuigen.ForLoop{
Index: "i",
Value: "v",
Iterable: "msg",
Position: tuigen.Position{Line: 14, Column: 2},
},
&tuigen.IfStmt{
Condition: "msg != nil",
Position: tuigen.Position{Line: 15, Column: 2},
},
&tuigen.ComponentCall{
Name: "Card",
Args: "msg",
Position: tuigen.Position{Line: 16, Column: 2},
},
&tuigen.LetBinding{Element: &tuigen.Element{Tag: "div", Children: []tuigen.Node{
&tuigen.GoExpr{Code: "msg", Position: tuigen.Position{Line: 17, Column: 7}},
}}},
}
var refs []Location
findVariableUsagesInNodesExcluding(nodes, "msg", uri, 9, 1, 4, &refs)
if len(refs) != 8 {
t.Fatalf("expected 8 usages (decl excluded), got %d: %+v", len(refs), refs)
}
if hasLocation(refs, uri, 9, 1, 3) {
t.Errorf("declaration at 9:1 should be excluded, got %+v", refs)
}
if !hasLocation(refs, uri, 9, 18, 3) {
t.Errorf("missing same-line usage at 9:18, got %+v", refs)
}
if !hasLocation(refs, uri, 10, 7, 3) {
t.Errorf("missing RawGoExpr usage at 10:7, got %+v", refs)
}
if !hasLocation(refs, uri, 11, 10, 3) {
t.Errorf("missing attribute usage at 11:10, got %+v", refs)
}
if !hasLocation(refs, uri, 12, 7, 3) {
t.Errorf("missing child expr usage at 12:7, got %+v", refs)
}
if !hasLocation(refs, uri, 13, 19, 3) {
t.Errorf("missing iterable usage at 13:19, got %+v", refs)
}
if !hasLocation(refs, uri, 14, 4, 3) {
t.Errorf("missing condition usage at 14:4, got %+v", refs)
}
if !hasLocation(refs, uri, 15, 7, 3) {
t.Errorf("missing args usage at 15:7, got %+v", refs)
}
if !hasLocation(refs, uri, 16, 7, 3) {
t.Errorf("missing let-binding usage at 16:7, got %+v", refs)
}
}
|