-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.go
927 lines (771 loc) · 22.3 KB
/
view.go
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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
// Package vimtea provides a Vim-like text editor component for terminal applications
package vimtea
import (
"fmt"
"regexp"
"strings"
"unicode/utf8"
"github.com/charmbracelet/lipgloss"
)
// Regular expression for matching ANSI escape sequences
// Used to correctly calculate visible text length with syntax highlighting
var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*[a-zA-Z]`)
// renderTab renders a tab character with visual representation using spaces
func renderTab(col int) string {
spaces := tabWidth - (col % tabWidth)
return strings.Repeat(" ", spaces)
}
// visualLength calculates the visual length of a string, counting tabs as tabWidth spaces
func visualLength(s string, startCol int) int {
length := 0
for _, r := range s {
if r == '\t' {
// Tab advances to the next tab stop
spaces := tabWidth - ((startCol + length) % tabWidth)
length += spaces
} else {
length++
}
}
return length
}
// bufferToVisualPosition converts a buffer position to a visual position
// This accounts for tabs that visually occupy multiple columns
func bufferToVisualPosition(line string, bufferCol int) int {
if bufferCol > len(line) {
bufferCol = len(line)
}
visualCol := 0
for i, r := range line {
if i >= bufferCol {
break
}
if r == '\t' {
spaces := tabWidth - (visualCol % tabWidth)
visualCol += spaces
} else {
visualCol++
}
}
return visualCol
}
// renderLineWithTabs renders a line with proper tab expansion
func renderLineWithTabs(line string) string {
var sb strings.Builder
visualCol := 0
for _, r := range line {
if r == '\t' {
spaces := tabWidth - (visualCol % tabWidth)
sb.WriteString(strings.Repeat(" ", spaces))
visualCol += spaces
} else {
sb.WriteRune(r)
visualCol++
}
}
return sb.String()
}
// View renders the editor and returns it as a string
// This is part of the bubbletea.Model interface
func (m *editorModel) View() string {
// Build components from top to bottom
components := []string{
m.renderContent(), // Main editor content
}
if m.enableStatusBar {
components = append(components, m.renderStatusLine()) // Status bar and command line
}
// Join all components vertically
return lipgloss.JoinVertical(
lipgloss.Top,
components...,
)
}
func (m *editorModel) renderContent() string {
var sb strings.Builder
var selStart, selEnd Cursor
if m.mode == ModeVisual {
selStart, selEnd = m.GetSelectionBoundary()
}
visibleContent := m.getVisibleContent()
for i, line := range visibleContent {
lineNum := i + m.viewport.YOffset + 1
rowIdx := lineNum - 1
sb.WriteString(m.renderLineNumber(lineNum, rowIdx))
if rowIdx >= m.buffer.lineCount() {
sb.WriteString("\n")
continue
}
inVisualSelection := m.mode == ModeVisual && rowIdx >= selStart.Row && rowIdx <= selEnd.Row
sb.WriteString(m.renderLine(line, rowIdx, inVisualSelection, selStart, selEnd))
sb.WriteString("\n")
}
return sb.String()
}
func (m *editorModel) renderLine(line string, rowIdx int, inVisualSelection bool, selStart, selEnd Cursor) string {
displayLine := renderLineWithTabs(line)
if m.mode == ModeVisual && m.isVisualLine && inVisualSelection {
return m.selectedStyle.Render(displayLine)
}
if m.mode != ModeVisual && m.yankHighlight.Active && m.isLineInYankHighlight(rowIdx) {
return m.renderLineWithYankHighlight(line, rowIdx)
}
var highlightedLine string
if m.highlighter != nil && m.highlighter.enabled {
highlightedLine = m.highlighter.HighlightLine(displayLine)
} else {
highlightedLine = displayLine
}
if rowIdx == m.cursor.Row {
if len(line) == 0 {
if m.cursor.Col == 0 {
return m.renderCursor(" ")
}
return ""
}
if m.cursor.Col >= len(line) {
return highlightedLine + m.renderCursor(" ")
}
if m.mode == ModeVisual && !m.isVisualLine && inVisualSelection {
return m.renderLineWithCursorInVisualSelection(line, rowIdx, selStart, selEnd)
}
if m.highlighter != nil && m.highlighter.enabled && displayLine != highlightedLine {
return m.renderSyntaxHighlightedCursorLine(highlightedLine, line)
}
return m.renderRegularCursorLine(line)
}
if m.mode == ModeVisual && !m.isVisualLine && inVisualSelection {
return m.renderLineInVisualSelection(line, rowIdx, selStart, selEnd)
}
return highlightedLine
}
func (m *editorModel) renderCursor(char string) string {
if !m.cursorBlink {
return char
}
switch m.mode {
case ModeInsert:
return lipgloss.NewStyle().Underline(true).Render(char)
case ModeCommand:
return char
default:
return m.cursorStyle.Render(char)
}
}
func (m *editorModel) renderLineNumber(lineNum int, rowIdx int) string {
if rowIdx >= m.buffer.lineCount() {
return m.lineNumberStyle.Render(" ")
}
if rowIdx == m.cursor.Row {
return m.currentLineNumberStyle.Render(fmt.Sprintf("%4d", lineNum))
}
if m.relativeNumbers {
distance := abs(rowIdx - m.cursor.Row)
return m.lineNumberStyle.Render(fmt.Sprintf("%4d", distance))
}
return m.lineNumberStyle.Render(fmt.Sprintf("%4d", lineNum))
}
func abs(n int) int {
if n < 0 {
return -n
}
return n
}
func (m *editorModel) renderRegularCursorLine(line string) string {
var sb strings.Builder
visualCol := 0
// Process characters up to the cursor position
for i, r := range line {
if i >= m.cursor.Col {
break
}
if r == '\t' {
spaces := tabWidth - (visualCol % tabWidth)
sb.WriteString(strings.Repeat(" ", spaces))
visualCol += spaces
} else {
sb.WriteRune(r)
visualCol++
}
}
// Handle cursor character
if m.cursor.Col < len(line) {
cursorRune, _ := utf8.DecodeRuneInString(line[m.cursor.Col:])
if cursorRune == '\t' {
// For tab, just highlight the first space
sb.WriteString(m.renderCursor(" "))
// Write the remaining spaces
spaces := tabWidth - 1 - (visualCol % tabWidth)
if spaces > 0 {
sb.WriteString(strings.Repeat(" ", spaces))
}
visualCol += tabWidth - (visualCol % tabWidth)
} else {
sb.WriteString(m.renderCursor(string(cursorRune)))
visualCol++
}
} else {
// Cursor at end of line
sb.WriteString(m.renderCursor(" "))
visualCol++
}
// Process remaining characters after cursor
if m.cursor.Col < len(line)-1 {
for _, r := range line[m.cursor.Col+1:] {
if r == '\t' {
spaces := tabWidth - (visualCol % tabWidth)
sb.WriteString(strings.Repeat(" ", spaces))
visualCol += spaces
} else {
sb.WriteRune(r)
visualCol++
}
}
}
return sb.String()
}
func (m *editorModel) renderSyntaxHighlightedCursorLine(highlightedLine, plainLine string) string {
// For syntax highlighting with tabs, we need to:
// 1. Render the plain line with proper tab expansion
// 2. Apply cursor highlighting at the correct position
// If the cursor is at the end, just append it
if m.cursor.Col >= len(plainLine) {
return highlightedLine + m.renderCursor(" ")
}
// Calculate the visual position of the cursor
visualCursorPos := bufferToVisualPosition(plainLine, m.cursor.Col)
// Get the character at the cursor position
var cursorChar string
if m.cursor.Col < len(plainLine) {
if plainLine[m.cursor.Col] == '\t' {
cursorChar = " " // Show first space of tab
} else {
cursorChar = string(plainLine[m.cursor.Col])
}
} else {
cursorChar = " "
}
// If we're dealing with a tab at cursor position, we need special handling
if m.cursor.Col < len(plainLine) && plainLine[m.cursor.Col] == '\t' {
return m.renderRegularCursorLine(plainLine)
}
// For non-tab characters, we can try to locate the cursor position in the highlighted line
// Find all ANSI escape sequences in the highlighted line
ansiMatches := ansiRegex.FindAllStringIndex(highlightedLine, -1)
// Match the visual position in the highlighted line
visibleIdx := 0
cursorHighlightPos := -1
for i := 0; i < len(highlightedLine); {
isAnsi := false
for _, match := range ansiMatches {
if match[0] == i {
i = match[1]
isAnsi = true
break
}
}
if isAnsi {
continue
}
if visibleIdx == visualCursorPos {
cursorHighlightPos = i
break
}
visibleIdx++
i++
}
// If we couldn't find the cursor position in the highlighted output,
// fall back to regular cursor line rendering
if cursorHighlightPos == -1 {
return m.renderRegularCursorLine(plainLine)
}
// Extract ANSI codes that should be active before the cursor
var ansiBeforeCursor string
for _, match := range ansiMatches {
if match[0] < cursorHighlightPos {
ansiBeforeCursor += highlightedLine[match[0]:match[1]]
}
}
// Build the final output with the cursor properly highlighted
var sb strings.Builder
sb.WriteString(highlightedLine[:cursorHighlightPos])
sb.WriteString("\x1b[0m") // Reset all ANSI formatting
sb.WriteString(m.renderCursor(cursorChar))
// Restore ANSI formatting for text after the cursor
sb.WriteString(ansiBeforeCursor)
if cursorHighlightPos+1 < len(highlightedLine) {
afterCursorStart := cursorHighlightPos + 1
// Skip any ANSI sequences immediately after the cursor
for _, match := range ansiMatches {
if afterCursorStart >= match[0] && afterCursorStart < match[1] {
afterCursorStart = match[1]
break
}
}
sb.WriteString(highlightedLine[afterCursorStart:])
}
return sb.String()
}
func (m *editorModel) renderLineWithCursorInVisualSelection(line string, rowIdx int, selStart, selEnd Cursor) string {
var sb strings.Builder
// Get selection boundaries in buffer coordinates
selBegin := 0
if rowIdx == selStart.Row {
selBegin = selStart.Col
}
selEndCol := len(line)
if rowIdx == selEnd.Row {
selEndCol = selEnd.Col + 1
}
// First, expand tabs to get the display line
displayLine := renderLineWithTabs(line)
// Apply syntax highlighting if enabled
var highlightedLine string
if m.highlighter != nil && m.highlighter.enabled {
highlightedLine = m.highlighter.HighlightLine(displayLine)
} else {
highlightedLine = displayLine
}
// If we're dealing with just plain text without highlighting, use the original rendering method
if highlightedLine == displayLine {
return m.renderLineWithCursorInVisualSelectionPlain(line, rowIdx, selStart, selEnd)
}
// When we have syntax highlighting, we need to modify our approach
// Extract ANSI escape sequences in the highlighted text
ansiMatches := ansiRegex.FindAllStringIndex(highlightedLine, -1)
// Calculate visual positions and create a mapping from visual position to highlighted text index
visToHighlightIndex := make(map[int]int)
visibleIdx := 0
for i := 0; i < len(highlightedLine); {
isAnsi := false
for _, match := range ansiMatches {
if match[0] == i {
i = match[1]
isAnsi = true
break
}
}
if isAnsi {
continue
}
visToHighlightIndex[visibleIdx] = i
visibleIdx++
i++
}
// Convert buffer positions to visual positions
visSelBegin := bufferToVisualPosition(line, selBegin)
visSelEnd := bufferToVisualPosition(line, selEndCol)
visCursorPos := bufferToVisualPosition(line, m.cursor.Col)
// Now render with proper selection and cursor highlighting
visPos := 0
inSelection := false
ansiStyling := "\x1b[0m" // Start with reset
// Process highlighting while preserving ANSI codes
for i := 0; i < len(highlightedLine); {
// Check if we're at an ANSI sequence
isAnsi := false
for _, match := range ansiMatches {
if match[0] == i {
ansiStyling = highlightedLine[match[0]:match[1]]
i = match[1]
isAnsi = true
break
}
}
if isAnsi {
continue
}
// Visual position transitions
if visPos == visSelBegin {
inSelection = true
}
if visPos == visSelEnd {
inSelection = false
}
// Get current character
char := string(highlightedLine[i])
// Handle cursor character with priority
if visPos == visCursorPos {
if m.cursorBlink {
sb.WriteString("\x1b[0m") // Reset all formatting
sb.WriteString(m.cursorStyle.Render(char))
sb.WriteString("\x1b[0m") // Reset again
} else {
sb.WriteString("\x1b[0m") // Reset all formatting
sb.WriteString(m.selectedStyle.Render(char))
sb.WriteString("\x1b[0m") // Reset again
}
} else if inSelection {
// In selection but not at cursor
sb.WriteString("\x1b[0m") // Reset all formatting
sb.WriteString(m.selectedStyle.Render(char))
sb.WriteString("\x1b[0m") // Reset again
} else {
// Not in selection, use syntax highlighting
sb.WriteString(ansiStyling) // Apply current styling
sb.WriteString(char)
}
visPos++
i++
// Restore ANSI styling for next character
if !inSelection && visPos != visCursorPos {
sb.WriteString(ansiStyling)
}
}
return sb.String()
}
// renderLineWithCursorInVisualSelectionPlain handles rendering a line with a cursor in visual selection
// when no syntax highlighting is applied.
func (m *editorModel) renderLineWithCursorInVisualSelectionPlain(line string, rowIdx int, selStart, selEnd Cursor) string {
var sb strings.Builder
// Get selection boundaries in buffer coordinates
selBegin := 0
if rowIdx == selStart.Row {
selBegin = selStart.Col
}
selEndCol := len(line)
if rowIdx == selEnd.Row {
selEndCol = selEnd.Col + 1
}
// Process the line with proper tab rendering
curVisualPos := 0
for i, r := range line {
// Handle character before selection start
if i < selBegin {
if r == '\t' {
spaces := tabWidth - (curVisualPos % tabWidth)
sb.WriteString(strings.Repeat(" ", spaces))
curVisualPos += spaces
} else {
sb.WriteRune(r)
curVisualPos++
}
continue
}
// Handle cursor character
if i == m.cursor.Col {
// Get appropriate character display
var cursorChar string
if r == '\t' {
cursorChar = " " // Show first space of tab
} else {
cursorChar = string(r)
}
if m.cursorBlink {
sb.WriteString(m.cursorStyle.Render(cursorChar))
} else {
sb.WriteString(m.selectedStyle.Render(cursorChar))
}
// Handle remaining spaces for tab
if r == '\t' {
spaces := tabWidth - 1 - (curVisualPos % tabWidth)
if spaces > 0 {
sb.WriteString(m.selectedStyle.Render(strings.Repeat(" ", spaces)))
}
curVisualPos += tabWidth - (curVisualPos % tabWidth)
} else {
curVisualPos++
}
continue
}
// Handle selection (non-cursor)
if i < selEndCol {
if r == '\t' {
spaces := tabWidth - (curVisualPos % tabWidth)
sb.WriteString(m.selectedStyle.Render(strings.Repeat(" ", spaces)))
curVisualPos += spaces
} else {
sb.WriteString(m.selectedStyle.Render(string(r)))
curVisualPos++
}
continue
}
// Handle character after selection end
if r == '\t' {
spaces := tabWidth - (curVisualPos % tabWidth)
sb.WriteString(strings.Repeat(" ", spaces))
curVisualPos += spaces
} else {
sb.WriteRune(r)
curVisualPos++
}
}
return sb.String()
}
func (m *editorModel) renderLineInVisualSelection(line string, rowIdx int, selStart, selEnd Cursor) string {
var sb strings.Builder
// Get selection boundaries in buffer coordinates
selBegin := 0
if rowIdx == selStart.Row {
selBegin = selStart.Col
}
selEndCol := len(line)
if rowIdx == selEnd.Row {
selEndCol = selEnd.Col + 1
}
// First, expand tabs to get the display line
displayLine := renderLineWithTabs(line)
// Apply syntax highlighting if enabled
var highlightedLine string
if m.highlighter != nil && m.highlighter.enabled {
highlightedLine = m.highlighter.HighlightLine(displayLine)
} else {
highlightedLine = displayLine
}
// If we're dealing with just plain text without highlighting, use the simplified method
if highlightedLine == displayLine {
return m.renderLineInVisualSelectionPlain(line, rowIdx, selStart, selEnd)
}
// When we have syntax highlighting, we need to modify our approach
// Extract ANSI escape sequences in the highlighted text
ansiMatches := ansiRegex.FindAllStringIndex(highlightedLine, -1)
// Calculate visual positions and create a mapping from visual position to highlighted text index
visToHighlightIndex := make(map[int]int)
visibleIdx := 0
for i := 0; i < len(highlightedLine); {
isAnsi := false
for _, match := range ansiMatches {
if match[0] == i {
i = match[1]
isAnsi = true
break
}
}
if isAnsi {
continue
}
visToHighlightIndex[visibleIdx] = i
visibleIdx++
i++
}
// Convert buffer positions to visual positions
visSelBegin := bufferToVisualPosition(line, selBegin)
visSelEnd := bufferToVisualPosition(line, selEndCol)
// Now render with proper selection highlighting
visPos := 0
inSelection := false
ansiStyling := "\x1b[0m" // Start with reset
// Process highlighting while preserving ANSI codes
for i := 0; i < len(highlightedLine); {
// Check if we're at an ANSI sequence
isAnsi := false
for _, match := range ansiMatches {
if match[0] == i {
ansiStyling = highlightedLine[match[0]:match[1]]
i = match[1]
isAnsi = true
break
}
}
if isAnsi {
continue
}
// Visual position transitions
if visPos == visSelBegin {
inSelection = true
}
if visPos == visSelEnd {
inSelection = false
}
// Get current character
char := string(highlightedLine[i])
if inSelection {
// In selection
sb.WriteString("\x1b[0m") // Reset all formatting
sb.WriteString(m.selectedStyle.Render(char))
sb.WriteString("\x1b[0m") // Reset again
} else {
// Not in selection, use syntax highlighting
sb.WriteString(ansiStyling) // Apply current styling
sb.WriteString(char)
}
visPos++
i++
// Restore ANSI styling for next character
if !inSelection {
sb.WriteString(ansiStyling)
}
}
return sb.String()
}
// renderLineInVisualSelectionPlain handles rendering a line in visual selection
// when no syntax highlighting is applied.
func (m *editorModel) renderLineInVisualSelectionPlain(line string, rowIdx int, selStart, selEnd Cursor) string {
var sb strings.Builder
// Get selection boundaries in buffer coordinates
selBegin := 0
if rowIdx == selStart.Row {
selBegin = selStart.Col
}
selEndCol := len(line)
if rowIdx == selEnd.Row {
selEndCol = selEnd.Col + 1
}
// Process the line with proper tab rendering
curVisualPos := 0
for i, r := range line {
// Handle character before selection start
if i < selBegin {
if r == '\t' {
spaces := tabWidth - (curVisualPos % tabWidth)
sb.WriteString(strings.Repeat(" ", spaces))
curVisualPos += spaces
} else {
sb.WriteRune(r)
curVisualPos++
}
continue
}
// Handle selection
if i < selEndCol {
if r == '\t' {
spaces := tabWidth - (curVisualPos % tabWidth)
sb.WriteString(m.selectedStyle.Render(strings.Repeat(" ", spaces)))
curVisualPos += spaces
} else {
sb.WriteString(m.selectedStyle.Render(string(r)))
curVisualPos++
}
continue
}
// Handle character after selection end
if r == '\t' {
spaces := tabWidth - (curVisualPos % tabWidth)
sb.WriteString(strings.Repeat(" ", spaces))
curVisualPos += spaces
} else {
sb.WriteRune(r)
curVisualPos++
}
}
return sb.String()
}
func (m editorModel) getVisibleContent() []string {
startLine := m.viewport.YOffset
endLine := startLine + m.height
if startLine < 0 {
startLine = 0
}
contentLines := []string{}
for i := startLine; i < min(endLine, m.buffer.lineCount()); i++ {
contentLines = append(contentLines, m.buffer.Line(i))
}
emptyLinesNeeded := m.height - len(contentLines)
for range emptyLinesNeeded {
contentLines = append(contentLines, "")
}
return contentLines
}
func (m *editorModel) renderStatusLine() string {
status := m.getStatusText()
cursorPos := fmt.Sprintf(" %d:%d ", m.cursor.Row+1, m.cursor.Col+1)
padding := max(m.width-lipgloss.Width(status)-lipgloss.Width(cursorPos), 0)
return m.statusStyle.Render(status + strings.Repeat(" ", padding) + cursorPos)
}
func (m *editorModel) getStatusText() string {
if m.mode == ModeCommand {
return ":" + m.commandBuffer
}
status := fmt.Sprintf(" %s", m.mode)
if len(m.keySequence) > 0 {
status += fmt.Sprintf(" | %s", strings.Join(m.keySequence, ""))
}
if m.statusMessage != "" {
status += fmt.Sprintf(" | %s", m.statusMessage)
}
return status
}
func (m *editorModel) isLineInYankHighlight(rowIdx int) bool {
return m.yankHighlight.Active &&
rowIdx >= m.yankHighlight.Start.Row && rowIdx <= m.yankHighlight.End.Row
}
func (m *editorModel) getYankHighlightBounds(rowIdx int) (int, int) {
if !m.yankHighlight.Active || !m.isLineInYankHighlight(rowIdx) {
return -1, -1
}
start := 0
end := m.buffer.lineLength(rowIdx)
if !m.yankHighlight.IsLinewise {
if rowIdx == m.yankHighlight.Start.Row {
start = m.yankHighlight.Start.Col
}
if rowIdx == m.yankHighlight.End.Row {
end = m.yankHighlight.End.Col + 1
}
}
return start, end
}
func (m *editorModel) renderLineWithYankHighlight(line string, rowIdx int) string {
var sb strings.Builder
highlightStyle := lipgloss.NewStyle().Background(lipgloss.Color("7"))
start, end := m.getYankHighlightBounds(rowIdx)
if start < 0 || end < 0 {
return renderLineWithTabs(line)
}
start = max(0, min(start, len(line)))
end = max(0, min(end, len(line)))
// Process the line with proper tab rendering
curVisualPos := 0
for i, r := range line {
// Handle character before highlight start
if i < start {
if r == '\t' {
spaces := tabWidth - (curVisualPos % tabWidth)
sb.WriteString(strings.Repeat(" ", spaces))
curVisualPos += spaces
} else {
sb.WriteRune(r)
curVisualPos++
}
continue
}
// Handle cursor character within highlight
if i == m.cursor.Col && rowIdx == m.cursor.Row && i >= start && i < end {
// Get appropriate character display
var cursorChar string
if r == '\t' {
cursorChar = " " // Show first space of tab
} else {
cursorChar = string(r)
}
if m.cursorBlink {
sb.WriteString(m.cursorStyle.Render(cursorChar))
} else {
sb.WriteString(highlightStyle.Render(cursorChar))
}
// Handle remaining spaces for tab
if r == '\t' {
spaces := tabWidth - 1 - (curVisualPos % tabWidth)
if spaces > 0 {
sb.WriteString(highlightStyle.Render(strings.Repeat(" ", spaces)))
}
curVisualPos += tabWidth - (curVisualPos % tabWidth)
} else {
curVisualPos++
}
continue
}
// Handle highlighted character (non-cursor)
if i < end {
if r == '\t' {
spaces := tabWidth - (curVisualPos % tabWidth)
sb.WriteString(highlightStyle.Render(strings.Repeat(" ", spaces)))
curVisualPos += spaces
} else {
sb.WriteString(highlightStyle.Render(string(r)))
curVisualPos++
}
continue
}
// Handle character after highlight end
if r == '\t' {
spaces := tabWidth - (curVisualPos % tabWidth)
sb.WriteString(strings.Repeat(" ", spaces))
curVisualPos += spaces
} else {
sb.WriteRune(r)
curVisualPos++
}
}
return sb.String()
}