scoop:scoop.ui:0.1.0
src/text/editor/history.scoop
Selected production source · src/text/editor/history.scoop
1
package scoop.ui.text3
import scoop.window.*5
internal enum EditorEditKind {6
Separate, Typing, Backspace, Delete7
}9
/** Owns only the changed bytes and endpoint metadata, never a complete old document. */10
internal struct EditorDelta(11
val start: Long,12
val removed: String,13
val inserted: String,14
val before: EditorSelection,15
val after: EditorSelection,16
val kind: EditorEditKind17
)19
internal enum EditorHistoryChange {20
Break, Reset, Undo, Redo,21
Append(val edits: Array<EditorDelta>, val breakBefore: Boolean, val breakAfter: Boolean)22
}24
internal class EditorHistory {25
private val entries = ArrayList<EditorDelta>()26
private var position = 0L27
private var mergeOpen = false28
val undoDelta: EditorDelta? get() =29
if (position > 0L)30
Some(entries[position - 1L])31
else32
None33
val redoDelta: EditorDelta? get() =34
if (position < entries.size)35
Some(entries[position])36
else37
None38
fun breakGroup() {39
mergeOpen = false40
}41
fun apply(change: EditorHistoryChange): Boolean {42
when (change) {43
case Break -> {44
breakGroup();45
return false46
}47
case Reset -> {48
val changed = entries.size > 0L49
entries.clear()50
position = 0L51
breakGroup()52
return changed53
}54
case Undo -> {55
position -= 1L;56
breakGroup();57
return true58
}59
case Redo -> {60
position += 1L;61
breakGroup();62
return true63
}64
case Append(val edits, val before, val after) -> {65
if (before)66
breakGroup()67
for (edit in edits)68
append(edit)69
if (after)70
breakGroup()71
return edits.size > 0L72
}73
}74
}75
private fun append(edit: EditorDelta) {76
while (entries.size > position)77
entries.removeAt(entries.size - 1L)78
val previous =79
if (mergeOpen && position > 0L)80
Some(entries[position - 1L])81
else82
None83
val merged =84
when (previous) {85
case Some(val value) -> mergeEditorDelta(value, edit)86
case None -> None87
}88
when (merged) {89
case Some(val value) -> entries[position - 1L] = value90
case None -> {91
entries.add(edit);92
position += 1L93
}94
}95
mergeOpen = edit.kind != EditorEditKind.Separate96
}97
}99
private fun mergeEditorDelta(a: EditorDelta, b: EditorDelta): EditorDelta? {100
if (101
a.kind !=102
b.kind ||103
a.after !=104
b.before ||105
a.before.anchor.byteOffset !=106
a.before.cursor.byteOffset ||107
b.before.anchor.byteOffset !=108
b.before.cursor.byteOffset109
)110
return None111
when (a.kind) {112
case Typing -> if (113
a.removed.byteLength ==114
0L &&115
b.removed.byteLength ==116
0L &&117
a.start +118
a.inserted.byteLength ==119
b.start120
)121
return Some(122
EditorDelta(a.start, "", a.inserted + b.inserted, a.before, b.after, a.kind)123
)124
case Backspace -> if (125
a.inserted.byteLength ==126
0L &&127
b.inserted.byteLength ==128
0L &&129
b.start +130
b.removed.byteLength ==131
a.start132
)133
return Some(EditorDelta(b.start, b.removed + a.removed, "", a.before, b.after, a.kind))134
case Delete -> if (135
a.inserted.byteLength == 0L && b.inserted.byteLength == 0L && a.start == b.start136
)137
return Some(EditorDelta(a.start, a.removed + b.removed, "", a.before, b.after, a.kind))138
case Separate -> {}139
}140
return None141
}143
internal fun appendEditorDelta(144
edits: ArrayList<EditorDelta>,145
before: EditorValue,146
after: EditorValue,147
kind: EditorEditKind148
) {149
val old = before.input.surroundingText150
val next = after.input.surroundingText151
if (old == next)152
return153
val a = old.toByteArray()154
val b = next.toByteArray()155
var prefix = 0L156
while (prefix < a.size && prefix < b.size && a[prefix] == b[prefix])157
prefix += 1L158
while (159
prefix >160
0L &&161
(162
(prefix < a.size && continuation(a[prefix])) ||163
(prefix < b.size && continuation(b[prefix]))164
)165
)166
prefix -= 1L167
var suffix = 0L168
while (169
suffix <170
a.size -171
prefix &&172
suffix <173
b.size -174
prefix &&175
a[a.size - suffix - 1L] ==176
b[b.size - suffix - 1L]177
)178
suffix += 1L179
while (suffix > 0L && (continuation(a[a.size - suffix]) || continuation(b[b.size - suffix])))180
suffix -= 1L181
edits.add(182
EditorDelta(183
prefix,184
old.utf8Slice(Utf8Range(prefix, a.size - suffix)),185
next.utf8Slice(Utf8Range(prefix, b.size - suffix)),186
before.endpoints,187
after.endpoints,188
kind189
)190
)191
}193
private fun continuation(value: Byte): Boolean = value.toInt().and(192) == 128195
internal fun replayEditorDelta(value: EditorValue, delta: EditorDelta, undo: Boolean): EditorValue {196
val removed =197
if (undo)198
delta.inserted199
else200
delta.removed201
val inserted =202
if (undo)203
delta.removed204
else205
delta.inserted206
val range = Utf8Range(delta.start, delta.start + removed.byteLength)207
val input =208
editorVersion(value, 0L).input.apply(209
TextInputBatch(210
None,211
[TextInputOperation.Commit(inserted, TextReplacement.Range(range))]212
)213
)214
return editorSelect(215
input,216
if (undo)217
delta.before218
else219
delta.after220
)221
}