@codemirror/state 0.19.7 → 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
@@ -681,7 +1336,14 @@ class SelectionRange {
681
1336
  updated document.
682
1337
  */
683
1338
  map(change, assoc = -1) {
684
- let from = change.mapPos(this.from, assoc), to = change.mapPos(this.to, assoc);
1339
+ let from, to;
1340
+ if (this.empty) {
1341
+ from = to = change.mapPos(this.from, assoc);
1342
+ }
1343
+ else {
1344
+ from = change.mapPos(this.from, 1);
1345
+ to = change.mapPos(this.to, -1);
1346
+ }
685
1347
  return from == this.from && to == this.to ? this : new SelectionRange(from, to, this.flags);
686
1348
  }
687
1349
  /**
@@ -864,10 +1526,10 @@ A facet is a labeled value that is associated with an editor
864
1526
  state. It takes inputs from any number of extensions, and combines
865
1527
  those into a single output value.
866
1528
 
867
- Examples of facets are the [theme](https://codemirror.net/6/docs/ref/#view.EditorView^theme) styles
868
- associated with an editor or the [tab
869
- size](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) (which is reduced to a single
870
- 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).
871
1533
  */
872
1534
  class Facet {
873
1535
  constructor(
@@ -905,7 +1567,7 @@ class Facet {
905
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);
906
1568
  }
907
1569
  /**
908
- Returns an extension that adds the given value for this facet.
1570
+ Returns an extension that adds the given value to this facet.
909
1571
  */
910
1572
  of(value) {
911
1573
  return new FacetProvider([], this, 0 /* Static */, value);
@@ -916,9 +1578,8 @@ class Facet {
916
1578
  this value depends on, since your function is only called again
917
1579
  for a new state when one of those parts changed.
918
1580
 
919
- In most cases, you'll want to use the
920
- [`provide`](https://codemirror.net/6/docs/ref/#state.StateField^define^config.provide) option when
921
- 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.
922
1583
  */
923
1584
  compute(deps, get) {
924
1585
  if (this.isStatic)
@@ -955,7 +1616,7 @@ class FacetProvider {
955
1616
  var _a;
956
1617
  let getter = this.value;
957
1618
  let compare = this.facet.compareInput;
958
- let idx = addresses[this.id] >> 1, multi = this.type == 2 /* Multi */;
1619
+ let id = this.id, idx = addresses[id] >> 1, multi = this.type == 2 /* Multi */;
959
1620
  let depDoc = false, depSel = false, depAddrs = [];
960
1621
  for (let dep of this.dependencies) {
961
1622
  if (dep == "doc")
@@ -965,24 +1626,37 @@ class FacetProvider {
965
1626
  else if ((((_a = addresses[dep.id]) !== null && _a !== void 0 ? _a : 1) & 1) == 0)
966
1627
  depAddrs.push(addresses[dep.id]);
967
1628
  }
968
- return (state, tr) => {
969
- let oldVal = state.values[idx];
970
- if (oldVal === Uninitialized) {
1629
+ return {
1630
+ create(state) {
971
1631
  state.values[idx] = getter(state);
972
1632
  return 1 /* Changed */;
973
- }
974
- if (tr) {
975
- let depChanged = (depDoc && tr.docChanged) || (depSel && (tr.docChanged || tr.selection)) ||
976
- depAddrs.some(addr => (ensureAddr(state, addr) & 1 /* Changed */) > 0);
977
- if (depChanged) {
1633
+ },
1634
+ update(state, tr) {
1635
+ if ((depDoc && tr.docChanged) || (depSel && (tr.docChanged || tr.selection)) || ensureAll(state, depAddrs)) {
978
1636
  let newVal = getter(state);
979
- if (multi ? !compareArray(newVal, oldVal, compare) : !compare(newVal, oldVal)) {
1637
+ if (multi ? !compareArray(newVal, state.values[idx], compare) : !compare(newVal, state.values[idx])) {
980
1638
  state.values[idx] = newVal;
981
1639
  return 1 /* Changed */;
982
1640
  }
983
1641
  }
1642
+ return 0;
1643
+ },
1644
+ reconfigure: (state, oldState) => {
1645
+ let newVal = getter(state);
1646
+ let oldAddr = oldState.config.address[id];
1647
+ if (oldAddr != null) {
1648
+ let oldVal = getAddr(oldState, oldAddr);
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))) {
1653
+ state.values[idx] = oldVal;
1654
+ return 0;
1655
+ }
1656
+ }
1657
+ state.values[idx] = newVal;
1658
+ return 1 /* Changed */;
984
1659
  }
985
- return 0;
986
1660
  };
987
1661
  }
988
1662
  }
@@ -994,19 +1668,19 @@ function compareArray(a, b, compare) {
994
1668
  return false;
995
1669
  return true;
996
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
+ }
997
1678
  function dynamicFacetSlot(addresses, facet, providers) {
998
1679
  let providerAddrs = providers.map(p => addresses[p.id]);
999
1680
  let providerTypes = providers.map(p => p.type);
1000
1681
  let dynamic = providerAddrs.filter(p => !(p & 1));
1001
1682
  let idx = addresses[facet.id] >> 1;
1002
- return (state, tr) => {
1003
- let oldVal = state.values[idx], changed = oldVal === Uninitialized;
1004
- for (let dynAddr of dynamic) {
1005
- if (ensureAddr(state, dynAddr) & 1 /* Changed */)
1006
- changed = true;
1007
- }
1008
- if (!changed)
1009
- return 0;
1683
+ function get(state) {
1010
1684
  let values = [];
1011
1685
  for (let i = 0; i < providerAddrs.length; i++) {
1012
1686
  let value = getAddr(state, providerAddrs[i]);
@@ -1016,11 +1690,39 @@ function dynamicFacetSlot(addresses, facet, providers) {
1016
1690
  else
1017
1691
  values.push(value);
1018
1692
  }
1019
- let value = facet.combine(values);
1020
- if (oldVal !== Uninitialized && facet.compare(value, oldVal))
1021
- return 0;
1022
- state.values[idx] = value;
1023
- return 1 /* Changed */;
1693
+ return facet.combine(values);
1694
+ }
1695
+ return {
1696
+ create(state) {
1697
+ for (let addr of providerAddrs)
1698
+ ensureAddr(state, addr);
1699
+ state.values[idx] = get(state);
1700
+ return 1 /* Changed */;
1701
+ },
1702
+ update(state, tr) {
1703
+ if (!ensureAll(state, dynamic))
1704
+ return 0;
1705
+ let value = get(state);
1706
+ if (facet.compare(value, state.values[idx]))
1707
+ return 0;
1708
+ state.values[idx] = value;
1709
+ return 1 /* Changed */;
1710
+ },
1711
+ reconfigure(state, oldState) {
1712
+ let depChanged = ensureAll(state, providerAddrs);
1713
+ let oldProviders = oldState.config.facets[facet.id], oldValue = oldState.facet(facet);
1714
+ if (oldProviders && !depChanged && sameArray(providers, oldProviders)) {
1715
+ state.values[idx] = oldValue;
1716
+ return 0;
1717
+ }
1718
+ let value = get(state);
1719
+ if (facet.compare(value, oldValue)) {
1720
+ state.values[idx] = oldValue;
1721
+ return 0;
1722
+ }
1723
+ state.values[idx] = value;
1724
+ return 1 /* Changed */;
1725
+ }
1024
1726
  };
1025
1727
  }
1026
1728
  const initField = /*@__PURE__*/Facet.define({ static: true });
@@ -1066,20 +1768,27 @@ class StateField {
1066
1768
  */
1067
1769
  slot(addresses) {
1068
1770
  let idx = addresses[this.id] >> 1;
1069
- return (state, tr) => {
1070
- let oldVal = state.values[idx];
1071
- if (oldVal === Uninitialized) {
1771
+ return {
1772
+ create: (state) => {
1072
1773
  state.values[idx] = this.create(state);
1073
1774
  return 1 /* Changed */;
1074
- }
1075
- if (tr) {
1775
+ },
1776
+ update: (state, tr) => {
1777
+ let oldVal = state.values[idx];
1076
1778
  let value = this.updateF(oldVal, tr);
1077
- if (!this.compareF(oldVal, value)) {
1078
- state.values[idx] = value;
1079
- return 1 /* Changed */;
1779
+ if (this.compareF(oldVal, value))
1780
+ return 0;
1781
+ state.values[idx] = value;
1782
+ return 1 /* Changed */;
1783
+ },
1784
+ reconfigure: (state, oldState) => {
1785
+ if (oldState.config.address[this.id] != null) {
1786
+ state.values[idx] = oldState.field(this);
1787
+ return 0;
1080
1788
  }
1789
+ state.values[idx] = this.create(state);
1790
+ return 1 /* Changed */;
1081
1791
  }
1082
- return 0;
1083
1792
  };
1084
1793
  }
1085
1794
  /**
@@ -1113,42 +1822,29 @@ precedence and then by order within each precedence.
1113
1822
  */
1114
1823
  const Prec = {
1115
1824
  /**
1116
- The lowest precedence level. Meant for things that should end up
1117
- near the end of the extension order.
1118
- */
1119
- lowest: /*@__PURE__*/prec(Prec_.lowest),
1120
- /**
1121
- A lower-than-default precedence, for extensions.
1122
- */
1123
- low: /*@__PURE__*/prec(Prec_.low),
1124
- /**
1125
- The default precedence, which is also used for extensions
1126
- without an explicit precedence.
1825
+ The highest precedence level, for extensions that should end up
1826
+ near the start of the precedence ordering.
1127
1827
  */
1128
- default: /*@__PURE__*/prec(Prec_.default),
1828
+ highest: /*@__PURE__*/prec(Prec_.highest),
1129
1829
  /**
1130
1830
  A higher-than-default precedence, for extensions that should
1131
1831
  come before those with default precedence.
1132
1832
  */
1133
1833
  high: /*@__PURE__*/prec(Prec_.high),
1134
1834
  /**
1135
- The highest precedence level, for extensions that should end up
1136
- near the start of the precedence ordering.
1137
- */
1138
- highest: /*@__PURE__*/prec(Prec_.highest),
1139
- // FIXME Drop these in some future breaking version
1140
- /**
1141
- Backwards-compatible synonym for `Prec.lowest`.
1835
+ The default precedence, which is also used for extensions
1836
+ without an explicit precedence.
1142
1837
  */
1143
- fallback: /*@__PURE__*/prec(Prec_.lowest),
1838
+ default: /*@__PURE__*/prec(Prec_.default),
1144
1839
  /**
1145
- Backwards-compatible synonym for `Prec.high`.
1840
+ A lower-than-default precedence.
1146
1841
  */
1147
- extend: /*@__PURE__*/prec(Prec_.high),
1842
+ low: /*@__PURE__*/prec(Prec_.low),
1148
1843
  /**
1149
- 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.
1150
1846
  */
1151
- override: /*@__PURE__*/prec(Prec_.highest)
1847
+ lowest: /*@__PURE__*/prec(Prec_.lowest)
1152
1848
  };
1153
1849
  class PrecExtension {
1154
1850
  constructor(inner, prec) {
@@ -1219,44 +1915,26 @@ class Configuration {
1219
1915
  let address = Object.create(null);
1220
1916
  let staticValues = [];
1221
1917
  let dynamicSlots = [];
1222
- let dynamicValues = [];
1223
1918
  for (let field of fields) {
1224
1919
  address[field.id] = dynamicSlots.length << 1;
1225
1920
  dynamicSlots.push(a => field.slot(a));
1226
- dynamicValues.push(oldState && oldState.config.address[field.id] != null ? oldState.field(field) : Uninitialized);
1227
1921
  }
1228
- let canReuseCache = new Map;
1229
- let canReuseDep = (dep) => {
1230
- if (!(dep instanceof Facet))
1231
- return true;
1232
- let cached = canReuseCache.get(dep);
1233
- if (cached != null)
1234
- return cached;
1235
- canReuseCache.set(dep, false);
1236
- if (!oldFacets || !sameArray(oldFacets[dep.id] || [], facets[dep.id] || []))
1237
- return;
1238
- for (let input of facets[dep.id] || [])
1239
- if (!input.dependencies.every(canReuseDep))
1240
- return;
1241
- canReuseCache.set(dep, true);
1242
- };
1243
1922
  let oldFacets = oldState === null || oldState === void 0 ? void 0 : oldState.config.facets;
1244
1923
  for (let id in facets) {
1245
1924
  let providers = facets[id], facet = providers[0].facet;
1246
1925
  let oldProviders = oldFacets && oldFacets[id] || [];
1247
- let canReuse = sameArray(providers, oldProviders);
1248
1926
  if (providers.every(p => p.type == 0 /* Static */)) {
1249
1927
  address[facet.id] = (staticValues.length << 1) | 1;
1250
- let value = canReuse ? oldState.facet(facet) : facet.combine(providers.map(p => p.value)), oldValue;
1251
- if (!canReuse && oldState && facet.compare(value, oldValue = oldState.facet(facet)))
1252
- value = oldValue;
1253
- staticValues.push(value);
1928
+ if (sameArray(oldProviders, providers)) {
1929
+ staticValues.push(oldState.facet(facet));
1930
+ }
1931
+ else {
1932
+ let value = facet.combine(providers.map(p => p.value));
1933
+ staticValues.push(oldState && facet.compare(value, oldState.facet(facet)) ? oldState.facet(facet) : value);
1934
+ }
1254
1935
  }
1255
1936
  else {
1256
1937
  for (let p of providers) {
1257
- let canReuseThis = p.dependencies.every(canReuseDep);
1258
- if (!canReuseThis)
1259
- canReuse = false;
1260
1938
  if (p.type == 0 /* Static */) {
1261
1939
  address[p.id] = (staticValues.length << 1) | 1;
1262
1940
  staticValues.push(p.value);
@@ -1264,20 +1942,14 @@ class Configuration {
1264
1942
  else {
1265
1943
  address[p.id] = dynamicSlots.length << 1;
1266
1944
  dynamicSlots.push(a => p.dynamicSlot(a));
1267
- let oldAddr = oldState && canReuseThis ? oldState.config.address[p.id] : null;
1268
- dynamicValues.push(oldAddr != null ? getAddr(oldState, oldAddr) : Uninitialized);
1269
1945
  }
1270
1946
  }
1271
1947
  address[facet.id] = dynamicSlots.length << 1;
1272
1948
  dynamicSlots.push(a => dynamicFacetSlot(a, facet, providers));
1273
- dynamicValues.push(canReuse || oldProviders.length ? oldState.facet(facet) : Uninitialized);
1274
1949
  }
1275
1950
  }
1276
1951
  let dynamic = dynamicSlots.map(f => f(address));
1277
- return {
1278
- configuration: new Configuration(base, newCompartments, dynamic, address, staticValues, facets),
1279
- values: dynamicValues
1280
- };
1952
+ return new Configuration(base, newCompartments, dynamic, address, staticValues, facets);
1281
1953
  }
1282
1954
  }
1283
1955
  function flatten(extension, compartments, newCompartments) {
@@ -1286,7 +1958,7 @@ function flatten(extension, compartments, newCompartments) {
1286
1958
  function inner(ext, prec) {
1287
1959
  let known = seen.get(ext);
1288
1960
  if (known != null) {
1289
- if (known >= prec)
1961
+ if (known <= prec)
1290
1962
  return;
1291
1963
  let found = result[known].indexOf(ext);
1292
1964
  if (found > -1)
@@ -1329,7 +2001,6 @@ function flatten(extension, compartments, newCompartments) {
1329
2001
  inner(extension, Prec_.default);
1330
2002
  return result.reduce((a, b) => a.concat(b));
1331
2003
  }
1332
- const Uninitialized = {};
1333
2004
  function ensureAddr(state, addr) {
1334
2005
  if (addr & 1)
1335
2006
  return 2 /* Computed */;
@@ -1340,7 +2011,7 @@ function ensureAddr(state, addr) {
1340
2011
  if (status & 2 /* Computed */)
1341
2012
  return status;
1342
2013
  state.status[idx] = 4 /* Computing */;
1343
- let changed = state.config.dynamicSlots[idx](state, state.applying);
2014
+ let changed = state.computeSlot(state, state.config.dynamicSlots[idx]);
1344
2015
  return state.status[idx] = 2 /* Computed */ | changed;
1345
2016
  }
1346
2017
  function getAddr(state, addr) {
@@ -1502,7 +2173,9 @@ Changes to the editor state are grouped into transactions.
1502
2173
  Typically, a user action creates a single transaction, which may
1503
2174
  contain any number of document changes, may change the selection,
1504
2175
  or have other effects. Create a transaction by calling
1505
- [`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).
1506
2179
  */
1507
2180
  class Transaction {
1508
2181
  /**
@@ -1576,7 +2249,7 @@ class Transaction {
1576
2249
  }
1577
2250
  /**
1578
2251
  The new state created by the transaction. Computed on demand
1579
- (but retained for subsequent access), so itis recommended not to
2252
+ (but retained for subsequent access), so it is recommended not to
1580
2253
  access it in [transaction
1581
2254
  filters](https://codemirror.net/6/docs/ref/#state.EditorState^transactionFilter) when possible.
1582
2255
  */
@@ -1618,7 +2291,8 @@ class Transaction {
1618
2291
  }
1619
2292
  }
1620
2293
  /**
1621
- Annotation used to store transaction timestamps.
2294
+ Annotation used to store transaction timestamps. Automatically
2295
+ added to every transaction, holding `Date.now()`.
1622
2296
  */
1623
2297
  Transaction.time = /*@__PURE__*/Annotation.define();
1624
2298
  /**
@@ -1858,24 +2532,20 @@ class EditorState {
1858
2532
  /**
1859
2533
  @internal
1860
2534
  */
1861
- values, tr = null) {
2535
+ values, computeSlot, tr) {
1862
2536
  this.config = config;
1863
2537
  this.doc = doc;
1864
2538
  this.selection = selection;
1865
2539
  this.values = values;
1866
- /**
1867
- @internal
1868
- */
1869
- this.applying = null;
1870
2540
  this.status = config.statusTemplate.slice();
1871
- this.applying = tr;
2541
+ this.computeSlot = computeSlot;
1872
2542
  // Fill in the computed state immediately, so that further queries
1873
2543
  // for it made during the update return this state
1874
2544
  if (tr)
1875
2545
  tr._state = this;
1876
2546
  for (let i = 0; i < this.config.dynamicSlots.length; i++)
1877
2547
  ensureAddr(this, i << 1);
1878
- this.applying = null;
2548
+ this.computeSlot = null;
1879
2549
  }
1880
2550
  field(field, require = true) {
1881
2551
  let addr = this.config.address[field.id];
@@ -1930,15 +2600,14 @@ class EditorState {
1930
2600
  }
1931
2601
  let startValues;
1932
2602
  if (!conf) {
1933
- let resolved = Configuration.resolve(base, compartments, this);
1934
- conf = resolved.configuration;
1935
- let intermediateState = new EditorState(conf, this.doc, this.selection, resolved.values, null);
2603
+ conf = Configuration.resolve(base, compartments, this);
2604
+ let intermediateState = new EditorState(conf, this.doc, this.selection, conf.dynamicSlots.map(() => null), (state, slot) => slot.reconfigure(state, this), null);
1936
2605
  startValues = intermediateState.values;
1937
2606
  }
1938
2607
  else {
1939
2608
  startValues = tr.startState.values.slice();
1940
2609
  }
1941
- new EditorState(conf, tr.newDoc, tr.newSelection, startValues, tr);
2610
+ new EditorState(conf, tr.newDoc, tr.newSelection, startValues, (state, slot) => slot.update(state, tr), tr);
1942
2611
  }
1943
2612
  /**
1944
2613
  Create a [transaction spec](https://codemirror.net/6/docs/ref/#state.TransactionSpec) that
@@ -1995,7 +2664,7 @@ class EditorState {
1995
2664
  /**
1996
2665
  Using the state's [line
1997
2666
  separator](https://codemirror.net/6/docs/ref/#state.EditorState^lineSeparator), create a
1998
- [`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.
1999
2668
  */
2000
2669
  toText(string) {
2001
2670
  return Text.of(string.split(this.facet(EditorState.lineSeparator) || DefaultSplit));
@@ -2062,7 +2731,7 @@ class EditorState {
2062
2731
  transactions.
2063
2732
  */
2064
2733
  static create(config = {}) {
2065
- let { configuration, values } = Configuration.resolve(config.extensions || [], new Map);
2734
+ let configuration = Configuration.resolve(config.extensions || [], new Map);
2066
2735
  let doc = config.doc instanceof Text ? config.doc
2067
2736
  : Text.of((config.doc || "").split(configuration.staticFacet(EditorState.lineSeparator) || DefaultSplit));
2068
2737
  let selection = !config.selection ? EditorSelection.single(0)
@@ -2071,7 +2740,7 @@ class EditorState {
2071
2740
  checkSelection(selection, doc.length);
2072
2741
  if (!configuration.staticFacet(allowMultipleSelections))
2073
2742
  selection = selection.asSingle();
2074
- return new EditorState(configuration, doc, selection, values);
2743
+ return new EditorState(configuration, doc, selection, configuration.dynamicSlots.map(() => null), (state, slot) => slot.create(state), null);
2075
2744
  }
2076
2745
  /**
2077
2746
  The size (in columns) of a tab in the document, determined by
@@ -2115,7 +2784,7 @@ class EditorState {
2115
2784
  }
2116
2785
  /**
2117
2786
  Return a function that can categorize strings (expected to
2118
- 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))
2119
2788
  into one of:
2120
2789
 
2121
2790
  - Word (contains an alphanumeric character or a character
@@ -2199,7 +2868,12 @@ Registers translation phrases. The
2199
2868
  all objects registered with this facet to find translations for
2200
2869
  its argument.
2201
2870
  */
2202
- 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
+ });
2203
2877
  /**
2204
2878
  A facet used to register [language
2205
2879
  data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) providers.
@@ -2215,7 +2889,7 @@ Such a function can return `true` to indicate that it doesn't
2215
2889
  want to do anything, `false` to completely stop the changes in
2216
2890
  the transaction, or a set of ranges in which changes should be
2217
2891
  suppressed. Such ranges are represented as an array of numbers,
2218
- 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
2219
2893
  range. So for example `[10, 20, 100, 110]` suppresses changes
2220
2894
  between 10 and 20, and between 100 and 110.
2221
2895
  */
@@ -2246,19 +2920,22 @@ This is a more limited form of
2246
2920
  which can only add
2247
2921
  [annotations](https://codemirror.net/6/docs/ref/#state.TransactionSpec.annotations) and
2248
2922
  [effects](https://codemirror.net/6/docs/ref/#state.TransactionSpec.effects). _But_, this type
2249
- of filter runs even the transaction has disabled regular
2923
+ of filter runs even if the transaction has disabled regular
2250
2924
  [filtering](https://codemirror.net/6/docs/ref/#state.TransactionSpec.filter), making it suitable
2251
2925
  for effects that don't need to touch the changes or selection,
2252
2926
  but do want to process every transaction.
2253
2927
 
2254
- Extenders run _after_ filters, when both are applied.
2928
+ Extenders run _after_ filters, when both are present.
2255
2929
  */
2256
2930
  EditorState.transactionExtender = transactionExtender;
2257
2931
  Compartment.reconfigure = /*@__PURE__*/StateEffect.define();
2258
2932
 
2259
2933
  /**
2260
2934
  Utility function for combining behaviors to fill in a config
2261
- 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
2262
2939
  when a field gets two values that aren't `===`-equal, but you can
2263
2940
  provide combine functions per field to do something else.
2264
2941
  */
@@ -2282,4 +2959,871 @@ combine = {}) {
2282
2959
  return result;
2283
2960
  }
2284
2961
 
2285
- 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 };