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
|
package provider
import (
"testing"
)
func TestDocumentSymbols_Components(t *testing.T) {
src := `package test
templ Header(title string) {
<div>{title}</div>
}
templ Footer() {
<span>footer</span>
}
`
doc := parseTestDoc(src)
sp := NewDocumentSymbolProvider()
result, err := sp.DocumentSymbols(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) != 2 {
t.Fatalf("expected 2 symbols (Header, Footer), got %d", len(result))
}
if result[0].Name != "Header" {
t.Errorf("expected first symbol name 'Header', got %q", result[0].Name)
}
if result[0].Kind != SymbolKindFunction {
t.Errorf("expected kind Function, got %d", result[0].Kind)
}
if result[0].Detail != "(title string)" {
t.Errorf("expected detail '(title string)', got %q", result[0].Detail)
}
if result[1].Name != "Footer" {
t.Errorf("expected second symbol name 'Footer', got %q", result[1].Name)
}
if result[1].Detail != "()" {
t.Errorf("expected detail '()', got %q", result[1].Detail)
}
}
func TestDocumentSymbols_Functions(t *testing.T) {
src := `package test
func helper(s string) string {
return s
}
`
doc := parseTestDoc(src)
sp := NewDocumentSymbolProvider()
result, err := sp.DocumentSymbols(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) != 1 {
t.Fatalf("expected 1 symbol (helper), got %d", len(result))
}
if result[0].Name != "helper" {
t.Errorf("expected symbol name 'helper', got %q", result[0].Name)
}
if result[0].Kind != SymbolKindFunction {
t.Errorf("expected kind Function, got %d", result[0].Kind)
}
if result[0].Detail != "func" {
t.Errorf("expected detail 'func', got %q", result[0].Detail)
}
}
func TestDocumentSymbols_LetBindingChildren(t *testing.T) {
src := `package test
templ Page() {
header := <div>Header</div>
{header}
}
`
doc := parseTestDoc(src)
sp := NewDocumentSymbolProvider()
result, err := sp.DocumentSymbols(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) != 1 {
t.Fatalf("expected 1 top-level symbol (Page), got %d", len(result))
}
page := result[0]
if page.Name != "Page" {
t.Errorf("expected 'Page', got %q", page.Name)
}
if len(page.Children) < 1 {
t.Errorf("expected at least 1 child symbol (let binding), got %d", len(page.Children))
} else {
found := false
for _, child := range page.Children {
if child.Name == "header" && child.Kind == SymbolKindVariable {
found = true
break
}
}
if !found {
t.Errorf("expected child symbol 'header' with kind Variable")
}
}
}
func TestDocumentSymbols_NilAST(t *testing.T) {
doc := &Document{
URI: "file:///test.gsx",
Content: "invalid content",
}
sp := NewDocumentSymbolProvider()
result, err := sp.DocumentSymbols(doc)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) != 0 {
t.Errorf("expected 0 symbols for nil AST, got %d", len(result))
}
}
func TestWorkspaceSymbols_NameMatching(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: 20},
},
},
}
index.components["Footer"] = &ComponentInfo{
Name: "Footer",
Location: Location{
URI: "file:///test.gsx",
Range: Range{
Start: Position{Line: 10, Character: 0},
End: Position{Line: 10, Character: 20},
},
},
}
wp := NewWorkspaceSymbolProvider(index)
result, err := wp.WorkspaceSymbols("")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) < 2 {
t.Errorf("expected at least 2 symbols for empty query, got %d", len(result))
}
result, err = wp.WorkspaceSymbols("head")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) != 1 {
t.Errorf("expected 1 symbol for 'head' query, got %d", len(result))
}
if len(result) > 0 && result[0].Name != "Header" {
t.Errorf("expected 'Header', got %q", result[0].Name)
}
}
func TestWorkspaceSymbols_FunctionSearch(t *testing.T) {
index := newStubIndex()
index.functions["formatLabel"] = &FuncInfo{
Name: "formatLabel",
Signature: "func formatLabel(s string) string",
Location: Location{
URI: "file:///test.gsx",
Range: Range{
Start: Position{Line: 5, Character: 0},
End: Position{Line: 5, Character: 30},
},
},
}
wp := NewWorkspaceSymbolProvider(index)
result, err := wp.WorkspaceSymbols("format")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) != 1 {
t.Errorf("expected 1 symbol for 'format' query, got %d", len(result))
}
if len(result) > 0 && result[0].Name != "formatLabel" {
t.Errorf("expected 'formatLabel', got %q", result[0].Name)
}
}
func TestWorkspaceSymbols_NoMatch(t *testing.T) {
index := newStubIndex()
index.components["Header"] = &ComponentInfo{Name: "Header"}
wp := NewWorkspaceSymbolProvider(index)
result, err := wp.WorkspaceSymbols("xyz")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(result) != 0 {
t.Errorf("expected 0 symbols for 'xyz' query, got %d", len(result))
}
}
|