gitstack

grindlemire/go-tui code browser

12.9 KB Go 530 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
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
package tuigen

import (
	"testing"
)

func TestLexer_CommentCollection_LineComment(t *testing.T) {
	type tc struct {
		input       string
		wantText    string
		wantLine    int
		wantEndLine int
		wantIsBlock bool
	}

	tests := map[string]tc{
		"simple line comment": {
			input:       "// hello",
			wantText:    "// hello",
			wantLine:    1,
			wantEndLine: 1,
			wantIsBlock: false,
		},
		"line comment with spaces": {
			input:       "//   spaced   ",
			wantText:    "//   spaced   ",
			wantLine:    1,
			wantEndLine: 1,
			wantIsBlock: false,
		},
		"empty line comment": {
			input:       "//",
			wantText:    "//",
			wantLine:    1,
			wantEndLine: 1,
			wantIsBlock: false,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			l := NewLexer("test.tui", tt.input)
			// Trigger comment collection by calling Next
			tok := l.Next()
			if tok.Type != TokenEOF {
				t.Errorf("expected EOF after comment, got %v", tok.Type)
			}

			comments := l.ConsumeComments()
			if len(comments) != 1 {
				t.Fatalf("expected 1 comment, got %d", len(comments))
			}

			c := comments[0]
			if c.Text != tt.wantText {
				t.Errorf("Text = %q, want %q", c.Text, tt.wantText)
			}
			if c.Position.Line != tt.wantLine {
				t.Errorf("Position.Line = %d, want %d", c.Position.Line, tt.wantLine)
			}
			if c.EndLine != tt.wantEndLine {
				t.Errorf("EndLine = %d, want %d", c.EndLine, tt.wantEndLine)
			}
			if c.IsBlock != tt.wantIsBlock {
				t.Errorf("IsBlock = %v, want %v", c.IsBlock, tt.wantIsBlock)
			}
		})
	}
}

func TestLexer_CommentCollection_BlockComment(t *testing.T) {
	type tc struct {
		input       string
		wantText    string
		wantLine    int
		wantEndLine int
		wantIsBlock bool
	}

	tests := map[string]tc{
		"simple block comment": {
			input:       "/* hello */",
			wantText:    "/* hello */",
			wantLine:    1,
			wantEndLine: 1,
			wantIsBlock: true,
		},
		"empty block comment": {
			input:       "/**/",
			wantText:    "/**/",
			wantLine:    1,
			wantEndLine: 1,
			wantIsBlock: true,
		},
		"block comment with asterisks": {
			input:       "/* * ** *** */",
			wantText:    "/* * ** *** */",
			wantLine:    1,
			wantEndLine: 1,
			wantIsBlock: true,
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			l := NewLexer("test.tui", tt.input)
			// Trigger comment collection by calling Next
			tok := l.Next()
			if tok.Type != TokenEOF {
				t.Errorf("expected EOF after comment, got %v", tok.Type)
			}

			comments := l.ConsumeComments()
			if len(comments) != 1 {
				t.Fatalf("expected 1 comment, got %d", len(comments))
			}

			c := comments[0]
			if c.Text != tt.wantText {
				t.Errorf("Text = %q, want %q", c.Text, tt.wantText)
			}
			if c.Position.Line != tt.wantLine {
				t.Errorf("Position.Line = %d, want %d", c.Position.Line, tt.wantLine)
			}
			if c.EndLine != tt.wantEndLine {
				t.Errorf("EndLine = %d, want %d", c.EndLine, tt.wantEndLine)
			}
			if c.IsBlock != tt.wantIsBlock {
				t.Errorf("IsBlock = %v, want %v", c.IsBlock, tt.wantIsBlock)
			}
		})
	}
}

func TestLexer_CommentCollection_MultilineBlock(t *testing.T) {
	input := `/* line 1
line 2
line 3 */`

	l := NewLexer("test.tui", input)
	tok := l.Next()
	if tok.Type != TokenEOF {
		t.Errorf("expected EOF after comment, got %v", tok.Type)
	}

	comments := l.ConsumeComments()
	if len(comments) != 1 {
		t.Fatalf("expected 1 comment, got %d", len(comments))
	}

	c := comments[0]
	if c.Text != input {
		t.Errorf("Text = %q, want %q", c.Text, input)
	}
	if c.Position.Line != 1 {
		t.Errorf("Position.Line = %d, want 1", c.Position.Line)
	}
	if c.EndLine != 3 {
		t.Errorf("EndLine = %d, want 3", c.EndLine)
	}
	if !c.IsBlock {
		t.Error("IsBlock should be true for block comment")
	}
}

func TestLexer_CommentCollection_UnterminatedBlockComment(t *testing.T) {
	input := "/* unclosed"

	l := NewLexer("test.tui", input)
	// Consume all tokens
	for {
		tok := l.Next()
		if tok.Type == TokenEOF {
			break
		}
	}

	if !l.Errors().HasErrors() {
		t.Error("expected error for unterminated block comment")
	}
}

func TestLexer_CommentCollection_BetweenTokens(t *testing.T) {
	input := "a // comment\nb"

	l := NewLexer("test.tui", input)

	// First token: identifier 'a'
	tok := l.Next()
	if tok.Type != TokenIdent || tok.Literal != "a" {
		t.Errorf("token 0: got %v %q, want Ident 'a'", tok.Type, tok.Literal)
	}

	// No comments collected yet (comment is after 'a', not before)
	comments := l.ConsumeComments()
	if len(comments) != 0 {
		t.Errorf("expected 0 comments before 'a', got %d", len(comments))
	}

	// Newline token
	tok = l.Next()
	if tok.Type != TokenNewline {
		t.Errorf("token 1: got %v, want Newline", tok.Type)
	}

	// Comment should be collected before the newline is returned
	// Actually, comments are collected during skipWhitespaceAndCollectComments
	// which happens at the START of Next(), so the comment will be collected
	// when we call Next() after 'a' (which gives us newline)
	// Let's re-check: Next() first calls skipWhitespaceAndCollectComments
	// After 'a', we're at ' ', so we skip space, then see '//' which collects comment
	// So comment IS collected before returning newline

	// Now get identifier 'b'
	tok = l.Next()
	if tok.Type != TokenIdent || tok.Literal != "b" {
		t.Errorf("token 2: got %v %q, want Ident 'b'", tok.Type, tok.Literal)
	}

	// The comment was collected before 'b'
	comments = l.ConsumeComments()
	if len(comments) != 1 {
		t.Fatalf("expected 1 comment before 'b', got %d", len(comments))
	}
	if comments[0].Text != "// comment" {
		t.Errorf("comment text = %q, want %q", comments[0].Text, "// comment")
	}
}

func TestLexer_CommentCollection_ConsumeCommentsClearsBuffer(t *testing.T) {
	input := "// comment\na"

	l := NewLexer("test.tui", input)

	// First call to Next() collects comment before returning token
	tok := l.Next() // returns newline, collects comment
	if tok.Type != TokenNewline {
		t.Errorf("expected newline, got %v", tok.Type)
	}

	// Get identifier
	tok = l.Next()
	if tok.Type != TokenIdent {
		t.Errorf("expected ident, got %v", tok.Type)
	}

	// Consume comments - should get the comment
	comments := l.ConsumeComments()
	if len(comments) != 1 {
		t.Fatalf("expected 1 comment, got %d", len(comments))
	}

	// Consume again - buffer should be cleared
	comments = l.ConsumeComments()
	if len(comments) != 0 {
		t.Errorf("expected 0 comments after clear, got %d", len(comments))
	}
}

func TestLexer_CommentCollection_MultipleComments(t *testing.T) {
	input := `// comment 1
// comment 2
a`

	l := NewLexer("test.tui", input)

	// First newline
	tok := l.Next()
	if tok.Type != TokenNewline {
		t.Errorf("token 0: got %v, want Newline", tok.Type)
	}

	// Second newline
	tok = l.Next()
	if tok.Type != TokenNewline {
		t.Errorf("token 1: got %v, want Newline", tok.Type)
	}

	// Identifier 'a'
	tok = l.Next()
	if tok.Type != TokenIdent {
		t.Errorf("token 2: got %v, want Ident", tok.Type)
	}

	// Should have collected both comments
	comments := l.ConsumeComments()
	if len(comments) != 2 {
		t.Fatalf("expected 2 comments, got %d", len(comments))
	}
	if comments[0].Text != "// comment 1" {
		t.Errorf("comment 0 text = %q, want %q", comments[0].Text, "// comment 1")
	}
	if comments[1].Text != "// comment 2" {
		t.Errorf("comment 1 text = %q, want %q", comments[1].Text, "// comment 2")
	}
}

func TestLexer_CommentCollection_BlockAndLine(t *testing.T) {
	input := `/* block */ // line
a`

	l := NewLexer("test.tui", input)

	// Newline
	tok := l.Next()
	if tok.Type != TokenNewline {
		t.Errorf("token 0: got %v, want Newline", tok.Type)
	}

	// Identifier 'a'
	tok = l.Next()
	if tok.Type != TokenIdent {
		t.Errorf("token 1: got %v, want Ident", tok.Type)
	}

	// Should have collected both comments
	comments := l.ConsumeComments()
	if len(comments) != 2 {
		t.Fatalf("expected 2 comments, got %d", len(comments))
	}
	if !comments[0].IsBlock {
		t.Error("comment 0 should be block comment")
	}
	if comments[1].IsBlock {
		t.Error("comment 1 should be line comment")
	}
}

func TestCommentGroup_Text(t *testing.T) {
	type tc struct {
		group    *CommentGroup
		expected string
	}

	tests := map[string]tc{
		"nil group": {
			group:    nil,
			expected: "",
		},
		"empty group": {
			group:    &CommentGroup{List: []*Comment{}},
			expected: "",
		},
		"single line comment": {
			group: &CommentGroup{
				List: []*Comment{
					{Text: "// hello world", IsBlock: false},
				},
			},
			expected: "hello world",
		},
		"single block comment": {
			group: &CommentGroup{
				List: []*Comment{
					{Text: "/* hello world */", IsBlock: true},
				},
			},
			expected: "hello world",
		},
		"multiple line comments": {
			group: &CommentGroup{
				List: []*Comment{
					{Text: "// line 1", IsBlock: false},
					{Text: "// line 2", IsBlock: false},
				},
			},
			expected: "line 1\nline 2",
		},
		"line comment no space after slashes": {
			group: &CommentGroup{
				List: []*Comment{
					{Text: "//nospace", IsBlock: false},
				},
			},
			expected: "nospace",
		},
		"block comment extra whitespace": {
			group: &CommentGroup{
				List: []*Comment{
					{Text: "/*   padded   */", IsBlock: true},
				},
			},
			expected: "padded",
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			result := tt.group.Text()
			if result != tt.expected {
				t.Errorf("Text() = %q, want %q", result, tt.expected)
			}
		})
	}
}

func TestLexer_CommentCollection_PositionTracking(t *testing.T) {
	input := `   // comment at column 4`

	l := NewLexer("test.tui", input)
	tok := l.Next()
	if tok.Type != TokenEOF {
		t.Errorf("expected EOF, got %v", tok.Type)
	}

	comments := l.ConsumeComments()
	if len(comments) != 1 {
		t.Fatalf("expected 1 comment, got %d", len(comments))
	}

	c := comments[0]
	if c.Position.Column != 4 {
		t.Errorf("Position.Column = %d, want 4", c.Position.Column)
	}
}

func TestLexer_SaveRestoreState_PreservesComments(t *testing.T) {
	// Verify that SaveState/RestoreState preserves pendingComments so that
	// comments encountered during a failed speculative parse don't leak.
	input := "a // comment\nb"

	l := NewLexer("test.tui", input)

	// Consume 'a', which triggers comment collection for "// comment"
	tok := l.Next()
	if tok.Type != TokenIdent || tok.Literal != "a" {
		t.Fatalf("expected ident 'a', got %v %q", tok.Type, tok.Literal)
	}

	// Save state (no pending comments yet since comment was just collected)
	saved := l.SaveState()

	// Advance past the newline — this calls Next() which may collect more comments
	tok = l.Next() // newline
	if tok.Type != TokenNewline {
		t.Fatalf("expected newline, got %v", tok.Type)
	}
	tok = l.Next() // 'b'
	if tok.Type != TokenIdent || tok.Literal != "b" {
		t.Fatalf("expected ident 'b', got %v %q", tok.Type, tok.Literal)
	}

	// At this point, the comment is in pendingComments
	preRestoreComments := l.ConsumeComments()

	// Restore and verify comments are back to the saved state (empty)
	l.RestoreState(saved)
	postRestoreComments := l.ConsumeComments()

	if len(postRestoreComments) != 0 {
		t.Errorf("after RestoreState, expected 0 pending comments, got %d (had %d before restore)",
			len(postRestoreComments), len(preRestoreComments))
	}
}

func TestLexer_SaveRestoreState_PreservesTokenPosition(t *testing.T) {
	// Verify that tokenLine/tokenColumn/tokenStartPos are saved/restored
	// so that tokens emitted after restore have correct positions.
	input := "a b c"

	l := NewLexer("test.tui", input)

	// Consume 'a'
	tok := l.Next()
	if tok.Type != TokenIdent || tok.Literal != "a" {
		t.Fatalf("expected ident 'a', got %v %q", tok.Type, tok.Literal)
	}

	saved := l.SaveState()

	// Advance past 'b' to 'c'
	l.Next()       // 'b'
	tok = l.Next() // 'c'
	if tok.Literal != "c" {
		t.Fatalf("expected 'c', got %q", tok.Literal)
	}
	cColumn := tok.Column

	// Restore and re-advance
	l.RestoreState(saved)
	l.Next()       // 'b' again
	tok = l.Next() // 'c' again
	if tok.Literal != "c" {
		t.Fatalf("after restore, expected 'c', got %q", tok.Literal)
	}
	if tok.Column != cColumn {
		t.Errorf("after restore, 'c' column = %d, want %d", tok.Column, cColumn)
	}
}

func TestLexer_ExistingCommentBehavior(t *testing.T) {
	// Verify that existing tests still pass - comments should be collected but
	// tokens should still be returned correctly

	type tc struct {
		input    string
		expected []TokenType
	}

	tests := map[string]tc{
		"line comment": {
			input:    "a // comment\nb",
			expected: []TokenType{TokenIdent, TokenNewline, TokenIdent, TokenEOF},
		},
		"block comment": {
			input:    "a /* comment */ b",
			expected: []TokenType{TokenIdent, TokenIdent, TokenEOF},
		},
		"multiline block": {
			input:    "a /* multi\nline */ b",
			expected: []TokenType{TokenIdent, TokenIdent, TokenEOF},
		},
		"comment at end": {
			input:    "a // comment",
			expected: []TokenType{TokenIdent, TokenEOF},
		},
	}

	for name, tt := range tests {
		t.Run(name, func(t *testing.T) {
			l := NewLexer("test.tui", tt.input)
			for i, expectedType := range tt.expected {
				tok := l.Next()
				if tok.Type != expectedType {
					t.Errorf("token %d: Type = %v, want %v", i, tok.Type, expectedType)
				}
			}
		})
	}
}