@codemirror/state 0.19.9 → 0.20.0

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/dist/index.js CHANGED
@@ -1,5 +1,655 @@
1
- import { Text, findClusterBreak } from '@codemirror/text';
2
- export { Text } from '@codemirror/text';
1
+ /**
2
+ The data structure for documents. @nonabstract
3
+ */
4
+ class Text {
5
+ /**
6
+ @internal
7
+ */
8
+ constructor() { }
9
+ /**
10
+ Get the line description around the given position.
11
+ */
12
+ lineAt(pos) {
13
+ if (pos < 0 || pos > this.length)
14
+ throw new RangeError(`Invalid position ${pos} in document of length ${this.length}`);
15
+ return this.lineInner(pos, false, 1, 0);
16
+ }
17
+ /**
18
+ Get the description for the given (1-based) line number.
19
+ */
20
+ line(n) {
21
+ if (n < 1 || n > this.lines)
22
+ throw new RangeError(`Invalid line number ${n} in ${this.lines}-line document`);
23
+ return this.lineInner(n, true, 1, 0);
24
+ }
25
+ /**
26
+ Replace a range of the text with the given content.
27
+ */
28
+ replace(from, to, text) {
29
+ let parts = [];
30
+ this.decompose(0, from, parts, 2 /* To */);
31
+ if (text.length)
32
+ text.decompose(0, text.length, parts, 1 /* From */ | 2 /* To */);
33
+ this.decompose(to, this.length, parts, 1 /* From */);
34
+ return TextNode.from(parts, this.length - (to - from) + text.length);
35
+ }
36
+ /**
37
+ Append another document to this one.
38
+ */
39
+ append(other) {
40
+ return this.replace(this.length, this.length, other);
41
+ }
42
+ /**
43
+ Retrieve the text between the given points.
44
+ */
45
+ slice(from, to = this.length) {
46
+ let parts = [];
47
+ this.decompose(from, to, parts, 0);
48
+ return TextNode.from(parts, to - from);
49
+ }
50
+ /**
51
+ Test whether this text is equal to another instance.
52
+ */
53
+ eq(other) {
54
+ if (other == this)
55
+ return true;
56
+ if (other.length != this.length || other.lines != this.lines)
57
+ return false;
58
+ let start = this.scanIdentical(other, 1), end = this.length - this.scanIdentical(other, -1);
59
+ let a = new RawTextCursor(this), b = new RawTextCursor(other);
60
+ for (let skip = start, pos = start;;) {
61
+ a.next(skip);
62
+ b.next(skip);
63
+ skip = 0;
64
+ if (a.lineBreak != b.lineBreak || a.done != b.done || a.value != b.value)
65
+ return false;
66
+ pos += a.value.length;
67
+ if (a.done || pos >= end)
68
+ return true;
69
+ }
70
+ }
71
+ /**
72
+ Iterate over the text. When `dir` is `-1`, iteration happens
73
+ from end to start. This will return lines and the breaks between
74
+ them as separate strings.
75
+ */
76
+ iter(dir = 1) { return new RawTextCursor(this, dir); }
77
+ /**
78
+ Iterate over a range of the text. When `from` > `to`, the
79
+ iterator will run in reverse.
80
+ */
81
+ iterRange(from, to = this.length) { return new PartialTextCursor(this, from, to); }
82
+ /**
83
+ Return a cursor that iterates over the given range of lines,
84
+ _without_ returning the line breaks between, and yielding empty
85
+ strings for empty lines.
86
+
87
+ When `from` and `to` are given, they should be 1-based line numbers.
88
+ */
89
+ iterLines(from, to) {
90
+ let inner;
91
+ if (from == null) {
92
+ inner = this.iter();
93
+ }
94
+ else {
95
+ if (to == null)
96
+ to = this.lines + 1;
97
+ let start = this.line(from).from;
98
+ inner = this.iterRange(start, Math.max(start, to == this.lines + 1 ? this.length : to <= 1 ? 0 : this.line(to - 1).to));
99
+ }
100
+ return new LineCursor(inner);
101
+ }
102
+ /**
103
+ @internal
104
+ */
105
+ toString() { return this.sliceString(0); }
106
+ /**
107
+ Convert the document to an array of lines (which can be
108
+ deserialized again via [`Text.of`](https://codemirror.net/6/docs/ref/#state.Text^of)).
109
+ */
110
+ toJSON() {
111
+ let lines = [];
112
+ this.flatten(lines);
113
+ return lines;
114
+ }
115
+ /**
116
+ Create a `Text` instance for the given array of lines.
117
+ */
118
+ static of(text) {
119
+ if (text.length == 0)
120
+ throw new RangeError("A document must have at least one line");
121
+ if (text.length == 1 && !text[0])
122
+ return Text.empty;
123
+ return text.length <= 32 /* Branch */ ? new TextLeaf(text) : TextNode.from(TextLeaf.split(text, []));
124
+ }
125
+ }
126
+ // Leaves store an array of line strings. There are always line breaks
127
+ // between these strings. Leaves are limited in size and have to be
128
+ // contained in TextNode instances for bigger documents.
129
+ class TextLeaf extends Text {
130
+ constructor(text, length = textLength(text)) {
131
+ super();
132
+ this.text = text;
133
+ this.length = length;
134
+ }
135
+ get lines() { return this.text.length; }
136
+ get children() { return null; }
137
+ lineInner(target, isLine, line, offset) {
138
+ for (let i = 0;; i++) {
139
+ let string = this.text[i], end = offset + string.length;
140
+ if ((isLine ? line : end) >= target)
141
+ return new Line(offset, end, line, string);
142
+ offset = end + 1;
143
+ line++;
144
+ }
145
+ }
146
+ decompose(from, to, target, open) {
147
+ let text = from <= 0 && to >= this.length ? this
148
+ : new TextLeaf(sliceText(this.text, from, to), Math.min(to, this.length) - Math.max(0, from));
149
+ if (open & 1 /* From */) {
150
+ let prev = target.pop();
151
+ let joined = appendText(text.text, prev.text.slice(), 0, text.length);
152
+ if (joined.length <= 32 /* Branch */) {
153
+ target.push(new TextLeaf(joined, prev.length + text.length));
154
+ }
155
+ else {
156
+ let mid = joined.length >> 1;
157
+ target.push(new TextLeaf(joined.slice(0, mid)), new TextLeaf(joined.slice(mid)));
158
+ }
159
+ }
160
+ else {
161
+ target.push(text);
162
+ }
163
+ }
164
+ replace(from, to, text) {
165
+ if (!(text instanceof TextLeaf))
166
+ return super.replace(from, to, text);
167
+ let lines = appendText(this.text, appendText(text.text, sliceText(this.text, 0, from)), to);
168
+ let newLen = this.length + text.length - (to - from);
169
+ if (lines.length <= 32 /* Branch */)
170
+ return new TextLeaf(lines, newLen);
171
+ return TextNode.from(TextLeaf.split(lines, []), newLen);
172
+ }
173
+ sliceString(from, to = this.length, lineSep = "\n") {
174
+ let result = "";
175
+ for (let pos = 0, i = 0; pos <= to && i < this.text.length; i++) {
176
+ let line = this.text[i], end = pos + line.length;
177
+ if (pos > from && i)
178
+ result += lineSep;
179
+ if (from < end && to > pos)
180
+ result += line.slice(Math.max(0, from - pos), to - pos);
181
+ pos = end + 1;
182
+ }
183
+ return result;
184
+ }
185
+ flatten(target) {
186
+ for (let line of this.text)
187
+ target.push(line);
188
+ }
189
+ scanIdentical() { return 0; }
190
+ static split(text, target) {
191
+ let part = [], len = -1;
192
+ for (let line of text) {
193
+ part.push(line);
194
+ len += line.length + 1;
195
+ if (part.length == 32 /* Branch */) {
196
+ target.push(new TextLeaf(part, len));
197
+ part = [];
198
+ len = -1;
199
+ }
200
+ }
201
+ if (len > -1)
202
+ target.push(new TextLeaf(part, len));
203
+ return target;
204
+ }
205
+ }
206
+ // Nodes provide the tree structure of the `Text` type. They store a
207
+ // number of other nodes or leaves, taking care to balance themselves
208
+ // on changes. There are implied line breaks _between_ the children of
209
+ // a node (but not before the first or after the last child).
210
+ class TextNode extends Text {
211
+ constructor(children, length) {
212
+ super();
213
+ this.children = children;
214
+ this.length = length;
215
+ this.lines = 0;
216
+ for (let child of children)
217
+ this.lines += child.lines;
218
+ }
219
+ lineInner(target, isLine, line, offset) {
220
+ for (let i = 0;; i++) {
221
+ let child = this.children[i], end = offset + child.length, endLine = line + child.lines - 1;
222
+ if ((isLine ? endLine : end) >= target)
223
+ return child.lineInner(target, isLine, line, offset);
224
+ offset = end + 1;
225
+ line = endLine + 1;
226
+ }
227
+ }
228
+ decompose(from, to, target, open) {
229
+ for (let i = 0, pos = 0; pos <= to && i < this.children.length; i++) {
230
+ let child = this.children[i], end = pos + child.length;
231
+ if (from <= end && to >= pos) {
232
+ let childOpen = open & ((pos <= from ? 1 /* From */ : 0) | (end >= to ? 2 /* To */ : 0));
233
+ if (pos >= from && end <= to && !childOpen)
234
+ target.push(child);
235
+ else
236
+ child.decompose(from - pos, to - pos, target, childOpen);
237
+ }
238
+ pos = end + 1;
239
+ }
240
+ }
241
+ replace(from, to, text) {
242
+ if (text.lines < this.lines)
243
+ for (let i = 0, pos = 0; i < this.children.length; i++) {
244
+ let child = this.children[i], end = pos + child.length;
245
+ // Fast path: if the change only affects one child and the
246
+ // child's size remains in the acceptable range, only update
247
+ // that child
248
+ if (from >= pos && to <= end) {
249
+ let updated = child.replace(from - pos, to - pos, text);
250
+ let totalLines = this.lines - child.lines + updated.lines;
251
+ if (updated.lines < (totalLines >> (5 /* BranchShift */ - 1)) &&
252
+ updated.lines > (totalLines >> (5 /* BranchShift */ + 1))) {
253
+ let copy = this.children.slice();
254
+ copy[i] = updated;
255
+ return new TextNode(copy, this.length - (to - from) + text.length);
256
+ }
257
+ return super.replace(pos, end, updated);
258
+ }
259
+ pos = end + 1;
260
+ }
261
+ return super.replace(from, to, text);
262
+ }
263
+ sliceString(from, to = this.length, lineSep = "\n") {
264
+ let result = "";
265
+ for (let i = 0, pos = 0; i < this.children.length && pos <= to; i++) {
266
+ let child = this.children[i], end = pos + child.length;
267
+ if (pos > from && i)
268
+ result += lineSep;
269
+ if (from < end && to > pos)
270
+ result += child.sliceString(from - pos, to - pos, lineSep);
271
+ pos = end + 1;
272
+ }
273
+ return result;
274
+ }
275
+ flatten(target) {
276
+ for (let child of this.children)
277
+ child.flatten(target);
278
+ }
279
+ scanIdentical(other, dir) {
280
+ if (!(other instanceof TextNode))
281
+ return 0;
282
+ let length = 0;
283
+ let [iA, iB, eA, eB] = dir > 0 ? [0, 0, this.children.length, other.children.length]
284
+ : [this.children.length - 1, other.children.length - 1, -1, -1];
285
+ for (;; iA += dir, iB += dir) {
286
+ if (iA == eA || iB == eB)
287
+ return length;
288
+ let chA = this.children[iA], chB = other.children[iB];
289
+ if (chA != chB)
290
+ return length + chA.scanIdentical(chB, dir);
291
+ length += chA.length + 1;
292
+ }
293
+ }
294
+ static from(children, length = children.reduce((l, ch) => l + ch.length + 1, -1)) {
295
+ let lines = 0;
296
+ for (let ch of children)
297
+ lines += ch.lines;
298
+ if (lines < 32 /* Branch */) {
299
+ let flat = [];
300
+ for (let ch of children)
301
+ ch.flatten(flat);
302
+ return new TextLeaf(flat, length);
303
+ }
304
+ let chunk = Math.max(32 /* Branch */, lines >> 5 /* BranchShift */), maxChunk = chunk << 1, minChunk = chunk >> 1;
305
+ let chunked = [], currentLines = 0, currentLen = -1, currentChunk = [];
306
+ function add(child) {
307
+ let last;
308
+ if (child.lines > maxChunk && child instanceof TextNode) {
309
+ for (let node of child.children)
310
+ add(node);
311
+ }
312
+ else if (child.lines > minChunk && (currentLines > minChunk || !currentLines)) {
313
+ flush();
314
+ chunked.push(child);
315
+ }
316
+ else if (child instanceof TextLeaf && currentLines &&
317
+ (last = currentChunk[currentChunk.length - 1]) instanceof TextLeaf &&
318
+ child.lines + last.lines <= 32 /* Branch */) {
319
+ currentLines += child.lines;
320
+ currentLen += child.length + 1;
321
+ currentChunk[currentChunk.length - 1] = new TextLeaf(last.text.concat(child.text), last.length + 1 + child.length);
322
+ }
323
+ else {
324
+ if (currentLines + child.lines > chunk)
325
+ flush();
326
+ currentLines += child.lines;
327
+ currentLen += child.length + 1;
328
+ currentChunk.push(child);
329
+ }
330
+ }
331
+ function flush() {
332
+ if (currentLines == 0)
333
+ return;
334
+ chunked.push(currentChunk.length == 1 ? currentChunk[0] : TextNode.from(currentChunk, currentLen));
335
+ currentLen = -1;
336
+ currentLines = currentChunk.length = 0;
337
+ }
338
+ for (let child of children)
339
+ add(child);
340
+ flush();
341
+ return chunked.length == 1 ? chunked[0] : new TextNode(chunked, length);
342
+ }
343
+ }
344
+ Text.empty = /*@__PURE__*/new TextLeaf([""], 0);
345
+ function textLength(text) {
346
+ let length = -1;
347
+ for (let line of text)
348
+ length += line.length + 1;
349
+ return length;
350
+ }
351
+ function appendText(text, target, from = 0, to = 1e9) {
352
+ for (let pos = 0, i = 0, first = true; i < text.length && pos <= to; i++) {
353
+ let line = text[i], end = pos + line.length;
354
+ if (end >= from) {
355
+ if (end > to)
356
+ line = line.slice(0, to - pos);
357
+ if (pos < from)
358
+ line = line.slice(from - pos);
359
+ if (first) {
360
+ target[target.length - 1] += line;
361
+ first = false;
362
+ }
363
+ else
364
+ target.push(line);
365
+ }
366
+ pos = end + 1;
367
+ }
368
+ return target;
369
+ }
370
+ function sliceText(text, from, to) {
371
+ return appendText(text, [""], from, to);
372
+ }
373
+ class RawTextCursor {
374
+ constructor(text, dir = 1) {
375
+ this.dir = dir;
376
+ this.done = false;
377
+ this.lineBreak = false;
378
+ this.value = "";
379
+ this.nodes = [text];
380
+ this.offsets = [dir > 0 ? 1 : (text instanceof TextLeaf ? text.text.length : text.children.length) << 1];
381
+ }
382
+ nextInner(skip, dir) {
383
+ this.done = this.lineBreak = false;
384
+ for (;;) {
385
+ let last = this.nodes.length - 1;
386
+ let top = this.nodes[last], offsetValue = this.offsets[last], offset = offsetValue >> 1;
387
+ let size = top instanceof TextLeaf ? top.text.length : top.children.length;
388
+ if (offset == (dir > 0 ? size : 0)) {
389
+ if (last == 0) {
390
+ this.done = true;
391
+ this.value = "";
392
+ return this;
393
+ }
394
+ if (dir > 0)
395
+ this.offsets[last - 1]++;
396
+ this.nodes.pop();
397
+ this.offsets.pop();
398
+ }
399
+ else if ((offsetValue & 1) == (dir > 0 ? 0 : 1)) {
400
+ this.offsets[last] += dir;
401
+ if (skip == 0) {
402
+ this.lineBreak = true;
403
+ this.value = "\n";
404
+ return this;
405
+ }
406
+ skip--;
407
+ }
408
+ else if (top instanceof TextLeaf) {
409
+ // Move to the next string
410
+ let next = top.text[offset + (dir < 0 ? -1 : 0)];
411
+ this.offsets[last] += dir;
412
+ if (next.length > Math.max(0, skip)) {
413
+ this.value = skip == 0 ? next : dir > 0 ? next.slice(skip) : next.slice(0, next.length - skip);
414
+ return this;
415
+ }
416
+ skip -= next.length;
417
+ }
418
+ else {
419
+ let next = top.children[offset + (dir < 0 ? -1 : 0)];
420
+ if (skip > next.length) {
421
+ skip -= next.length;
422
+ this.offsets[last] += dir;
423
+ }
424
+ else {
425
+ if (dir < 0)
426
+ this.offsets[last]--;
427
+ this.nodes.push(next);
428
+ this.offsets.push(dir > 0 ? 1 : (next instanceof TextLeaf ? next.text.length : next.children.length) << 1);
429
+ }
430
+ }
431
+ }
432
+ }
433
+ next(skip = 0) {
434
+ if (skip < 0) {
435
+ this.nextInner(-skip, (-this.dir));
436
+ skip = this.value.length;
437
+ }
438
+ return this.nextInner(skip, this.dir);
439
+ }
440
+ }
441
+ class PartialTextCursor {
442
+ constructor(text, start, end) {
443
+ this.value = "";
444
+ this.done = false;
445
+ this.cursor = new RawTextCursor(text, start > end ? -1 : 1);
446
+ this.pos = start > end ? text.length : 0;
447
+ this.from = Math.min(start, end);
448
+ this.to = Math.max(start, end);
449
+ }
450
+ nextInner(skip, dir) {
451
+ if (dir < 0 ? this.pos <= this.from : this.pos >= this.to) {
452
+ this.value = "";
453
+ this.done = true;
454
+ return this;
455
+ }
456
+ skip += Math.max(0, dir < 0 ? this.pos - this.to : this.from - this.pos);
457
+ let limit = dir < 0 ? this.pos - this.from : this.to - this.pos;
458
+ if (skip > limit)
459
+ skip = limit;
460
+ limit -= skip;
461
+ let { value } = this.cursor.next(skip);
462
+ this.pos += (value.length + skip) * dir;
463
+ this.value = value.length <= limit ? value : dir < 0 ? value.slice(value.length - limit) : value.slice(0, limit);
464
+ this.done = !this.value;
465
+ return this;
466
+ }
467
+ next(skip = 0) {
468
+ if (skip < 0)
469
+ skip = Math.max(skip, this.from - this.pos);
470
+ else if (skip > 0)
471
+ skip = Math.min(skip, this.to - this.pos);
472
+ return this.nextInner(skip, this.cursor.dir);
473
+ }
474
+ get lineBreak() { return this.cursor.lineBreak && this.value != ""; }
475
+ }
476
+ class LineCursor {
477
+ constructor(inner) {
478
+ this.inner = inner;
479
+ this.afterBreak = true;
480
+ this.value = "";
481
+ this.done = false;
482
+ }
483
+ next(skip = 0) {
484
+ let { done, lineBreak, value } = this.inner.next(skip);
485
+ if (done) {
486
+ this.done = true;
487
+ this.value = "";
488
+ }
489
+ else if (lineBreak) {
490
+ if (this.afterBreak) {
491
+ this.value = "";
492
+ }
493
+ else {
494
+ this.afterBreak = true;
495
+ this.next();
496
+ }
497
+ }
498
+ else {
499
+ this.value = value;
500
+ this.afterBreak = false;
501
+ }
502
+ return this;
503
+ }
504
+ get lineBreak() { return false; }
505
+ }
506
+ if (typeof Symbol != "undefined") {
507
+ Text.prototype[Symbol.iterator] = function () { return this.iter(); };
508
+ RawTextCursor.prototype[Symbol.iterator] = PartialTextCursor.prototype[Symbol.iterator] =
509
+ LineCursor.prototype[Symbol.iterator] = function () { return this; };
510
+ }
511
+ /**
512
+ This type describes a line in the document. It is created
513
+ on-demand when lines are [queried](https://codemirror.net/6/docs/ref/#state.Text.lineAt).
514
+ */
515
+ class Line {
516
+ /**
517
+ @internal
518
+ */
519
+ constructor(
520
+ /**
521
+ The position of the start of the line.
522
+ */
523
+ from,
524
+ /**
525
+ The position at the end of the line (_before_ the line break,
526
+ or at the end of document for the last line).
527
+ */
528
+ to,
529
+ /**
530
+ This line's line number (1-based).
531
+ */
532
+ number,
533
+ /**
534
+ The line's content.
535
+ */
536
+ text) {
537
+ this.from = from;
538
+ this.to = to;
539
+ this.number = number;
540
+ this.text = text;
541
+ }
542
+ /**
543
+ The length of the line (not including any line break after it).
544
+ */
545
+ get length() { return this.to - this.from; }
546
+ }
547
+
548
+ // Compressed representation of the Grapheme_Cluster_Break=Extend
549
+ // information from
550
+ // http://www.unicode.org/Public/13.0.0/ucd/auxiliary/GraphemeBreakProperty.txt.
551
+ // Each pair of elements represents a range, as an offet from the
552
+ // previous range and a length. Numbers are in base-36, with the empty
553
+ // string being a shorthand for 1.
554
+ let extend = /*@__PURE__*/"lc,34,7n,7,7b,19,,,,2,,2,,,20,b,1c,l,g,,2t,7,2,6,2,2,,4,z,,u,r,2j,b,1m,9,9,,o,4,,9,,3,,5,17,3,3b,f,,w,1j,,,,4,8,4,,3,7,a,2,t,,1m,,,,2,4,8,,9,,a,2,q,,2,2,1l,,4,2,4,2,2,3,3,,u,2,3,,b,2,1l,,4,5,,2,4,,k,2,m,6,,,1m,,,2,,4,8,,7,3,a,2,u,,1n,,,,c,,9,,14,,3,,1l,3,5,3,,4,7,2,b,2,t,,1m,,2,,2,,3,,5,2,7,2,b,2,s,2,1l,2,,,2,4,8,,9,,a,2,t,,20,,4,,2,3,,,8,,29,,2,7,c,8,2q,,2,9,b,6,22,2,r,,,,,,1j,e,,5,,2,5,b,,10,9,,2u,4,,6,,2,2,2,p,2,4,3,g,4,d,,2,2,6,,f,,jj,3,qa,3,t,3,t,2,u,2,1s,2,,7,8,,2,b,9,,19,3,3b,2,y,,3a,3,4,2,9,,6,3,63,2,2,,1m,,,7,,,,,2,8,6,a,2,,1c,h,1r,4,1c,7,,,5,,14,9,c,2,w,4,2,2,,3,1k,,,2,3,,,3,1m,8,2,2,48,3,,d,,7,4,,6,,3,2,5i,1m,,5,ek,,5f,x,2da,3,3x,,2o,w,fe,6,2x,2,n9w,4,,a,w,2,28,2,7k,,3,,4,,p,2,5,,47,2,q,i,d,,12,8,p,b,1a,3,1c,,2,4,2,2,13,,1v,6,2,2,2,2,c,,8,,1b,,1f,,,3,2,2,5,2,,,16,2,8,,6m,,2,,4,,fn4,,kh,g,g,g,a6,2,gt,,6a,,45,5,1ae,3,,2,5,4,14,3,4,,4l,2,fx,4,ar,2,49,b,4w,,1i,f,1k,3,1d,4,2,2,1x,3,10,5,,8,1q,,c,2,1g,9,a,4,2,,2n,3,2,,,2,6,,4g,,3,8,l,2,1l,2,,,,,m,,e,7,3,5,5f,8,2,3,,,n,,29,,2,6,,,2,,,2,,2,6j,,2,4,6,2,,2,r,2,2d,8,2,,,2,2y,,,,2,6,,,2t,3,2,4,,5,77,9,,2,6t,,a,2,,,4,,40,4,2,2,4,,w,a,14,6,2,4,8,,9,6,2,3,1a,d,,2,ba,7,,6,,,2a,m,2,7,,2,,2,3e,6,3,,,2,,7,,,20,2,3,,,,9n,2,f0b,5,1n,7,t4,,1r,4,29,,f5k,2,43q,,,3,4,5,8,8,2,7,u,4,44,3,1iz,1j,4,1e,8,,e,,m,5,,f,11s,7,,h,2,7,,2,,5,79,7,c5,4,15s,7,31,7,240,5,gx7k,2o,3k,6o".split(",").map(s => s ? parseInt(s, 36) : 1);
555
+ // Convert offsets into absolute values
556
+ for (let i = 1; i < extend.length; i++)
557
+ extend[i] += extend[i - 1];
558
+ function isExtendingChar(code) {
559
+ for (let i = 1; i < extend.length; i += 2)
560
+ if (extend[i] > code)
561
+ return extend[i - 1] <= code;
562
+ return false;
563
+ }
564
+ function isRegionalIndicator(code) {
565
+ return code >= 0x1F1E6 && code <= 0x1F1FF;
566
+ }
567
+ const ZWJ = 0x200d;
568
+ /**
569
+ Returns a next grapheme cluster break _after_ (not equal to)
570
+ `pos`, if `forward` is true, or before otherwise. Returns `pos`
571
+ itself if no further cluster break is available in the string.
572
+ Moves across surrogate pairs, extending characters (when
573
+ `includeExtending` is true), characters joined with zero-width
574
+ joiners, and flag emoji.
575
+ */
576
+ function findClusterBreak(str, pos, forward = true, includeExtending = true) {
577
+ return (forward ? nextClusterBreak : prevClusterBreak)(str, pos, includeExtending);
578
+ }
579
+ function nextClusterBreak(str, pos, includeExtending) {
580
+ if (pos == str.length)
581
+ return pos;
582
+ // If pos is in the middle of a surrogate pair, move to its start
583
+ if (pos && surrogateLow(str.charCodeAt(pos)) && surrogateHigh(str.charCodeAt(pos - 1)))
584
+ pos--;
585
+ let prev = codePointAt(str, pos);
586
+ pos += codePointSize(prev);
587
+ while (pos < str.length) {
588
+ let next = codePointAt(str, pos);
589
+ if (prev == ZWJ || next == ZWJ || includeExtending && isExtendingChar(next)) {
590
+ pos += codePointSize(next);
591
+ prev = next;
592
+ }
593
+ else if (isRegionalIndicator(next)) {
594
+ let countBefore = 0, i = pos - 2;
595
+ while (i >= 0 && isRegionalIndicator(codePointAt(str, i))) {
596
+ countBefore++;
597
+ i -= 2;
598
+ }
599
+ if (countBefore % 2 == 0)
600
+ break;
601
+ else
602
+ pos += 2;
603
+ }
604
+ else {
605
+ break;
606
+ }
607
+ }
608
+ return pos;
609
+ }
610
+ function prevClusterBreak(str, pos, includeExtending) {
611
+ while (pos > 0) {
612
+ let found = nextClusterBreak(str, pos - 2, includeExtending);
613
+ if (found < pos)
614
+ return found;
615
+ pos--;
616
+ }
617
+ return 0;
618
+ }
619
+ function surrogateLow(ch) { return ch >= 0xDC00 && ch < 0xE000; }
620
+ function surrogateHigh(ch) { return ch >= 0xD800 && ch < 0xDC00; }
621
+ /**
622
+ Find the code point at the given position in a string (like the
623
+ [`codePointAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt)
624
+ string method).
625
+ */
626
+ function codePointAt(str, pos) {
627
+ let code0 = str.charCodeAt(pos);
628
+ if (!surrogateHigh(code0) || pos + 1 == str.length)
629
+ return code0;
630
+ let code1 = str.charCodeAt(pos + 1);
631
+ if (!surrogateLow(code1))
632
+ return code0;
633
+ return ((code0 - 0xd800) << 10) + (code1 - 0xdc00) + 0x10000;
634
+ }
635
+ /**
636
+ Given a Unicode codepoint, return the JavaScript string that
637
+ respresents it (like
638
+ [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint)).
639
+ */
640
+ function fromCodePoint(code) {
641
+ if (code <= 0xffff)
642
+ return String.fromCharCode(code);
643
+ code -= 0x10000;
644
+ return String.fromCharCode((code >> 10) + 0xd800, (code & 1023) + 0xdc00);
645
+ }
646
+ /**
647
+ The first character that takes up two positions in a JavaScript
648
+ string. It is often useful to compare with this after calling
649
+ `codePointAt`, to figure out whether your character takes up 1 or
650
+ 2 index positions.
651
+ */
652
+ function codePointSize(code) { return code < 0x10000 ? 1 : 2; }
3
653
 
4
654
  const DefaultSplit = /\r\n?|\n/;
5
655
  /**
@@ -70,7 +720,9 @@ class ChangeDesc {
70
720
  */
71
721
  get empty() { return this.sections.length == 0 || this.sections.length == 2 && this.sections[1] < 0; }
72
722
  /**
73
- Iterate over the unchanged parts left by these changes.
723
+ Iterate over the unchanged parts left by these changes. `posA`
724
+ provides the position of the range in the old document, `posB`
725
+ the new position in the changed document.
74
726
  */
75
727
  iterGaps(f) {
76
728
  for (let i = 0, posA = 0, posB = 0; i < this.sections.length;) {
@@ -89,6 +741,9 @@ class ChangeDesc {
89
741
  Iterate over the ranges changed by these changes. (See
90
742
  [`ChangeSet.iterChanges`](https://codemirror.net/6/docs/ref/#state.ChangeSet.iterChanges) for a
91
743
  variant that also provides you with the inserted text.)
744
+ `fromA`/`toA` provides the extent of the change in the starting
745
+ document, `fromB`/`toB` the extent of the replacement in the
746
+ changed document.
92
747
 
93
748
  When `individual` is true, adjacent changes (which are kept
94
749
  separate for [position mapping](https://codemirror.net/6/docs/ref/#state.ChangeDesc.mapPos)) are
@@ -871,10 +1526,10 @@ A facet is a labeled value that is associated with an editor
871
1526
  state. It takes inputs from any number of extensions, and combines
872
1527
  those into a single output value.
873
1528
 
874
- Examples of facets are the [theme](https://codemirror.net/6/docs/ref/#view.EditorView^theme) styles
875
- associated with an editor or the [tab
876
- size](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) (which is reduced to a single
877
- value, using the input with the hightest precedence).
1529
+ Examples of uses of facets are the [tab
1530
+ size](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize), [editor
1531
+ attributes](https://codemirror.net/6/docs/ref/#view.EditorView^editorAttributes), and [update
1532
+ listeners](https://codemirror.net/6/docs/ref/#view.EditorView^updateListener).
878
1533
  */
879
1534
  class Facet {
880
1535
  constructor(
@@ -912,7 +1567,7 @@ class Facet {
912
1567
  return new Facet(config.combine || ((a) => a), config.compareInput || ((a, b) => a === b), config.compare || (!config.combine ? sameArray : (a, b) => a === b), !!config.static, config.enables);
913
1568
  }
914
1569
  /**
915
- Returns an extension that adds the given value for this facet.
1570
+ Returns an extension that adds the given value to this facet.
916
1571
  */
917
1572
  of(value) {
918
1573
  return new FacetProvider([], this, 0 /* Static */, value);
@@ -923,9 +1578,8 @@ class Facet {
923
1578
  this value depends on, since your function is only called again
924
1579
  for a new state when one of those parts changed.
925
1580
 
926
- In most cases, you'll want to use the
927
- [`provide`](https://codemirror.net/6/docs/ref/#state.StateField^define^config.provide) option when
928
- defining a field instead.
1581
+ In cases where your value depends only on a single field, you'll
1582
+ want to use the [`from`](https://codemirror.net/6/docs/ref/#state.Facet.from) method instead.
929
1583
  */
930
1584
  compute(deps, get) {
931
1585
  if (this.isStatic)
@@ -978,8 +1632,7 @@ class FacetProvider {
978
1632
  return 1 /* Changed */;
979
1633
  },
980
1634
  update(state, tr) {
981
- if ((depDoc && tr.docChanged) || (depSel && (tr.docChanged || tr.selection)) ||
982
- depAddrs.some(addr => (ensureAddr(state, addr) & 1 /* Changed */) > 0)) {
1635
+ if ((depDoc && tr.docChanged) || (depSel && (tr.docChanged || tr.selection)) || ensureAll(state, depAddrs)) {
983
1636
  let newVal = getter(state);
984
1637
  if (multi ? !compareArray(newVal, state.values[idx], compare) : !compare(newVal, state.values[idx])) {
985
1638
  state.values[idx] = newVal;
@@ -988,12 +1641,15 @@ class FacetProvider {
988
1641
  }
989
1642
  return 0;
990
1643
  },
991
- reconfigure(state, oldState) {
1644
+ reconfigure: (state, oldState) => {
992
1645
  let newVal = getter(state);
993
1646
  let oldAddr = oldState.config.address[id];
994
1647
  if (oldAddr != null) {
995
1648
  let oldVal = getAddr(oldState, oldAddr);
996
- if (multi ? compareArray(newVal, oldVal, compare) : compare(newVal, oldVal)) {
1649
+ if (this.dependencies.every(dep => {
1650
+ return dep instanceof Facet ? oldState.facet(dep) === state.facet(dep) :
1651
+ dep instanceof StateField ? oldState.field(dep, false) == state.field(dep, false) : true;
1652
+ }) || (multi ? compareArray(newVal, oldVal, compare) : compare(newVal, oldVal))) {
997
1653
  state.values[idx] = oldVal;
998
1654
  return 0;
999
1655
  }
@@ -1012,6 +1668,13 @@ function compareArray(a, b, compare) {
1012
1668
  return false;
1013
1669
  return true;
1014
1670
  }
1671
+ function ensureAll(state, addrs) {
1672
+ let changed = false;
1673
+ for (let addr of addrs)
1674
+ if (ensureAddr(state, addr) & 1 /* Changed */)
1675
+ changed = true;
1676
+ return changed;
1677
+ }
1015
1678
  function dynamicFacetSlot(addresses, facet, providers) {
1016
1679
  let providerAddrs = providers.map(p => addresses[p.id]);
1017
1680
  let providerTypes = providers.map(p => p.type);
@@ -1037,7 +1700,7 @@ function dynamicFacetSlot(addresses, facet, providers) {
1037
1700
  return 1 /* Changed */;
1038
1701
  },
1039
1702
  update(state, tr) {
1040
- if (!dynamic.some(dynAddr => ensureAddr(state, dynAddr) & 1 /* Changed */))
1703
+ if (!ensureAll(state, dynamic))
1041
1704
  return 0;
1042
1705
  let value = get(state);
1043
1706
  if (facet.compare(value, state.values[idx]))
@@ -1046,7 +1709,7 @@ function dynamicFacetSlot(addresses, facet, providers) {
1046
1709
  return 1 /* Changed */;
1047
1710
  },
1048
1711
  reconfigure(state, oldState) {
1049
- let depChanged = providerAddrs.some(addr => ensureAddr(state, addr) & 1 /* Changed */);
1712
+ let depChanged = ensureAll(state, providerAddrs);
1050
1713
  let oldProviders = oldState.config.facets[facet.id], oldValue = oldState.facet(facet);
1051
1714
  if (oldProviders && !depChanged && sameArray(providers, oldProviders)) {
1052
1715
  state.values[idx] = oldValue;
@@ -1159,42 +1822,29 @@ precedence and then by order within each precedence.
1159
1822
  */
1160
1823
  const Prec = {
1161
1824
  /**
1162
- The lowest precedence level. Meant for things that should end up
1163
- near the end of the extension order.
1164
- */
1165
- lowest: /*@__PURE__*/prec(Prec_.lowest),
1166
- /**
1167
- A lower-than-default precedence, for extensions.
1168
- */
1169
- low: /*@__PURE__*/prec(Prec_.low),
1170
- /**
1171
- The default precedence, which is also used for extensions
1172
- without an explicit precedence.
1825
+ The highest precedence level, for extensions that should end up
1826
+ near the start of the precedence ordering.
1173
1827
  */
1174
- default: /*@__PURE__*/prec(Prec_.default),
1828
+ highest: /*@__PURE__*/prec(Prec_.highest),
1175
1829
  /**
1176
1830
  A higher-than-default precedence, for extensions that should
1177
1831
  come before those with default precedence.
1178
1832
  */
1179
1833
  high: /*@__PURE__*/prec(Prec_.high),
1180
1834
  /**
1181
- The highest precedence level, for extensions that should end up
1182
- near the start of the precedence ordering.
1183
- */
1184
- highest: /*@__PURE__*/prec(Prec_.highest),
1185
- // FIXME Drop these in some future breaking version
1186
- /**
1187
- Backwards-compatible synonym for `Prec.lowest`.
1835
+ The default precedence, which is also used for extensions
1836
+ without an explicit precedence.
1188
1837
  */
1189
- fallback: /*@__PURE__*/prec(Prec_.lowest),
1838
+ default: /*@__PURE__*/prec(Prec_.default),
1190
1839
  /**
1191
- Backwards-compatible synonym for `Prec.high`.
1840
+ A lower-than-default precedence.
1192
1841
  */
1193
- extend: /*@__PURE__*/prec(Prec_.high),
1842
+ low: /*@__PURE__*/prec(Prec_.low),
1194
1843
  /**
1195
- Backwards-compatible synonym for `Prec.highest`.
1844
+ The lowest precedence level. Meant for things that should end up
1845
+ near the end of the extension order.
1196
1846
  */
1197
- override: /*@__PURE__*/prec(Prec_.highest)
1847
+ lowest: /*@__PURE__*/prec(Prec_.lowest)
1198
1848
  };
1199
1849
  class PrecExtension {
1200
1850
  constructor(inner, prec) {
@@ -1308,7 +1958,7 @@ function flatten(extension, compartments, newCompartments) {
1308
1958
  function inner(ext, prec) {
1309
1959
  let known = seen.get(ext);
1310
1960
  if (known != null) {
1311
- if (known >= prec)
1961
+ if (known <= prec)
1312
1962
  return;
1313
1963
  let found = result[known].indexOf(ext);
1314
1964
  if (found > -1)
@@ -1523,7 +2173,9 @@ Changes to the editor state are grouped into transactions.
1523
2173
  Typically, a user action creates a single transaction, which may
1524
2174
  contain any number of document changes, may change the selection,
1525
2175
  or have other effects. Create a transaction by calling
1526
- [`EditorState.update`](https://codemirror.net/6/docs/ref/#state.EditorState.update).
2176
+ [`EditorState.update`](https://codemirror.net/6/docs/ref/#state.EditorState.update), or immediately
2177
+ dispatch one by calling
2178
+ [`EditorView.dispatch`](https://codemirror.net/6/docs/ref/#view.EditorView.dispatch).
1527
2179
  */
1528
2180
  class Transaction {
1529
2181
  /**
@@ -1597,7 +2249,7 @@ class Transaction {
1597
2249
  }
1598
2250
  /**
1599
2251
  The new state created by the transaction. Computed on demand
1600
- (but retained for subsequent access), so itis recommended not to
2252
+ (but retained for subsequent access), so it is recommended not to
1601
2253
  access it in [transaction
1602
2254
  filters](https://codemirror.net/6/docs/ref/#state.EditorState^transactionFilter) when possible.
1603
2255
  */
@@ -1639,7 +2291,8 @@ class Transaction {
1639
2291
  }
1640
2292
  }
1641
2293
  /**
1642
- Annotation used to store transaction timestamps.
2294
+ Annotation used to store transaction timestamps. Automatically
2295
+ added to every transaction, holding `Date.now()`.
1643
2296
  */
1644
2297
  Transaction.time = /*@__PURE__*/Annotation.define();
1645
2298
  /**
@@ -2011,7 +2664,7 @@ class EditorState {
2011
2664
  /**
2012
2665
  Using the state's [line
2013
2666
  separator](https://codemirror.net/6/docs/ref/#state.EditorState^lineSeparator), create a
2014
- [`Text`](https://codemirror.net/6/docs/ref/#text.Text) instance from the given string.
2667
+ [`Text`](https://codemirror.net/6/docs/ref/#state.Text) instance from the given string.
2015
2668
  */
2016
2669
  toText(string) {
2017
2670
  return Text.of(string.split(this.facet(EditorState.lineSeparator) || DefaultSplit));
@@ -2131,7 +2784,7 @@ class EditorState {
2131
2784
  }
2132
2785
  /**
2133
2786
  Return a function that can categorize strings (expected to
2134
- represent a single [grapheme cluster](https://codemirror.net/6/docs/ref/#text.findClusterBreak))
2787
+ represent a single [grapheme cluster](https://codemirror.net/6/docs/ref/#state.findClusterBreak))
2135
2788
  into one of:
2136
2789
 
2137
2790
  - Word (contains an alphanumeric character or a character
@@ -2215,7 +2868,12 @@ Registers translation phrases. The
2215
2868
  all objects registered with this facet to find translations for
2216
2869
  its argument.
2217
2870
  */
2218
- EditorState.phrases = /*@__PURE__*/Facet.define();
2871
+ EditorState.phrases = /*@__PURE__*/Facet.define({
2872
+ compare(a, b) {
2873
+ let kA = Object.keys(a), kB = Object.keys(b);
2874
+ return kA.length == kB.length && kA.every(k => a[k] == b[k]);
2875
+ }
2876
+ });
2219
2877
  /**
2220
2878
  A facet used to register [language
2221
2879
  data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) providers.
@@ -2231,7 +2889,7 @@ Such a function can return `true` to indicate that it doesn't
2231
2889
  want to do anything, `false` to completely stop the changes in
2232
2890
  the transaction, or a set of ranges in which changes should be
2233
2891
  suppressed. Such ranges are represented as an array of numbers,
2234
- with each pair of two number indicating the start and end of a
2892
+ with each pair of two numbers indicating the start and end of a
2235
2893
  range. So for example `[10, 20, 100, 110]` suppresses changes
2236
2894
  between 10 and 20, and between 100 and 110.
2237
2895
  */
@@ -2262,19 +2920,22 @@ This is a more limited form of
2262
2920
  which can only add
2263
2921
  [annotations](https://codemirror.net/6/docs/ref/#state.TransactionSpec.annotations) and
2264
2922
  [effects](https://codemirror.net/6/docs/ref/#state.TransactionSpec.effects). _But_, this type
2265
- of filter runs even the transaction has disabled regular
2923
+ of filter runs even if the transaction has disabled regular
2266
2924
  [filtering](https://codemirror.net/6/docs/ref/#state.TransactionSpec.filter), making it suitable
2267
2925
  for effects that don't need to touch the changes or selection,
2268
2926
  but do want to process every transaction.
2269
2927
 
2270
- Extenders run _after_ filters, when both are applied.
2928
+ Extenders run _after_ filters, when both are present.
2271
2929
  */
2272
2930
  EditorState.transactionExtender = transactionExtender;
2273
2931
  Compartment.reconfigure = /*@__PURE__*/StateEffect.define();
2274
2932
 
2275
2933
  /**
2276
2934
  Utility function for combining behaviors to fill in a config
2277
- object from an array of provided configs. Will, by default, error
2935
+ object from an array of provided configs. `defaults` should hold
2936
+ default values for all optional fields in `Config`.
2937
+
2938
+ The function will, by default, error
2278
2939
  when a field gets two values that aren't `===`-equal, but you can
2279
2940
  provide combine functions per field to do something else.
2280
2941
  */
@@ -2298,4 +2959,871 @@ combine = {}) {
2298
2959
  return result;
2299
2960
  }
2300
2961
 
2301
- export { Annotation, AnnotationType, ChangeDesc, ChangeSet, CharCategory, Compartment, EditorSelection, EditorState, Facet, MapMode, Prec, SelectionRange, StateEffect, StateEffectType, StateField, Transaction, combineConfig };
2962
+ /**
2963
+ Each range is associated with a value, which must inherit from
2964
+ this class.
2965
+ */
2966
+ class RangeValue {
2967
+ /**
2968
+ Compare this value with another value. Used when comparing
2969
+ rangesets. The default implementation compares by identity.
2970
+ Unless you are only creating a fixed number of unique instances
2971
+ of your value type, it is a good idea to implement this
2972
+ properly.
2973
+ */
2974
+ eq(other) { return this == other; }
2975
+ /**
2976
+ Create a [range](https://codemirror.net/6/docs/ref/#state.Range) with this value.
2977
+ */
2978
+ range(from, to = from) { return new Range(from, to, this); }
2979
+ }
2980
+ RangeValue.prototype.startSide = RangeValue.prototype.endSide = 0;
2981
+ RangeValue.prototype.point = false;
2982
+ RangeValue.prototype.mapMode = MapMode.TrackDel;
2983
+ /**
2984
+ A range associates a value with a range of positions.
2985
+ */
2986
+ class Range {
2987
+ /**
2988
+ @internal
2989
+ */
2990
+ constructor(
2991
+ /**
2992
+ The range's start position.
2993
+ */
2994
+ from,
2995
+ /**
2996
+ Its end position.
2997
+ */
2998
+ to,
2999
+ /**
3000
+ The value associated with this range.
3001
+ */
3002
+ value) {
3003
+ this.from = from;
3004
+ this.to = to;
3005
+ this.value = value;
3006
+ }
3007
+ }
3008
+ function cmpRange(a, b) {
3009
+ return a.from - b.from || a.value.startSide - b.value.startSide;
3010
+ }
3011
+ class Chunk {
3012
+ constructor(from, to, value,
3013
+ // Chunks are marked with the largest point that occurs
3014
+ // in them (or -1 for no points), so that scans that are
3015
+ // only interested in points (such as the
3016
+ // heightmap-related logic) can skip range-only chunks.
3017
+ maxPoint) {
3018
+ this.from = from;
3019
+ this.to = to;
3020
+ this.value = value;
3021
+ this.maxPoint = maxPoint;
3022
+ }
3023
+ get length() { return this.to[this.to.length - 1]; }
3024
+ // Find the index of the given position and side. Use the ranges'
3025
+ // `from` pos when `end == false`, `to` when `end == true`.
3026
+ findIndex(pos, side, end, startAt = 0) {
3027
+ let arr = end ? this.to : this.from;
3028
+ for (let lo = startAt, hi = arr.length;;) {
3029
+ if (lo == hi)
3030
+ return lo;
3031
+ let mid = (lo + hi) >> 1;
3032
+ let diff = arr[mid] - pos || (end ? this.value[mid].endSide : this.value[mid].startSide) - side;
3033
+ if (mid == lo)
3034
+ return diff >= 0 ? lo : hi;
3035
+ if (diff >= 0)
3036
+ hi = mid;
3037
+ else
3038
+ lo = mid + 1;
3039
+ }
3040
+ }
3041
+ between(offset, from, to, f) {
3042
+ for (let i = this.findIndex(from, -1000000000 /* Far */, true), e = this.findIndex(to, 1000000000 /* Far */, false, i); i < e; i++)
3043
+ if (f(this.from[i] + offset, this.to[i] + offset, this.value[i]) === false)
3044
+ return false;
3045
+ }
3046
+ map(offset, changes) {
3047
+ let value = [], from = [], to = [], newPos = -1, maxPoint = -1;
3048
+ for (let i = 0; i < this.value.length; i++) {
3049
+ let val = this.value[i], curFrom = this.from[i] + offset, curTo = this.to[i] + offset, newFrom, newTo;
3050
+ if (curFrom == curTo) {
3051
+ let mapped = changes.mapPos(curFrom, val.startSide, val.mapMode);
3052
+ if (mapped == null)
3053
+ continue;
3054
+ newFrom = newTo = mapped;
3055
+ if (val.startSide != val.endSide) {
3056
+ newTo = changes.mapPos(curFrom, val.endSide);
3057
+ if (newTo < newFrom)
3058
+ continue;
3059
+ }
3060
+ }
3061
+ else {
3062
+ newFrom = changes.mapPos(curFrom, val.startSide);
3063
+ newTo = changes.mapPos(curTo, val.endSide);
3064
+ if (newFrom > newTo || newFrom == newTo && val.startSide > 0 && val.endSide <= 0)
3065
+ continue;
3066
+ }
3067
+ if ((newTo - newFrom || val.endSide - val.startSide) < 0)
3068
+ continue;
3069
+ if (newPos < 0)
3070
+ newPos = newFrom;
3071
+ if (val.point)
3072
+ maxPoint = Math.max(maxPoint, newTo - newFrom);
3073
+ value.push(val);
3074
+ from.push(newFrom - newPos);
3075
+ to.push(newTo - newPos);
3076
+ }
3077
+ return { mapped: value.length ? new Chunk(from, to, value, maxPoint) : null, pos: newPos };
3078
+ }
3079
+ }
3080
+ /**
3081
+ A range set stores a collection of [ranges](https://codemirror.net/6/docs/ref/#state.Range) in a
3082
+ way that makes them efficient to [map](https://codemirror.net/6/docs/ref/#state.RangeSet.map) and
3083
+ [update](https://codemirror.net/6/docs/ref/#state.RangeSet.update). This is an immutable data
3084
+ structure.
3085
+ */
3086
+ class RangeSet {
3087
+ /**
3088
+ @internal
3089
+ */
3090
+ constructor(
3091
+ /**
3092
+ @internal
3093
+ */
3094
+ chunkPos,
3095
+ /**
3096
+ @internal
3097
+ */
3098
+ chunk,
3099
+ /**
3100
+ @internal
3101
+ */
3102
+ nextLayer = RangeSet.empty,
3103
+ /**
3104
+ @internal
3105
+ */
3106
+ maxPoint) {
3107
+ this.chunkPos = chunkPos;
3108
+ this.chunk = chunk;
3109
+ this.nextLayer = nextLayer;
3110
+ this.maxPoint = maxPoint;
3111
+ }
3112
+ /**
3113
+ @internal
3114
+ */
3115
+ get length() {
3116
+ let last = this.chunk.length - 1;
3117
+ return last < 0 ? 0 : Math.max(this.chunkEnd(last), this.nextLayer.length);
3118
+ }
3119
+ /**
3120
+ The number of ranges in the set.
3121
+ */
3122
+ get size() {
3123
+ if (this.isEmpty)
3124
+ return 0;
3125
+ let size = this.nextLayer.size;
3126
+ for (let chunk of this.chunk)
3127
+ size += chunk.value.length;
3128
+ return size;
3129
+ }
3130
+ /**
3131
+ @internal
3132
+ */
3133
+ chunkEnd(index) {
3134
+ return this.chunkPos[index] + this.chunk[index].length;
3135
+ }
3136
+ /**
3137
+ Update the range set, optionally adding new ranges or filtering
3138
+ out existing ones.
3139
+
3140
+ (Note: The type parameter is just there as a kludge to work
3141
+ around TypeScript variance issues that prevented `RangeSet<X>`
3142
+ from being a subtype of `RangeSet<Y>` when `X` is a subtype of
3143
+ `Y`.)
3144
+ */
3145
+ update(updateSpec) {
3146
+ let { add = [], sort = false, filterFrom = 0, filterTo = this.length } = updateSpec;
3147
+ let filter = updateSpec.filter;
3148
+ if (add.length == 0 && !filter)
3149
+ return this;
3150
+ if (sort)
3151
+ add = add.slice().sort(cmpRange);
3152
+ if (this.isEmpty)
3153
+ return add.length ? RangeSet.of(add) : this;
3154
+ let cur = new LayerCursor(this, null, -1).goto(0), i = 0, spill = [];
3155
+ let builder = new RangeSetBuilder();
3156
+ while (cur.value || i < add.length) {
3157
+ if (i < add.length && (cur.from - add[i].from || cur.startSide - add[i].value.startSide) >= 0) {
3158
+ let range = add[i++];
3159
+ if (!builder.addInner(range.from, range.to, range.value))
3160
+ spill.push(range);
3161
+ }
3162
+ else if (cur.rangeIndex == 1 && cur.chunkIndex < this.chunk.length &&
3163
+ (i == add.length || this.chunkEnd(cur.chunkIndex) < add[i].from) &&
3164
+ (!filter || filterFrom > this.chunkEnd(cur.chunkIndex) || filterTo < this.chunkPos[cur.chunkIndex]) &&
3165
+ builder.addChunk(this.chunkPos[cur.chunkIndex], this.chunk[cur.chunkIndex])) {
3166
+ cur.nextChunk();
3167
+ }
3168
+ else {
3169
+ if (!filter || filterFrom > cur.to || filterTo < cur.from || filter(cur.from, cur.to, cur.value)) {
3170
+ if (!builder.addInner(cur.from, cur.to, cur.value))
3171
+ spill.push(new Range(cur.from, cur.to, cur.value));
3172
+ }
3173
+ cur.next();
3174
+ }
3175
+ }
3176
+ return builder.finishInner(this.nextLayer.isEmpty && !spill.length ? RangeSet.empty
3177
+ : this.nextLayer.update({ add: spill, filter, filterFrom, filterTo }));
3178
+ }
3179
+ /**
3180
+ Map this range set through a set of changes, return the new set.
3181
+ */
3182
+ map(changes) {
3183
+ if (changes.empty || this.isEmpty)
3184
+ return this;
3185
+ let chunks = [], chunkPos = [], maxPoint = -1;
3186
+ for (let i = 0; i < this.chunk.length; i++) {
3187
+ let start = this.chunkPos[i], chunk = this.chunk[i];
3188
+ let touch = changes.touchesRange(start, start + chunk.length);
3189
+ if (touch === false) {
3190
+ maxPoint = Math.max(maxPoint, chunk.maxPoint);
3191
+ chunks.push(chunk);
3192
+ chunkPos.push(changes.mapPos(start));
3193
+ }
3194
+ else if (touch === true) {
3195
+ let { mapped, pos } = chunk.map(start, changes);
3196
+ if (mapped) {
3197
+ maxPoint = Math.max(maxPoint, mapped.maxPoint);
3198
+ chunks.push(mapped);
3199
+ chunkPos.push(pos);
3200
+ }
3201
+ }
3202
+ }
3203
+ let next = this.nextLayer.map(changes);
3204
+ return chunks.length == 0 ? next : new RangeSet(chunkPos, chunks, next, maxPoint);
3205
+ }
3206
+ /**
3207
+ Iterate over the ranges that touch the region `from` to `to`,
3208
+ calling `f` for each. There is no guarantee that the ranges will
3209
+ be reported in any specific order. When the callback returns
3210
+ `false`, iteration stops.
3211
+ */
3212
+ between(from, to, f) {
3213
+ if (this.isEmpty)
3214
+ return;
3215
+ for (let i = 0; i < this.chunk.length; i++) {
3216
+ let start = this.chunkPos[i], chunk = this.chunk[i];
3217
+ if (to >= start && from <= start + chunk.length &&
3218
+ chunk.between(start, from - start, to - start, f) === false)
3219
+ return;
3220
+ }
3221
+ this.nextLayer.between(from, to, f);
3222
+ }
3223
+ /**
3224
+ Iterate over the ranges in this set, in order, including all
3225
+ ranges that end at or after `from`.
3226
+ */
3227
+ iter(from = 0) {
3228
+ return HeapCursor.from([this]).goto(from);
3229
+ }
3230
+ /**
3231
+ @internal
3232
+ */
3233
+ get isEmpty() { return this.nextLayer == this; }
3234
+ /**
3235
+ Iterate over the ranges in a collection of sets, in order,
3236
+ starting from `from`.
3237
+ */
3238
+ static iter(sets, from = 0) {
3239
+ return HeapCursor.from(sets).goto(from);
3240
+ }
3241
+ /**
3242
+ Iterate over two groups of sets, calling methods on `comparator`
3243
+ to notify it of possible differences.
3244
+ */
3245
+ static compare(oldSets, newSets,
3246
+ /**
3247
+ This indicates how the underlying data changed between these
3248
+ ranges, and is needed to synchronize the iteration. `from` and
3249
+ `to` are coordinates in the _new_ space, after these changes.
3250
+ */
3251
+ textDiff, comparator,
3252
+ /**
3253
+ Can be used to ignore all non-point ranges, and points below
3254
+ the given size. When -1, all ranges are compared.
3255
+ */
3256
+ minPointSize = -1) {
3257
+ let a = oldSets.filter(set => set.maxPoint > 0 || !set.isEmpty && set.maxPoint >= minPointSize);
3258
+ let b = newSets.filter(set => set.maxPoint > 0 || !set.isEmpty && set.maxPoint >= minPointSize);
3259
+ let sharedChunks = findSharedChunks(a, b, textDiff);
3260
+ let sideA = new SpanCursor(a, sharedChunks, minPointSize);
3261
+ let sideB = new SpanCursor(b, sharedChunks, minPointSize);
3262
+ textDiff.iterGaps((fromA, fromB, length) => compare(sideA, fromA, sideB, fromB, length, comparator));
3263
+ if (textDiff.empty && textDiff.length == 0)
3264
+ compare(sideA, 0, sideB, 0, 0, comparator);
3265
+ }
3266
+ /**
3267
+ Compare the contents of two groups of range sets, returning true
3268
+ if they are equivalent in the given range.
3269
+ */
3270
+ static eq(oldSets, newSets, from = 0, to) {
3271
+ if (to == null)
3272
+ to = 1000000000 /* Far */;
3273
+ let a = oldSets.filter(set => !set.isEmpty && newSets.indexOf(set) < 0);
3274
+ let b = newSets.filter(set => !set.isEmpty && oldSets.indexOf(set) < 0);
3275
+ if (a.length != b.length)
3276
+ return false;
3277
+ if (!a.length)
3278
+ return true;
3279
+ let sharedChunks = findSharedChunks(a, b);
3280
+ let sideA = new SpanCursor(a, sharedChunks, 0).goto(from), sideB = new SpanCursor(b, sharedChunks, 0).goto(from);
3281
+ for (;;) {
3282
+ if (sideA.to != sideB.to ||
3283
+ !sameValues(sideA.active, sideB.active) ||
3284
+ sideA.point && (!sideB.point || !sideA.point.eq(sideB.point)))
3285
+ return false;
3286
+ if (sideA.to > to)
3287
+ return true;
3288
+ sideA.next();
3289
+ sideB.next();
3290
+ }
3291
+ }
3292
+ /**
3293
+ Iterate over a group of range sets at the same time, notifying
3294
+ the iterator about the ranges covering every given piece of
3295
+ content. Returns the open count (see
3296
+ [`SpanIterator.span`](https://codemirror.net/6/docs/ref/#state.SpanIterator.span)) at the end
3297
+ of the iteration.
3298
+ */
3299
+ static spans(sets, from, to, iterator,
3300
+ /**
3301
+ When given and greater than -1, only points of at least this
3302
+ size are taken into account.
3303
+ */
3304
+ minPointSize = -1) {
3305
+ let cursor = new SpanCursor(sets, null, minPointSize).goto(from), pos = from;
3306
+ let open = cursor.openStart;
3307
+ for (;;) {
3308
+ let curTo = Math.min(cursor.to, to);
3309
+ if (cursor.point) {
3310
+ iterator.point(pos, curTo, cursor.point, cursor.activeForPoint(cursor.to), open, cursor.pointRank);
3311
+ open = cursor.openEnd(curTo) + (cursor.to > curTo ? 1 : 0);
3312
+ }
3313
+ else if (curTo > pos) {
3314
+ iterator.span(pos, curTo, cursor.active, open);
3315
+ open = cursor.openEnd(curTo);
3316
+ }
3317
+ if (cursor.to > to)
3318
+ break;
3319
+ pos = cursor.to;
3320
+ cursor.next();
3321
+ }
3322
+ return open;
3323
+ }
3324
+ /**
3325
+ Create a range set for the given range or array of ranges. By
3326
+ default, this expects the ranges to be _sorted_ (by start
3327
+ position and, if two start at the same position,
3328
+ `value.startSide`). You can pass `true` as second argument to
3329
+ cause the method to sort them.
3330
+ */
3331
+ static of(ranges, sort = false) {
3332
+ let build = new RangeSetBuilder();
3333
+ for (let range of ranges instanceof Range ? [ranges] : sort ? lazySort(ranges) : ranges)
3334
+ build.add(range.from, range.to, range.value);
3335
+ return build.finish();
3336
+ }
3337
+ }
3338
+ /**
3339
+ The empty set of ranges.
3340
+ */
3341
+ RangeSet.empty = /*@__PURE__*/new RangeSet([], [], null, -1);
3342
+ function lazySort(ranges) {
3343
+ if (ranges.length > 1)
3344
+ for (let prev = ranges[0], i = 1; i < ranges.length; i++) {
3345
+ let cur = ranges[i];
3346
+ if (cmpRange(prev, cur) > 0)
3347
+ return ranges.slice().sort(cmpRange);
3348
+ prev = cur;
3349
+ }
3350
+ return ranges;
3351
+ }
3352
+ RangeSet.empty.nextLayer = RangeSet.empty;
3353
+ /**
3354
+ A range set builder is a data structure that helps build up a
3355
+ [range set](https://codemirror.net/6/docs/ref/#state.RangeSet) directly, without first allocating
3356
+ an array of [`Range`](https://codemirror.net/6/docs/ref/#state.Range) objects.
3357
+ */
3358
+ class RangeSetBuilder {
3359
+ /**
3360
+ Create an empty builder.
3361
+ */
3362
+ constructor() {
3363
+ this.chunks = [];
3364
+ this.chunkPos = [];
3365
+ this.chunkStart = -1;
3366
+ this.last = null;
3367
+ this.lastFrom = -1000000000 /* Far */;
3368
+ this.lastTo = -1000000000 /* Far */;
3369
+ this.from = [];
3370
+ this.to = [];
3371
+ this.value = [];
3372
+ this.maxPoint = -1;
3373
+ this.setMaxPoint = -1;
3374
+ this.nextLayer = null;
3375
+ }
3376
+ finishChunk(newArrays) {
3377
+ this.chunks.push(new Chunk(this.from, this.to, this.value, this.maxPoint));
3378
+ this.chunkPos.push(this.chunkStart);
3379
+ this.chunkStart = -1;
3380
+ this.setMaxPoint = Math.max(this.setMaxPoint, this.maxPoint);
3381
+ this.maxPoint = -1;
3382
+ if (newArrays) {
3383
+ this.from = [];
3384
+ this.to = [];
3385
+ this.value = [];
3386
+ }
3387
+ }
3388
+ /**
3389
+ Add a range. Ranges should be added in sorted (by `from` and
3390
+ `value.startSide`) order.
3391
+ */
3392
+ add(from, to, value) {
3393
+ if (!this.addInner(from, to, value))
3394
+ (this.nextLayer || (this.nextLayer = new RangeSetBuilder)).add(from, to, value);
3395
+ }
3396
+ /**
3397
+ @internal
3398
+ */
3399
+ addInner(from, to, value) {
3400
+ let diff = from - this.lastTo || value.startSide - this.last.endSide;
3401
+ if (diff <= 0 && (from - this.lastFrom || value.startSide - this.last.startSide) < 0)
3402
+ throw new Error("Ranges must be added sorted by `from` position and `startSide`");
3403
+ if (diff < 0)
3404
+ return false;
3405
+ if (this.from.length == 250 /* ChunkSize */)
3406
+ this.finishChunk(true);
3407
+ if (this.chunkStart < 0)
3408
+ this.chunkStart = from;
3409
+ this.from.push(from - this.chunkStart);
3410
+ this.to.push(to - this.chunkStart);
3411
+ this.last = value;
3412
+ this.lastFrom = from;
3413
+ this.lastTo = to;
3414
+ this.value.push(value);
3415
+ if (value.point)
3416
+ this.maxPoint = Math.max(this.maxPoint, to - from);
3417
+ return true;
3418
+ }
3419
+ /**
3420
+ @internal
3421
+ */
3422
+ addChunk(from, chunk) {
3423
+ if ((from - this.lastTo || chunk.value[0].startSide - this.last.endSide) < 0)
3424
+ return false;
3425
+ if (this.from.length)
3426
+ this.finishChunk(true);
3427
+ this.setMaxPoint = Math.max(this.setMaxPoint, chunk.maxPoint);
3428
+ this.chunks.push(chunk);
3429
+ this.chunkPos.push(from);
3430
+ let last = chunk.value.length - 1;
3431
+ this.last = chunk.value[last];
3432
+ this.lastFrom = chunk.from[last] + from;
3433
+ this.lastTo = chunk.to[last] + from;
3434
+ return true;
3435
+ }
3436
+ /**
3437
+ Finish the range set. Returns the new set. The builder can't be
3438
+ used anymore after this has been called.
3439
+ */
3440
+ finish() { return this.finishInner(RangeSet.empty); }
3441
+ /**
3442
+ @internal
3443
+ */
3444
+ finishInner(next) {
3445
+ if (this.from.length)
3446
+ this.finishChunk(false);
3447
+ if (this.chunks.length == 0)
3448
+ return next;
3449
+ let result = new RangeSet(this.chunkPos, this.chunks, this.nextLayer ? this.nextLayer.finishInner(next) : next, this.setMaxPoint);
3450
+ this.from = null; // Make sure further `add` calls produce errors
3451
+ return result;
3452
+ }
3453
+ }
3454
+ function findSharedChunks(a, b, textDiff) {
3455
+ let inA = new Map();
3456
+ for (let set of a)
3457
+ for (let i = 0; i < set.chunk.length; i++)
3458
+ if (set.chunk[i].maxPoint <= 0)
3459
+ inA.set(set.chunk[i], set.chunkPos[i]);
3460
+ let shared = new Set();
3461
+ for (let set of b)
3462
+ for (let i = 0; i < set.chunk.length; i++) {
3463
+ let known = inA.get(set.chunk[i]);
3464
+ if (known != null && (textDiff ? textDiff.mapPos(known) : known) == set.chunkPos[i] &&
3465
+ !(textDiff === null || textDiff === void 0 ? void 0 : textDiff.touchesRange(known, known + set.chunk[i].length)))
3466
+ shared.add(set.chunk[i]);
3467
+ }
3468
+ return shared;
3469
+ }
3470
+ class LayerCursor {
3471
+ constructor(layer, skip, minPoint, rank = 0) {
3472
+ this.layer = layer;
3473
+ this.skip = skip;
3474
+ this.minPoint = minPoint;
3475
+ this.rank = rank;
3476
+ }
3477
+ get startSide() { return this.value ? this.value.startSide : 0; }
3478
+ get endSide() { return this.value ? this.value.endSide : 0; }
3479
+ goto(pos, side = -1000000000 /* Far */) {
3480
+ this.chunkIndex = this.rangeIndex = 0;
3481
+ this.gotoInner(pos, side, false);
3482
+ return this;
3483
+ }
3484
+ gotoInner(pos, side, forward) {
3485
+ while (this.chunkIndex < this.layer.chunk.length) {
3486
+ let next = this.layer.chunk[this.chunkIndex];
3487
+ if (!(this.skip && this.skip.has(next) ||
3488
+ this.layer.chunkEnd(this.chunkIndex) < pos ||
3489
+ next.maxPoint < this.minPoint))
3490
+ break;
3491
+ this.chunkIndex++;
3492
+ forward = false;
3493
+ }
3494
+ if (this.chunkIndex < this.layer.chunk.length) {
3495
+ let rangeIndex = this.layer.chunk[this.chunkIndex].findIndex(pos - this.layer.chunkPos[this.chunkIndex], side, true);
3496
+ if (!forward || this.rangeIndex < rangeIndex)
3497
+ this.setRangeIndex(rangeIndex);
3498
+ }
3499
+ this.next();
3500
+ }
3501
+ forward(pos, side) {
3502
+ if ((this.to - pos || this.endSide - side) < 0)
3503
+ this.gotoInner(pos, side, true);
3504
+ }
3505
+ next() {
3506
+ for (;;) {
3507
+ if (this.chunkIndex == this.layer.chunk.length) {
3508
+ this.from = this.to = 1000000000 /* Far */;
3509
+ this.value = null;
3510
+ break;
3511
+ }
3512
+ else {
3513
+ let chunkPos = this.layer.chunkPos[this.chunkIndex], chunk = this.layer.chunk[this.chunkIndex];
3514
+ let from = chunkPos + chunk.from[this.rangeIndex];
3515
+ this.from = from;
3516
+ this.to = chunkPos + chunk.to[this.rangeIndex];
3517
+ this.value = chunk.value[this.rangeIndex];
3518
+ this.setRangeIndex(this.rangeIndex + 1);
3519
+ if (this.minPoint < 0 || this.value.point && this.to - this.from >= this.minPoint)
3520
+ break;
3521
+ }
3522
+ }
3523
+ }
3524
+ setRangeIndex(index) {
3525
+ if (index == this.layer.chunk[this.chunkIndex].value.length) {
3526
+ this.chunkIndex++;
3527
+ if (this.skip) {
3528
+ while (this.chunkIndex < this.layer.chunk.length && this.skip.has(this.layer.chunk[this.chunkIndex]))
3529
+ this.chunkIndex++;
3530
+ }
3531
+ this.rangeIndex = 0;
3532
+ }
3533
+ else {
3534
+ this.rangeIndex = index;
3535
+ }
3536
+ }
3537
+ nextChunk() {
3538
+ this.chunkIndex++;
3539
+ this.rangeIndex = 0;
3540
+ this.next();
3541
+ }
3542
+ compare(other) {
3543
+ return this.from - other.from || this.startSide - other.startSide || this.rank - other.rank ||
3544
+ this.to - other.to || this.endSide - other.endSide;
3545
+ }
3546
+ }
3547
+ class HeapCursor {
3548
+ constructor(heap) {
3549
+ this.heap = heap;
3550
+ }
3551
+ static from(sets, skip = null, minPoint = -1) {
3552
+ let heap = [];
3553
+ for (let i = 0; i < sets.length; i++) {
3554
+ for (let cur = sets[i]; !cur.isEmpty; cur = cur.nextLayer) {
3555
+ if (cur.maxPoint >= minPoint)
3556
+ heap.push(new LayerCursor(cur, skip, minPoint, i));
3557
+ }
3558
+ }
3559
+ return heap.length == 1 ? heap[0] : new HeapCursor(heap);
3560
+ }
3561
+ get startSide() { return this.value ? this.value.startSide : 0; }
3562
+ goto(pos, side = -1000000000 /* Far */) {
3563
+ for (let cur of this.heap)
3564
+ cur.goto(pos, side);
3565
+ for (let i = this.heap.length >> 1; i >= 0; i--)
3566
+ heapBubble(this.heap, i);
3567
+ this.next();
3568
+ return this;
3569
+ }
3570
+ forward(pos, side) {
3571
+ for (let cur of this.heap)
3572
+ cur.forward(pos, side);
3573
+ for (let i = this.heap.length >> 1; i >= 0; i--)
3574
+ heapBubble(this.heap, i);
3575
+ if ((this.to - pos || this.value.endSide - side) < 0)
3576
+ this.next();
3577
+ }
3578
+ next() {
3579
+ if (this.heap.length == 0) {
3580
+ this.from = this.to = 1000000000 /* Far */;
3581
+ this.value = null;
3582
+ this.rank = -1;
3583
+ }
3584
+ else {
3585
+ let top = this.heap[0];
3586
+ this.from = top.from;
3587
+ this.to = top.to;
3588
+ this.value = top.value;
3589
+ this.rank = top.rank;
3590
+ if (top.value)
3591
+ top.next();
3592
+ heapBubble(this.heap, 0);
3593
+ }
3594
+ }
3595
+ }
3596
+ function heapBubble(heap, index) {
3597
+ for (let cur = heap[index];;) {
3598
+ let childIndex = (index << 1) + 1;
3599
+ if (childIndex >= heap.length)
3600
+ break;
3601
+ let child = heap[childIndex];
3602
+ if (childIndex + 1 < heap.length && child.compare(heap[childIndex + 1]) >= 0) {
3603
+ child = heap[childIndex + 1];
3604
+ childIndex++;
3605
+ }
3606
+ if (cur.compare(child) < 0)
3607
+ break;
3608
+ heap[childIndex] = cur;
3609
+ heap[index] = child;
3610
+ index = childIndex;
3611
+ }
3612
+ }
3613
+ class SpanCursor {
3614
+ constructor(sets, skip, minPoint) {
3615
+ this.minPoint = minPoint;
3616
+ this.active = [];
3617
+ this.activeTo = [];
3618
+ this.activeRank = [];
3619
+ this.minActive = -1;
3620
+ // A currently active point range, if any
3621
+ this.point = null;
3622
+ this.pointFrom = 0;
3623
+ this.pointRank = 0;
3624
+ this.to = -1000000000 /* Far */;
3625
+ this.endSide = 0;
3626
+ this.openStart = -1;
3627
+ this.cursor = HeapCursor.from(sets, skip, minPoint);
3628
+ }
3629
+ goto(pos, side = -1000000000 /* Far */) {
3630
+ this.cursor.goto(pos, side);
3631
+ this.active.length = this.activeTo.length = this.activeRank.length = 0;
3632
+ this.minActive = -1;
3633
+ this.to = pos;
3634
+ this.endSide = side;
3635
+ this.openStart = -1;
3636
+ this.next();
3637
+ return this;
3638
+ }
3639
+ forward(pos, side) {
3640
+ while (this.minActive > -1 && (this.activeTo[this.minActive] - pos || this.active[this.minActive].endSide - side) < 0)
3641
+ this.removeActive(this.minActive);
3642
+ this.cursor.forward(pos, side);
3643
+ }
3644
+ removeActive(index) {
3645
+ remove(this.active, index);
3646
+ remove(this.activeTo, index);
3647
+ remove(this.activeRank, index);
3648
+ this.minActive = findMinIndex(this.active, this.activeTo);
3649
+ }
3650
+ addActive(trackOpen) {
3651
+ let i = 0, { value, to, rank } = this.cursor;
3652
+ while (i < this.activeRank.length && this.activeRank[i] <= rank)
3653
+ i++;
3654
+ insert(this.active, i, value);
3655
+ insert(this.activeTo, i, to);
3656
+ insert(this.activeRank, i, rank);
3657
+ if (trackOpen)
3658
+ insert(trackOpen, i, this.cursor.from);
3659
+ this.minActive = findMinIndex(this.active, this.activeTo);
3660
+ }
3661
+ // After calling this, if `this.point` != null, the next range is a
3662
+ // point. Otherwise, it's a regular range, covered by `this.active`.
3663
+ next() {
3664
+ let from = this.to, wasPoint = this.point;
3665
+ this.point = null;
3666
+ let trackOpen = this.openStart < 0 ? [] : null, trackExtra = 0;
3667
+ for (;;) {
3668
+ let a = this.minActive;
3669
+ if (a > -1 && (this.activeTo[a] - this.cursor.from || this.active[a].endSide - this.cursor.startSide) < 0) {
3670
+ if (this.activeTo[a] > from) {
3671
+ this.to = this.activeTo[a];
3672
+ this.endSide = this.active[a].endSide;
3673
+ break;
3674
+ }
3675
+ this.removeActive(a);
3676
+ if (trackOpen)
3677
+ remove(trackOpen, a);
3678
+ }
3679
+ else if (!this.cursor.value) {
3680
+ this.to = this.endSide = 1000000000 /* Far */;
3681
+ break;
3682
+ }
3683
+ else if (this.cursor.from > from) {
3684
+ this.to = this.cursor.from;
3685
+ this.endSide = this.cursor.startSide;
3686
+ break;
3687
+ }
3688
+ else {
3689
+ let nextVal = this.cursor.value;
3690
+ if (!nextVal.point) { // Opening a range
3691
+ this.addActive(trackOpen);
3692
+ this.cursor.next();
3693
+ }
3694
+ else if (wasPoint && this.cursor.to == this.to && this.cursor.from < this.cursor.to) {
3695
+ // Ignore any non-empty points that end precisely at the end of the prev point
3696
+ this.cursor.next();
3697
+ }
3698
+ else { // New point
3699
+ this.point = nextVal;
3700
+ this.pointFrom = this.cursor.from;
3701
+ this.pointRank = this.cursor.rank;
3702
+ this.to = this.cursor.to;
3703
+ this.endSide = nextVal.endSide;
3704
+ if (this.cursor.from < from)
3705
+ trackExtra = 1;
3706
+ this.cursor.next();
3707
+ this.forward(this.to, this.endSide);
3708
+ break;
3709
+ }
3710
+ }
3711
+ }
3712
+ if (trackOpen) {
3713
+ let openStart = 0;
3714
+ while (openStart < trackOpen.length && trackOpen[openStart] < from)
3715
+ openStart++;
3716
+ this.openStart = openStart + trackExtra;
3717
+ }
3718
+ }
3719
+ activeForPoint(to) {
3720
+ if (!this.active.length)
3721
+ return this.active;
3722
+ let active = [];
3723
+ for (let i = this.active.length - 1; i >= 0; i--) {
3724
+ if (this.activeRank[i] < this.pointRank)
3725
+ break;
3726
+ if (this.activeTo[i] > to || this.activeTo[i] == to && this.active[i].endSide >= this.point.endSide)
3727
+ active.push(this.active[i]);
3728
+ }
3729
+ return active.reverse();
3730
+ }
3731
+ openEnd(to) {
3732
+ let open = 0;
3733
+ for (let i = this.activeTo.length - 1; i >= 0 && this.activeTo[i] > to; i--)
3734
+ open++;
3735
+ return open;
3736
+ }
3737
+ }
3738
+ function compare(a, startA, b, startB, length, comparator) {
3739
+ a.goto(startA);
3740
+ b.goto(startB);
3741
+ let endB = startB + length;
3742
+ let pos = startB, dPos = startB - startA;
3743
+ for (;;) {
3744
+ let diff = (a.to + dPos) - b.to || a.endSide - b.endSide;
3745
+ let end = diff < 0 ? a.to + dPos : b.to, clipEnd = Math.min(end, endB);
3746
+ if (a.point || b.point) {
3747
+ if (!(a.point && b.point && (a.point == b.point || a.point.eq(b.point)) &&
3748
+ sameValues(a.activeForPoint(a.to + dPos), b.activeForPoint(b.to))))
3749
+ comparator.comparePoint(pos, clipEnd, a.point, b.point);
3750
+ }
3751
+ else {
3752
+ if (clipEnd > pos && !sameValues(a.active, b.active))
3753
+ comparator.compareRange(pos, clipEnd, a.active, b.active);
3754
+ }
3755
+ if (end > endB)
3756
+ break;
3757
+ pos = end;
3758
+ if (diff <= 0)
3759
+ a.next();
3760
+ if (diff >= 0)
3761
+ b.next();
3762
+ }
3763
+ }
3764
+ function sameValues(a, b) {
3765
+ if (a.length != b.length)
3766
+ return false;
3767
+ for (let i = 0; i < a.length; i++)
3768
+ if (a[i] != b[i] && !a[i].eq(b[i]))
3769
+ return false;
3770
+ return true;
3771
+ }
3772
+ function remove(array, index) {
3773
+ for (let i = index, e = array.length - 1; i < e; i++)
3774
+ array[i] = array[i + 1];
3775
+ array.pop();
3776
+ }
3777
+ function insert(array, index, value) {
3778
+ for (let i = array.length - 1; i >= index; i--)
3779
+ array[i + 1] = array[i];
3780
+ array[index] = value;
3781
+ }
3782
+ function findMinIndex(value, array) {
3783
+ let found = -1, foundPos = 1000000000 /* Far */;
3784
+ for (let i = 0; i < array.length; i++)
3785
+ if ((array[i] - foundPos || value[i].endSide - value[found].endSide) < 0) {
3786
+ found = i;
3787
+ foundPos = array[i];
3788
+ }
3789
+ return found;
3790
+ }
3791
+
3792
+ /**
3793
+ Count the column position at the given offset into the string,
3794
+ taking extending characters and tab size into account.
3795
+ */
3796
+ function countColumn(string, tabSize, to = string.length) {
3797
+ let n = 0;
3798
+ for (let i = 0; i < to;) {
3799
+ if (string.charCodeAt(i) == 9) {
3800
+ n += tabSize - (n % tabSize);
3801
+ i++;
3802
+ }
3803
+ else {
3804
+ n++;
3805
+ i = findClusterBreak(string, i);
3806
+ }
3807
+ }
3808
+ return n;
3809
+ }
3810
+ /**
3811
+ Find the offset that corresponds to the given column position in a
3812
+ string, taking extending characters and tab size into account. By
3813
+ default, the string length is returned when it is too short to
3814
+ reach the column. Pass `strict` true to make it return -1 in that
3815
+ situation.
3816
+ */
3817
+ function findColumn(string, col, tabSize, strict) {
3818
+ for (let i = 0, n = 0;;) {
3819
+ if (n >= col)
3820
+ return i;
3821
+ if (i == string.length)
3822
+ break;
3823
+ n += string.charCodeAt(i) == 9 ? tabSize - (n % tabSize) : 1;
3824
+ i = findClusterBreak(string, i);
3825
+ }
3826
+ return strict === true ? -1 : string.length;
3827
+ }
3828
+
3829
+ export { Annotation, AnnotationType, ChangeDesc, ChangeSet, CharCategory, Compartment, EditorSelection, EditorState, Facet, Line, MapMode, Prec, Range, RangeSet, RangeSetBuilder, RangeValue, SelectionRange, StateEffect, StateEffectType, StateField, Text, Transaction, codePointAt, codePointSize, combineConfig, countColumn, findClusterBreak, findColumn, fromCodePoint };