Scoop API

scoop:scoop.ui:0.1.0

src/text/editor/history.scoop

Selected production source · src/text/editor/history.scoop

1package scoop.ui.text
3import scoop.window.*
5internal enum EditorEditKind {
6 Separate, Typing, Backspace, Delete
7}
9/** Owns only the changed bytes and endpoint metadata, never a complete old document. */
10internal 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: EditorEditKind
19internal enum EditorHistoryChange {
20 Break, Reset, Undo, Redo,
21 Append(val edits: Array<EditorDelta>, val breakBefore: Boolean, val breakAfter: Boolean)
24internal class EditorHistory {
25 private val entries = ArrayList<EditorDelta>()
26 private var position = 0L
27 private var mergeOpen = false
28 val undoDelta: EditorDelta? get() =
29 if (position > 0L)
30 Some(entries[position - 1L])
31 else
32 None
33 val redoDelta: EditorDelta? get() =
34 if (position < entries.size)
35 Some(entries[position])
36 else
37 None
38 fun breakGroup() {
39 mergeOpen = false
40 }
41 fun apply(change: EditorHistoryChange): Boolean {
42 when (change) {
43 case Break -> {
44 breakGroup();
45 return false
46 }
47 case Reset -> {
48 val changed = entries.size > 0L
49 entries.clear()
50 position = 0L
51 breakGroup()
52 return changed
53 }
54 case Undo -> {
55 position -= 1L;
56 breakGroup();
57 return true
58 }
59 case Redo -> {
60 position += 1L;
61 breakGroup();
62 return true
63 }
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 > 0L
72 }
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 else
82 None
83 val merged =
84 when (previous) {
85 case Some(val value) -> mergeEditorDelta(value, edit)
86 case None -> None
87 }
88 when (merged) {
89 case Some(val value) -> entries[position - 1L] = value
90 case None -> {
91 entries.add(edit);
92 position += 1L
93 }
94 }
95 mergeOpen = edit.kind != EditorEditKind.Separate
96 }
99private 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.byteOffset
109 )
110 return None
111 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.start
120 )
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.start
132 )
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.start
136 )
137 return Some(EditorDelta(a.start, a.removed + b.removed, "", a.before, b.after, a.kind))
138 case Separate -> {}
139 }
140 return None
143internal fun appendEditorDelta(
144 edits: ArrayList<EditorDelta>,
145 before: EditorValue,
146 after: EditorValue,
147 kind: EditorEditKind
148) {
149 val old = before.input.surroundingText
150 val next = after.input.surroundingText
151 if (old == next)
152 return
153 val a = old.toByteArray()
154 val b = next.toByteArray()
155 var prefix = 0L
156 while (prefix < a.size && prefix < b.size && a[prefix] == b[prefix])
157 prefix += 1L
158 while (
159 prefix >
160 0L &&
161 (
162 (prefix < a.size && continuation(a[prefix])) ||
163 (prefix < b.size && continuation(b[prefix]))
164 )
165 )
166 prefix -= 1L
167 var suffix = 0L
168 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 += 1L
179 while (suffix > 0L && (continuation(a[a.size - suffix]) || continuation(b[b.size - suffix])))
180 suffix -= 1L
181 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 kind
189 )
190 )
193private fun continuation(value: Byte): Boolean = value.toInt().and(192) == 128
195internal fun replayEditorDelta(value: EditorValue, delta: EditorDelta, undo: Boolean): EditorValue {
196 val removed =
197 if (undo)
198 delta.inserted
199 else
200 delta.removed
201 val inserted =
202 if (undo)
203 delta.removed
204 else
205 delta.inserted
206 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.before
218 else
219 delta.after
220 )