gitstack

grindlemire/go-tui code browser

5.9 KB Go 239 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
package tuigen

import (
	"testing"
)

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

// This is a doc comment for Header
// It spans 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)
	}

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

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

	if len(comp.LeadingComments.List) != 2 {
		t.Fatalf("expected 2 comments in group, got %d", len(comp.LeadingComments.List))
	}

	if comp.LeadingComments.List[0].Text != "// This is a doc comment for Header" {
		t.Errorf("comment 0 text = %q", comp.LeadingComments.List[0].Text)
	}
	if comp.LeadingComments.List[1].Text != "// It spans multiple lines" {
		t.Errorf("comment 1 text = %q", comp.LeadingComments.List[1].Text)
	}
}

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

templ Header() { // trailing comment on brace
	<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.TrailingComments == nil {
		t.Fatal("expected TrailingComments, got nil")
	}

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

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

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

templ Header() {
	// orphan comment in body
	<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]

	// The orphan comment should be attached as leading comment to the span element
	if len(comp.Body) != 1 {
		t.Fatalf("expected 1 body node, got %d", len(comp.Body))
	}

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

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

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

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

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

templ Header() {
	<span>Hello</span>
	// trailing orphan comment
}`

	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]

	// The trailing orphan comment should be in OrphanComments
	if len(comp.OrphanComments) != 1 {
		t.Fatalf("expected 1 orphan comment group, got %d", len(comp.OrphanComments))
	}

	if len(comp.OrphanComments[0].List) != 1 {
		t.Fatalf("expected 1 comment in orphan group, got %d", len(comp.OrphanComments[0].List))
	}

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

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

templ Header() {
	<span>Hello</span>
}

// orphan comment at end of file`

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

	if len(file.OrphanComments) != 1 {
		t.Fatalf("expected 1 orphan comment group in file, got %d", len(file.OrphanComments))
	}

	if file.OrphanComments[0].List[0].Text != "// orphan comment at end of file" {
		t.Errorf("orphan comment text = %q", file.OrphanComments[0].List[0].Text)
	}
}

func TestParser_CommentAttachment_LeadingCommentBeforePackage(t *testing.T) {
	input := `// File header comment
// License info
package x

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)
	}

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

	if len(file.LeadingComments.List) != 2 {
		t.Fatalf("expected 2 comments in file leading group, got %d", len(file.LeadingComments.List))
	}
}

func TestParser_CommentGrouping_BlankLineSeparation(t *testing.T) {
	type tc struct {
		comments   []*Comment
		wantGroups int
	}

	tests := map[string]tc{
		"single comment": {
			comments: []*Comment{
				{Text: "// a", Position: Position{Line: 1}, EndLine: 1},
			},
			wantGroups: 1,
		},
		"adjacent comments": {
			comments: []*Comment{
				{Text: "// a", Position: Position{Line: 1}, EndLine: 1},
				{Text: "// b", Position: Position{Line: 2}, EndLine: 2},
			},
			wantGroups: 1,
		},
		"blank line separation": {
			comments: []*Comment{
				{Text: "// a", Position: Position{Line: 1}, EndLine: 1},
				{Text: "// b", Position: Position{Line: 3}, EndLine: 3}, // line 2 is blank
			},
			wantGroups: 2,
		},
		"multiple groups": {
			comments: []*Comment{
				{Text: "// a", Position: Position{Line: 1}, EndLine: 1},
				{Text: "// b", Position: Position{Line: 2}, EndLine: 2},
				{Text: "// c", Position: Position{Line: 5}, EndLine: 5}, // blank lines
				{Text: "// d", Position: Position{Line: 6}, EndLine: 6},
			},
			wantGroups: 2,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			groups := groupComments(tt.comments)
			if len(groups) != tt.wantGroups {
				t.Errorf("got %d groups, want %d", len(groups), tt.wantGroups)
			}
		})
	}
}