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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
|
package provider
import (
"sort"
"strings"
"testing"
"github.com/grindlemire/go-tui/internal/tuigen"
)
func TestSemanticTokens_ComponentCalls(t *testing.T) {
type tc struct {
content string
wantDecorator int
}
tests := map[string]tc{
"component call": {
content: `package main
templ App() {
@Header("title")
}
`,
wantDecorator: 1,
},
}
sp := newTestSemanticProvider()
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
doc := parseTestDoc(tt.content)
result, err := sp.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tokens := decodeTokens(result.Data)
decoratorCount := countByType(tokens, TokenTypeDecorator)
if decoratorCount < tt.wantDecorator {
t.Errorf("got %d decorator tokens, want at least %d", decoratorCount, tt.wantDecorator)
}
})
}
}
func TestSemanticTokens_TokenTypeConstants(t *testing.T) {
type tc struct {
name string
constant int
expected int
}
tests := []tc{
{"namespace", TokenTypeNamespace, 0},
{"type", TokenTypeType, 1},
{"class", TokenTypeClass, 2},
{"function", TokenTypeFunction, 3},
{"parameter", TokenTypeParameter, 4},
{"variable", TokenTypeVariable, 5},
{"property", TokenTypeProperty, 6},
{"keyword", TokenTypeKeyword, 7},
{"string", TokenTypeString, 8},
{"number", TokenTypeNumber, 9},
{"operator", TokenTypeOperator, 10},
{"decorator", TokenTypeDecorator, 11},
{"regexp", TokenTypeRegexp, 12},
{"comment", TokenTypeComment, 13},
{"label", TokenTypeLabel, 14},
{"typeParameter", TokenTypeTypeParameter, 15},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.constant != tt.expected {
t.Errorf("TokenType%s = %d, want %d", tt.name, tt.constant, tt.expected)
}
})
}
}
func TestSemanticTokens_Encoding(t *testing.T) {
type tc struct {
tokens []SemanticToken
expected []int
}
tests := map[string]tc{
"single token": {
tokens: []SemanticToken{
{Line: 0, StartChar: 0, Length: 7, TokenType: TokenTypeKeyword, Modifiers: 0},
},
expected: []int{0, 0, 7, TokenTypeKeyword, 0},
},
"two tokens same line": {
tokens: []SemanticToken{
{Line: 0, StartChar: 0, Length: 7, TokenType: TokenTypeKeyword, Modifiers: 0},
{Line: 0, StartChar: 8, Length: 5, TokenType: TokenTypeClass, Modifiers: 0},
},
expected: []int{
0, 0, 7, TokenTypeKeyword, 0,
0, 8, 5, TokenTypeClass, 0,
},
},
"two tokens different lines": {
tokens: []SemanticToken{
{Line: 0, StartChar: 0, Length: 7, TokenType: TokenTypeKeyword, Modifiers: 0},
{Line: 2, StartChar: 1, Length: 4, TokenType: TokenTypeVariable, Modifiers: 0},
},
expected: []int{
0, 0, 7, TokenTypeKeyword, 0,
2, 1, 4, TokenTypeVariable, 0,
},
},
"empty": {
tokens: []SemanticToken{},
expected: []int{},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
sort.Slice(tt.tokens, func(i, j int) bool {
if tt.tokens[i].Line != tt.tokens[j].Line {
return tt.tokens[i].Line < tt.tokens[j].Line
}
return tt.tokens[i].StartChar < tt.tokens[j].StartChar
})
result := EncodeSemanticTokens(tt.tokens)
if len(result) != len(tt.expected) {
t.Fatalf("got %d ints, want %d", len(result), len(tt.expected))
}
for i := range result {
if result[i] != tt.expected[i] {
t.Errorf("result[%d] = %d, want %d", i, result[i], tt.expected[i])
}
}
})
}
}
func TestSemanticTokens_NilAST(t *testing.T) {
sp := newTestSemanticProvider()
doc := &Document{
URI: "file:///test.gsx",
Content: "",
Version: 1,
AST: nil,
}
result, err := sp.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if result == nil {
t.Fatal("expected non-nil result")
}
if len(result.Data) != 0 {
t.Errorf("expected empty data, got %d ints", len(result.Data))
}
}
func TestSemanticTokens_Variables(t *testing.T) {
sp := newTestSemanticProvider()
doc := parseTestDoc(`package main
templ Greeting(name string) {
<span>{name}</span>
}
`)
result, err := sp.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tokens := decodeTokens(result.Data)
paramCount := countByType(tokens, TokenTypeParameter)
if paramCount == 0 {
t.Error("expected at least one parameter token for 'name'")
}
}
func TestSemanticTokens_RefAttr(t *testing.T) {
type tc struct {
content string
wantRefAttr int
wantVarDecl int
}
tests := map[string]tc{
"simple ref attr": {
content: `package main
templ Layout() {
<div ref={header} class="p-1">title</div>
}
`,
wantRefAttr: 1,
wantVarDecl: 1,
},
}
sp := newTestSemanticProvider()
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
doc := parseTestDoc(tt.content)
result, err := sp.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tokens := decodeTokens(result.Data)
refAttrCount := 0
for _, tok := range tokens {
if tok.TokenType == TokenTypeFunction && tok.Length == len("ref") {
refAttrCount++
}
}
if refAttrCount < tt.wantRefAttr {
t.Errorf("got %d ref attribute tokens, want at least %d", refAttrCount, tt.wantRefAttr)
}
varDeclCount := 0
for _, tok := range tokens {
if tok.TokenType == TokenTypeVariable && tok.Modifiers&TokenModDeclaration != 0 {
varDeclCount++
}
}
if varDeclCount < tt.wantVarDecl {
t.Errorf("got %d variable declaration tokens, want at least %d (for ref value)", varDeclCount, tt.wantVarDecl)
}
})
}
}
func TestSemanticTokens_EventHandlerAttributes(t *testing.T) {
type tc struct {
content string
wantDecorator int
wantFunction int
}
tests := map[string]tc{
"event handler vs regular attribute": {
content: `package main
templ Button() {
<button onFocus={handleFocus} class="p-1">Click</button>
}
`,
wantDecorator: 2,
wantFunction: 1,
},
}
sp := newTestSemanticProvider()
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
doc := parseTestDoc(tt.content)
result, err := sp.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tokens := decodeTokens(result.Data)
decoratorCount := countByType(tokens, TokenTypeDecorator)
if decoratorCount < 1 {
t.Errorf("got %d decorator tokens, want at least 1 (for onFocus)", decoratorCount)
}
funcCount := countByType(tokens, TokenTypeFunction)
if funcCount < tt.wantFunction {
t.Errorf("got %d function tokens, want at least %d (for regular attributes)", funcCount, tt.wantFunction)
}
})
}
}
func TestSemanticTokens_StateVarDeclaration(t *testing.T) {
type tc struct {
content string
wantReadonlyDecl int
}
tests := map[string]tc{
"state variable declaration": {
content: `package main
templ Counter() {
count := tui.NewState(0)
<span>{count}</span>
}
`,
wantReadonlyDecl: 1,
},
}
sp := newTestSemanticProvider()
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
doc := parseTestDoc(tt.content)
result, err := sp.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tokens := decodeTokens(result.Data)
readonlyDeclCount := 0
for _, tok := range tokens {
if tok.TokenType == TokenTypeVariable &&
tok.Modifiers&TokenModDeclaration != 0 &&
tok.Modifiers&TokenModReadonly != 0 {
readonlyDeclCount++
}
}
if readonlyDeclCount < tt.wantReadonlyDecl {
t.Errorf("got %d variable tokens with declaration+readonly modifiers, want at least %d",
readonlyDeclCount, tt.wantReadonlyDecl)
}
})
}
}
func TestSemanticTokens_MultipleComponents(t *testing.T) {
sp := newTestSemanticProvider()
doc := parseTestDoc(`package main
templ Header(title string) {
<div>{title}</div>
}
templ Footer() {
<span>Footer</span>
}
`)
result, err := sp.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tokens := decodeTokens(result.Data)
keywordCount := countByType(tokens, TokenTypeKeyword)
if keywordCount < 2 {
t.Errorf("got %d keyword tokens, want at least 2", keywordCount)
}
classCount := countByType(tokens, TokenTypeClass)
if classCount < 2 {
t.Errorf("got %d class tokens, want at least 2", classCount)
}
}
func TestSemanticTokens_StateModifierOnlyOnStateVar(t *testing.T) {
src := `package test
templ Counter() {
count := tui.NewState(0)
<span>{count.Get()}</span>
}
`
sp := newTestSemanticProvider()
doc := parseTestDoc(src)
result, err := sp.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tokens := decodeTokens(result.Data)
for _, tok := range tokens {
if tok.TokenType == TokenTypeVariable && (tok.Modifiers&TokenModDeclaration) != 0 {
if tok.StartChar == 1 {
if (tok.Modifiers & TokenModReadonly) == 0 {
t.Error("state variable 'count' should have readonly modifier")
}
}
}
}
}
func TestSemanticTokens_NonStateVarNoReadonly(t *testing.T) {
src := `package test
templ Example() {
count := tui.NewState(0)
<span>{count.Get()}</span>
}
`
sp := newTestSemanticProvider()
doc := parseTestDoc(src)
result, err := sp.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tokens := decodeTokens(result.Data)
varDeclCount := 0
readonlyVarCount := 0
for _, tok := range tokens {
if tok.TokenType == TokenTypeVariable && (tok.Modifiers&TokenModDeclaration) != 0 {
varDeclCount++
if (tok.Modifiers & TokenModReadonly) != 0 {
readonlyVarCount++
}
}
}
if varDeclCount == 0 {
t.Error("expected at least one variable declaration token")
}
if readonlyVarCount > 1 {
t.Errorf("expected at most 1 readonly variable, got %d", readonlyVarCount)
}
}
func TestCollectTokensInGoCode_MultiByteChars(t *testing.T) {
type tc struct {
code string
paramNames map[string]bool
localVars map[string]bool
expected []struct {
startChar int
length int
tokenType int
}
}
tests := map[string]tc{
"param after multibyte string": {
code: `return "▶ " + vn.node.Name`,
paramNames: map[string]bool{"vn": true},
localVars: map[string]bool{},
expected: []struct {
startChar int
length int
tokenType int
}{
{startChar: 7, length: 4, tokenType: TokenTypeString},
{startChar: 14, length: 2, tokenType: TokenTypeParameter},
{startChar: 22, length: 4, tokenType: TokenTypeFunction},
},
},
"no multibyte baseline": {
code: `return "X " + vn.node.Name`,
paramNames: map[string]bool{"vn": true},
localVars: map[string]bool{},
expected: []struct {
startChar int
length int
tokenType int
}{
{startChar: 7, length: 4, tokenType: TokenTypeString},
{startChar: 14, length: 2, tokenType: TokenTypeParameter},
{startChar: 22, length: 4, tokenType: TokenTypeFunction},
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
sp := newTestSemanticProvider()
var tokens []SemanticToken
pos := tuigen.Position{Line: 1, Column: 1}
sp.collectTokensInGoCode(tt.code, pos, 0, tt.paramNames, tt.localVars, &tokens)
sort.Slice(tokens, func(i, j int) bool {
if tokens[i].Line != tokens[j].Line {
return tokens[i].Line < tokens[j].Line
}
return tokens[i].StartChar < tokens[j].StartChar
})
for _, exp := range tt.expected {
found := false
for _, tok := range tokens {
if tok.StartChar == exp.startChar && tok.Length == exp.length && tok.TokenType == exp.tokenType {
found = true
break
}
}
if !found {
t.Errorf("expected token at startChar=%d length=%d type=%d, got tokens: %+v",
exp.startChar, exp.length, exp.tokenType, tokens)
}
}
})
}
}
func TestSemanticTokens_GoFuncWithMultiByteChars(t *testing.T) {
src := `package main
import (
"strings"
)
type Node struct {
Name string
Children []Node
}
type visibleNode struct {
node Node
depth int
path string
isDir bool
isLast bool
ancestors []bool
onPath bool
}
func buildPrefix(vn visibleNode) string {
if vn.depth == 0 {
return ""
}
var b strings.Builder
for i := 0; i < vn.depth-1; i++ {
if vn.ancestors[i+1] {
b.WriteString(" ")
} else {
b.WriteString("│ ")
}
}
if vn.isLast {
b.WriteString("└── ")
} else {
b.WriteString("├── ")
}
return b.String()
}
func nodeLabel(vn visibleNode, expanded map[string]bool) string {
if vn.isDir {
if expanded[vn.path] {
return "▼ " + vn.node.Name
}
return "▶ " + vn.node.Name
}
return vn.node.Name
}
`
sp := newTestSemanticProvider()
doc := parseTestDoc(src)
result, err := sp.SemanticTokensFull(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
tokens := decodeTokens(result.Data)
srcLines := strings.Split(src, "\n")
targetLine := -1
asciiLine := -1
for i, line := range srcLines {
if strings.Contains(line, "\"▶ \"") {
targetLine = i
}
if strings.TrimSpace(line) == "return vn.node.Name" {
asciiLine = i
}
}
if targetLine == -1 {
t.Fatal("could not find line with ▶ in source")
}
if asciiLine == -1 {
t.Fatal("could not find ASCII-only return line in source")
}
var lineTokens []SemanticToken
for _, tok := range tokens {
if tok.Line == targetLine {
lineTokens = append(lineTokens, tok)
}
}
t.Logf("All tokens on line %d: %+v", targetLine, lineTokens)
if !hasTokenAt(tokens, targetLine, 9, 4, TokenTypeString) {
t.Errorf("expected string token for '\"▶ \"' at col 9, length 4 on line %d", targetLine)
}
if !hasTokenAt(tokens, targetLine, 16, 2, TokenTypeParameter) {
t.Errorf("expected parameter token for 'vn' at col 16, length 2 on line %d", targetLine)
}
if !hasTokenAt(tokens, targetLine, 24, 4, TokenTypeFunction) {
t.Errorf("expected function token for 'Name' at col 24, length 4 on line %d", targetLine)
}
var asciiLineTokens []SemanticToken
for _, tok := range tokens {
if tok.Line == asciiLine {
asciiLineTokens = append(asciiLineTokens, tok)
}
}
t.Logf("All tokens on line %d: %+v", asciiLine, asciiLineTokens)
if !hasTokenAt(tokens, asciiLine, 8, 2, TokenTypeParameter) {
t.Errorf("expected parameter token for 'vn' at col 8, length 2 on line %d", asciiLine)
}
if !hasTokenAt(tokens, asciiLine, 16, 4, TokenTypeFunction) {
t.Errorf("expected function token for 'Name' at col 16, length 4 on line %d", asciiLine)
}
}
|