-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapped_buffer.go
79 lines (65 loc) · 2.41 KB
/
wrapped_buffer.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
// Package vimtea provides a Vim-like text editor component for terminal applications
package vimtea
import tea "github.com/charmbracelet/bubbletea"
// wrappedBuffer adapts the internal buffer implementation to the Buffer interface
// This wrapping pattern allows the editor model to expose a buffer interface
// without exposing its internal state directly
type wrappedBuffer struct {
m *editorModel // Reference to the parent editor model
}
// Text returns the entire buffer content as a string
func (w *wrappedBuffer) Text() string {
return w.m.buffer.text()
}
// Lines returns all lines in the buffer as a string slice
func (w *wrappedBuffer) Lines() []string {
return w.m.buffer.lines
}
// LineCount returns the number of lines in the buffer
func (w *wrappedBuffer) LineCount() int {
return w.m.buffer.lineCount()
}
// LineLength returns the length of the line at the given row
func (w *wrappedBuffer) LineLength(row int) int {
return w.m.buffer.lineLength(row)
}
// VisualLineLength returns the visual length of the line at the given row
// This accounts for tabs which visually occupy multiple spaces
func (w *wrappedBuffer) VisualLineLength(row int) int {
return w.m.buffer.visualLineLength(row)
}
// InsertAt inserts text at the specified position
func (w *wrappedBuffer) InsertAt(row int, col int, text string) {
w.m.buffer.saveUndoState(w.m.cursor)
w.m.buffer.insertAt(row, col, text)
}
// DeleteAt deletes text between the specified positions
func (w *wrappedBuffer) DeleteAt(startRow int, startCol int, endRow int, endCol int) {
w.m.buffer.saveUndoState(w.m.cursor)
w.m.buffer.deleteAt(startRow, startCol, endRow, endCol)
}
// Undo reverts the last change and returns a command with the new cursor position
func (w *wrappedBuffer) Undo() tea.Cmd {
return w.m.buffer.undo(w.m.cursor)
}
// Redo reapplies a previously undone change
func (w *wrappedBuffer) Redo() tea.Cmd {
return w.m.buffer.redo(w.m.cursor)
}
// CanUndo returns whether there are changes that can be undone
func (w *wrappedBuffer) CanUndo() bool {
return w.m.buffer.canUndo()
}
// CanRedo returns whether there are changes that can be redone
func (w *wrappedBuffer) CanRedo() bool {
return w.m.buffer.canRedo()
}
// Clear removes all content from the buffer and resets to empty state
func (w *wrappedBuffer) Clear() tea.Cmd {
w.m.buffer.saveUndoState(w.m.cursor)
w.m.buffer.clear()
w.m.cursor = newCursor(0, 0)
return func() tea.Msg {
return nil
}
}