gitstack

grindlemire/go-tui code browser

6.7 KB Go 298 lines 2026-06-03 · d15bb9f raw
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
package tuigen

import (
	"testing"
)

func TestParser_CommentAttachment_InForLoop(t *testing.T) {
	input := `package x

templ List(items []string) {
	for _, item := range items { // loop comment
		// comment before span
		<span>{item}</span>
	}
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	comp := file.Components[0]
	forLoop, ok := comp.Body[0].(*ForLoop)
	if !ok {
		t.Fatalf("expected *ForLoop, got %T", comp.Body[0])
	}

	// Check trailing comment on opening brace
	if forLoop.TrailingComments == nil {
		t.Fatal("expected TrailingComments on ForLoop, got nil")
	}

	if forLoop.TrailingComments.List[0].Text != "// loop comment" {
		t.Errorf("trailing comment text = %q", forLoop.TrailingComments.List[0].Text)
	}

	// Check leading comment on span
	if len(forLoop.Body) != 1 {
		t.Fatalf("expected 1 body node, got %d", len(forLoop.Body))
	}

	span, ok := forLoop.Body[0].(*Element)
	if !ok {
		t.Fatalf("expected *Element, got %T", forLoop.Body[0])
	}

	if span.LeadingComments == nil {
		t.Fatal("expected LeadingComments on span, got nil")
	}

	if span.LeadingComments.List[0].Text != "// comment before span" {
		t.Errorf("leading comment text = %q", span.LeadingComments.List[0].Text)
	}
}

func TestParser_CommentAttachment_InIfStmt(t *testing.T) {
	input := `package x

templ Cond(show bool) {
	if show { // if comment
		// comment before visible
		<span>Visible</span>
	}
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	comp := file.Components[0]
	ifStmt, ok := comp.Body[0].(*IfStmt)
	if !ok {
		t.Fatalf("expected *IfStmt, got %T", comp.Body[0])
	}

	// Check trailing comment on opening brace
	if ifStmt.TrailingComments == nil {
		t.Fatal("expected TrailingComments on IfStmt, got nil")
	}

	if ifStmt.TrailingComments.List[0].Text != "// if comment" {
		t.Errorf("trailing comment text = %q", ifStmt.TrailingComments.List[0].Text)
	}

	// Check leading comment on span
	if len(ifStmt.Then) != 1 {
		t.Fatalf("expected 1 then node, got %d", len(ifStmt.Then))
	}

	span, ok := ifStmt.Then[0].(*Element)
	if !ok {
		t.Fatalf("expected *Element, got %T", ifStmt.Then[0])
	}

	if span.LeadingComments == nil {
		t.Fatal("expected LeadingComments on span, got nil")
	}
}

func TestParser_CommentAttachment_EmptyForLoopBody(t *testing.T) {
	input := `package x

templ Empty() {
	for _, item := range items {
		// only a comment, no elements
	}
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	comp := file.Components[0]
	forLoop, ok := comp.Body[0].(*ForLoop)
	if !ok {
		t.Fatalf("expected *ForLoop, got %T", comp.Body[0])
	}

	// Comment in empty body should be an orphan
	if len(forLoop.OrphanComments) != 1 {
		t.Fatalf("expected 1 orphan comment group, got %d", len(forLoop.OrphanComments))
	}

	if forLoop.OrphanComments[0].List[0].Text != "// only a comment, no elements" {
		t.Errorf("orphan comment text = %q", forLoop.OrphanComments[0].List[0].Text)
	}
}

func TestParser_CommentAttachment_TrailingOnElement(t *testing.T) {
	input := `package x

templ Test() {
	<span>Hello</span>  // trailing on span
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	comp := file.Components[0]
	elem, ok := comp.Body[0].(*Element)
	if !ok {
		t.Fatalf("expected *Element, got %T", comp.Body[0])
	}

	if elem.TrailingComments == nil {
		t.Fatal("expected TrailingComments on element, got nil")
	}

	if elem.TrailingComments.List[0].Text != "// trailing on span" {
		t.Errorf("trailing comment text = %q", elem.TrailingComments.List[0].Text)
	}
}

func TestParser_CommentAttachment_SelfClosingElement(t *testing.T) {
	input := `package x

templ Test() {
	<input />  // trailing on self-closing
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	comp := file.Components[0]
	elem, ok := comp.Body[0].(*Element)
	if !ok {
		t.Fatalf("expected *Element, got %T", comp.Body[0])
	}

	if !elem.SelfClose {
		t.Error("expected SelfClose to be true")
	}

	if elem.TrailingComments == nil {
		t.Fatal("expected TrailingComments on self-closing element, got nil")
	}

	if elem.TrailingComments.List[0].Text != "// trailing on self-closing" {
		t.Errorf("trailing comment text = %q", elem.TrailingComments.List[0].Text)
	}
}

func TestParser_CommentAttachment_LeadingOnGoFunc(t *testing.T) {
	input := `package x

// Helper function comment
func helper() string {
	return "hello"
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	if len(file.Funcs) != 1 {
		t.Fatalf("expected 1 func, got %d", len(file.Funcs))
	}

	fn := file.Funcs[0]
	if fn.LeadingComments == nil {
		t.Fatal("expected LeadingComments on func, got nil")
	}

	if fn.LeadingComments.List[0].Text != "// Helper function comment" {
		t.Errorf("leading comment text = %q", fn.LeadingComments.List[0].Text)
	}
}

func TestParser_CommentAttachment_BlockComment(t *testing.T) {
	input := `package x

/* Block comment
   spanning multiple lines */
templ Header() {
	<span>Hello</span>
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	comp := file.Components[0]
	if comp.LeadingComments == nil {
		t.Fatal("expected LeadingComments, got nil")
	}

	if len(comp.LeadingComments.List) != 1 {
		t.Fatalf("expected 1 comment, got %d", len(comp.LeadingComments.List))
	}

	comment := comp.LeadingComments.List[0]
	if !comment.IsBlock {
		t.Error("expected block comment")
	}
}

func TestParser_CommentAttachment_NestedElements(t *testing.T) {
	input := `package x

templ Nested() {
	<div>
		// comment before inner span
		<span>Hello</span>
	</div>
}`

	l := NewLexer("test.gsx", input)
	p := NewParser(l)
	file, err := p.ParseFile()
	if err != nil {
		t.Fatalf("unexpected error: %v", err)
	}

	comp := file.Components[0]
	div, ok := comp.Body[0].(*Element)
	if !ok {
		t.Fatalf("expected *Element, got %T", comp.Body[0])
	}

	if len(div.Children) != 1 {
		t.Fatalf("expected 1 child, got %d", len(div.Children))
	}

	span, ok := div.Children[0].(*Element)
	if !ok {
		t.Fatalf("expected *Element child, got %T", div.Children[0])
	}

	if span.LeadingComments == nil {
		t.Fatal("expected LeadingComments on nested span, got nil")
	}

	if span.LeadingComments.List[0].Text != "// comment before inner span" {
		t.Errorf("leading comment text = %q", span.LeadingComments.List[0].Text)
	}
}