-
Notifications
You must be signed in to change notification settings - Fork 0
/
buffer.go
202 lines (175 loc) · 4.86 KB
/
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
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
package buffer
import (
"io"
"os"
"path"
"github.com/Ardelean-Calin/moe/pkg/common"
"github.com/Ardelean-Calin/moe/pkg/gapbuffer"
"github.com/Ardelean-Calin/moe/ui/components/cursor"
)
// Buffer represents an opened file.
type Buffer struct {
parentNode *BufferNode
Path string // Absolute path on disk.
fd *os.File // File descriptor.
Val *gapbuffer.GapBuffer[rune] // Actual raw text data. Gap Buffer is a nice compromise between Piece Chain and buffer.
Lines *gapbuffer.GapBuffer[int] // The line numbers are also stored in a Gap Buffer
modified bool // Content was modified and not saved to disk
CursorRow, CursorCol int // ???
Cursor cursor.Model // Cursor position inside this buffer.
}
// NewBuffer constructs a new buffer from a path. If that file exists, it opens it for reading,
// otherwise it will just open a fake file in memory
func NewBuffer(path string) (*Buffer, error) {
var bytes []byte
fd, err := os.OpenFile(path, os.O_RDWR, 0664) // taken from helix
if err == nil {
// File exists
bytes, err = io.ReadAll(fd)
if err != nil {
// Some weird error happened. Display it.
return nil, err
}
}
// Ok by this point I either have a fd with some bytes or a nil fd and nil bytes
// Create a gap buffer with the contents of the file
content := []rune(string(bytes))
buf := gapbuffer.NewGapBuffer(content)
// And create a gap buffer with all the newline indices. This way I can simply
// index the line as line[n] and get the index inside the gap buffer where the n-th line
// starts.
indices := []int{-1}
indices = append(indices, buf.FindAll('\n')...)
// Increment the indices so that they point to the line starts
for i := range indices {
indices[i]++
}
lineBuf := gapbuffer.NewGapBuffer(indices)
return &Buffer{
parentNode: nil,
Path: path,
fd: fd,
Val: &buf,
Lines: &lineBuf,
modified: false,
Cursor: cursor.New(),
}, nil
}
// String returns the string contained in this buffer
func (b *Buffer) String() string {
content := b.Val
if content == nil {
return ""
}
return string(b.Val.Collect())
}
// Name returns the title of the buffer window to display
func (b Buffer) Name() string {
_, name := path.Split(b.Path)
return name
}
// CursorRight moves the cursor one position to the right
func (b *Buffer) CursorRight() {
// lineLength := b.Lines[b.Cursor.Col+1] - b.Lines[b.Cursor.Col]
// pos := min(b.Cursor.Col+1, b.val.Len())
// char := b.val.ElementAt(pos)
// b.Cursor.Char = string(char)
// b.Cursor.Pos = pos
}
func (b *Buffer) CursorLeft() {
// pos := max(b.Cursor.Pos-1, 0)
// char := b.val.ElementAt(pos)
// b.Cursor.Char = string(char)
// b.Cursor.Pos = pos
}
// The bufferline is composed of a linked-list
type BufferNode struct {
Prev *BufferNode
Next *BufferNode
Buffer *Buffer
}
// InsertNode inserts node `n` before node `src`
func InsertNode(src *BufferNode, n *BufferNode) {
n.Prev = src.Prev
n.Next = src
src.Prev.Next = n
src.Prev = n
}
// ReplaceNode replaces node `old` with `new` in the Linked List
func ReplaceNode(old *BufferNode, new *BufferNode) {
old.Prev.Next = new
old.Next.Prev = new
new.Next = old.Next
new.Prev = old.Prev
}
// Node takes a *buffer and returns a *BufferNode
func Node(buf *Buffer) *BufferNode {
node := BufferNode{
Prev: nil,
Next: nil,
Buffer: buf,
}
buf.parentNode = &node
return &node
}
type LinkedList struct {
head *BufferNode
tail *BufferNode
}
func NewList() LinkedList {
head := &BufferNode{
Prev: nil,
Next: nil,
Buffer: nil,
}
tail := &BufferNode{
Prev: nil,
Next: nil,
Buffer: nil,
}
head.Next = tail
tail.Prev = head
return LinkedList{
head: head,
tail: tail,
}
}
func (l *LinkedList) AddNode(n *BufferNode) {
n.Prev = l.tail.Prev
n.Next = l.tail
l.tail.Prev.Next = n
l.tail.Prev = n
}
// NodeIterator implements the Iterator interface for LinkedList
type NodeIterator struct {
n *BufferNode
}
// HasNext tells us wether the iterator still has elements to consume
func (i *NodeIterator) HasNext() bool {
next := i.n.Next
// Only tail nodes have next equal to nil
return next.Next != nil
}
// Next gets the next element in this iterator
func (i *NodeIterator) Next() *BufferNode {
if i.HasNext() {
node := i.n.Next
i.n = node
return i.n
}
return nil
}
// Creates an iterator over the LinkedList elements
func (l *LinkedList) Iter() common.Iterator[BufferNode] {
return &NodeIterator{n: l.head}
}
/* Use this when GOEXPERIMENT=rangefunc is merged */
// func (l *LinkedList) Iter() func(func(*BufferNode) bool) {
// return func(yield func(*BufferNode) bool) {
// for node := l.head.Next; node.Next != nil; node = node.Next {
// if !yield(node) {
// return
// }
// }
// }
// }