-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
250 lines (217 loc) · 5.23 KB
/
action.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
package main
import (
"bytes"
)
//----------------------------------------------------------------------------
// action
//
// A single entity of undo/redo history. All changes to contents of a buffer
// must be initiated by an action.
//----------------------------------------------------------------------------
type action_type int
const (
action_insert action_type = 1
action_delete action_type = -1
)
type action struct {
what action_type
data []byte
cursor cursor_location
lines []*line
}
func (a *action) apply(v *view) {
a.do(v, a.what)
}
func (a *action) revert(v *view) {
a.do(v, -a.what)
}
func (a *action) insert_line(line, prev *line, v *view) {
bi := prev
ai := prev.next
// 'bi' is always a non-nil line
bi.next = line
line.prev = bi
// 'ai' could be nil (means we're inserting a new last line)
if ai == nil {
v.buf.last_line = line
} else {
ai.prev = line
}
line.next = ai
}
func (a *action) delete_line(line *line, v *view) {
bi := line.prev
ai := line.next
if ai != nil {
ai.prev = bi
} else {
v.buf.last_line = bi
}
if bi != nil {
bi.next = ai
} else {
v.buf.first_line = ai
}
line.data = line.data[:0]
}
func (a *action) insert(v *view) {
var data_chunk []byte
nline := 0
offset := a.cursor.boffset
line := a.cursor.line
iter_lines(a.data, func(data []byte) {
if data[0] == '\n' {
v.buf.bytes_n++
v.buf.lines_n++
if offset < len(line.data) {
// a case where we insert at the middle of the
// line, need to save that chunk for later
// insertion at the end of the operation
data_chunk = line.data[offset:]
line.data = line.data[:offset]
}
// insert a line
a.insert_line(a.lines[nline], line, v)
line = a.lines[nline]
nline++
offset = 0
} else {
v.buf.bytes_n += len(data)
// insert a chunk of data
line.data = insert_bytes(line.data, offset, data)
offset += len(data)
}
})
if data_chunk != nil {
line.data = append(line.data, data_chunk...)
}
}
func (a *action) delete(v *view) {
nline := 0
offset := a.cursor.boffset
line := a.cursor.line
iter_lines(a.data, func(data []byte) {
if data[0] == '\n' {
v.buf.bytes_n--
v.buf.lines_n--
// append the contents of the deleted line the current line
line.data = append(line.data, a.lines[nline].data...)
// delete a line
a.delete_line(a.lines[nline], v)
nline++
} else {
v.buf.bytes_n -= len(data)
// delete a chunk of data
copy(line.data[offset:], line.data[offset+len(data):])
line.data = line.data[:len(line.data)-len(data)]
}
})
}
func (a *action) do(v *view, what action_type) {
switch what {
case action_insert:
a.insert(v)
v.on_insert_adjust_top_line(a)
v.buf.other_views(v, func(v *view) {
v.on_insert(a)
})
if v.buf.is_mark_set() {
v.buf.mark.on_insert_adjust(a)
}
case action_delete:
a.delete(v)
v.on_delete_adjust_top_line(a)
v.buf.other_views(v, func(v *view) {
v.on_delete(a)
})
if v.buf.is_mark_set() {
v.buf.mark.on_delete_adjust(a)
}
}
v.dirty = dirty_everything
// any change to the buffer causes words cache invalidation
v.buf.words_cache_valid = false
}
func (a *action) last_line() *line {
return a.lines[len(a.lines)-1]
}
func (a *action) last_line_affection_len() int {
i := bytes.LastIndex(a.data, []byte{'\n'})
if i == -1 {
return len(a.data)
}
return len(a.data) - i - 1
}
func (a *action) first_line_affection_len() int {
i := bytes.Index(a.data, []byte{'\n'})
if i == -1 {
return len(a.data)
}
return i
}
// returns the range of deleted lines, the first and the last one
func (a *action) deleted_lines() (int, int) {
first := a.cursor.line_num + 1
last := first + len(a.lines) - 1
return first, last
}
func (a *action) try_merge(b *action) bool {
if a.what != b.what {
// can only merge actions of the same type
return false
}
if a.cursor.line_num != b.cursor.line_num {
return false
}
if a.cursor.boffset == b.cursor.boffset {
pa, pb := a, b
if a.what == action_insert {
// on insertion merge as 'ba', on deletion as 'ab'
pa, pb = pb, pa
}
pa.data = append(pa.data, pb.data...)
pa.lines = append(pa.lines, pb.lines...)
*a = *pa
return true
}
// different boffsets, try to restore the sequence
pa, pb := a, b
if pb.cursor.boffset < pa.cursor.boffset {
pa, pb = pb, pa
}
if pa.cursor.boffset+len(pa.data) == pb.cursor.boffset {
pa.data = append(pa.data, pb.data...)
pa.lines = append(pa.lines, pb.lines...)
*a = *pa
return true
}
return false
}
//----------------------------------------------------------------------------
// action group
//----------------------------------------------------------------------------
type action_group struct {
actions []action
next *action_group
prev *action_group
before cursor_location
after cursor_location
}
func (ag *action_group) append(a *action) {
if len(ag.actions) != 0 {
// Oh, we have something in the group already, let's try to
// merge this action with the last one.
last := &ag.actions[len(ag.actions)-1]
if last.try_merge(a) {
return
}
}
ag.actions = append(ag.actions, *a)
}
// Valid only as long as no new actions were added to the action group.
func (ag *action_group) last_action() *action {
if len(ag.actions) == 0 {
return nil
}
return &ag.actions[len(ag.actions)-1]
}