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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
|
package provider
import (
"testing"
"github.com/grindlemire/go-tui/internal/tuigen"
)
func newTestDefinitionProvider(index ComponentIndex) *definitionProvider {
return &definitionProvider{
index: index,
goplsProxy: &nilGoplsProxy{},
virtualFiles: &nilVirtualFiles{},
docs: &stubDocAccessor{},
}
}
func TestDefinition_ComponentCall(t *testing.T) {
index := newStubIndex()
index.components["Header"] = &ComponentInfo{
Name: "Header",
Location: Location{
URI: "file:///test.gsx",
Range: Range{
Start: Position{Line: 2, Character: 0},
End: Position{Line: 2, Character: 17},
},
},
}
dp := newTestDefinitionProvider(index)
src := `package test
templ Page() {
@Header("title")
}
templ Header(title string) {
<div>{title}</div>
}
`
doc := parseTestDoc(src)
call := doc.AST.Components[0].Body[0].(*tuigen.ComponentCall)
ctx := makeCtx(doc, NodeKindComponentCall, "@Header")
ctx.Node = call
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location")
}
if result[0].URI != "file:///test.gsx" {
t.Errorf("expected URI file:///test.gsx, got %s", result[0].URI)
}
}
func TestDefinition_FunctionByWord(t *testing.T) {
index := newStubIndex()
index.functions["helper"] = &FuncInfo{
Name: "helper",
Signature: "func helper(s string) string",
Location: Location{
URI: "file:///test.gsx",
Range: Range{
Start: Position{Line: 5, Character: 0},
End: Position{Line: 5, Character: 15},
},
},
}
dp := newTestDefinitionProvider(index)
doc := parseTestDoc("package test")
ctx := makeCtx(doc, NodeKindUnknown, "helper")
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location for function")
}
if result[0].Range.Start.Line != 5 {
t.Errorf("expected line 5, got %d", result[0].Range.Start.Line)
}
}
func TestDefinition_Parameter(t *testing.T) {
index := newStubIndex()
index.params["Header.title"] = &ParamInfo{
Name: "title",
Type: "string",
ComponentName: "Header",
Location: Location{
URI: "file:///test.gsx",
Range: Range{
Start: Position{Line: 2, Character: 13},
End: Position{Line: 2, Character: 18},
},
},
}
dp := newTestDefinitionProvider(index)
src := `package test
templ Header(title string) {
<div>{title}</div>
}
`
doc := parseTestDoc(src)
ctx := makeCtx(doc, NodeKindUnknown, "title")
ctx.Scope.Component = doc.AST.Components[0]
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location for parameter")
}
if result[0].Range.Start.Line != 2 {
t.Errorf("expected line 2, got %d", result[0].Range.Start.Line)
}
}
func TestDefinition_LetBinding(t *testing.T) {
index := newStubIndex()
dp := newTestDefinitionProvider(index)
src := `package test
templ Example() {
header := <div>title</div>
{header}
}
`
doc := parseTestDoc(src)
ctx := makeCtx(doc, NodeKindUnknown, "header")
ctx.Scope.Component = doc.AST.Components[0]
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location for let binding")
}
}
func TestDefinition_ForLoopVariable(t *testing.T) {
index := newStubIndex()
dp := newTestDefinitionProvider(index)
src := `package test
templ List(items []string) {
<div>
for _, item := range items {
<span>{item}</span>
}
</div>
}
`
doc := parseTestDoc(src)
ctx := makeCtx(doc, NodeKindUnknown, "item")
ctx.Scope.Component = doc.AST.Components[0]
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location for loop variable")
}
}
func TestDefinition_NilDocument(t *testing.T) {
index := newStubIndex()
dp := newTestDefinitionProvider(index)
doc := &Document{URI: "file:///test.gsx", Content: "", Version: 1}
ctx := makeCtx(doc, NodeKindUnknown, "")
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result != nil {
t.Errorf("expected nil for empty word, got %v", result)
}
}
func TestDefinition_RefAttr(t *testing.T) {
index := newStubIndex()
dp := newTestDefinitionProvider(index)
src := `package test
templ Layout() {
<div ref={header} class="p-1">title</div>
}
`
doc := parseTestDoc(src)
elem := doc.AST.Components[0].Body[0].(*tuigen.Element)
ctx := makeCtx(doc, NodeKindRefAttr, "header")
ctx.Node = elem
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location for ref attr")
}
loc := result[0]
line := loc.Range.Start.Line
char := loc.Range.Start.Character
endChar := loc.Range.End.Character
if line != 3 {
t.Errorf("expected line 3, got %d", line)
}
if endChar-char != len("ref={header}") {
t.Errorf("expected range length %d, got %d", len("ref={header}"), endChar-char)
}
}
func TestDefinition_RefAttr_Usage(t *testing.T) {
index := newStubIndex()
dp := newTestDefinitionProvider(index)
src := `package test
templ Layout() {
<div
ref={content}
class="p-1">title</div>
<span>{content}</span>
}
`
doc := parseTestDoc(src)
elem := doc.AST.Components[0].Body[0].(*tuigen.Element)
ctx := makeCtx(doc, NodeKindGoExpr, "content")
ctx.InGoExpr = true
ctx.Scope.Component = doc.AST.Components[0]
ctx.Scope.Refs = []tuigen.RefInfo{
{Name: "content", Element: elem},
}
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location for ref usage in Go expr")
}
loc := result[0]
if loc.Range.Start.Line != 4 {
t.Errorf("expected line 4, got %d", loc.Range.Start.Line)
}
if loc.Range.End.Character-loc.Range.Start.Character != len("ref={content}") {
t.Errorf("expected range length %d, got %d", len("ref={content}"), loc.Range.End.Character-loc.Range.Start.Character)
}
}
func TestDefinition_RefAttr_WithDeclaration(t *testing.T) {
index := newStubIndex()
dp := newTestDefinitionProvider(index)
src := `package test
templ StreamApp() {
content := tui.NewRef()
<div ref={content} class="p-1">title</div>
}
`
doc := parseTestDoc(src)
elem := doc.AST.Components[0].Body[1].(*tuigen.Element)
ctx := makeCtx(doc, NodeKindRefAttr, "content")
ctx.Node = elem
ctx.Scope.Component = doc.AST.Components[0]
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location for ref attr with declaration")
}
loc := result[0]
if loc.Range.Start.Line != 3 {
t.Errorf("expected line 3 (declaration), got %d", loc.Range.Start.Line)
}
if loc.Range.End.Character-loc.Range.Start.Character != len("content") {
t.Errorf("expected range length %d, got %d", len("content"), loc.Range.End.Character-loc.Range.Start.Character)
}
}
func TestDefinition_RefAttr_Multiline(t *testing.T) {
index := newStubIndex()
dp := newTestDefinitionProvider(index)
src := `package test
templ Layout() {
<div
ref={header}
class="p-1">title</div>
}
`
doc := parseTestDoc(src)
elem := doc.AST.Components[0].Body[0].(*tuigen.Element)
ctx := makeCtx(doc, NodeKindRefAttr, "header")
ctx.Node = elem
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location for multiline ref attr")
}
loc := result[0]
if loc.Range.Start.Line != 4 {
t.Errorf("expected line 4, got %d", loc.Range.Start.Line)
}
if loc.Range.End.Character-loc.Range.Start.Character != len("ref={header}") {
t.Errorf("expected range length %d, got %d", len("ref={header}"), loc.Range.End.Character-loc.Range.Start.Character)
}
}
func TestDefinition_StateDecl(t *testing.T) {
index := newStubIndex()
dp := newTestDefinitionProvider(index)
src := `package test
templ Counter() {
count := tui.NewState(0)
<span>{count.Get()}</span>
}
`
doc := parseTestDoc(src)
ctx := makeCtx(doc, NodeKindStateDecl, "count")
ctx.Scope.Component = doc.AST.Components[0]
ctx.Scope.StateVars = []tuigen.StateVar{
{Name: "count", Type: "int", InitExpr: "0", Position: tuigen.Position{Line: 4, Column: 2}},
}
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location for state decl")
}
if result[0].Range.Start.Line != 3 {
t.Errorf("expected line 3, got %d", result[0].Range.Start.Line)
}
}
func TestDefinition_StateAccess(t *testing.T) {
index := newStubIndex()
dp := newTestDefinitionProvider(index)
src := `package test
templ Counter() {
count := tui.NewState(0)
<span>{count.Get()}</span>
}
`
doc := parseTestDoc(src)
ctx := makeCtx(doc, NodeKindStateAccess, "count")
ctx.Scope.Component = doc.AST.Components[0]
ctx.Scope.StateVars = []tuigen.StateVar{
{Name: "count", Type: "int", InitExpr: "0", Position: tuigen.Position{Line: 4, Column: 2}},
}
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) == 0 {
t.Fatal("expected definition location for state access")
}
if result[0].Range.Start.Line != 3 {
t.Errorf("expected line 3, got %d", result[0].Range.Start.Line)
}
}
func TestDefinition_EventHandler(t *testing.T) {
index := newStubIndex()
dp := newTestDefinitionProvider(index)
doc := parseTestDoc("package test")
ctx := makeCtx(doc, NodeKindEventHandler, "handleClick")
ctx.InGoExpr = true
result, err := dp.Definition(ctx)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) > 0 {
t.Errorf("expected nil result without gopls, got %v", result)
}
}
func TestFindVarDeclPosition_WordBoundary(t *testing.T) {
type tc struct {
code string
varName string
want int
}
tests := map[string]tc{
"simple decl": {
code: "count := 1", varName: "count", want: 0,
},
"no substring match": {
code: "accountCount := 1", varName: "count", want: -1,
},
"multi var decl": {
code: "a, count := 1, 2", varName: "count", want: 3,
},
"var keyword": {
code: "var count = 1", varName: "count", want: 4,
},
"var keyword no substring": {
code: "var accountCount = 1", varName: "count", want: -1,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := findVarDeclPosition(tt.code, tt.varName)
if got != tt.want {
t.Errorf("findVarDeclPosition(%q, %q) = %d, want %d", tt.code, tt.varName, got, tt.want)
}
})
}
}
func TestContainsVarDecl(t *testing.T) {
type tc struct {
code string
varName string
want bool
}
tests := map[string]tc{
"simple match": {
code: "x := 1", varName: "x", want: true,
},
"no match": {
code: "y := 1", varName: "x", want: false,
},
"multi var": {
code: "a, b := 1, 2", varName: "b", want: true,
},
"var keyword": {
code: "var x = 1", varName: "x", want: true,
},
"no assignment": {
code: "fmt.Println(x)", varName: "x", want: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got := containsVarDecl(tt.code, tt.varName)
if got != tt.want {
t.Errorf("containsVarDecl(%q, %q) = %v, want %v", tt.code, tt.varName, got, tt.want)
}
})
}
}
|