@kerebron/extension-basic-editor 0.8.5 → 0.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/history/ExtensionHistory.d.ts.map +1 -0
- package/esm/{ExtensionHistory.js → history/ExtensionHistory.js} +1 -1
- package/esm/history/ExtensionHistory.js.map +1 -0
- package/esm/history/RopeSequence.d.ts +35 -0
- package/esm/history/RopeSequence.d.ts.map +1 -0
- package/esm/history/RopeSequence.js +198 -0
- package/esm/history/RopeSequence.js.map +1 -0
- package/esm/history/history.d.ts +16 -0
- package/esm/history/history.d.ts.map +1 -0
- package/esm/history/history.js +488 -0
- package/esm/history/history.js.map +1 -0
- package/package.json +3 -4
- package/src/{ExtensionHistory.ts → history/ExtensionHistory.ts} +2 -2
- package/src/history/RopeSequence.ts +294 -0
- package/src/history/history.ts +634 -0
- package/esm/ExtensionHistory.d.ts.map +0 -1
- package/esm/ExtensionHistory.js.map +0 -1
- /package/esm/{ExtensionHistory.d.ts → history/ExtensionHistory.d.ts} +0 -0
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
// Fork and TS port of https://code.haverbeke.berlin/marijn/rope-sequence/
|
|
2
|
+
|
|
3
|
+
const GOOD_LEAF_SIZE = 200;
|
|
4
|
+
|
|
5
|
+
// :: class<T> A rope sequence is a persistent sequence data structure
|
|
6
|
+
// that supports appending, prepending, and slicing without doing a
|
|
7
|
+
// full copy. It is represented as a mostly-balanced tree.
|
|
8
|
+
export abstract class RopeSequence<T> {
|
|
9
|
+
abstract length: number;
|
|
10
|
+
abstract depth: number;
|
|
11
|
+
abstract flatten(): T[];
|
|
12
|
+
|
|
13
|
+
abstract leafAppend(other: RopeSequence<T>): RopeSequence<T> | undefined;
|
|
14
|
+
abstract leafPrepend(other: RopeSequence<T>): RopeSequence<T> | undefined;
|
|
15
|
+
|
|
16
|
+
// :: (union<[T], RopeSequence<T>>) → RopeSequence<T>
|
|
17
|
+
// Append an array or other rope to this one, returning a new rope.
|
|
18
|
+
append(other: RopeSequence<T> | T[]) {
|
|
19
|
+
if (!other.length) return this;
|
|
20
|
+
other = RopeSequence.from(other);
|
|
21
|
+
|
|
22
|
+
return (!this.length && other) ||
|
|
23
|
+
(other.length < GOOD_LEAF_SIZE && this.leafAppend(other)) ||
|
|
24
|
+
(this.length < GOOD_LEAF_SIZE && other.leafPrepend(this)) ||
|
|
25
|
+
this.appendInner(other);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// :: (union<[T], RopeSequence<T>>) → RopeSequence<T>
|
|
29
|
+
// Prepend an array or other rope to this one, returning a new rope.
|
|
30
|
+
prepend(other: RopeSequence<T> | T[]) {
|
|
31
|
+
if (!other.length) return this;
|
|
32
|
+
return RopeSequence.from(other).append(this);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
appendInner(other: RopeSequence<T>): RopeSequence<T> {
|
|
36
|
+
return new Append(this, other);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
abstract sliceInner(from: number, to: number): RopeSequence<T>;
|
|
40
|
+
|
|
41
|
+
// :: (?number, ?number) → RopeSequence<T>
|
|
42
|
+
// Create a rope repesenting a sub-sequence of this rope.
|
|
43
|
+
slice(from = 0, to = this.length): RopeSequence<T> {
|
|
44
|
+
if (from >= to) return emptyRopeSequence();
|
|
45
|
+
return this.sliceInner(Math.max(0, from), Math.min(this.length, to));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
abstract getInner(i: number): T | undefined;
|
|
49
|
+
|
|
50
|
+
// :: (number) → T
|
|
51
|
+
// Retrieve the element at the given position from this rope.
|
|
52
|
+
get(i: number): T | undefined {
|
|
53
|
+
if (i < 0 || i >= this.length) return undefined;
|
|
54
|
+
return this.getInner(i);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
abstract forEachInner(
|
|
58
|
+
f: (item: T, i: number) => T | boolean,
|
|
59
|
+
from: number,
|
|
60
|
+
to: number,
|
|
61
|
+
start: number,
|
|
62
|
+
): boolean;
|
|
63
|
+
abstract forEachInvertedInner(
|
|
64
|
+
f: (item: T, i: number) => T | boolean,
|
|
65
|
+
from: number,
|
|
66
|
+
to: number,
|
|
67
|
+
start: number,
|
|
68
|
+
): boolean;
|
|
69
|
+
|
|
70
|
+
// :: ((element: T, index: number) → ?bool, ?number, ?number)
|
|
71
|
+
// Call the given function for each element between the given
|
|
72
|
+
// indices. This tends to be more efficient than looping over the
|
|
73
|
+
// indices and calling `get`, because it doesn't have to descend the
|
|
74
|
+
// tree for every element.
|
|
75
|
+
forEach(
|
|
76
|
+
f: (item: T, i: number) => T | boolean,
|
|
77
|
+
from = 0,
|
|
78
|
+
to = this.length,
|
|
79
|
+
): void {
|
|
80
|
+
if (from <= to) {
|
|
81
|
+
this.forEachInner(f, from, to, 0);
|
|
82
|
+
} else {
|
|
83
|
+
this.forEachInvertedInner(f, from, to, 0);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// :: ((element: T, index: number) → U, ?number, ?number) → [U]
|
|
88
|
+
// Map the given functions over the elements of the rope, producing
|
|
89
|
+
// a flat array.
|
|
90
|
+
map(f: (item: T, i: number) => T, from = 0, to = this.length): T[] {
|
|
91
|
+
let result: T[] = [];
|
|
92
|
+
this.forEach(
|
|
93
|
+
(elt, i) => {
|
|
94
|
+
result.push(f(elt, i));
|
|
95
|
+
return elt;
|
|
96
|
+
},
|
|
97
|
+
from,
|
|
98
|
+
to,
|
|
99
|
+
);
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// :: (?union<[T], RopeSequence<T>>) → RopeSequence<T>
|
|
104
|
+
// Create a rope representing the given array, or return the rope
|
|
105
|
+
// itself if a rope was given.
|
|
106
|
+
static from<T>(values: RopeSequence<T> | T[]): RopeSequence<T> {
|
|
107
|
+
if (values instanceof RopeSequence) return values;
|
|
108
|
+
return values && values.length ? new Leaf(values) : emptyRopeSequence();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// flatten:: () → [T]
|
|
112
|
+
// Return the content of this rope as an array.
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
class Leaf<T> extends RopeSequence<T> {
|
|
116
|
+
values: T[];
|
|
117
|
+
|
|
118
|
+
constructor(values: T[]) {
|
|
119
|
+
super();
|
|
120
|
+
this.values = values;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
flatten(): T[] {
|
|
124
|
+
return this.values;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
sliceInner(from: number, to: number): RopeSequence<T> {
|
|
128
|
+
if (from == 0 && to == this.length) return this;
|
|
129
|
+
return new Leaf(this.values.slice(from, to));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
getInner(i: number): T {
|
|
133
|
+
return this.values[i];
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
forEachInner(
|
|
137
|
+
f: (item: T, i: number) => T,
|
|
138
|
+
from: number,
|
|
139
|
+
to: number,
|
|
140
|
+
start: number,
|
|
141
|
+
): boolean {
|
|
142
|
+
for (let i = from; i < to; i++) {
|
|
143
|
+
if (f(this.values[i], start + i) === false) return false;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return true;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
forEachInvertedInner(
|
|
150
|
+
f: (item: T, i: number) => T,
|
|
151
|
+
from: number,
|
|
152
|
+
to: number,
|
|
153
|
+
start: number,
|
|
154
|
+
): boolean {
|
|
155
|
+
for (let i = from - 1; i >= to; i--) {
|
|
156
|
+
if (f(this.values[i], start + i) === false) return false;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
leafAppend(other: RopeSequence<T>): RopeSequence<T> | undefined {
|
|
163
|
+
if (this.length + other.length <= GOOD_LEAF_SIZE) {
|
|
164
|
+
return new Leaf(this.values.concat(other.flatten()));
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
leafPrepend(other: RopeSequence<T>): RopeSequence<T> | undefined {
|
|
169
|
+
if (this.length + other.length <= GOOD_LEAF_SIZE) {
|
|
170
|
+
return new Leaf(other.flatten().concat(this.values));
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
get length() {
|
|
175
|
+
return this.values.length;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
get depth() {
|
|
179
|
+
return 0;
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
export function emptyRopeSequence() {
|
|
184
|
+
return new Leaf([]);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
class Append<T> extends RopeSequence<T> {
|
|
188
|
+
left: RopeSequence<T>;
|
|
189
|
+
right: RopeSequence<T>;
|
|
190
|
+
depth: number;
|
|
191
|
+
length: number;
|
|
192
|
+
|
|
193
|
+
constructor(left: RopeSequence<T>, right: RopeSequence<T>) {
|
|
194
|
+
super();
|
|
195
|
+
this.left = left;
|
|
196
|
+
this.right = right;
|
|
197
|
+
this.length = left.length + right.length;
|
|
198
|
+
this.depth = Math.max(left.depth, right.depth) + 1;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
flatten() {
|
|
202
|
+
return this.left.flatten().concat(this.right.flatten());
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
getInner(i: number): T | undefined {
|
|
206
|
+
return i < this.left.length
|
|
207
|
+
? this.left.get(i)
|
|
208
|
+
: this.right.get(i - this.left.length);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
forEachInner(
|
|
212
|
+
f: (item: T, i: number) => T,
|
|
213
|
+
from: number,
|
|
214
|
+
to: number,
|
|
215
|
+
start: number,
|
|
216
|
+
): boolean {
|
|
217
|
+
let leftLen = this.left.length;
|
|
218
|
+
if (
|
|
219
|
+
from < leftLen &&
|
|
220
|
+
this.left.forEachInner(f, from, Math.min(to, leftLen), start) === false
|
|
221
|
+
) {
|
|
222
|
+
return false;
|
|
223
|
+
}
|
|
224
|
+
if (
|
|
225
|
+
to > leftLen &&
|
|
226
|
+
this.right.forEachInner(
|
|
227
|
+
f,
|
|
228
|
+
Math.max(from - leftLen, 0),
|
|
229
|
+
Math.min(this.length, to) - leftLen,
|
|
230
|
+
start + leftLen,
|
|
231
|
+
) === false
|
|
232
|
+
) {
|
|
233
|
+
return false;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
return true;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
forEachInvertedInner(
|
|
240
|
+
f: (item: T, i: number) => T,
|
|
241
|
+
from: number,
|
|
242
|
+
to: number,
|
|
243
|
+
start: number,
|
|
244
|
+
): boolean {
|
|
245
|
+
let leftLen = this.left.length;
|
|
246
|
+
if (
|
|
247
|
+
from > leftLen &&
|
|
248
|
+
this.right.forEachInvertedInner(
|
|
249
|
+
f,
|
|
250
|
+
from - leftLen,
|
|
251
|
+
Math.max(to, leftLen) - leftLen,
|
|
252
|
+
start + leftLen,
|
|
253
|
+
) === false
|
|
254
|
+
) {
|
|
255
|
+
return false;
|
|
256
|
+
}
|
|
257
|
+
if (
|
|
258
|
+
to < leftLen &&
|
|
259
|
+
this.left.forEachInvertedInner(f, Math.min(from, leftLen), to, start) ===
|
|
260
|
+
false
|
|
261
|
+
) {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
return true;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
sliceInner(from: number, to: number) {
|
|
269
|
+
if (from == 0 && to == this.length) return this;
|
|
270
|
+
let leftLen = this.left.length;
|
|
271
|
+
if (to <= leftLen) return this.left.slice(from, to);
|
|
272
|
+
if (from >= leftLen) return this.right.slice(from - leftLen, to - leftLen);
|
|
273
|
+
return this.left.slice(from, leftLen).append(
|
|
274
|
+
this.right.slice(0, to - leftLen),
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
leafAppend(other: RopeSequence<T>) {
|
|
279
|
+
let inner = this.right.leafAppend(other);
|
|
280
|
+
if (inner) return new Append(this.left, inner);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
leafPrepend(other: RopeSequence<T>) {
|
|
284
|
+
let inner = this.left.leafPrepend(other);
|
|
285
|
+
if (inner) return new Append(inner, this.right);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
override appendInner(other: RopeSequence<T>): RopeSequence<T> {
|
|
289
|
+
if (this.left.depth >= Math.max(this.right.depth, other.depth) + 1) {
|
|
290
|
+
return new Append(this.left, new Append(this.right, other));
|
|
291
|
+
}
|
|
292
|
+
return new Append(this, other);
|
|
293
|
+
}
|
|
294
|
+
}
|