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
|
package editor
import (
"testing"
"glint/internal/theme"
tea "github.com/charmbracelet/bubbletea"
)
func newEditorWith(lines ...string) *Editor {
e := New()
e.Lines = append([]string{}, lines...)
e.SetSize(80, 10)
return e
}
func TestSetContentSplitsLines(t *testing.T) {
e := New()
e.SetContent([]byte("a\nb\nc"))
if len(e.Lines) != 3 || e.Lines[1] != "b" {
t.Fatalf("Lines = %v", e.Lines)
}
if e.Dirty {
t.Error("SetContent should clear Dirty")
}
}
func TestBytesRoundTrip(t *testing.T) {
e := New()
e.SetContent([]byte("x\ny"))
if string(e.Bytes()) != "x\ny" {
t.Errorf("Bytes = %q", string(e.Bytes()))
}
}
func TestInsertRune(t *testing.T) {
e := newEditorWith("ac")
e.Cursor = Position{Row: 0, Col: 1}
e.InsertRune('b')
if e.Lines[0] != "abc" {
t.Errorf("Lines[0] = %q, want abc", e.Lines[0])
}
if e.Cursor.Col != 2 {
t.Errorf("Cursor.Col = %d, want 2", e.Cursor.Col)
}
if !e.Dirty {
t.Error("insert should set Dirty")
}
}
func TestInsertNewlineSplitsLine(t *testing.T) {
e := newEditorWith("abcd")
e.Cursor = Position{Row: 0, Col: 2}
e.InsertNewline()
if len(e.Lines) != 2 || e.Lines[0] != "ab" || e.Lines[1] != "cd" {
t.Fatalf("Lines = %v", e.Lines)
}
if e.Cursor.Row != 1 || e.Cursor.Col != 0 {
t.Errorf("Cursor = %+v, want {1 0}", e.Cursor)
}
}
func TestBackspaceJoinsLines(t *testing.T) {
e := newEditorWith("ab", "cd")
e.Cursor = Position{Row: 1, Col: 0}
e.Backspace()
if len(e.Lines) != 1 || e.Lines[0] != "abcd" {
t.Fatalf("Lines = %v", e.Lines)
}
if e.Cursor.Row != 0 || e.Cursor.Col != 2 {
t.Errorf("Cursor = %+v, want {0 2}", e.Cursor)
}
}
func TestBackspaceWithinLine(t *testing.T) {
e := newEditorWith("abc")
e.Cursor = Position{Row: 0, Col: 2}
e.Backspace()
if e.Lines[0] != "ac" || e.Cursor.Col != 1 {
t.Errorf("Lines[0]=%q Cursor.Col=%d", e.Lines[0], e.Cursor.Col)
}
}
func TestMovementClampsAndCrossesLines(t *testing.T) {
e := newEditorWith("ab", "cde")
e.Cursor = Position{Row: 0, Col: 2} // end of "ab"
e.MoveRight() // wraps to start of next line
if e.Cursor != (Position{Row: 1, Col: 0}) {
t.Errorf("after MoveRight: %+v", e.Cursor)
}
e.MoveLeft() // back to end of "ab"
if e.Cursor != (Position{Row: 0, Col: 2}) {
t.Errorf("after MoveLeft: %+v", e.Cursor)
}
e.MoveEnd()
e.MoveDown() // col clamps to len("cde")=3
if e.Cursor != (Position{Row: 1, Col: 2}) {
t.Errorf("after MoveDown: %+v, want {1 2}", e.Cursor)
}
}
func TestScrollFollowsCursorDown(t *testing.T) {
e := New()
e.SetSize(80, 3) // 3 visible rows
for i := 0; i < 10; i++ {
e.Lines = append(e.Lines, "x")
}
e.Cursor = Position{Row: 0, Col: 0}
for i := 0; i < 9; i++ {
e.MoveDown()
}
// cursor at row 9 must be visible within [Scroll, Scroll+3)
if e.Cursor.Row < e.Scroll || e.Cursor.Row >= e.Scroll+e.Height {
t.Errorf("cursor row %d not in viewport [%d,%d)", e.Cursor.Row, e.Scroll, e.Scroll+e.Height)
}
}
func TestHandleKeyInsertsRunes(t *testing.T) {
e := newEditorWith("")
e.HandleKey(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune{'h', 'i'}})
if e.Lines[0] != "hi" {
t.Errorf("Lines[0] = %q, want hi", e.Lines[0])
}
}
func TestViewRendersVisibleRowsOnly(t *testing.T) {
e := New()
e.SetSize(80, 2)
e.Lines = []string{"one", "two", "three"}
e.Cursor = Position{Row: 0, Col: 0}
view := e.View()
// 2 visible rows -> exactly 2 newline-terminated lines
if got := countByte(view, '\n'); got != 2 {
t.Errorf("view has %d newlines, want 2", got)
}
}
func countByte(s string, b byte) int {
n := 0
for i := 0; i < len(s); i++ {
if s[i] == b {
n++
}
}
return n
}
func TestHandleKeySpaceInsertsSingleSpace(t *testing.T) {
e := newEditorWith("ab")
e.Cursor = Position{Row: 0, Col: 1}
e.HandleKey(tea.KeyMsg{Type: tea.KeySpace, Runes: []rune{' '}})
if e.Lines[0] != "a b" {
t.Errorf("Lines[0] = %q, want 'a b'", e.Lines[0])
}
}
func TestDeleteWithinLine(t *testing.T) {
e := newEditorWith("abc")
e.Cursor = Position{Row: 0, Col: 1}
e.Delete()
if e.Lines[0] != "ac" {
t.Errorf("Lines[0] = %q, want 'ac'", e.Lines[0])
}
if e.Cursor.Col != 1 {
t.Errorf("Cursor.Col = %d, want 1", e.Cursor.Col)
}
if !e.Dirty {
t.Error("Delete should set Dirty")
}
}
func TestDeleteAtEndOfLastLineIsNoop(t *testing.T) {
e := newEditorWith("ab")
e.Cursor = Position{Row: 0, Col: 2}
original := e.Lines[0]
e.Delete()
if e.Lines[0] != original {
t.Errorf("Lines[0] = %q, want %q", e.Lines[0], original)
}
if len(e.Lines) != 1 {
t.Errorf("len(Lines) = %d, want 1", len(e.Lines))
}
}
func TestDeleteJoinsNextLine(t *testing.T) {
e := newEditorWith("ab", "cd")
e.Cursor = Position{Row: 0, Col: 2}
e.Delete()
if e.Lines[0] != "abcd" {
t.Errorf("Lines[0] = %q, want 'abcd'", e.Lines[0])
}
if len(e.Lines) != 1 {
t.Errorf("len(Lines) = %d, want 1", len(e.Lines))
}
}
func TestBackspaceAtOriginIsNoop(t *testing.T) {
e := newEditorWith("ab")
e.Cursor = Position{Row: 0, Col: 0}
original := e.Lines[0]
e.Backspace()
if e.Lines[0] != original {
t.Errorf("Lines[0] = %q, want %q", e.Lines[0], original)
}
if e.Cursor != (Position{Row: 0, Col: 0}) {
t.Errorf("Cursor = %+v, want {0 0}", e.Cursor)
}
}
func TestMoveHomeAndEnd(t *testing.T) {
e := newEditorWith("hello")
e.Cursor = Position{Row: 0, Col: 3}
e.MoveHome()
if e.Cursor.Col != 0 {
t.Errorf("after MoveHome: Cursor.Col = %d, want 0", e.Cursor.Col)
}
e.MoveEnd()
if e.Cursor.Col != 5 {
t.Errorf("after MoveEnd: Cursor.Col = %d, want 5", e.Cursor.Col)
}
}
func TestVisualDownAndUpAcrossWrappedLine(t *testing.T) {
e := New()
e.Lines = []string{"aaaa bbbb cccc"} // wraps at width 9 into ["aaaa ","bbbb cccc"]
e.SetSize(9, 10)
e.Cursor = Position{Row: 0, Col: 0}
e.setGoal() // ops normally set the goal; set it directly here
e.MoveDown() // to the second visual row, goal column 0
if e.Cursor != (Position{Row: 0, Col: 5}) {
t.Errorf("after MoveDown: %+v, want {0 5}", e.Cursor)
}
e.MoveUp() // back to the first visual row, goal column 0
if e.Cursor != (Position{Row: 0, Col: 0}) {
t.Errorf("after MoveUp: %+v, want {0 0}", e.Cursor)
}
}
func TestVisualDownKeepsGoalColumn(t *testing.T) {
e := New()
e.Lines = []string{"aaaa bbbb cccc"}
e.SetSize(9, 10)
e.Cursor = Position{Row: 0, Col: 2} // visual column 2 in segment 0
e.setGoal() // ops normally set the goal; set it directly here
e.MoveDown() // segment 1 starts at col 5 → col 5+2 = 7
if e.Cursor != (Position{Row: 0, Col: 7}) {
t.Errorf("after MoveDown with goal: %+v, want {0 7}", e.Cursor)
}
}
func TestBytesRoundTripUnaffectedByWrap(t *testing.T) {
e := New()
e.SetContent([]byte("a very long single logical line that will wrap many times\nsecond"))
e.SetSize(10, 5)
if string(e.Bytes()) != "a very long single logical line that will wrap many times\nsecond" {
t.Errorf("wrap altered the buffer: %q", string(e.Bytes()))
}
}
func TestScrollIsVisualRowIndex(t *testing.T) {
e := New()
e.Lines = []string{"aaaa bbbb cccc dddd eeee ffff"} // one logical line, many visual rows at width 9
e.SetSize(9, 2) // only 2 visible visual rows
e.Cursor = Position{Row: 0, Col: 0}
// Move down several visual rows; Scroll must follow so the cursor stays visible.
for i := 0; i < 3; i++ {
e.MoveDown()
}
rows := e.buildVisual()
ci := cursorVIndex(rows, e.Cursor)
if ci < e.Scroll || ci >= e.Scroll+e.Height {
t.Errorf("cursor visual row %d not in viewport [%d,%d)", ci, e.Scroll, e.Scroll+e.Height)
}
}
func TestBuildVisualSpansCarryThemeBackground(t *testing.T) {
e := New()
th := theme.FlexokiDark()
e.SetTheme(th)
e.Lines = []string{"# Heading **bold**", "plain text", ""}
for _, vr := range e.buildVisual() {
for _, sp := range vr.spans {
if sp.Style.GetBackground() != th.Background {
t.Errorf("span %q background = %v, want theme background %v",
sp.Text, sp.Style.GetBackground(), th.Background)
}
}
}
}
func TestKillToLineStart(t *testing.T) {
e := newEditorWith("hello world")
e.Cursor = Position{Row: 0, Col: 6}
e.KillToLineStart()
if e.Lines[0] != "world" {
t.Errorf("Lines[0] = %q, want 'world'", e.Lines[0])
}
if e.Cursor.Col != 0 {
t.Errorf("Cursor.Col = %d, want 0", e.Cursor.Col)
}
if !e.Dirty {
t.Error("kill should set Dirty")
}
}
func TestKillToLineEnd(t *testing.T) {
e := newEditorWith("hello world")
e.Cursor = Position{Row: 0, Col: 5}
e.KillToLineEnd()
if e.Lines[0] != "hello" {
t.Errorf("Lines[0] = %q, want 'hello'", e.Lines[0])
}
if !e.Dirty {
t.Error("kill should set Dirty")
}
}
func TestKillToLineStartAtColZeroNoop(t *testing.T) {
e := newEditorWith("abc")
e.Cursor = Position{Row: 0, Col: 0}
e.KillToLineStart()
if e.Lines[0] != "abc" || e.Dirty {
t.Errorf("kill at col 0 should be a noop; line=%q dirty=%v", e.Lines[0], e.Dirty)
}
}
func TestVisualMoveDownDoesNotSkipWrapBoundary(t *testing.T) {
e := New()
e.Lines = []string{"zzzzz", "aaaa bbbb"} // line1 wraps at width 5 into ["aaaa ","bbbb"]
e.SetSize(5, 10)
e.Cursor = Position{Row: 0, Col: 5} // end of line0
e.setGoal() // goalCol = 5
e.MoveDown()
rows := e.buildVisual()
// rows: [0]=line0, [1]=line1 "aaaa ", [2]=line1 "bbbb".
// MoveDown must land on visual row 1 (first segment of line1), not skip to 2.
if ci := cursorVIndex(rows, e.Cursor); ci != 1 {
t.Errorf("MoveDown landed on visual row %d, want 1 (did not stay on the wrap-boundary row)", ci)
}
if e.Cursor.Row != 1 {
t.Errorf("Cursor.Row = %d, want 1", e.Cursor.Row)
}
}
func TestScrollByClamps(t *testing.T) {
e := New()
for i := 0; i < 20; i++ {
e.Lines = append(e.Lines, "x")
}
e.SetSize(80, 5)
e.Cursor = Position{Row: 0, Col: 0}
e.ScrollBy(-5) // can't go below 0
if e.Scroll != 0 {
t.Errorf("Scroll = %d, want 0", e.Scroll)
}
e.ScrollBy(100) // clamps to max
max := len(e.buildVisual()) - e.Height
if e.Scroll != max {
t.Errorf("Scroll = %d, want %d (clamped)", e.Scroll, max)
}
}
func TestWordNavigationAndDelete(t *testing.T) {
e := newEditorWith("hello world foo")
e.Cursor = Position{Row: 0, Col: 15} // end
e.MoveWordLeft()
if e.Cursor.Col != 12 { // start of "foo"
t.Errorf("MoveWordLeft → col %d, want 12", e.Cursor.Col)
}
e.MoveWordLeft()
if e.Cursor.Col != 6 { // start of "world"
t.Errorf("MoveWordLeft → col %d, want 6", e.Cursor.Col)
}
e.MoveWordRight()
if e.Cursor.Col != 11 { // end of "world"
t.Errorf("MoveWordRight → col %d, want 11", e.Cursor.Col)
}
// delete the word before the cursor ("world")
e.DeleteWordLeft()
if e.Lines[0] != "hello foo" {
t.Errorf("DeleteWordLeft → %q, want 'hello foo'", e.Lines[0])
}
}
func TestHandleKeyAltWord(t *testing.T) {
e := newEditorWith("alpha beta")
e.Cursor = Position{Row: 0, Col: 10}
e.HandleKey(tea.KeyMsg{Type: tea.KeyLeft, Alt: true})
if e.Cursor.Col != 6 {
t.Errorf("Alt+Left → col %d, want 6 (start of beta)", e.Cursor.Col)
}
e.Cursor.Col = 10 // back to end
e.HandleKey(tea.KeyMsg{Type: tea.KeyBackspace, Alt: true})
if e.Lines[0] != "alpha " {
t.Errorf("Alt+Backspace at end → %q, want 'alpha '", e.Lines[0])
}
}
func TestHandleKeyAltBFAreWordMotionNotText(t *testing.T) {
e := newEditorWith("alpha beta gamma")
e.Cursor = Position{Row: 0, Col: 16}
e.HandleKey(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("b"), Alt: true})
if e.Cursor.Col != 11 {
t.Errorf("Alt+b → col %d, want 11 (start of gamma)", e.Cursor.Col)
}
if e.Lines[0] != "alpha beta gamma" {
t.Errorf("Alt+b inserted text: %q", e.Lines[0])
}
e.HandleKey(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("f"), Alt: true})
if e.Cursor.Col != 16 {
t.Errorf("Alt+f → col %d, want 16 (end of gamma)", e.Cursor.Col)
}
}
func TestDocStartEnd(t *testing.T) {
e := newEditorWith("first", "middle", "last line")
e.Cursor = Position{Row: 1, Col: 2}
e.HandleKey(tea.KeyMsg{Type: tea.KeyEnd})
if e.Cursor != (Position{Row: 2, Col: 9}) {
t.Errorf("End → %+v, want {2 9}", e.Cursor)
}
e.HandleKey(tea.KeyMsg{Type: tea.KeyHome})
if e.Cursor != (Position{Row: 0, Col: 0}) {
t.Errorf("Home → %+v, want {0 0}", e.Cursor)
}
}
func TestLineStartEndViaCtrlUpDown(t *testing.T) {
e := newEditorWith(" indented line")
e.Cursor = Position{Row: 0, Col: 8}
e.HandleKey(tea.KeyMsg{Type: tea.KeyCtrlDown})
if e.Cursor.Col != len(" indented line") {
t.Errorf("Ctrl+Down → col %d, want %d (line end)", e.Cursor.Col, len(" indented line"))
}
e.HandleKey(tea.KeyMsg{Type: tea.KeyCtrlUp})
if e.Cursor.Col != 2 {
t.Errorf("Ctrl+Up → col %d, want 2 (first non-blank)", e.Cursor.Col)
}
e.HandleKey(tea.KeyMsg{Type: tea.KeyCtrlUp})
if e.Cursor.Col != 0 {
t.Errorf("Ctrl+Up again → col %d, want 0 (toggle to column 0)", e.Cursor.Col)
}
}
func TestShiftSelectAndReplace(t *testing.T) {
e := newEditorWith("hello world")
e.Cursor = Position{Row: 0, Col: 0}
for i := 0; i < 5; i++ {
e.HandleKey(tea.KeyMsg{Type: tea.KeyShiftRight})
}
if !e.HasSelection() || e.SelectedText() != "hello" {
t.Fatalf("selection = %q, want hello", e.SelectedText())
}
e.HandleKey(tea.KeyMsg{Type: tea.KeyRunes, Runes: []rune("X")})
if e.Lines[0] != "X world" {
t.Errorf("typing over selection → %q, want 'X world'", e.Lines[0])
}
if e.HasSelection() {
t.Error("selection should clear after replace")
}
}
func TestBackspaceDeletesSelection(t *testing.T) {
e := newEditorWith("abcdef")
e.Cursor = Position{Row: 0, Col: 2}
for i := 0; i < 3; i++ {
e.HandleKey(tea.KeyMsg{Type: tea.KeyShiftRight}) // select "cde"
}
e.HandleKey(tea.KeyMsg{Type: tea.KeyBackspace})
if e.Lines[0] != "abf" {
t.Errorf("backspace over selection → %q, want abf", e.Lines[0])
}
}
func TestMultilineSelectedTextAndDelete(t *testing.T) {
e := newEditorWith("abc", "def", "ghi")
e.anchor = &Position{Row: 0, Col: 1}
e.Cursor = Position{Row: 2, Col: 2}
if got := e.SelectedText(); got != "bc\ndef\ngh" {
t.Errorf("SelectedText = %q, want 'bc\\ndef\\ngh'", got)
}
e.DeleteSelection()
if len(e.Lines) != 1 || e.Lines[0] != "ai" {
t.Errorf("after delete: %v, want [ai]", e.Lines)
}
}
func TestPlainMoveClearsSelection(t *testing.T) {
e := newEditorWith("hello")
e.Cursor = Position{Row: 0, Col: 0}
e.HandleKey(tea.KeyMsg{Type: tea.KeyShiftRight})
e.HandleKey(tea.KeyMsg{Type: tea.KeyRight}) // plain move collapses
if e.HasSelection() {
t.Error("plain move should clear selection")
}
}
|