-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbuffer.go
More file actions
226 lines (195 loc) · 3.75 KB
/
buffer.go
File metadata and controls
226 lines (195 loc) · 3.75 KB
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
package wig
import (
"bufio"
"bytes"
"os"
"strings"
)
type Mode int
const (
MODE_NORMAL Mode = 0
MODE_INSERT Mode = 1
MODE_VISUAL Mode = 2
MODE_VISUAL_LINE Mode = 3
)
func (m Mode) String() string {
if m == MODE_NORMAL {
return "NOR"
}
if m == MODE_INSERT {
return "INS"
}
if m == MODE_VISUAL_LINE {
return "VIS LINE"
}
return "VIS"
}
// Driver represents anything that can run selected text. it can be sql connection,
// or rest client.
type Driver interface {
// Execute thing under cursor: line or seleciton
Exec(*Editor, *Buffer, *Element[Line])
// Execute whole buffer
ExecBuffer()
}
type Buffer struct {
mode Mode
FilePath string
Lines List[Line]
Selection *Selection
Driver Driver
IndentCh []rune
Tx *Transaction
UndoRedo *UndoRedo
Highlighter Highlighter
KeyHandler *KeyHandler
rootDir string
}
func NewBuffer() *Buffer {
lines := List[Line]{}
lines.PushBack([]rune("\n"))
b := &Buffer{
Lines: lines,
IndentCh: []rune{'\t'},
Selection: nil,
Driver: nil,
Tx: nil,
Highlighter: nil,
}
b.UndoRedo = NewUndoRedo(b)
return b
}
func BufferReadFile(path string) (*Buffer, error) {
file, err := os.OpenFile(path, os.O_RDONLY, 0644)
if err != nil {
return nil, err
}
defer file.Close()
buf := NewBuffer()
buf.FilePath = path
buf.Selection = nil
buf.ResetLines()
newLine := "\n"
sc := bufio.NewScanner(file)
i := 0
for sc.Scan() {
buf.Lines.PushBack([]rune(string(sc.Bytes()) + newLine))
i++
}
if i == 0 {
buf.Lines.PushBack([]rune(newLine))
}
return buf, nil
}
func BufferReloadFile(buf *Buffer) error {
file, err := os.OpenFile(buf.FilePath, os.O_RDONLY, 0644)
if err != nil {
return err
}
defer file.Close()
buf.Selection = nil
buf.Lines = List[Line]{}
newLine := "\n"
sc := bufio.NewScanner(file)
for sc.Scan() {
buf.Lines.PushBack([]rune(string(sc.Bytes()) + newLine))
}
return nil
}
func (buf *Buffer) SetMode(m Mode) {
buf.mode = m
}
func (buf *Buffer) GetRootRir() string {
if buf.rootDir == "" {
buf.rootDir, _ = EditorInst.Projects.FindRoot(buf)
}
return buf.rootDir
}
func (b *Buffer) TxStart() (started bool) {
if b.Tx != nil {
return
}
b.Tx = NewTx(b)
b.Tx.Start()
return true
}
func (b *Buffer) TxEnd() {
if b.Tx == nil {
return
}
b.Tx.End()
b.Tx = nil
}
func (b *Buffer) GetName() string {
if len(b.FilePath) > 0 {
return strings.Replace(b.FilePath, b.GetRootRir()+"/", "", 1)
}
return "[No Name]"
}
func (b *Buffer) Mode() Mode {
return b.mode
}
func (b *Buffer) Save() error {
f, err := os.Create(b.FilePath)
if err != nil {
return err
}
line := b.Lines.First()
for line != nil {
// temp check
{
count := 0
for _, c := range line.Value {
if c == '\n' {
count++
}
}
if count != 1 {
EditorInst.LogMessage("wrong number of new lines")
buf := EditorInst.BufferFindByFilePath("[Messages]", true)
EditorInst.EnsureBufferIsVisible(buf)
}
}
_, err := f.WriteString(string(line.Value))
if err != nil {
return err
}
line = line.Next()
}
return nil
}
func (b *Buffer) Append(s string) {
// TODO: rewrite. use TextInsert as this messes up lsp and treesitter
for line := range strings.SplitSeq(s, "\n") {
b.Lines.PushBack([]rune(line + "\n"))
}
}
// Remove all lines
func (b *Buffer) ResetLines() {
l := b.Lines.First()
for l != nil {
next := l.Next()
l.Value = nil
b.Lines.Remove(l)
l = next
}
b.Lines = List[Line]{}
}
func (b *Buffer) CountLines() int {
i := 0
l := b.Lines.First()
for l != nil {
i++
l = l.Next()
}
return i
}
func (b *Buffer) String() string {
buf := bytes.NewBuffer(nil)
line := b.Lines.First()
for line != nil {
buf.WriteString(string(line.Value))
line = line.Next()
}
return buf.String()
}