@pyscript/core 0.6.7 → 0.6.9

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.
Files changed (81) hide show
  1. package/dist/codemirror-1dOfKU91.js +1832 -0
  2. package/dist/{codemirror-BFaBdIfQ.js.map → codemirror-1dOfKU91.js.map} +1 -1
  3. package/dist/codemirror_commands-CKMxnUcp.js +1714 -0
  4. package/dist/codemirror_commands-CKMxnUcp.js.map +1 -0
  5. package/dist/codemirror_lang-python-D9DYtDWr.js +2462 -0
  6. package/dist/{codemirror_lang-python-DvjhpaK2.js.map → codemirror_lang-python-D9DYtDWr.js.map} +1 -1
  7. package/dist/codemirror_language-B5lTPv2c.js +5337 -0
  8. package/dist/{codemirror_language-4kk4Aqm3.js.map → codemirror_language-B5lTPv2c.js.map} +1 -1
  9. package/dist/codemirror_state-n1sFh2hh.js +3930 -0
  10. package/dist/{codemirror_state-1d1uncXx.js.map → codemirror_state-n1sFh2hh.js.map} +1 -1
  11. package/dist/codemirror_view-1a1gCrKZ.js +11428 -0
  12. package/dist/{codemirror_view-aIuSN42d.js.map → codemirror_view-1a1gCrKZ.js.map} +1 -1
  13. package/dist/core-D2ytnMSk.js +4349 -0
  14. package/dist/core-D2ytnMSk.js.map +1 -0
  15. package/dist/core.css +75 -1
  16. package/dist/core.js +1 -1
  17. package/dist/deprecations-manager-Cf_aFUPK.js +29 -0
  18. package/dist/{deprecations-manager-BWDz_-4h.js.map → deprecations-manager-Cf_aFUPK.js.map} +1 -1
  19. package/dist/donkey-Cc8svH2Q.js +170 -0
  20. package/dist/{donkey-DvIAJuTW.js.map → donkey-Cc8svH2Q.js.map} +1 -1
  21. package/dist/error-DWoeKPDI.js +59 -0
  22. package/dist/{error-CVKnHrMr.js.map → error-DWoeKPDI.js.map} +1 -1
  23. package/dist/index-BVFrv_QY.js +1911 -0
  24. package/dist/{index-DqgFLybq.js.map → index-BVFrv_QY.js.map} +1 -1
  25. package/dist/mpy-CQiuZcjW.js +255 -0
  26. package/dist/{mpy-DoOh7bKP.js.map → mpy-CQiuZcjW.js.map} +1 -1
  27. package/dist/py-BgH4mprx.js +182 -0
  28. package/dist/{py-D401PjM8.js.map → py-BgH4mprx.js.map} +1 -1
  29. package/dist/py-editor-Dzsa3rsF.js +433 -0
  30. package/dist/{py-editor-Dxs1clgy.js.map → py-editor-Dzsa3rsF.js.map} +1 -1
  31. package/dist/py-terminal-BX_wPoTi.js +61 -0
  32. package/dist/{py-terminal-eUGMSqwz.js.map → py-terminal-BX_wPoTi.js.map} +1 -1
  33. package/dist/storage.js +421 -1
  34. package/dist/storage.js.map +1 -1
  35. package/dist/toml-CkEFU7ly.js +103 -0
  36. package/dist/{toml-DiUM0_qs.js.map → toml-CkEFU7ly.js.map} +1 -1
  37. package/dist/toml-DTuPg5as.js +5 -0
  38. package/dist/toml-DTuPg5as.js.map +1 -0
  39. package/dist/xterm-eU6F6YKE.js +10 -0
  40. package/dist/xterm-eU6F6YKE.js.map +1 -0
  41. package/dist/xterm-readline-C7HDczqS.js +501 -0
  42. package/dist/xterm-readline-C7HDczqS.js.map +1 -0
  43. package/dist/xterm_addon-fit-Cotzxdkz.js +10 -0
  44. package/dist/xterm_addon-fit-Cotzxdkz.js.map +1 -0
  45. package/dist/xterm_addon-web-links-DoNfCjeX.js +10 -0
  46. package/dist/xterm_addon-web-links-DoNfCjeX.js.map +1 -0
  47. package/dist/zip-Cj6c-tjp.js +13 -0
  48. package/dist/zip-Cj6c-tjp.js.map +1 -0
  49. package/package.json +5 -5
  50. package/src/core.js +2 -2
  51. package/src/stdlib/pyscript.js +13 -13
  52. package/dist/codemirror-BFaBdIfQ.js +0 -2
  53. package/dist/codemirror_commands-CvnouHng.js +0 -2
  54. package/dist/codemirror_commands-CvnouHng.js.map +0 -1
  55. package/dist/codemirror_lang-python-DvjhpaK2.js +0 -2
  56. package/dist/codemirror_language-4kk4Aqm3.js +0 -2
  57. package/dist/codemirror_state-1d1uncXx.js +0 -2
  58. package/dist/codemirror_view-aIuSN42d.js +0 -2
  59. package/dist/core-S0XBQFwV.js +0 -2
  60. package/dist/core-S0XBQFwV.js.map +0 -1
  61. package/dist/deprecations-manager-BWDz_-4h.js +0 -2
  62. package/dist/donkey-DvIAJuTW.js +0 -2
  63. package/dist/error-CVKnHrMr.js +0 -2
  64. package/dist/index-DqgFLybq.js +0 -2
  65. package/dist/mpy-DoOh7bKP.js +0 -2
  66. package/dist/py-D401PjM8.js +0 -2
  67. package/dist/py-editor-Dxs1clgy.js +0 -2
  68. package/dist/py-terminal-eUGMSqwz.js +0 -2
  69. package/dist/toml-CvAfdf9_.js +0 -3
  70. package/dist/toml-CvAfdf9_.js.map +0 -1
  71. package/dist/toml-DiUM0_qs.js +0 -3
  72. package/dist/xterm-BY7uk_OU.js +0 -2
  73. package/dist/xterm-BY7uk_OU.js.map +0 -1
  74. package/dist/xterm-readline-CZfBw7ic.js +0 -2
  75. package/dist/xterm-readline-CZfBw7ic.js.map +0 -1
  76. package/dist/xterm_addon-fit--gyF3PcZ.js +0 -2
  77. package/dist/xterm_addon-fit--gyF3PcZ.js.map +0 -1
  78. package/dist/xterm_addon-web-links-Cnej-nJ6.js +0 -2
  79. package/dist/xterm_addon-web-links-Cnej-nJ6.js.map +0 -1
  80. package/dist/zip-Bf48tRr5.js +0 -2
  81. package/dist/zip-Bf48tRr5.js.map +0 -1
@@ -0,0 +1,3930 @@
1
+ /**
2
+ The data structure for documents. @nonabstract
3
+ */
4
+ class Text {
5
+ /**
6
+ Get the line description around the given position.
7
+ */
8
+ lineAt(pos) {
9
+ if (pos < 0 || pos > this.length)
10
+ throw new RangeError(`Invalid position ${pos} in document of length ${this.length}`);
11
+ return this.lineInner(pos, false, 1, 0);
12
+ }
13
+ /**
14
+ Get the description for the given (1-based) line number.
15
+ */
16
+ line(n) {
17
+ if (n < 1 || n > this.lines)
18
+ throw new RangeError(`Invalid line number ${n} in ${this.lines}-line document`);
19
+ return this.lineInner(n, true, 1, 0);
20
+ }
21
+ /**
22
+ Replace a range of the text with the given content.
23
+ */
24
+ replace(from, to, text) {
25
+ [from, to] = clip(this, from, to);
26
+ let parts = [];
27
+ this.decompose(0, from, parts, 2 /* Open.To */);
28
+ if (text.length)
29
+ text.decompose(0, text.length, parts, 1 /* Open.From */ | 2 /* Open.To */);
30
+ this.decompose(to, this.length, parts, 1 /* Open.From */);
31
+ return TextNode.from(parts, this.length - (to - from) + text.length);
32
+ }
33
+ /**
34
+ Append another document to this one.
35
+ */
36
+ append(other) {
37
+ return this.replace(this.length, this.length, other);
38
+ }
39
+ /**
40
+ Retrieve the text between the given points.
41
+ */
42
+ slice(from, to = this.length) {
43
+ [from, to] = clip(this, from, to);
44
+ let parts = [];
45
+ this.decompose(from, to, parts, 0);
46
+ return TextNode.from(parts, to - from);
47
+ }
48
+ /**
49
+ Test whether this text is equal to another instance.
50
+ */
51
+ eq(other) {
52
+ if (other == this)
53
+ return true;
54
+ if (other.length != this.length || other.lines != this.lines)
55
+ return false;
56
+ let start = this.scanIdentical(other, 1), end = this.length - this.scanIdentical(other, -1);
57
+ let a = new RawTextCursor(this), b = new RawTextCursor(other);
58
+ for (let skip = start, pos = start;;) {
59
+ a.next(skip);
60
+ b.next(skip);
61
+ skip = 0;
62
+ if (a.lineBreak != b.lineBreak || a.done != b.done || a.value != b.value)
63
+ return false;
64
+ pos += a.value.length;
65
+ if (a.done || pos >= end)
66
+ return true;
67
+ }
68
+ }
69
+ /**
70
+ Iterate over the text. When `dir` is `-1`, iteration happens
71
+ from end to start. This will return lines and the breaks between
72
+ them as separate strings.
73
+ */
74
+ iter(dir = 1) { return new RawTextCursor(this, dir); }
75
+ /**
76
+ Iterate over a range of the text. When `from` > `to`, the
77
+ iterator will run in reverse.
78
+ */
79
+ iterRange(from, to = this.length) { return new PartialTextCursor(this, from, to); }
80
+ /**
81
+ Return a cursor that iterates over the given range of lines,
82
+ _without_ returning the line breaks between, and yielding empty
83
+ strings for empty lines.
84
+
85
+ When `from` and `to` are given, they should be 1-based line numbers.
86
+ */
87
+ iterLines(from, to) {
88
+ let inner;
89
+ if (from == null) {
90
+ inner = this.iter();
91
+ }
92
+ else {
93
+ if (to == null)
94
+ to = this.lines + 1;
95
+ let start = this.line(from).from;
96
+ inner = this.iterRange(start, Math.max(start, to == this.lines + 1 ? this.length : to <= 1 ? 0 : this.line(to - 1).to));
97
+ }
98
+ return new LineCursor(inner);
99
+ }
100
+ /**
101
+ Return the document as a string, using newline characters to
102
+ separate lines.
103
+ */
104
+ toString() { return this.sliceString(0); }
105
+ /**
106
+ Convert the document to an array of lines (which can be
107
+ deserialized again via [`Text.of`](https://codemirror.net/6/docs/ref/#state.Text^of)).
108
+ */
109
+ toJSON() {
110
+ let lines = [];
111
+ this.flatten(lines);
112
+ return lines;
113
+ }
114
+ /**
115
+ @internal
116
+ */
117
+ constructor() { }
118
+ /**
119
+ Create a `Text` instance for the given array of lines.
120
+ */
121
+ static of(text) {
122
+ if (text.length == 0)
123
+ throw new RangeError("A document must have at least one line");
124
+ if (text.length == 1 && !text[0])
125
+ return Text.empty;
126
+ return text.length <= 32 /* Tree.Branch */ ? new TextLeaf(text) : TextNode.from(TextLeaf.split(text, []));
127
+ }
128
+ }
129
+ // Leaves store an array of line strings. There are always line breaks
130
+ // between these strings. Leaves are limited in size and have to be
131
+ // contained in TextNode instances for bigger documents.
132
+ class TextLeaf extends Text {
133
+ constructor(text, length = textLength(text)) {
134
+ super();
135
+ this.text = text;
136
+ this.length = length;
137
+ }
138
+ get lines() { return this.text.length; }
139
+ get children() { return null; }
140
+ lineInner(target, isLine, line, offset) {
141
+ for (let i = 0;; i++) {
142
+ let string = this.text[i], end = offset + string.length;
143
+ if ((isLine ? line : end) >= target)
144
+ return new Line(offset, end, line, string);
145
+ offset = end + 1;
146
+ line++;
147
+ }
148
+ }
149
+ decompose(from, to, target, open) {
150
+ let text = from <= 0 && to >= this.length ? this
151
+ : new TextLeaf(sliceText(this.text, from, to), Math.min(to, this.length) - Math.max(0, from));
152
+ if (open & 1 /* Open.From */) {
153
+ let prev = target.pop();
154
+ let joined = appendText(text.text, prev.text.slice(), 0, text.length);
155
+ if (joined.length <= 32 /* Tree.Branch */) {
156
+ target.push(new TextLeaf(joined, prev.length + text.length));
157
+ }
158
+ else {
159
+ let mid = joined.length >> 1;
160
+ target.push(new TextLeaf(joined.slice(0, mid)), new TextLeaf(joined.slice(mid)));
161
+ }
162
+ }
163
+ else {
164
+ target.push(text);
165
+ }
166
+ }
167
+ replace(from, to, text) {
168
+ if (!(text instanceof TextLeaf))
169
+ return super.replace(from, to, text);
170
+ [from, to] = clip(this, from, to);
171
+ let lines = appendText(this.text, appendText(text.text, sliceText(this.text, 0, from)), to);
172
+ let newLen = this.length + text.length - (to - from);
173
+ if (lines.length <= 32 /* Tree.Branch */)
174
+ return new TextLeaf(lines, newLen);
175
+ return TextNode.from(TextLeaf.split(lines, []), newLen);
176
+ }
177
+ sliceString(from, to = this.length, lineSep = "\n") {
178
+ [from, to] = clip(this, from, to);
179
+ let result = "";
180
+ for (let pos = 0, i = 0; pos <= to && i < this.text.length; i++) {
181
+ let line = this.text[i], end = pos + line.length;
182
+ if (pos > from && i)
183
+ result += lineSep;
184
+ if (from < end && to > pos)
185
+ result += line.slice(Math.max(0, from - pos), to - pos);
186
+ pos = end + 1;
187
+ }
188
+ return result;
189
+ }
190
+ flatten(target) {
191
+ for (let line of this.text)
192
+ target.push(line);
193
+ }
194
+ scanIdentical() { return 0; }
195
+ static split(text, target) {
196
+ let part = [], len = -1;
197
+ for (let line of text) {
198
+ part.push(line);
199
+ len += line.length + 1;
200
+ if (part.length == 32 /* Tree.Branch */) {
201
+ target.push(new TextLeaf(part, len));
202
+ part = [];
203
+ len = -1;
204
+ }
205
+ }
206
+ if (len > -1)
207
+ target.push(new TextLeaf(part, len));
208
+ return target;
209
+ }
210
+ }
211
+ // Nodes provide the tree structure of the `Text` type. They store a
212
+ // number of other nodes or leaves, taking care to balance themselves
213
+ // on changes. There are implied line breaks _between_ the children of
214
+ // a node (but not before the first or after the last child).
215
+ class TextNode extends Text {
216
+ constructor(children, length) {
217
+ super();
218
+ this.children = children;
219
+ this.length = length;
220
+ this.lines = 0;
221
+ for (let child of children)
222
+ this.lines += child.lines;
223
+ }
224
+ lineInner(target, isLine, line, offset) {
225
+ for (let i = 0;; i++) {
226
+ let child = this.children[i], end = offset + child.length, endLine = line + child.lines - 1;
227
+ if ((isLine ? endLine : end) >= target)
228
+ return child.lineInner(target, isLine, line, offset);
229
+ offset = end + 1;
230
+ line = endLine + 1;
231
+ }
232
+ }
233
+ decompose(from, to, target, open) {
234
+ for (let i = 0, pos = 0; pos <= to && i < this.children.length; i++) {
235
+ let child = this.children[i], end = pos + child.length;
236
+ if (from <= end && to >= pos) {
237
+ let childOpen = open & ((pos <= from ? 1 /* Open.From */ : 0) | (end >= to ? 2 /* Open.To */ : 0));
238
+ if (pos >= from && end <= to && !childOpen)
239
+ target.push(child);
240
+ else
241
+ child.decompose(from - pos, to - pos, target, childOpen);
242
+ }
243
+ pos = end + 1;
244
+ }
245
+ }
246
+ replace(from, to, text) {
247
+ [from, to] = clip(this, from, to);
248
+ if (text.lines < this.lines)
249
+ for (let i = 0, pos = 0; i < this.children.length; i++) {
250
+ let child = this.children[i], end = pos + child.length;
251
+ // Fast path: if the change only affects one child and the
252
+ // child's size remains in the acceptable range, only update
253
+ // that child
254
+ if (from >= pos && to <= end) {
255
+ let updated = child.replace(from - pos, to - pos, text);
256
+ let totalLines = this.lines - child.lines + updated.lines;
257
+ if (updated.lines < (totalLines >> (5 /* Tree.BranchShift */ - 1)) &&
258
+ updated.lines > (totalLines >> (5 /* Tree.BranchShift */ + 1))) {
259
+ let copy = this.children.slice();
260
+ copy[i] = updated;
261
+ return new TextNode(copy, this.length - (to - from) + text.length);
262
+ }
263
+ return super.replace(pos, end, updated);
264
+ }
265
+ pos = end + 1;
266
+ }
267
+ return super.replace(from, to, text);
268
+ }
269
+ sliceString(from, to = this.length, lineSep = "\n") {
270
+ [from, to] = clip(this, from, to);
271
+ let result = "";
272
+ for (let i = 0, pos = 0; i < this.children.length && pos <= to; i++) {
273
+ let child = this.children[i], end = pos + child.length;
274
+ if (pos > from && i)
275
+ result += lineSep;
276
+ if (from < end && to > pos)
277
+ result += child.sliceString(from - pos, to - pos, lineSep);
278
+ pos = end + 1;
279
+ }
280
+ return result;
281
+ }
282
+ flatten(target) {
283
+ for (let child of this.children)
284
+ child.flatten(target);
285
+ }
286
+ scanIdentical(other, dir) {
287
+ if (!(other instanceof TextNode))
288
+ return 0;
289
+ let length = 0;
290
+ let [iA, iB, eA, eB] = dir > 0 ? [0, 0, this.children.length, other.children.length]
291
+ : [this.children.length - 1, other.children.length - 1, -1, -1];
292
+ for (;; iA += dir, iB += dir) {
293
+ if (iA == eA || iB == eB)
294
+ return length;
295
+ let chA = this.children[iA], chB = other.children[iB];
296
+ if (chA != chB)
297
+ return length + chA.scanIdentical(chB, dir);
298
+ length += chA.length + 1;
299
+ }
300
+ }
301
+ static from(children, length = children.reduce((l, ch) => l + ch.length + 1, -1)) {
302
+ let lines = 0;
303
+ for (let ch of children)
304
+ lines += ch.lines;
305
+ if (lines < 32 /* Tree.Branch */) {
306
+ let flat = [];
307
+ for (let ch of children)
308
+ ch.flatten(flat);
309
+ return new TextLeaf(flat, length);
310
+ }
311
+ let chunk = Math.max(32 /* Tree.Branch */, lines >> 5 /* Tree.BranchShift */), maxChunk = chunk << 1, minChunk = chunk >> 1;
312
+ let chunked = [], currentLines = 0, currentLen = -1, currentChunk = [];
313
+ function add(child) {
314
+ let last;
315
+ if (child.lines > maxChunk && child instanceof TextNode) {
316
+ for (let node of child.children)
317
+ add(node);
318
+ }
319
+ else if (child.lines > minChunk && (currentLines > minChunk || !currentLines)) {
320
+ flush();
321
+ chunked.push(child);
322
+ }
323
+ else if (child instanceof TextLeaf && currentLines &&
324
+ (last = currentChunk[currentChunk.length - 1]) instanceof TextLeaf &&
325
+ child.lines + last.lines <= 32 /* Tree.Branch */) {
326
+ currentLines += child.lines;
327
+ currentLen += child.length + 1;
328
+ currentChunk[currentChunk.length - 1] = new TextLeaf(last.text.concat(child.text), last.length + 1 + child.length);
329
+ }
330
+ else {
331
+ if (currentLines + child.lines > chunk)
332
+ flush();
333
+ currentLines += child.lines;
334
+ currentLen += child.length + 1;
335
+ currentChunk.push(child);
336
+ }
337
+ }
338
+ function flush() {
339
+ if (currentLines == 0)
340
+ return;
341
+ chunked.push(currentChunk.length == 1 ? currentChunk[0] : TextNode.from(currentChunk, currentLen));
342
+ currentLen = -1;
343
+ currentLines = currentChunk.length = 0;
344
+ }
345
+ for (let child of children)
346
+ add(child);
347
+ flush();
348
+ return chunked.length == 1 ? chunked[0] : new TextNode(chunked, length);
349
+ }
350
+ }
351
+ Text.empty = /*@__PURE__*/new TextLeaf([""], 0);
352
+ function textLength(text) {
353
+ let length = -1;
354
+ for (let line of text)
355
+ length += line.length + 1;
356
+ return length;
357
+ }
358
+ function appendText(text, target, from = 0, to = 1e9) {
359
+ for (let pos = 0, i = 0, first = true; i < text.length && pos <= to; i++) {
360
+ let line = text[i], end = pos + line.length;
361
+ if (end >= from) {
362
+ if (end > to)
363
+ line = line.slice(0, to - pos);
364
+ if (pos < from)
365
+ line = line.slice(from - pos);
366
+ if (first) {
367
+ target[target.length - 1] += line;
368
+ first = false;
369
+ }
370
+ else
371
+ target.push(line);
372
+ }
373
+ pos = end + 1;
374
+ }
375
+ return target;
376
+ }
377
+ function sliceText(text, from, to) {
378
+ return appendText(text, [""], from, to);
379
+ }
380
+ class RawTextCursor {
381
+ constructor(text, dir = 1) {
382
+ this.dir = dir;
383
+ this.done = false;
384
+ this.lineBreak = false;
385
+ this.value = "";
386
+ this.nodes = [text];
387
+ this.offsets = [dir > 0 ? 1 : (text instanceof TextLeaf ? text.text.length : text.children.length) << 1];
388
+ }
389
+ nextInner(skip, dir) {
390
+ this.done = this.lineBreak = false;
391
+ for (;;) {
392
+ let last = this.nodes.length - 1;
393
+ let top = this.nodes[last], offsetValue = this.offsets[last], offset = offsetValue >> 1;
394
+ let size = top instanceof TextLeaf ? top.text.length : top.children.length;
395
+ if (offset == (dir > 0 ? size : 0)) {
396
+ if (last == 0) {
397
+ this.done = true;
398
+ this.value = "";
399
+ return this;
400
+ }
401
+ if (dir > 0)
402
+ this.offsets[last - 1]++;
403
+ this.nodes.pop();
404
+ this.offsets.pop();
405
+ }
406
+ else if ((offsetValue & 1) == (dir > 0 ? 0 : 1)) {
407
+ this.offsets[last] += dir;
408
+ if (skip == 0) {
409
+ this.lineBreak = true;
410
+ this.value = "\n";
411
+ return this;
412
+ }
413
+ skip--;
414
+ }
415
+ else if (top instanceof TextLeaf) {
416
+ // Move to the next string
417
+ let next = top.text[offset + (dir < 0 ? -1 : 0)];
418
+ this.offsets[last] += dir;
419
+ if (next.length > Math.max(0, skip)) {
420
+ this.value = skip == 0 ? next : dir > 0 ? next.slice(skip) : next.slice(0, next.length - skip);
421
+ return this;
422
+ }
423
+ skip -= next.length;
424
+ }
425
+ else {
426
+ let next = top.children[offset + (dir < 0 ? -1 : 0)];
427
+ if (skip > next.length) {
428
+ skip -= next.length;
429
+ this.offsets[last] += dir;
430
+ }
431
+ else {
432
+ if (dir < 0)
433
+ this.offsets[last]--;
434
+ this.nodes.push(next);
435
+ this.offsets.push(dir > 0 ? 1 : (next instanceof TextLeaf ? next.text.length : next.children.length) << 1);
436
+ }
437
+ }
438
+ }
439
+ }
440
+ next(skip = 0) {
441
+ if (skip < 0) {
442
+ this.nextInner(-skip, (-this.dir));
443
+ skip = this.value.length;
444
+ }
445
+ return this.nextInner(skip, this.dir);
446
+ }
447
+ }
448
+ class PartialTextCursor {
449
+ constructor(text, start, end) {
450
+ this.value = "";
451
+ this.done = false;
452
+ this.cursor = new RawTextCursor(text, start > end ? -1 : 1);
453
+ this.pos = start > end ? text.length : 0;
454
+ this.from = Math.min(start, end);
455
+ this.to = Math.max(start, end);
456
+ }
457
+ nextInner(skip, dir) {
458
+ if (dir < 0 ? this.pos <= this.from : this.pos >= this.to) {
459
+ this.value = "";
460
+ this.done = true;
461
+ return this;
462
+ }
463
+ skip += Math.max(0, dir < 0 ? this.pos - this.to : this.from - this.pos);
464
+ let limit = dir < 0 ? this.pos - this.from : this.to - this.pos;
465
+ if (skip > limit)
466
+ skip = limit;
467
+ limit -= skip;
468
+ let { value } = this.cursor.next(skip);
469
+ this.pos += (value.length + skip) * dir;
470
+ this.value = value.length <= limit ? value : dir < 0 ? value.slice(value.length - limit) : value.slice(0, limit);
471
+ this.done = !this.value;
472
+ return this;
473
+ }
474
+ next(skip = 0) {
475
+ if (skip < 0)
476
+ skip = Math.max(skip, this.from - this.pos);
477
+ else if (skip > 0)
478
+ skip = Math.min(skip, this.to - this.pos);
479
+ return this.nextInner(skip, this.cursor.dir);
480
+ }
481
+ get lineBreak() { return this.cursor.lineBreak && this.value != ""; }
482
+ }
483
+ class LineCursor {
484
+ constructor(inner) {
485
+ this.inner = inner;
486
+ this.afterBreak = true;
487
+ this.value = "";
488
+ this.done = false;
489
+ }
490
+ next(skip = 0) {
491
+ let { done, lineBreak, value } = this.inner.next(skip);
492
+ if (done && this.afterBreak) {
493
+ this.value = "";
494
+ this.afterBreak = false;
495
+ }
496
+ else if (done) {
497
+ this.done = true;
498
+ this.value = "";
499
+ }
500
+ else if (lineBreak) {
501
+ if (this.afterBreak) {
502
+ this.value = "";
503
+ }
504
+ else {
505
+ this.afterBreak = true;
506
+ this.next();
507
+ }
508
+ }
509
+ else {
510
+ this.value = value;
511
+ this.afterBreak = false;
512
+ }
513
+ return this;
514
+ }
515
+ get lineBreak() { return false; }
516
+ }
517
+ if (typeof Symbol != "undefined") {
518
+ Text.prototype[Symbol.iterator] = function () { return this.iter(); };
519
+ RawTextCursor.prototype[Symbol.iterator] = PartialTextCursor.prototype[Symbol.iterator] =
520
+ LineCursor.prototype[Symbol.iterator] = function () { return this; };
521
+ }
522
+ /**
523
+ This type describes a line in the document. It is created
524
+ on-demand when lines are [queried](https://codemirror.net/6/docs/ref/#state.Text.lineAt).
525
+ */
526
+ class Line {
527
+ /**
528
+ @internal
529
+ */
530
+ constructor(
531
+ /**
532
+ The position of the start of the line.
533
+ */
534
+ from,
535
+ /**
536
+ The position at the end of the line (_before_ the line break,
537
+ or at the end of document for the last line).
538
+ */
539
+ to,
540
+ /**
541
+ This line's line number (1-based).
542
+ */
543
+ number,
544
+ /**
545
+ The line's content.
546
+ */
547
+ text) {
548
+ this.from = from;
549
+ this.to = to;
550
+ this.number = number;
551
+ this.text = text;
552
+ }
553
+ /**
554
+ The length of the line (not including any line break after it).
555
+ */
556
+ get length() { return this.to - this.from; }
557
+ }
558
+ function clip(text, from, to) {
559
+ from = Math.max(0, Math.min(text.length, from));
560
+ return [from, Math.max(from, Math.min(text.length, to))];
561
+ }
562
+
563
+ // Compressed representation of the Grapheme_Cluster_Break=Extend
564
+ // information from
565
+ // http://www.unicode.org/Public/13.0.0/ucd/auxiliary/GraphemeBreakProperty.txt.
566
+ // Each pair of elements represents a range, as an offet from the
567
+ // previous range and a length. Numbers are in base-36, with the empty
568
+ // string being a shorthand for 1.
569
+ 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);
570
+ // Convert offsets into absolute values
571
+ for (let i = 1; i < extend.length; i++)
572
+ extend[i] += extend[i - 1];
573
+ function isExtendingChar(code) {
574
+ for (let i = 1; i < extend.length; i += 2)
575
+ if (extend[i] > code)
576
+ return extend[i - 1] <= code;
577
+ return false;
578
+ }
579
+ function isRegionalIndicator(code) {
580
+ return code >= 0x1F1E6 && code <= 0x1F1FF;
581
+ }
582
+ const ZWJ = 0x200d;
583
+ /**
584
+ Returns a next grapheme cluster break _after_ (not equal to)
585
+ `pos`, if `forward` is true, or before otherwise. Returns `pos`
586
+ itself if no further cluster break is available in the string.
587
+ Moves across surrogate pairs, extending characters (when
588
+ `includeExtending` is true), characters joined with zero-width
589
+ joiners, and flag emoji.
590
+ */
591
+ function findClusterBreak(str, pos, forward = true, includeExtending = true) {
592
+ return (forward ? nextClusterBreak : prevClusterBreak)(str, pos, includeExtending);
593
+ }
594
+ function nextClusterBreak(str, pos, includeExtending) {
595
+ if (pos == str.length)
596
+ return pos;
597
+ // If pos is in the middle of a surrogate pair, move to its start
598
+ if (pos && surrogateLow(str.charCodeAt(pos)) && surrogateHigh(str.charCodeAt(pos - 1)))
599
+ pos--;
600
+ let prev = codePointAt(str, pos);
601
+ pos += codePointSize(prev);
602
+ while (pos < str.length) {
603
+ let next = codePointAt(str, pos);
604
+ if (prev == ZWJ || next == ZWJ || includeExtending && isExtendingChar(next)) {
605
+ pos += codePointSize(next);
606
+ prev = next;
607
+ }
608
+ else if (isRegionalIndicator(next)) {
609
+ let countBefore = 0, i = pos - 2;
610
+ while (i >= 0 && isRegionalIndicator(codePointAt(str, i))) {
611
+ countBefore++;
612
+ i -= 2;
613
+ }
614
+ if (countBefore % 2 == 0)
615
+ break;
616
+ else
617
+ pos += 2;
618
+ }
619
+ else {
620
+ break;
621
+ }
622
+ }
623
+ return pos;
624
+ }
625
+ function prevClusterBreak(str, pos, includeExtending) {
626
+ while (pos > 0) {
627
+ let found = nextClusterBreak(str, pos - 2, includeExtending);
628
+ if (found < pos)
629
+ return found;
630
+ pos--;
631
+ }
632
+ return 0;
633
+ }
634
+ function surrogateLow(ch) { return ch >= 0xDC00 && ch < 0xE000; }
635
+ function surrogateHigh(ch) { return ch >= 0xD800 && ch < 0xDC00; }
636
+ /**
637
+ Find the code point at the given position in a string (like the
638
+ [`codePointAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/codePointAt)
639
+ string method).
640
+ */
641
+ function codePointAt(str, pos) {
642
+ let code0 = str.charCodeAt(pos);
643
+ if (!surrogateHigh(code0) || pos + 1 == str.length)
644
+ return code0;
645
+ let code1 = str.charCodeAt(pos + 1);
646
+ if (!surrogateLow(code1))
647
+ return code0;
648
+ return ((code0 - 0xd800) << 10) + (code1 - 0xdc00) + 0x10000;
649
+ }
650
+ /**
651
+ Given a Unicode codepoint, return the JavaScript string that
652
+ respresents it (like
653
+ [`String.fromCodePoint`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/fromCodePoint)).
654
+ */
655
+ function fromCodePoint(code) {
656
+ if (code <= 0xffff)
657
+ return String.fromCharCode(code);
658
+ code -= 0x10000;
659
+ return String.fromCharCode((code >> 10) + 0xd800, (code & 1023) + 0xdc00);
660
+ }
661
+ /**
662
+ The amount of positions a character takes up a JavaScript string.
663
+ */
664
+ function codePointSize(code) { return code < 0x10000 ? 1 : 2; }
665
+
666
+ const DefaultSplit = /\r\n?|\n/;
667
+ /**
668
+ Distinguishes different ways in which positions can be mapped.
669
+ */
670
+ var MapMode = /*@__PURE__*/(function (MapMode) {
671
+ /**
672
+ Map a position to a valid new position, even when its context
673
+ was deleted.
674
+ */
675
+ MapMode[MapMode["Simple"] = 0] = "Simple";
676
+ /**
677
+ Return null if deletion happens across the position.
678
+ */
679
+ MapMode[MapMode["TrackDel"] = 1] = "TrackDel";
680
+ /**
681
+ Return null if the character _before_ the position is deleted.
682
+ */
683
+ MapMode[MapMode["TrackBefore"] = 2] = "TrackBefore";
684
+ /**
685
+ Return null if the character _after_ the position is deleted.
686
+ */
687
+ MapMode[MapMode["TrackAfter"] = 3] = "TrackAfter";
688
+ return MapMode})(MapMode || (MapMode = {}));
689
+ /**
690
+ A change description is a variant of [change set](https://codemirror.net/6/docs/ref/#state.ChangeSet)
691
+ that doesn't store the inserted text. As such, it can't be
692
+ applied, but is cheaper to store and manipulate.
693
+ */
694
+ class ChangeDesc {
695
+ // Sections are encoded as pairs of integers. The first is the
696
+ // length in the current document, and the second is -1 for
697
+ // unaffected sections, and the length of the replacement content
698
+ // otherwise. So an insertion would be (0, n>0), a deletion (n>0,
699
+ // 0), and a replacement two positive numbers.
700
+ /**
701
+ @internal
702
+ */
703
+ constructor(
704
+ /**
705
+ @internal
706
+ */
707
+ sections) {
708
+ this.sections = sections;
709
+ }
710
+ /**
711
+ The length of the document before the change.
712
+ */
713
+ get length() {
714
+ let result = 0;
715
+ for (let i = 0; i < this.sections.length; i += 2)
716
+ result += this.sections[i];
717
+ return result;
718
+ }
719
+ /**
720
+ The length of the document after the change.
721
+ */
722
+ get newLength() {
723
+ let result = 0;
724
+ for (let i = 0; i < this.sections.length; i += 2) {
725
+ let ins = this.sections[i + 1];
726
+ result += ins < 0 ? this.sections[i] : ins;
727
+ }
728
+ return result;
729
+ }
730
+ /**
731
+ False when there are actual changes in this set.
732
+ */
733
+ get empty() { return this.sections.length == 0 || this.sections.length == 2 && this.sections[1] < 0; }
734
+ /**
735
+ Iterate over the unchanged parts left by these changes. `posA`
736
+ provides the position of the range in the old document, `posB`
737
+ the new position in the changed document.
738
+ */
739
+ iterGaps(f) {
740
+ for (let i = 0, posA = 0, posB = 0; i < this.sections.length;) {
741
+ let len = this.sections[i++], ins = this.sections[i++];
742
+ if (ins < 0) {
743
+ f(posA, posB, len);
744
+ posB += len;
745
+ }
746
+ else {
747
+ posB += ins;
748
+ }
749
+ posA += len;
750
+ }
751
+ }
752
+ /**
753
+ Iterate over the ranges changed by these changes. (See
754
+ [`ChangeSet.iterChanges`](https://codemirror.net/6/docs/ref/#state.ChangeSet.iterChanges) for a
755
+ variant that also provides you with the inserted text.)
756
+ `fromA`/`toA` provides the extent of the change in the starting
757
+ document, `fromB`/`toB` the extent of the replacement in the
758
+ changed document.
759
+
760
+ When `individual` is true, adjacent changes (which are kept
761
+ separate for [position mapping](https://codemirror.net/6/docs/ref/#state.ChangeDesc.mapPos)) are
762
+ reported separately.
763
+ */
764
+ iterChangedRanges(f, individual = false) {
765
+ iterChanges(this, f, individual);
766
+ }
767
+ /**
768
+ Get a description of the inverted form of these changes.
769
+ */
770
+ get invertedDesc() {
771
+ let sections = [];
772
+ for (let i = 0; i < this.sections.length;) {
773
+ let len = this.sections[i++], ins = this.sections[i++];
774
+ if (ins < 0)
775
+ sections.push(len, ins);
776
+ else
777
+ sections.push(ins, len);
778
+ }
779
+ return new ChangeDesc(sections);
780
+ }
781
+ /**
782
+ Compute the combined effect of applying another set of changes
783
+ after this one. The length of the document after this set should
784
+ match the length before `other`.
785
+ */
786
+ composeDesc(other) { return this.empty ? other : other.empty ? this : composeSets(this, other); }
787
+ /**
788
+ Map this description, which should start with the same document
789
+ as `other`, over another set of changes, so that it can be
790
+ applied after it. When `before` is true, map as if the changes
791
+ in `other` happened before the ones in `this`.
792
+ */
793
+ mapDesc(other, before = false) { return other.empty ? this : mapSet(this, other, before); }
794
+ mapPos(pos, assoc = -1, mode = MapMode.Simple) {
795
+ let posA = 0, posB = 0;
796
+ for (let i = 0; i < this.sections.length;) {
797
+ let len = this.sections[i++], ins = this.sections[i++], endA = posA + len;
798
+ if (ins < 0) {
799
+ if (endA > pos)
800
+ return posB + (pos - posA);
801
+ posB += len;
802
+ }
803
+ else {
804
+ if (mode != MapMode.Simple && endA >= pos &&
805
+ (mode == MapMode.TrackDel && posA < pos && endA > pos ||
806
+ mode == MapMode.TrackBefore && posA < pos ||
807
+ mode == MapMode.TrackAfter && endA > pos))
808
+ return null;
809
+ if (endA > pos || endA == pos && assoc < 0 && !len)
810
+ return pos == posA || assoc < 0 ? posB : posB + ins;
811
+ posB += ins;
812
+ }
813
+ posA = endA;
814
+ }
815
+ if (pos > posA)
816
+ throw new RangeError(`Position ${pos} is out of range for changeset of length ${posA}`);
817
+ return posB;
818
+ }
819
+ /**
820
+ Check whether these changes touch a given range. When one of the
821
+ changes entirely covers the range, the string `"cover"` is
822
+ returned.
823
+ */
824
+ touchesRange(from, to = from) {
825
+ for (let i = 0, pos = 0; i < this.sections.length && pos <= to;) {
826
+ let len = this.sections[i++], ins = this.sections[i++], end = pos + len;
827
+ if (ins >= 0 && pos <= to && end >= from)
828
+ return pos < from && end > to ? "cover" : true;
829
+ pos = end;
830
+ }
831
+ return false;
832
+ }
833
+ /**
834
+ @internal
835
+ */
836
+ toString() {
837
+ let result = "";
838
+ for (let i = 0; i < this.sections.length;) {
839
+ let len = this.sections[i++], ins = this.sections[i++];
840
+ result += (result ? " " : "") + len + (ins >= 0 ? ":" + ins : "");
841
+ }
842
+ return result;
843
+ }
844
+ /**
845
+ Serialize this change desc to a JSON-representable value.
846
+ */
847
+ toJSON() { return this.sections; }
848
+ /**
849
+ Create a change desc from its JSON representation (as produced
850
+ by [`toJSON`](https://codemirror.net/6/docs/ref/#state.ChangeDesc.toJSON).
851
+ */
852
+ static fromJSON(json) {
853
+ if (!Array.isArray(json) || json.length % 2 || json.some(a => typeof a != "number"))
854
+ throw new RangeError("Invalid JSON representation of ChangeDesc");
855
+ return new ChangeDesc(json);
856
+ }
857
+ /**
858
+ @internal
859
+ */
860
+ static create(sections) { return new ChangeDesc(sections); }
861
+ }
862
+ /**
863
+ A change set represents a group of modifications to a document. It
864
+ stores the document length, and can only be applied to documents
865
+ with exactly that length.
866
+ */
867
+ class ChangeSet extends ChangeDesc {
868
+ constructor(sections,
869
+ /**
870
+ @internal
871
+ */
872
+ inserted) {
873
+ super(sections);
874
+ this.inserted = inserted;
875
+ }
876
+ /**
877
+ Apply the changes to a document, returning the modified
878
+ document.
879
+ */
880
+ apply(doc) {
881
+ if (this.length != doc.length)
882
+ throw new RangeError("Applying change set to a document with the wrong length");
883
+ iterChanges(this, (fromA, toA, fromB, _toB, text) => doc = doc.replace(fromB, fromB + (toA - fromA), text), false);
884
+ return doc;
885
+ }
886
+ mapDesc(other, before = false) { return mapSet(this, other, before, true); }
887
+ /**
888
+ Given the document as it existed _before_ the changes, return a
889
+ change set that represents the inverse of this set, which could
890
+ be used to go from the document created by the changes back to
891
+ the document as it existed before the changes.
892
+ */
893
+ invert(doc) {
894
+ let sections = this.sections.slice(), inserted = [];
895
+ for (let i = 0, pos = 0; i < sections.length; i += 2) {
896
+ let len = sections[i], ins = sections[i + 1];
897
+ if (ins >= 0) {
898
+ sections[i] = ins;
899
+ sections[i + 1] = len;
900
+ let index = i >> 1;
901
+ while (inserted.length < index)
902
+ inserted.push(Text.empty);
903
+ inserted.push(len ? doc.slice(pos, pos + len) : Text.empty);
904
+ }
905
+ pos += len;
906
+ }
907
+ return new ChangeSet(sections, inserted);
908
+ }
909
+ /**
910
+ Combine two subsequent change sets into a single set. `other`
911
+ must start in the document produced by `this`. If `this` goes
912
+ `docA` → `docB` and `other` represents `docB` → `docC`, the
913
+ returned value will represent the change `docA` → `docC`.
914
+ */
915
+ compose(other) { return this.empty ? other : other.empty ? this : composeSets(this, other, true); }
916
+ /**
917
+ Given another change set starting in the same document, maps this
918
+ change set over the other, producing a new change set that can be
919
+ applied to the document produced by applying `other`. When
920
+ `before` is `true`, order changes as if `this` comes before
921
+ `other`, otherwise (the default) treat `other` as coming first.
922
+
923
+ Given two changes `A` and `B`, `A.compose(B.map(A))` and
924
+ `B.compose(A.map(B, true))` will produce the same document. This
925
+ provides a basic form of [operational
926
+ transformation](https://en.wikipedia.org/wiki/Operational_transformation),
927
+ and can be used for collaborative editing.
928
+ */
929
+ map(other, before = false) { return other.empty ? this : mapSet(this, other, before, true); }
930
+ /**
931
+ Iterate over the changed ranges in the document, calling `f` for
932
+ each, with the range in the original document (`fromA`-`toA`)
933
+ and the range that replaces it in the new document
934
+ (`fromB`-`toB`).
935
+
936
+ When `individual` is true, adjacent changes are reported
937
+ separately.
938
+ */
939
+ iterChanges(f, individual = false) {
940
+ iterChanges(this, f, individual);
941
+ }
942
+ /**
943
+ Get a [change description](https://codemirror.net/6/docs/ref/#state.ChangeDesc) for this change
944
+ set.
945
+ */
946
+ get desc() { return ChangeDesc.create(this.sections); }
947
+ /**
948
+ @internal
949
+ */
950
+ filter(ranges) {
951
+ let resultSections = [], resultInserted = [], filteredSections = [];
952
+ let iter = new SectionIter(this);
953
+ done: for (let i = 0, pos = 0;;) {
954
+ let next = i == ranges.length ? 1e9 : ranges[i++];
955
+ while (pos < next || pos == next && iter.len == 0) {
956
+ if (iter.done)
957
+ break done;
958
+ let len = Math.min(iter.len, next - pos);
959
+ addSection(filteredSections, len, -1);
960
+ let ins = iter.ins == -1 ? -1 : iter.off == 0 ? iter.ins : 0;
961
+ addSection(resultSections, len, ins);
962
+ if (ins > 0)
963
+ addInsert(resultInserted, resultSections, iter.text);
964
+ iter.forward(len);
965
+ pos += len;
966
+ }
967
+ let end = ranges[i++];
968
+ while (pos < end) {
969
+ if (iter.done)
970
+ break done;
971
+ let len = Math.min(iter.len, end - pos);
972
+ addSection(resultSections, len, -1);
973
+ addSection(filteredSections, len, iter.ins == -1 ? -1 : iter.off == 0 ? iter.ins : 0);
974
+ iter.forward(len);
975
+ pos += len;
976
+ }
977
+ }
978
+ return { changes: new ChangeSet(resultSections, resultInserted),
979
+ filtered: ChangeDesc.create(filteredSections) };
980
+ }
981
+ /**
982
+ Serialize this change set to a JSON-representable value.
983
+ */
984
+ toJSON() {
985
+ let parts = [];
986
+ for (let i = 0; i < this.sections.length; i += 2) {
987
+ let len = this.sections[i], ins = this.sections[i + 1];
988
+ if (ins < 0)
989
+ parts.push(len);
990
+ else if (ins == 0)
991
+ parts.push([len]);
992
+ else
993
+ parts.push([len].concat(this.inserted[i >> 1].toJSON()));
994
+ }
995
+ return parts;
996
+ }
997
+ /**
998
+ Create a change set for the given changes, for a document of the
999
+ given length, using `lineSep` as line separator.
1000
+ */
1001
+ static of(changes, length, lineSep) {
1002
+ let sections = [], inserted = [], pos = 0;
1003
+ let total = null;
1004
+ function flush(force = false) {
1005
+ if (!force && !sections.length)
1006
+ return;
1007
+ if (pos < length)
1008
+ addSection(sections, length - pos, -1);
1009
+ let set = new ChangeSet(sections, inserted);
1010
+ total = total ? total.compose(set.map(total)) : set;
1011
+ sections = [];
1012
+ inserted = [];
1013
+ pos = 0;
1014
+ }
1015
+ function process(spec) {
1016
+ if (Array.isArray(spec)) {
1017
+ for (let sub of spec)
1018
+ process(sub);
1019
+ }
1020
+ else if (spec instanceof ChangeSet) {
1021
+ if (spec.length != length)
1022
+ throw new RangeError(`Mismatched change set length (got ${spec.length}, expected ${length})`);
1023
+ flush();
1024
+ total = total ? total.compose(spec.map(total)) : spec;
1025
+ }
1026
+ else {
1027
+ let { from, to = from, insert } = spec;
1028
+ if (from > to || from < 0 || to > length)
1029
+ throw new RangeError(`Invalid change range ${from} to ${to} (in doc of length ${length})`);
1030
+ let insText = !insert ? Text.empty : typeof insert == "string" ? Text.of(insert.split(lineSep || DefaultSplit)) : insert;
1031
+ let insLen = insText.length;
1032
+ if (from == to && insLen == 0)
1033
+ return;
1034
+ if (from < pos)
1035
+ flush();
1036
+ if (from > pos)
1037
+ addSection(sections, from - pos, -1);
1038
+ addSection(sections, to - from, insLen);
1039
+ addInsert(inserted, sections, insText);
1040
+ pos = to;
1041
+ }
1042
+ }
1043
+ process(changes);
1044
+ flush(!total);
1045
+ return total;
1046
+ }
1047
+ /**
1048
+ Create an empty changeset of the given length.
1049
+ */
1050
+ static empty(length) {
1051
+ return new ChangeSet(length ? [length, -1] : [], []);
1052
+ }
1053
+ /**
1054
+ Create a changeset from its JSON representation (as produced by
1055
+ [`toJSON`](https://codemirror.net/6/docs/ref/#state.ChangeSet.toJSON).
1056
+ */
1057
+ static fromJSON(json) {
1058
+ if (!Array.isArray(json))
1059
+ throw new RangeError("Invalid JSON representation of ChangeSet");
1060
+ let sections = [], inserted = [];
1061
+ for (let i = 0; i < json.length; i++) {
1062
+ let part = json[i];
1063
+ if (typeof part == "number") {
1064
+ sections.push(part, -1);
1065
+ }
1066
+ else if (!Array.isArray(part) || typeof part[0] != "number" || part.some((e, i) => i && typeof e != "string")) {
1067
+ throw new RangeError("Invalid JSON representation of ChangeSet");
1068
+ }
1069
+ else if (part.length == 1) {
1070
+ sections.push(part[0], 0);
1071
+ }
1072
+ else {
1073
+ while (inserted.length < i)
1074
+ inserted.push(Text.empty);
1075
+ inserted[i] = Text.of(part.slice(1));
1076
+ sections.push(part[0], inserted[i].length);
1077
+ }
1078
+ }
1079
+ return new ChangeSet(sections, inserted);
1080
+ }
1081
+ /**
1082
+ @internal
1083
+ */
1084
+ static createSet(sections, inserted) {
1085
+ return new ChangeSet(sections, inserted);
1086
+ }
1087
+ }
1088
+ function addSection(sections, len, ins, forceJoin = false) {
1089
+ if (len == 0 && ins <= 0)
1090
+ return;
1091
+ let last = sections.length - 2;
1092
+ if (last >= 0 && ins <= 0 && ins == sections[last + 1])
1093
+ sections[last] += len;
1094
+ else if (len == 0 && sections[last] == 0)
1095
+ sections[last + 1] += ins;
1096
+ else if (forceJoin) {
1097
+ sections[last] += len;
1098
+ sections[last + 1] += ins;
1099
+ }
1100
+ else
1101
+ sections.push(len, ins);
1102
+ }
1103
+ function addInsert(values, sections, value) {
1104
+ if (value.length == 0)
1105
+ return;
1106
+ let index = (sections.length - 2) >> 1;
1107
+ if (index < values.length) {
1108
+ values[values.length - 1] = values[values.length - 1].append(value);
1109
+ }
1110
+ else {
1111
+ while (values.length < index)
1112
+ values.push(Text.empty);
1113
+ values.push(value);
1114
+ }
1115
+ }
1116
+ function iterChanges(desc, f, individual) {
1117
+ let inserted = desc.inserted;
1118
+ for (let posA = 0, posB = 0, i = 0; i < desc.sections.length;) {
1119
+ let len = desc.sections[i++], ins = desc.sections[i++];
1120
+ if (ins < 0) {
1121
+ posA += len;
1122
+ posB += len;
1123
+ }
1124
+ else {
1125
+ let endA = posA, endB = posB, text = Text.empty;
1126
+ for (;;) {
1127
+ endA += len;
1128
+ endB += ins;
1129
+ if (ins && inserted)
1130
+ text = text.append(inserted[(i - 2) >> 1]);
1131
+ if (individual || i == desc.sections.length || desc.sections[i + 1] < 0)
1132
+ break;
1133
+ len = desc.sections[i++];
1134
+ ins = desc.sections[i++];
1135
+ }
1136
+ f(posA, endA, posB, endB, text);
1137
+ posA = endA;
1138
+ posB = endB;
1139
+ }
1140
+ }
1141
+ }
1142
+ function mapSet(setA, setB, before, mkSet = false) {
1143
+ // Produce a copy of setA that applies to the document after setB
1144
+ // has been applied (assuming both start at the same document).
1145
+ let sections = [], insert = mkSet ? [] : null;
1146
+ let a = new SectionIter(setA), b = new SectionIter(setB);
1147
+ // Iterate over both sets in parallel. inserted tracks, for changes
1148
+ // in A that have to be processed piece-by-piece, whether their
1149
+ // content has been inserted already, and refers to the section
1150
+ // index.
1151
+ for (let inserted = -1;;) {
1152
+ if (a.ins == -1 && b.ins == -1) {
1153
+ // Move across ranges skipped by both sets.
1154
+ let len = Math.min(a.len, b.len);
1155
+ addSection(sections, len, -1);
1156
+ a.forward(len);
1157
+ b.forward(len);
1158
+ }
1159
+ else if (b.ins >= 0 && (a.ins < 0 || inserted == a.i || a.off == 0 && (b.len < a.len || b.len == a.len && !before))) {
1160
+ // If there's a change in B that comes before the next change in
1161
+ // A (ordered by start pos, then len, then before flag), skip
1162
+ // that (and process any changes in A it covers).
1163
+ let len = b.len;
1164
+ addSection(sections, b.ins, -1);
1165
+ while (len) {
1166
+ let piece = Math.min(a.len, len);
1167
+ if (a.ins >= 0 && inserted < a.i && a.len <= piece) {
1168
+ addSection(sections, 0, a.ins);
1169
+ if (insert)
1170
+ addInsert(insert, sections, a.text);
1171
+ inserted = a.i;
1172
+ }
1173
+ a.forward(piece);
1174
+ len -= piece;
1175
+ }
1176
+ b.next();
1177
+ }
1178
+ else if (a.ins >= 0) {
1179
+ // Process the part of a change in A up to the start of the next
1180
+ // non-deletion change in B (if overlapping).
1181
+ let len = 0, left = a.len;
1182
+ while (left) {
1183
+ if (b.ins == -1) {
1184
+ let piece = Math.min(left, b.len);
1185
+ len += piece;
1186
+ left -= piece;
1187
+ b.forward(piece);
1188
+ }
1189
+ else if (b.ins == 0 && b.len < left) {
1190
+ left -= b.len;
1191
+ b.next();
1192
+ }
1193
+ else {
1194
+ break;
1195
+ }
1196
+ }
1197
+ addSection(sections, len, inserted < a.i ? a.ins : 0);
1198
+ if (insert && inserted < a.i)
1199
+ addInsert(insert, sections, a.text);
1200
+ inserted = a.i;
1201
+ a.forward(a.len - left);
1202
+ }
1203
+ else if (a.done && b.done) {
1204
+ return insert ? ChangeSet.createSet(sections, insert) : ChangeDesc.create(sections);
1205
+ }
1206
+ else {
1207
+ throw new Error("Mismatched change set lengths");
1208
+ }
1209
+ }
1210
+ }
1211
+ function composeSets(setA, setB, mkSet = false) {
1212
+ let sections = [];
1213
+ let insert = mkSet ? [] : null;
1214
+ let a = new SectionIter(setA), b = new SectionIter(setB);
1215
+ for (let open = false;;) {
1216
+ if (a.done && b.done) {
1217
+ return insert ? ChangeSet.createSet(sections, insert) : ChangeDesc.create(sections);
1218
+ }
1219
+ else if (a.ins == 0) { // Deletion in A
1220
+ addSection(sections, a.len, 0, open);
1221
+ a.next();
1222
+ }
1223
+ else if (b.len == 0 && !b.done) { // Insertion in B
1224
+ addSection(sections, 0, b.ins, open);
1225
+ if (insert)
1226
+ addInsert(insert, sections, b.text);
1227
+ b.next();
1228
+ }
1229
+ else if (a.done || b.done) {
1230
+ throw new Error("Mismatched change set lengths");
1231
+ }
1232
+ else {
1233
+ let len = Math.min(a.len2, b.len), sectionLen = sections.length;
1234
+ if (a.ins == -1) {
1235
+ let insB = b.ins == -1 ? -1 : b.off ? 0 : b.ins;
1236
+ addSection(sections, len, insB, open);
1237
+ if (insert && insB)
1238
+ addInsert(insert, sections, b.text);
1239
+ }
1240
+ else if (b.ins == -1) {
1241
+ addSection(sections, a.off ? 0 : a.len, len, open);
1242
+ if (insert)
1243
+ addInsert(insert, sections, a.textBit(len));
1244
+ }
1245
+ else {
1246
+ addSection(sections, a.off ? 0 : a.len, b.off ? 0 : b.ins, open);
1247
+ if (insert && !b.off)
1248
+ addInsert(insert, sections, b.text);
1249
+ }
1250
+ open = (a.ins > len || b.ins >= 0 && b.len > len) && (open || sections.length > sectionLen);
1251
+ a.forward2(len);
1252
+ b.forward(len);
1253
+ }
1254
+ }
1255
+ }
1256
+ class SectionIter {
1257
+ constructor(set) {
1258
+ this.set = set;
1259
+ this.i = 0;
1260
+ this.next();
1261
+ }
1262
+ next() {
1263
+ let { sections } = this.set;
1264
+ if (this.i < sections.length) {
1265
+ this.len = sections[this.i++];
1266
+ this.ins = sections[this.i++];
1267
+ }
1268
+ else {
1269
+ this.len = 0;
1270
+ this.ins = -2;
1271
+ }
1272
+ this.off = 0;
1273
+ }
1274
+ get done() { return this.ins == -2; }
1275
+ get len2() { return this.ins < 0 ? this.len : this.ins; }
1276
+ get text() {
1277
+ let { inserted } = this.set, index = (this.i - 2) >> 1;
1278
+ return index >= inserted.length ? Text.empty : inserted[index];
1279
+ }
1280
+ textBit(len) {
1281
+ let { inserted } = this.set, index = (this.i - 2) >> 1;
1282
+ return index >= inserted.length && !len ? Text.empty
1283
+ : inserted[index].slice(this.off, len == null ? undefined : this.off + len);
1284
+ }
1285
+ forward(len) {
1286
+ if (len == this.len)
1287
+ this.next();
1288
+ else {
1289
+ this.len -= len;
1290
+ this.off += len;
1291
+ }
1292
+ }
1293
+ forward2(len) {
1294
+ if (this.ins == -1)
1295
+ this.forward(len);
1296
+ else if (len == this.ins)
1297
+ this.next();
1298
+ else {
1299
+ this.ins -= len;
1300
+ this.off += len;
1301
+ }
1302
+ }
1303
+ }
1304
+
1305
+ /**
1306
+ A single selection range. When
1307
+ [`allowMultipleSelections`](https://codemirror.net/6/docs/ref/#state.EditorState^allowMultipleSelections)
1308
+ is enabled, a [selection](https://codemirror.net/6/docs/ref/#state.EditorSelection) may hold
1309
+ multiple ranges. By default, selections hold exactly one range.
1310
+ */
1311
+ class SelectionRange {
1312
+ constructor(
1313
+ /**
1314
+ The lower boundary of the range.
1315
+ */
1316
+ from,
1317
+ /**
1318
+ The upper boundary of the range.
1319
+ */
1320
+ to, flags) {
1321
+ this.from = from;
1322
+ this.to = to;
1323
+ this.flags = flags;
1324
+ }
1325
+ /**
1326
+ The anchor of the range—the side that doesn't move when you
1327
+ extend it.
1328
+ */
1329
+ get anchor() { return this.flags & 32 /* RangeFlag.Inverted */ ? this.to : this.from; }
1330
+ /**
1331
+ The head of the range, which is moved when the range is
1332
+ [extended](https://codemirror.net/6/docs/ref/#state.SelectionRange.extend).
1333
+ */
1334
+ get head() { return this.flags & 32 /* RangeFlag.Inverted */ ? this.from : this.to; }
1335
+ /**
1336
+ True when `anchor` and `head` are at the same position.
1337
+ */
1338
+ get empty() { return this.from == this.to; }
1339
+ /**
1340
+ If this is a cursor that is explicitly associated with the
1341
+ character on one of its sides, this returns the side. -1 means
1342
+ the character before its position, 1 the character after, and 0
1343
+ means no association.
1344
+ */
1345
+ get assoc() { return this.flags & 8 /* RangeFlag.AssocBefore */ ? -1 : this.flags & 16 /* RangeFlag.AssocAfter */ ? 1 : 0; }
1346
+ /**
1347
+ The bidirectional text level associated with this cursor, if
1348
+ any.
1349
+ */
1350
+ get bidiLevel() {
1351
+ let level = this.flags & 7 /* RangeFlag.BidiLevelMask */;
1352
+ return level == 7 ? null : level;
1353
+ }
1354
+ /**
1355
+ The goal column (stored vertical offset) associated with a
1356
+ cursor. This is used to preserve the vertical position when
1357
+ [moving](https://codemirror.net/6/docs/ref/#view.EditorView.moveVertically) across
1358
+ lines of different length.
1359
+ */
1360
+ get goalColumn() {
1361
+ let value = this.flags >> 6 /* RangeFlag.GoalColumnOffset */;
1362
+ return value == 16777215 /* RangeFlag.NoGoalColumn */ ? undefined : value;
1363
+ }
1364
+ /**
1365
+ Map this range through a change, producing a valid range in the
1366
+ updated document.
1367
+ */
1368
+ map(change, assoc = -1) {
1369
+ let from, to;
1370
+ if (this.empty) {
1371
+ from = to = change.mapPos(this.from, assoc);
1372
+ }
1373
+ else {
1374
+ from = change.mapPos(this.from, 1);
1375
+ to = change.mapPos(this.to, -1);
1376
+ }
1377
+ return from == this.from && to == this.to ? this : new SelectionRange(from, to, this.flags);
1378
+ }
1379
+ /**
1380
+ Extend this range to cover at least `from` to `to`.
1381
+ */
1382
+ extend(from, to = from) {
1383
+ if (from <= this.anchor && to >= this.anchor)
1384
+ return EditorSelection.range(from, to);
1385
+ let head = Math.abs(from - this.anchor) > Math.abs(to - this.anchor) ? from : to;
1386
+ return EditorSelection.range(this.anchor, head);
1387
+ }
1388
+ /**
1389
+ Compare this range to another range.
1390
+ */
1391
+ eq(other, includeAssoc = false) {
1392
+ return this.anchor == other.anchor && this.head == other.head &&
1393
+ (!includeAssoc || !this.empty || this.assoc == other.assoc);
1394
+ }
1395
+ /**
1396
+ Return a JSON-serializable object representing the range.
1397
+ */
1398
+ toJSON() { return { anchor: this.anchor, head: this.head }; }
1399
+ /**
1400
+ Convert a JSON representation of a range to a `SelectionRange`
1401
+ instance.
1402
+ */
1403
+ static fromJSON(json) {
1404
+ if (!json || typeof json.anchor != "number" || typeof json.head != "number")
1405
+ throw new RangeError("Invalid JSON representation for SelectionRange");
1406
+ return EditorSelection.range(json.anchor, json.head);
1407
+ }
1408
+ /**
1409
+ @internal
1410
+ */
1411
+ static create(from, to, flags) {
1412
+ return new SelectionRange(from, to, flags);
1413
+ }
1414
+ }
1415
+ /**
1416
+ An editor selection holds one or more selection ranges.
1417
+ */
1418
+ class EditorSelection {
1419
+ constructor(
1420
+ /**
1421
+ The ranges in the selection, sorted by position. Ranges cannot
1422
+ overlap (but they may touch, if they aren't empty).
1423
+ */
1424
+ ranges,
1425
+ /**
1426
+ The index of the _main_ range in the selection (which is
1427
+ usually the range that was added last).
1428
+ */
1429
+ mainIndex) {
1430
+ this.ranges = ranges;
1431
+ this.mainIndex = mainIndex;
1432
+ }
1433
+ /**
1434
+ Map a selection through a change. Used to adjust the selection
1435
+ position for changes.
1436
+ */
1437
+ map(change, assoc = -1) {
1438
+ if (change.empty)
1439
+ return this;
1440
+ return EditorSelection.create(this.ranges.map(r => r.map(change, assoc)), this.mainIndex);
1441
+ }
1442
+ /**
1443
+ Compare this selection to another selection. By default, ranges
1444
+ are compared only by position. When `includeAssoc` is true,
1445
+ cursor ranges must also have the same
1446
+ [`assoc`](https://codemirror.net/6/docs/ref/#state.SelectionRange.assoc) value.
1447
+ */
1448
+ eq(other, includeAssoc = false) {
1449
+ if (this.ranges.length != other.ranges.length ||
1450
+ this.mainIndex != other.mainIndex)
1451
+ return false;
1452
+ for (let i = 0; i < this.ranges.length; i++)
1453
+ if (!this.ranges[i].eq(other.ranges[i], includeAssoc))
1454
+ return false;
1455
+ return true;
1456
+ }
1457
+ /**
1458
+ Get the primary selection range. Usually, you should make sure
1459
+ your code applies to _all_ ranges, by using methods like
1460
+ [`changeByRange`](https://codemirror.net/6/docs/ref/#state.EditorState.changeByRange).
1461
+ */
1462
+ get main() { return this.ranges[this.mainIndex]; }
1463
+ /**
1464
+ Make sure the selection only has one range. Returns a selection
1465
+ holding only the main range from this selection.
1466
+ */
1467
+ asSingle() {
1468
+ return this.ranges.length == 1 ? this : new EditorSelection([this.main], 0);
1469
+ }
1470
+ /**
1471
+ Extend this selection with an extra range.
1472
+ */
1473
+ addRange(range, main = true) {
1474
+ return EditorSelection.create([range].concat(this.ranges), main ? 0 : this.mainIndex + 1);
1475
+ }
1476
+ /**
1477
+ Replace a given range with another range, and then normalize the
1478
+ selection to merge and sort ranges if necessary.
1479
+ */
1480
+ replaceRange(range, which = this.mainIndex) {
1481
+ let ranges = this.ranges.slice();
1482
+ ranges[which] = range;
1483
+ return EditorSelection.create(ranges, this.mainIndex);
1484
+ }
1485
+ /**
1486
+ Convert this selection to an object that can be serialized to
1487
+ JSON.
1488
+ */
1489
+ toJSON() {
1490
+ return { ranges: this.ranges.map(r => r.toJSON()), main: this.mainIndex };
1491
+ }
1492
+ /**
1493
+ Create a selection from a JSON representation.
1494
+ */
1495
+ static fromJSON(json) {
1496
+ if (!json || !Array.isArray(json.ranges) || typeof json.main != "number" || json.main >= json.ranges.length)
1497
+ throw new RangeError("Invalid JSON representation for EditorSelection");
1498
+ return new EditorSelection(json.ranges.map((r) => SelectionRange.fromJSON(r)), json.main);
1499
+ }
1500
+ /**
1501
+ Create a selection holding a single range.
1502
+ */
1503
+ static single(anchor, head = anchor) {
1504
+ return new EditorSelection([EditorSelection.range(anchor, head)], 0);
1505
+ }
1506
+ /**
1507
+ Sort and merge the given set of ranges, creating a valid
1508
+ selection.
1509
+ */
1510
+ static create(ranges, mainIndex = 0) {
1511
+ if (ranges.length == 0)
1512
+ throw new RangeError("A selection needs at least one range");
1513
+ for (let pos = 0, i = 0; i < ranges.length; i++) {
1514
+ let range = ranges[i];
1515
+ if (range.empty ? range.from <= pos : range.from < pos)
1516
+ return EditorSelection.normalized(ranges.slice(), mainIndex);
1517
+ pos = range.to;
1518
+ }
1519
+ return new EditorSelection(ranges, mainIndex);
1520
+ }
1521
+ /**
1522
+ Create a cursor selection range at the given position. You can
1523
+ safely ignore the optional arguments in most situations.
1524
+ */
1525
+ static cursor(pos, assoc = 0, bidiLevel, goalColumn) {
1526
+ return SelectionRange.create(pos, pos, (assoc == 0 ? 0 : assoc < 0 ? 8 /* RangeFlag.AssocBefore */ : 16 /* RangeFlag.AssocAfter */) |
1527
+ (bidiLevel == null ? 7 : Math.min(6, bidiLevel)) |
1528
+ ((goalColumn !== null && goalColumn !== void 0 ? goalColumn : 16777215 /* RangeFlag.NoGoalColumn */) << 6 /* RangeFlag.GoalColumnOffset */));
1529
+ }
1530
+ /**
1531
+ Create a selection range.
1532
+ */
1533
+ static range(anchor, head, goalColumn, bidiLevel) {
1534
+ let flags = ((goalColumn !== null && goalColumn !== void 0 ? goalColumn : 16777215 /* RangeFlag.NoGoalColumn */) << 6 /* RangeFlag.GoalColumnOffset */) |
1535
+ (bidiLevel == null ? 7 : Math.min(6, bidiLevel));
1536
+ return head < anchor ? SelectionRange.create(head, anchor, 32 /* RangeFlag.Inverted */ | 16 /* RangeFlag.AssocAfter */ | flags)
1537
+ : SelectionRange.create(anchor, head, (head > anchor ? 8 /* RangeFlag.AssocBefore */ : 0) | flags);
1538
+ }
1539
+ /**
1540
+ @internal
1541
+ */
1542
+ static normalized(ranges, mainIndex = 0) {
1543
+ let main = ranges[mainIndex];
1544
+ ranges.sort((a, b) => a.from - b.from);
1545
+ mainIndex = ranges.indexOf(main);
1546
+ for (let i = 1; i < ranges.length; i++) {
1547
+ let range = ranges[i], prev = ranges[i - 1];
1548
+ if (range.empty ? range.from <= prev.to : range.from < prev.to) {
1549
+ let from = prev.from, to = Math.max(range.to, prev.to);
1550
+ if (i <= mainIndex)
1551
+ mainIndex--;
1552
+ ranges.splice(--i, 2, range.anchor > range.head ? EditorSelection.range(to, from) : EditorSelection.range(from, to));
1553
+ }
1554
+ }
1555
+ return new EditorSelection(ranges, mainIndex);
1556
+ }
1557
+ }
1558
+ function checkSelection(selection, docLength) {
1559
+ for (let range of selection.ranges)
1560
+ if (range.to > docLength)
1561
+ throw new RangeError("Selection points outside of document");
1562
+ }
1563
+
1564
+ let nextID = 0;
1565
+ /**
1566
+ A facet is a labeled value that is associated with an editor
1567
+ state. It takes inputs from any number of extensions, and combines
1568
+ those into a single output value.
1569
+
1570
+ Examples of uses of facets are the [tab
1571
+ size](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize), [editor
1572
+ attributes](https://codemirror.net/6/docs/ref/#view.EditorView^editorAttributes), and [update
1573
+ listeners](https://codemirror.net/6/docs/ref/#view.EditorView^updateListener).
1574
+
1575
+ Note that `Facet` instances can be used anywhere where
1576
+ [`FacetReader`](https://codemirror.net/6/docs/ref/#state.FacetReader) is expected.
1577
+ */
1578
+ class Facet {
1579
+ constructor(
1580
+ /**
1581
+ @internal
1582
+ */
1583
+ combine,
1584
+ /**
1585
+ @internal
1586
+ */
1587
+ compareInput,
1588
+ /**
1589
+ @internal
1590
+ */
1591
+ compare, isStatic, enables) {
1592
+ this.combine = combine;
1593
+ this.compareInput = compareInput;
1594
+ this.compare = compare;
1595
+ this.isStatic = isStatic;
1596
+ /**
1597
+ @internal
1598
+ */
1599
+ this.id = nextID++;
1600
+ this.default = combine([]);
1601
+ this.extensions = typeof enables == "function" ? enables(this) : enables;
1602
+ }
1603
+ /**
1604
+ Returns a facet reader for this facet, which can be used to
1605
+ [read](https://codemirror.net/6/docs/ref/#state.EditorState.facet) it but not to define values for it.
1606
+ */
1607
+ get reader() { return this; }
1608
+ /**
1609
+ Define a new facet.
1610
+ */
1611
+ static define(config = {}) {
1612
+ 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);
1613
+ }
1614
+ /**
1615
+ Returns an extension that adds the given value to this facet.
1616
+ */
1617
+ of(value) {
1618
+ return new FacetProvider([], this, 0 /* Provider.Static */, value);
1619
+ }
1620
+ /**
1621
+ Create an extension that computes a value for the facet from a
1622
+ state. You must take care to declare the parts of the state that
1623
+ this value depends on, since your function is only called again
1624
+ for a new state when one of those parts changed.
1625
+
1626
+ In cases where your value depends only on a single field, you'll
1627
+ want to use the [`from`](https://codemirror.net/6/docs/ref/#state.Facet.from) method instead.
1628
+ */
1629
+ compute(deps, get) {
1630
+ if (this.isStatic)
1631
+ throw new Error("Can't compute a static facet");
1632
+ return new FacetProvider(deps, this, 1 /* Provider.Single */, get);
1633
+ }
1634
+ /**
1635
+ Create an extension that computes zero or more values for this
1636
+ facet from a state.
1637
+ */
1638
+ computeN(deps, get) {
1639
+ if (this.isStatic)
1640
+ throw new Error("Can't compute a static facet");
1641
+ return new FacetProvider(deps, this, 2 /* Provider.Multi */, get);
1642
+ }
1643
+ from(field, get) {
1644
+ if (!get)
1645
+ get = x => x;
1646
+ return this.compute([field], state => get(state.field(field)));
1647
+ }
1648
+ }
1649
+ function sameArray(a, b) {
1650
+ return a == b || a.length == b.length && a.every((e, i) => e === b[i]);
1651
+ }
1652
+ class FacetProvider {
1653
+ constructor(dependencies, facet, type, value) {
1654
+ this.dependencies = dependencies;
1655
+ this.facet = facet;
1656
+ this.type = type;
1657
+ this.value = value;
1658
+ this.id = nextID++;
1659
+ }
1660
+ dynamicSlot(addresses) {
1661
+ var _a;
1662
+ let getter = this.value;
1663
+ let compare = this.facet.compareInput;
1664
+ let id = this.id, idx = addresses[id] >> 1, multi = this.type == 2 /* Provider.Multi */;
1665
+ let depDoc = false, depSel = false, depAddrs = [];
1666
+ for (let dep of this.dependencies) {
1667
+ if (dep == "doc")
1668
+ depDoc = true;
1669
+ else if (dep == "selection")
1670
+ depSel = true;
1671
+ else if ((((_a = addresses[dep.id]) !== null && _a !== void 0 ? _a : 1) & 1) == 0)
1672
+ depAddrs.push(addresses[dep.id]);
1673
+ }
1674
+ return {
1675
+ create(state) {
1676
+ state.values[idx] = getter(state);
1677
+ return 1 /* SlotStatus.Changed */;
1678
+ },
1679
+ update(state, tr) {
1680
+ if ((depDoc && tr.docChanged) || (depSel && (tr.docChanged || tr.selection)) || ensureAll(state, depAddrs)) {
1681
+ let newVal = getter(state);
1682
+ if (multi ? !compareArray(newVal, state.values[idx], compare) : !compare(newVal, state.values[idx])) {
1683
+ state.values[idx] = newVal;
1684
+ return 1 /* SlotStatus.Changed */;
1685
+ }
1686
+ }
1687
+ return 0;
1688
+ },
1689
+ reconfigure: (state, oldState) => {
1690
+ let newVal, oldAddr = oldState.config.address[id];
1691
+ if (oldAddr != null) {
1692
+ let oldVal = getAddr(oldState, oldAddr);
1693
+ if (this.dependencies.every(dep => {
1694
+ return dep instanceof Facet ? oldState.facet(dep) === state.facet(dep) :
1695
+ dep instanceof StateField ? oldState.field(dep, false) == state.field(dep, false) : true;
1696
+ }) || (multi ? compareArray(newVal = getter(state), oldVal, compare) : compare(newVal = getter(state), oldVal))) {
1697
+ state.values[idx] = oldVal;
1698
+ return 0;
1699
+ }
1700
+ }
1701
+ else {
1702
+ newVal = getter(state);
1703
+ }
1704
+ state.values[idx] = newVal;
1705
+ return 1 /* SlotStatus.Changed */;
1706
+ }
1707
+ };
1708
+ }
1709
+ }
1710
+ function compareArray(a, b, compare) {
1711
+ if (a.length != b.length)
1712
+ return false;
1713
+ for (let i = 0; i < a.length; i++)
1714
+ if (!compare(a[i], b[i]))
1715
+ return false;
1716
+ return true;
1717
+ }
1718
+ function ensureAll(state, addrs) {
1719
+ let changed = false;
1720
+ for (let addr of addrs)
1721
+ if (ensureAddr(state, addr) & 1 /* SlotStatus.Changed */)
1722
+ changed = true;
1723
+ return changed;
1724
+ }
1725
+ function dynamicFacetSlot(addresses, facet, providers) {
1726
+ let providerAddrs = providers.map(p => addresses[p.id]);
1727
+ let providerTypes = providers.map(p => p.type);
1728
+ let dynamic = providerAddrs.filter(p => !(p & 1));
1729
+ let idx = addresses[facet.id] >> 1;
1730
+ function get(state) {
1731
+ let values = [];
1732
+ for (let i = 0; i < providerAddrs.length; i++) {
1733
+ let value = getAddr(state, providerAddrs[i]);
1734
+ if (providerTypes[i] == 2 /* Provider.Multi */)
1735
+ for (let val of value)
1736
+ values.push(val);
1737
+ else
1738
+ values.push(value);
1739
+ }
1740
+ return facet.combine(values);
1741
+ }
1742
+ return {
1743
+ create(state) {
1744
+ for (let addr of providerAddrs)
1745
+ ensureAddr(state, addr);
1746
+ state.values[idx] = get(state);
1747
+ return 1 /* SlotStatus.Changed */;
1748
+ },
1749
+ update(state, tr) {
1750
+ if (!ensureAll(state, dynamic))
1751
+ return 0;
1752
+ let value = get(state);
1753
+ if (facet.compare(value, state.values[idx]))
1754
+ return 0;
1755
+ state.values[idx] = value;
1756
+ return 1 /* SlotStatus.Changed */;
1757
+ },
1758
+ reconfigure(state, oldState) {
1759
+ let depChanged = ensureAll(state, providerAddrs);
1760
+ let oldProviders = oldState.config.facets[facet.id], oldValue = oldState.facet(facet);
1761
+ if (oldProviders && !depChanged && sameArray(providers, oldProviders)) {
1762
+ state.values[idx] = oldValue;
1763
+ return 0;
1764
+ }
1765
+ let value = get(state);
1766
+ if (facet.compare(value, oldValue)) {
1767
+ state.values[idx] = oldValue;
1768
+ return 0;
1769
+ }
1770
+ state.values[idx] = value;
1771
+ return 1 /* SlotStatus.Changed */;
1772
+ }
1773
+ };
1774
+ }
1775
+ const initField = /*@__PURE__*/Facet.define({ static: true });
1776
+ /**
1777
+ Fields can store additional information in an editor state, and
1778
+ keep it in sync with the rest of the state.
1779
+ */
1780
+ class StateField {
1781
+ constructor(
1782
+ /**
1783
+ @internal
1784
+ */
1785
+ id, createF, updateF, compareF,
1786
+ /**
1787
+ @internal
1788
+ */
1789
+ spec) {
1790
+ this.id = id;
1791
+ this.createF = createF;
1792
+ this.updateF = updateF;
1793
+ this.compareF = compareF;
1794
+ this.spec = spec;
1795
+ /**
1796
+ @internal
1797
+ */
1798
+ this.provides = undefined;
1799
+ }
1800
+ /**
1801
+ Define a state field.
1802
+ */
1803
+ static define(config) {
1804
+ let field = new StateField(nextID++, config.create, config.update, config.compare || ((a, b) => a === b), config);
1805
+ if (config.provide)
1806
+ field.provides = config.provide(field);
1807
+ return field;
1808
+ }
1809
+ create(state) {
1810
+ let init = state.facet(initField).find(i => i.field == this);
1811
+ return ((init === null || init === void 0 ? void 0 : init.create) || this.createF)(state);
1812
+ }
1813
+ /**
1814
+ @internal
1815
+ */
1816
+ slot(addresses) {
1817
+ let idx = addresses[this.id] >> 1;
1818
+ return {
1819
+ create: (state) => {
1820
+ state.values[idx] = this.create(state);
1821
+ return 1 /* SlotStatus.Changed */;
1822
+ },
1823
+ update: (state, tr) => {
1824
+ let oldVal = state.values[idx];
1825
+ let value = this.updateF(oldVal, tr);
1826
+ if (this.compareF(oldVal, value))
1827
+ return 0;
1828
+ state.values[idx] = value;
1829
+ return 1 /* SlotStatus.Changed */;
1830
+ },
1831
+ reconfigure: (state, oldState) => {
1832
+ if (oldState.config.address[this.id] != null) {
1833
+ state.values[idx] = oldState.field(this);
1834
+ return 0;
1835
+ }
1836
+ state.values[idx] = this.create(state);
1837
+ return 1 /* SlotStatus.Changed */;
1838
+ }
1839
+ };
1840
+ }
1841
+ /**
1842
+ Returns an extension that enables this field and overrides the
1843
+ way it is initialized. Can be useful when you need to provide a
1844
+ non-default starting value for the field.
1845
+ */
1846
+ init(create) {
1847
+ return [this, initField.of({ field: this, create })];
1848
+ }
1849
+ /**
1850
+ State field instances can be used as
1851
+ [`Extension`](https://codemirror.net/6/docs/ref/#state.Extension) values to enable the field in a
1852
+ given state.
1853
+ */
1854
+ get extension() { return this; }
1855
+ }
1856
+ const Prec_ = { lowest: 4, low: 3, default: 2, high: 1, highest: 0 };
1857
+ function prec(value) {
1858
+ return (ext) => new PrecExtension(ext, value);
1859
+ }
1860
+ /**
1861
+ By default extensions are registered in the order they are found
1862
+ in the flattened form of nested array that was provided.
1863
+ Individual extension values can be assigned a precedence to
1864
+ override this. Extensions that do not have a precedence set get
1865
+ the precedence of the nearest parent with a precedence, or
1866
+ [`default`](https://codemirror.net/6/docs/ref/#state.Prec.default) if there is no such parent. The
1867
+ final ordering of extensions is determined by first sorting by
1868
+ precedence and then by order within each precedence.
1869
+ */
1870
+ const Prec = {
1871
+ /**
1872
+ The highest precedence level, for extensions that should end up
1873
+ near the start of the precedence ordering.
1874
+ */
1875
+ highest: /*@__PURE__*/prec(Prec_.highest),
1876
+ /**
1877
+ A higher-than-default precedence, for extensions that should
1878
+ come before those with default precedence.
1879
+ */
1880
+ high: /*@__PURE__*/prec(Prec_.high),
1881
+ /**
1882
+ The default precedence, which is also used for extensions
1883
+ without an explicit precedence.
1884
+ */
1885
+ default: /*@__PURE__*/prec(Prec_.default),
1886
+ /**
1887
+ A lower-than-default precedence.
1888
+ */
1889
+ low: /*@__PURE__*/prec(Prec_.low),
1890
+ /**
1891
+ The lowest precedence level. Meant for things that should end up
1892
+ near the end of the extension order.
1893
+ */
1894
+ lowest: /*@__PURE__*/prec(Prec_.lowest)
1895
+ };
1896
+ class PrecExtension {
1897
+ constructor(inner, prec) {
1898
+ this.inner = inner;
1899
+ this.prec = prec;
1900
+ }
1901
+ }
1902
+ /**
1903
+ Extension compartments can be used to make a configuration
1904
+ dynamic. By [wrapping](https://codemirror.net/6/docs/ref/#state.Compartment.of) part of your
1905
+ configuration in a compartment, you can later
1906
+ [replace](https://codemirror.net/6/docs/ref/#state.Compartment.reconfigure) that part through a
1907
+ transaction.
1908
+ */
1909
+ class Compartment {
1910
+ /**
1911
+ Create an instance of this compartment to add to your [state
1912
+ configuration](https://codemirror.net/6/docs/ref/#state.EditorStateConfig.extensions).
1913
+ */
1914
+ of(ext) { return new CompartmentInstance(this, ext); }
1915
+ /**
1916
+ Create an [effect](https://codemirror.net/6/docs/ref/#state.TransactionSpec.effects) that
1917
+ reconfigures this compartment.
1918
+ */
1919
+ reconfigure(content) {
1920
+ return Compartment.reconfigure.of({ compartment: this, extension: content });
1921
+ }
1922
+ /**
1923
+ Get the current content of the compartment in the state, or
1924
+ `undefined` if it isn't present.
1925
+ */
1926
+ get(state) {
1927
+ return state.config.compartments.get(this);
1928
+ }
1929
+ }
1930
+ class CompartmentInstance {
1931
+ constructor(compartment, inner) {
1932
+ this.compartment = compartment;
1933
+ this.inner = inner;
1934
+ }
1935
+ }
1936
+ class Configuration {
1937
+ constructor(base, compartments, dynamicSlots, address, staticValues, facets) {
1938
+ this.base = base;
1939
+ this.compartments = compartments;
1940
+ this.dynamicSlots = dynamicSlots;
1941
+ this.address = address;
1942
+ this.staticValues = staticValues;
1943
+ this.facets = facets;
1944
+ this.statusTemplate = [];
1945
+ while (this.statusTemplate.length < dynamicSlots.length)
1946
+ this.statusTemplate.push(0 /* SlotStatus.Unresolved */);
1947
+ }
1948
+ staticFacet(facet) {
1949
+ let addr = this.address[facet.id];
1950
+ return addr == null ? facet.default : this.staticValues[addr >> 1];
1951
+ }
1952
+ static resolve(base, compartments, oldState) {
1953
+ let fields = [];
1954
+ let facets = Object.create(null);
1955
+ let newCompartments = new Map();
1956
+ for (let ext of flatten(base, compartments, newCompartments)) {
1957
+ if (ext instanceof StateField)
1958
+ fields.push(ext);
1959
+ else
1960
+ (facets[ext.facet.id] || (facets[ext.facet.id] = [])).push(ext);
1961
+ }
1962
+ let address = Object.create(null);
1963
+ let staticValues = [];
1964
+ let dynamicSlots = [];
1965
+ for (let field of fields) {
1966
+ address[field.id] = dynamicSlots.length << 1;
1967
+ dynamicSlots.push(a => field.slot(a));
1968
+ }
1969
+ let oldFacets = oldState === null || oldState === void 0 ? void 0 : oldState.config.facets;
1970
+ for (let id in facets) {
1971
+ let providers = facets[id], facet = providers[0].facet;
1972
+ let oldProviders = oldFacets && oldFacets[id] || [];
1973
+ if (providers.every(p => p.type == 0 /* Provider.Static */)) {
1974
+ address[facet.id] = (staticValues.length << 1) | 1;
1975
+ if (sameArray(oldProviders, providers)) {
1976
+ staticValues.push(oldState.facet(facet));
1977
+ }
1978
+ else {
1979
+ let value = facet.combine(providers.map(p => p.value));
1980
+ staticValues.push(oldState && facet.compare(value, oldState.facet(facet)) ? oldState.facet(facet) : value);
1981
+ }
1982
+ }
1983
+ else {
1984
+ for (let p of providers) {
1985
+ if (p.type == 0 /* Provider.Static */) {
1986
+ address[p.id] = (staticValues.length << 1) | 1;
1987
+ staticValues.push(p.value);
1988
+ }
1989
+ else {
1990
+ address[p.id] = dynamicSlots.length << 1;
1991
+ dynamicSlots.push(a => p.dynamicSlot(a));
1992
+ }
1993
+ }
1994
+ address[facet.id] = dynamicSlots.length << 1;
1995
+ dynamicSlots.push(a => dynamicFacetSlot(a, facet, providers));
1996
+ }
1997
+ }
1998
+ let dynamic = dynamicSlots.map(f => f(address));
1999
+ return new Configuration(base, newCompartments, dynamic, address, staticValues, facets);
2000
+ }
2001
+ }
2002
+ function flatten(extension, compartments, newCompartments) {
2003
+ let result = [[], [], [], [], []];
2004
+ let seen = new Map();
2005
+ function inner(ext, prec) {
2006
+ let known = seen.get(ext);
2007
+ if (known != null) {
2008
+ if (known <= prec)
2009
+ return;
2010
+ let found = result[known].indexOf(ext);
2011
+ if (found > -1)
2012
+ result[known].splice(found, 1);
2013
+ if (ext instanceof CompartmentInstance)
2014
+ newCompartments.delete(ext.compartment);
2015
+ }
2016
+ seen.set(ext, prec);
2017
+ if (Array.isArray(ext)) {
2018
+ for (let e of ext)
2019
+ inner(e, prec);
2020
+ }
2021
+ else if (ext instanceof CompartmentInstance) {
2022
+ if (newCompartments.has(ext.compartment))
2023
+ throw new RangeError(`Duplicate use of compartment in extensions`);
2024
+ let content = compartments.get(ext.compartment) || ext.inner;
2025
+ newCompartments.set(ext.compartment, content);
2026
+ inner(content, prec);
2027
+ }
2028
+ else if (ext instanceof PrecExtension) {
2029
+ inner(ext.inner, ext.prec);
2030
+ }
2031
+ else if (ext instanceof StateField) {
2032
+ result[prec].push(ext);
2033
+ if (ext.provides)
2034
+ inner(ext.provides, prec);
2035
+ }
2036
+ else if (ext instanceof FacetProvider) {
2037
+ result[prec].push(ext);
2038
+ if (ext.facet.extensions)
2039
+ inner(ext.facet.extensions, Prec_.default);
2040
+ }
2041
+ else {
2042
+ let content = ext.extension;
2043
+ if (!content)
2044
+ throw new Error(`Unrecognized extension value in extension set (${ext}). This sometimes happens because multiple instances of @codemirror/state are loaded, breaking instanceof checks.`);
2045
+ inner(content, prec);
2046
+ }
2047
+ }
2048
+ inner(extension, Prec_.default);
2049
+ return result.reduce((a, b) => a.concat(b));
2050
+ }
2051
+ function ensureAddr(state, addr) {
2052
+ if (addr & 1)
2053
+ return 2 /* SlotStatus.Computed */;
2054
+ let idx = addr >> 1;
2055
+ let status = state.status[idx];
2056
+ if (status == 4 /* SlotStatus.Computing */)
2057
+ throw new Error("Cyclic dependency between fields and/or facets");
2058
+ if (status & 2 /* SlotStatus.Computed */)
2059
+ return status;
2060
+ state.status[idx] = 4 /* SlotStatus.Computing */;
2061
+ let changed = state.computeSlot(state, state.config.dynamicSlots[idx]);
2062
+ return state.status[idx] = 2 /* SlotStatus.Computed */ | changed;
2063
+ }
2064
+ function getAddr(state, addr) {
2065
+ return addr & 1 ? state.config.staticValues[addr >> 1] : state.values[addr >> 1];
2066
+ }
2067
+
2068
+ const languageData = /*@__PURE__*/Facet.define();
2069
+ const allowMultipleSelections = /*@__PURE__*/Facet.define({
2070
+ combine: values => values.some(v => v),
2071
+ static: true
2072
+ });
2073
+ const lineSeparator = /*@__PURE__*/Facet.define({
2074
+ combine: values => values.length ? values[0] : undefined,
2075
+ static: true
2076
+ });
2077
+ const changeFilter = /*@__PURE__*/Facet.define();
2078
+ const transactionFilter = /*@__PURE__*/Facet.define();
2079
+ const transactionExtender = /*@__PURE__*/Facet.define();
2080
+ const readOnly = /*@__PURE__*/Facet.define({
2081
+ combine: values => values.length ? values[0] : false
2082
+ });
2083
+
2084
+ /**
2085
+ Annotations are tagged values that are used to add metadata to
2086
+ transactions in an extensible way. They should be used to model
2087
+ things that effect the entire transaction (such as its [time
2088
+ stamp](https://codemirror.net/6/docs/ref/#state.Transaction^time) or information about its
2089
+ [origin](https://codemirror.net/6/docs/ref/#state.Transaction^userEvent)). For effects that happen
2090
+ _alongside_ the other changes made by the transaction, [state
2091
+ effects](https://codemirror.net/6/docs/ref/#state.StateEffect) are more appropriate.
2092
+ */
2093
+ class Annotation {
2094
+ /**
2095
+ @internal
2096
+ */
2097
+ constructor(
2098
+ /**
2099
+ The annotation type.
2100
+ */
2101
+ type,
2102
+ /**
2103
+ The value of this annotation.
2104
+ */
2105
+ value) {
2106
+ this.type = type;
2107
+ this.value = value;
2108
+ }
2109
+ /**
2110
+ Define a new type of annotation.
2111
+ */
2112
+ static define() { return new AnnotationType(); }
2113
+ }
2114
+ /**
2115
+ Marker that identifies a type of [annotation](https://codemirror.net/6/docs/ref/#state.Annotation).
2116
+ */
2117
+ class AnnotationType {
2118
+ /**
2119
+ Create an instance of this annotation.
2120
+ */
2121
+ of(value) { return new Annotation(this, value); }
2122
+ }
2123
+ /**
2124
+ Representation of a type of state effect. Defined with
2125
+ [`StateEffect.define`](https://codemirror.net/6/docs/ref/#state.StateEffect^define).
2126
+ */
2127
+ class StateEffectType {
2128
+ /**
2129
+ @internal
2130
+ */
2131
+ constructor(
2132
+ // The `any` types in these function types are there to work
2133
+ // around TypeScript issue #37631, where the type guard on
2134
+ // `StateEffect.is` mysteriously stops working when these properly
2135
+ // have type `Value`.
2136
+ /**
2137
+ @internal
2138
+ */
2139
+ map) {
2140
+ this.map = map;
2141
+ }
2142
+ /**
2143
+ Create a [state effect](https://codemirror.net/6/docs/ref/#state.StateEffect) instance of this
2144
+ type.
2145
+ */
2146
+ of(value) { return new StateEffect(this, value); }
2147
+ }
2148
+ /**
2149
+ State effects can be used to represent additional effects
2150
+ associated with a [transaction](https://codemirror.net/6/docs/ref/#state.Transaction.effects). They
2151
+ are often useful to model changes to custom [state
2152
+ fields](https://codemirror.net/6/docs/ref/#state.StateField), when those changes aren't implicit in
2153
+ document or selection changes.
2154
+ */
2155
+ class StateEffect {
2156
+ /**
2157
+ @internal
2158
+ */
2159
+ constructor(
2160
+ /**
2161
+ @internal
2162
+ */
2163
+ type,
2164
+ /**
2165
+ The value of this effect.
2166
+ */
2167
+ value) {
2168
+ this.type = type;
2169
+ this.value = value;
2170
+ }
2171
+ /**
2172
+ Map this effect through a position mapping. Will return
2173
+ `undefined` when that ends up deleting the effect.
2174
+ */
2175
+ map(mapping) {
2176
+ let mapped = this.type.map(this.value, mapping);
2177
+ return mapped === undefined ? undefined : mapped == this.value ? this : new StateEffect(this.type, mapped);
2178
+ }
2179
+ /**
2180
+ Tells you whether this effect object is of a given
2181
+ [type](https://codemirror.net/6/docs/ref/#state.StateEffectType).
2182
+ */
2183
+ is(type) { return this.type == type; }
2184
+ /**
2185
+ Define a new effect type. The type parameter indicates the type
2186
+ of values that his effect holds. It should be a type that
2187
+ doesn't include `undefined`, since that is used in
2188
+ [mapping](https://codemirror.net/6/docs/ref/#state.StateEffect.map) to indicate that an effect is
2189
+ removed.
2190
+ */
2191
+ static define(spec = {}) {
2192
+ return new StateEffectType(spec.map || (v => v));
2193
+ }
2194
+ /**
2195
+ Map an array of effects through a change set.
2196
+ */
2197
+ static mapEffects(effects, mapping) {
2198
+ if (!effects.length)
2199
+ return effects;
2200
+ let result = [];
2201
+ for (let effect of effects) {
2202
+ let mapped = effect.map(mapping);
2203
+ if (mapped)
2204
+ result.push(mapped);
2205
+ }
2206
+ return result;
2207
+ }
2208
+ }
2209
+ /**
2210
+ This effect can be used to reconfigure the root extensions of
2211
+ the editor. Doing this will discard any extensions
2212
+ [appended](https://codemirror.net/6/docs/ref/#state.StateEffect^appendConfig), but does not reset
2213
+ the content of [reconfigured](https://codemirror.net/6/docs/ref/#state.Compartment.reconfigure)
2214
+ compartments.
2215
+ */
2216
+ StateEffect.reconfigure = /*@__PURE__*/StateEffect.define();
2217
+ /**
2218
+ Append extensions to the top-level configuration of the editor.
2219
+ */
2220
+ StateEffect.appendConfig = /*@__PURE__*/StateEffect.define();
2221
+ /**
2222
+ Changes to the editor state are grouped into transactions.
2223
+ Typically, a user action creates a single transaction, which may
2224
+ contain any number of document changes, may change the selection,
2225
+ or have other effects. Create a transaction by calling
2226
+ [`EditorState.update`](https://codemirror.net/6/docs/ref/#state.EditorState.update), or immediately
2227
+ dispatch one by calling
2228
+ [`EditorView.dispatch`](https://codemirror.net/6/docs/ref/#view.EditorView.dispatch).
2229
+ */
2230
+ class Transaction {
2231
+ constructor(
2232
+ /**
2233
+ The state from which the transaction starts.
2234
+ */
2235
+ startState,
2236
+ /**
2237
+ The document changes made by this transaction.
2238
+ */
2239
+ changes,
2240
+ /**
2241
+ The selection set by this transaction, or undefined if it
2242
+ doesn't explicitly set a selection.
2243
+ */
2244
+ selection,
2245
+ /**
2246
+ The effects added to the transaction.
2247
+ */
2248
+ effects,
2249
+ /**
2250
+ @internal
2251
+ */
2252
+ annotations,
2253
+ /**
2254
+ Whether the selection should be scrolled into view after this
2255
+ transaction is dispatched.
2256
+ */
2257
+ scrollIntoView) {
2258
+ this.startState = startState;
2259
+ this.changes = changes;
2260
+ this.selection = selection;
2261
+ this.effects = effects;
2262
+ this.annotations = annotations;
2263
+ this.scrollIntoView = scrollIntoView;
2264
+ /**
2265
+ @internal
2266
+ */
2267
+ this._doc = null;
2268
+ /**
2269
+ @internal
2270
+ */
2271
+ this._state = null;
2272
+ if (selection)
2273
+ checkSelection(selection, changes.newLength);
2274
+ if (!annotations.some((a) => a.type == Transaction.time))
2275
+ this.annotations = annotations.concat(Transaction.time.of(Date.now()));
2276
+ }
2277
+ /**
2278
+ @internal
2279
+ */
2280
+ static create(startState, changes, selection, effects, annotations, scrollIntoView) {
2281
+ return new Transaction(startState, changes, selection, effects, annotations, scrollIntoView);
2282
+ }
2283
+ /**
2284
+ The new document produced by the transaction. Contrary to
2285
+ [`.state`](https://codemirror.net/6/docs/ref/#state.Transaction.state)`.doc`, accessing this won't
2286
+ force the entire new state to be computed right away, so it is
2287
+ recommended that [transaction
2288
+ filters](https://codemirror.net/6/docs/ref/#state.EditorState^transactionFilter) use this getter
2289
+ when they need to look at the new document.
2290
+ */
2291
+ get newDoc() {
2292
+ return this._doc || (this._doc = this.changes.apply(this.startState.doc));
2293
+ }
2294
+ /**
2295
+ The new selection produced by the transaction. If
2296
+ [`this.selection`](https://codemirror.net/6/docs/ref/#state.Transaction.selection) is undefined,
2297
+ this will [map](https://codemirror.net/6/docs/ref/#state.EditorSelection.map) the start state's
2298
+ current selection through the changes made by the transaction.
2299
+ */
2300
+ get newSelection() {
2301
+ return this.selection || this.startState.selection.map(this.changes);
2302
+ }
2303
+ /**
2304
+ The new state created by the transaction. Computed on demand
2305
+ (but retained for subsequent access), so it is recommended not to
2306
+ access it in [transaction
2307
+ filters](https://codemirror.net/6/docs/ref/#state.EditorState^transactionFilter) when possible.
2308
+ */
2309
+ get state() {
2310
+ if (!this._state)
2311
+ this.startState.applyTransaction(this);
2312
+ return this._state;
2313
+ }
2314
+ /**
2315
+ Get the value of the given annotation type, if any.
2316
+ */
2317
+ annotation(type) {
2318
+ for (let ann of this.annotations)
2319
+ if (ann.type == type)
2320
+ return ann.value;
2321
+ return undefined;
2322
+ }
2323
+ /**
2324
+ Indicates whether the transaction changed the document.
2325
+ */
2326
+ get docChanged() { return !this.changes.empty; }
2327
+ /**
2328
+ Indicates whether this transaction reconfigures the state
2329
+ (through a [configuration compartment](https://codemirror.net/6/docs/ref/#state.Compartment) or
2330
+ with a top-level configuration
2331
+ [effect](https://codemirror.net/6/docs/ref/#state.StateEffect^reconfigure).
2332
+ */
2333
+ get reconfigured() { return this.startState.config != this.state.config; }
2334
+ /**
2335
+ Returns true if the transaction has a [user
2336
+ event](https://codemirror.net/6/docs/ref/#state.Transaction^userEvent) annotation that is equal to
2337
+ or more specific than `event`. For example, if the transaction
2338
+ has `"select.pointer"` as user event, `"select"` and
2339
+ `"select.pointer"` will match it.
2340
+ */
2341
+ isUserEvent(event) {
2342
+ let e = this.annotation(Transaction.userEvent);
2343
+ return !!(e && (e == event || e.length > event.length && e.slice(0, event.length) == event && e[event.length] == "."));
2344
+ }
2345
+ }
2346
+ /**
2347
+ Annotation used to store transaction timestamps. Automatically
2348
+ added to every transaction, holding `Date.now()`.
2349
+ */
2350
+ Transaction.time = /*@__PURE__*/Annotation.define();
2351
+ /**
2352
+ Annotation used to associate a transaction with a user interface
2353
+ event. Holds a string identifying the event, using a
2354
+ dot-separated format to support attaching more specific
2355
+ information. The events used by the core libraries are:
2356
+
2357
+ - `"input"` when content is entered
2358
+ - `"input.type"` for typed input
2359
+ - `"input.type.compose"` for composition
2360
+ - `"input.paste"` for pasted input
2361
+ - `"input.drop"` when adding content with drag-and-drop
2362
+ - `"input.complete"` when autocompleting
2363
+ - `"delete"` when the user deletes content
2364
+ - `"delete.selection"` when deleting the selection
2365
+ - `"delete.forward"` when deleting forward from the selection
2366
+ - `"delete.backward"` when deleting backward from the selection
2367
+ - `"delete.cut"` when cutting to the clipboard
2368
+ - `"move"` when content is moved
2369
+ - `"move.drop"` when content is moved within the editor through drag-and-drop
2370
+ - `"select"` when explicitly changing the selection
2371
+ - `"select.pointer"` when selecting with a mouse or other pointing device
2372
+ - `"undo"` and `"redo"` for history actions
2373
+
2374
+ Use [`isUserEvent`](https://codemirror.net/6/docs/ref/#state.Transaction.isUserEvent) to check
2375
+ whether the annotation matches a given event.
2376
+ */
2377
+ Transaction.userEvent = /*@__PURE__*/Annotation.define();
2378
+ /**
2379
+ Annotation indicating whether a transaction should be added to
2380
+ the undo history or not.
2381
+ */
2382
+ Transaction.addToHistory = /*@__PURE__*/Annotation.define();
2383
+ /**
2384
+ Annotation indicating (when present and true) that a transaction
2385
+ represents a change made by some other actor, not the user. This
2386
+ is used, for example, to tag other people's changes in
2387
+ collaborative editing.
2388
+ */
2389
+ Transaction.remote = /*@__PURE__*/Annotation.define();
2390
+ function joinRanges(a, b) {
2391
+ let result = [];
2392
+ for (let iA = 0, iB = 0;;) {
2393
+ let from, to;
2394
+ if (iA < a.length && (iB == b.length || b[iB] >= a[iA])) {
2395
+ from = a[iA++];
2396
+ to = a[iA++];
2397
+ }
2398
+ else if (iB < b.length) {
2399
+ from = b[iB++];
2400
+ to = b[iB++];
2401
+ }
2402
+ else
2403
+ return result;
2404
+ if (!result.length || result[result.length - 1] < from)
2405
+ result.push(from, to);
2406
+ else if (result[result.length - 1] < to)
2407
+ result[result.length - 1] = to;
2408
+ }
2409
+ }
2410
+ function mergeTransaction(a, b, sequential) {
2411
+ var _a;
2412
+ let mapForA, mapForB, changes;
2413
+ if (sequential) {
2414
+ mapForA = b.changes;
2415
+ mapForB = ChangeSet.empty(b.changes.length);
2416
+ changes = a.changes.compose(b.changes);
2417
+ }
2418
+ else {
2419
+ mapForA = b.changes.map(a.changes);
2420
+ mapForB = a.changes.mapDesc(b.changes, true);
2421
+ changes = a.changes.compose(mapForA);
2422
+ }
2423
+ return {
2424
+ changes,
2425
+ selection: b.selection ? b.selection.map(mapForB) : (_a = a.selection) === null || _a === void 0 ? void 0 : _a.map(mapForA),
2426
+ effects: StateEffect.mapEffects(a.effects, mapForA).concat(StateEffect.mapEffects(b.effects, mapForB)),
2427
+ annotations: a.annotations.length ? a.annotations.concat(b.annotations) : b.annotations,
2428
+ scrollIntoView: a.scrollIntoView || b.scrollIntoView
2429
+ };
2430
+ }
2431
+ function resolveTransactionInner(state, spec, docSize) {
2432
+ let sel = spec.selection, annotations = asArray(spec.annotations);
2433
+ if (spec.userEvent)
2434
+ annotations = annotations.concat(Transaction.userEvent.of(spec.userEvent));
2435
+ return {
2436
+ changes: spec.changes instanceof ChangeSet ? spec.changes
2437
+ : ChangeSet.of(spec.changes || [], docSize, state.facet(lineSeparator)),
2438
+ selection: sel && (sel instanceof EditorSelection ? sel : EditorSelection.single(sel.anchor, sel.head)),
2439
+ effects: asArray(spec.effects),
2440
+ annotations,
2441
+ scrollIntoView: !!spec.scrollIntoView
2442
+ };
2443
+ }
2444
+ function resolveTransaction(state, specs, filter) {
2445
+ let s = resolveTransactionInner(state, specs.length ? specs[0] : {}, state.doc.length);
2446
+ if (specs.length && specs[0].filter === false)
2447
+ filter = false;
2448
+ for (let i = 1; i < specs.length; i++) {
2449
+ if (specs[i].filter === false)
2450
+ filter = false;
2451
+ let seq = !!specs[i].sequential;
2452
+ s = mergeTransaction(s, resolveTransactionInner(state, specs[i], seq ? s.changes.newLength : state.doc.length), seq);
2453
+ }
2454
+ let tr = Transaction.create(state, s.changes, s.selection, s.effects, s.annotations, s.scrollIntoView);
2455
+ return extendTransaction(filter ? filterTransaction(tr) : tr);
2456
+ }
2457
+ // Finish a transaction by applying filters if necessary.
2458
+ function filterTransaction(tr) {
2459
+ let state = tr.startState;
2460
+ // Change filters
2461
+ let result = true;
2462
+ for (let filter of state.facet(changeFilter)) {
2463
+ let value = filter(tr);
2464
+ if (value === false) {
2465
+ result = false;
2466
+ break;
2467
+ }
2468
+ if (Array.isArray(value))
2469
+ result = result === true ? value : joinRanges(result, value);
2470
+ }
2471
+ if (result !== true) {
2472
+ let changes, back;
2473
+ if (result === false) {
2474
+ back = tr.changes.invertedDesc;
2475
+ changes = ChangeSet.empty(state.doc.length);
2476
+ }
2477
+ else {
2478
+ let filtered = tr.changes.filter(result);
2479
+ changes = filtered.changes;
2480
+ back = filtered.filtered.mapDesc(filtered.changes).invertedDesc;
2481
+ }
2482
+ tr = Transaction.create(state, changes, tr.selection && tr.selection.map(back), StateEffect.mapEffects(tr.effects, back), tr.annotations, tr.scrollIntoView);
2483
+ }
2484
+ // Transaction filters
2485
+ let filters = state.facet(transactionFilter);
2486
+ for (let i = filters.length - 1; i >= 0; i--) {
2487
+ let filtered = filters[i](tr);
2488
+ if (filtered instanceof Transaction)
2489
+ tr = filtered;
2490
+ else if (Array.isArray(filtered) && filtered.length == 1 && filtered[0] instanceof Transaction)
2491
+ tr = filtered[0];
2492
+ else
2493
+ tr = resolveTransaction(state, asArray(filtered), false);
2494
+ }
2495
+ return tr;
2496
+ }
2497
+ function extendTransaction(tr) {
2498
+ let state = tr.startState, extenders = state.facet(transactionExtender), spec = tr;
2499
+ for (let i = extenders.length - 1; i >= 0; i--) {
2500
+ let extension = extenders[i](tr);
2501
+ if (extension && Object.keys(extension).length)
2502
+ spec = mergeTransaction(spec, resolveTransactionInner(state, extension, tr.changes.newLength), true);
2503
+ }
2504
+ return spec == tr ? tr : Transaction.create(state, tr.changes, tr.selection, spec.effects, spec.annotations, spec.scrollIntoView);
2505
+ }
2506
+ const none = [];
2507
+ function asArray(value) {
2508
+ return value == null ? none : Array.isArray(value) ? value : [value];
2509
+ }
2510
+
2511
+ /**
2512
+ The categories produced by a [character
2513
+ categorizer](https://codemirror.net/6/docs/ref/#state.EditorState.charCategorizer). These are used
2514
+ do things like selecting by word.
2515
+ */
2516
+ var CharCategory = /*@__PURE__*/(function (CharCategory) {
2517
+ /**
2518
+ Word characters.
2519
+ */
2520
+ CharCategory[CharCategory["Word"] = 0] = "Word";
2521
+ /**
2522
+ Whitespace.
2523
+ */
2524
+ CharCategory[CharCategory["Space"] = 1] = "Space";
2525
+ /**
2526
+ Anything else.
2527
+ */
2528
+ CharCategory[CharCategory["Other"] = 2] = "Other";
2529
+ return CharCategory})(CharCategory || (CharCategory = {}));
2530
+ const nonASCIISingleCaseWordChar = /[\u00df\u0587\u0590-\u05f4\u0600-\u06ff\u3040-\u309f\u30a0-\u30ff\u3400-\u4db5\u4e00-\u9fcc\uac00-\ud7af]/;
2531
+ let wordChar;
2532
+ try {
2533
+ wordChar = /*@__PURE__*/new RegExp("[\\p{Alphabetic}\\p{Number}_]", "u");
2534
+ }
2535
+ catch (_) { }
2536
+ function hasWordChar(str) {
2537
+ if (wordChar)
2538
+ return wordChar.test(str);
2539
+ for (let i = 0; i < str.length; i++) {
2540
+ let ch = str[i];
2541
+ if (/\w/.test(ch) || ch > "\x80" && (ch.toUpperCase() != ch.toLowerCase() || nonASCIISingleCaseWordChar.test(ch)))
2542
+ return true;
2543
+ }
2544
+ return false;
2545
+ }
2546
+ function makeCategorizer(wordChars) {
2547
+ return (char) => {
2548
+ if (!/\S/.test(char))
2549
+ return CharCategory.Space;
2550
+ if (hasWordChar(char))
2551
+ return CharCategory.Word;
2552
+ for (let i = 0; i < wordChars.length; i++)
2553
+ if (char.indexOf(wordChars[i]) > -1)
2554
+ return CharCategory.Word;
2555
+ return CharCategory.Other;
2556
+ };
2557
+ }
2558
+
2559
+ /**
2560
+ The editor state class is a persistent (immutable) data structure.
2561
+ To update a state, you [create](https://codemirror.net/6/docs/ref/#state.EditorState.update) a
2562
+ [transaction](https://codemirror.net/6/docs/ref/#state.Transaction), which produces a _new_ state
2563
+ instance, without modifying the original object.
2564
+
2565
+ As such, _never_ mutate properties of a state directly. That'll
2566
+ just break things.
2567
+ */
2568
+ class EditorState {
2569
+ constructor(
2570
+ /**
2571
+ @internal
2572
+ */
2573
+ config,
2574
+ /**
2575
+ The current document.
2576
+ */
2577
+ doc,
2578
+ /**
2579
+ The current selection.
2580
+ */
2581
+ selection,
2582
+ /**
2583
+ @internal
2584
+ */
2585
+ values, computeSlot, tr) {
2586
+ this.config = config;
2587
+ this.doc = doc;
2588
+ this.selection = selection;
2589
+ this.values = values;
2590
+ this.status = config.statusTemplate.slice();
2591
+ this.computeSlot = computeSlot;
2592
+ // Fill in the computed state immediately, so that further queries
2593
+ // for it made during the update return this state
2594
+ if (tr)
2595
+ tr._state = this;
2596
+ for (let i = 0; i < this.config.dynamicSlots.length; i++)
2597
+ ensureAddr(this, i << 1);
2598
+ this.computeSlot = null;
2599
+ }
2600
+ field(field, require = true) {
2601
+ let addr = this.config.address[field.id];
2602
+ if (addr == null) {
2603
+ if (require)
2604
+ throw new RangeError("Field is not present in this state");
2605
+ return undefined;
2606
+ }
2607
+ ensureAddr(this, addr);
2608
+ return getAddr(this, addr);
2609
+ }
2610
+ /**
2611
+ Create a [transaction](https://codemirror.net/6/docs/ref/#state.Transaction) that updates this
2612
+ state. Any number of [transaction specs](https://codemirror.net/6/docs/ref/#state.TransactionSpec)
2613
+ can be passed. Unless
2614
+ [`sequential`](https://codemirror.net/6/docs/ref/#state.TransactionSpec.sequential) is set, the
2615
+ [changes](https://codemirror.net/6/docs/ref/#state.TransactionSpec.changes) (if any) of each spec
2616
+ are assumed to start in the _current_ document (not the document
2617
+ produced by previous specs), and its
2618
+ [selection](https://codemirror.net/6/docs/ref/#state.TransactionSpec.selection) and
2619
+ [effects](https://codemirror.net/6/docs/ref/#state.TransactionSpec.effects) are assumed to refer
2620
+ to the document created by its _own_ changes. The resulting
2621
+ transaction contains the combined effect of all the different
2622
+ specs. For [selection](https://codemirror.net/6/docs/ref/#state.TransactionSpec.selection), later
2623
+ specs take precedence over earlier ones.
2624
+ */
2625
+ update(...specs) {
2626
+ return resolveTransaction(this, specs, true);
2627
+ }
2628
+ /**
2629
+ @internal
2630
+ */
2631
+ applyTransaction(tr) {
2632
+ let conf = this.config, { base, compartments } = conf;
2633
+ for (let effect of tr.effects) {
2634
+ if (effect.is(Compartment.reconfigure)) {
2635
+ if (conf) {
2636
+ compartments = new Map;
2637
+ conf.compartments.forEach((val, key) => compartments.set(key, val));
2638
+ conf = null;
2639
+ }
2640
+ compartments.set(effect.value.compartment, effect.value.extension);
2641
+ }
2642
+ else if (effect.is(StateEffect.reconfigure)) {
2643
+ conf = null;
2644
+ base = effect.value;
2645
+ }
2646
+ else if (effect.is(StateEffect.appendConfig)) {
2647
+ conf = null;
2648
+ base = asArray(base).concat(effect.value);
2649
+ }
2650
+ }
2651
+ let startValues;
2652
+ if (!conf) {
2653
+ conf = Configuration.resolve(base, compartments, this);
2654
+ let intermediateState = new EditorState(conf, this.doc, this.selection, conf.dynamicSlots.map(() => null), (state, slot) => slot.reconfigure(state, this), null);
2655
+ startValues = intermediateState.values;
2656
+ }
2657
+ else {
2658
+ startValues = tr.startState.values.slice();
2659
+ }
2660
+ let selection = tr.startState.facet(allowMultipleSelections) ? tr.newSelection : tr.newSelection.asSingle();
2661
+ new EditorState(conf, tr.newDoc, selection, startValues, (state, slot) => slot.update(state, tr), tr);
2662
+ }
2663
+ /**
2664
+ Create a [transaction spec](https://codemirror.net/6/docs/ref/#state.TransactionSpec) that
2665
+ replaces every selection range with the given content.
2666
+ */
2667
+ replaceSelection(text) {
2668
+ if (typeof text == "string")
2669
+ text = this.toText(text);
2670
+ return this.changeByRange(range => ({ changes: { from: range.from, to: range.to, insert: text },
2671
+ range: EditorSelection.cursor(range.from + text.length) }));
2672
+ }
2673
+ /**
2674
+ Create a set of changes and a new selection by running the given
2675
+ function for each range in the active selection. The function
2676
+ can return an optional set of changes (in the coordinate space
2677
+ of the start document), plus an updated range (in the coordinate
2678
+ space of the document produced by the call's own changes). This
2679
+ method will merge all the changes and ranges into a single
2680
+ changeset and selection, and return it as a [transaction
2681
+ spec](https://codemirror.net/6/docs/ref/#state.TransactionSpec), which can be passed to
2682
+ [`update`](https://codemirror.net/6/docs/ref/#state.EditorState.update).
2683
+ */
2684
+ changeByRange(f) {
2685
+ let sel = this.selection;
2686
+ let result1 = f(sel.ranges[0]);
2687
+ let changes = this.changes(result1.changes), ranges = [result1.range];
2688
+ let effects = asArray(result1.effects);
2689
+ for (let i = 1; i < sel.ranges.length; i++) {
2690
+ let result = f(sel.ranges[i]);
2691
+ let newChanges = this.changes(result.changes), newMapped = newChanges.map(changes);
2692
+ for (let j = 0; j < i; j++)
2693
+ ranges[j] = ranges[j].map(newMapped);
2694
+ let mapBy = changes.mapDesc(newChanges, true);
2695
+ ranges.push(result.range.map(mapBy));
2696
+ changes = changes.compose(newMapped);
2697
+ effects = StateEffect.mapEffects(effects, newMapped).concat(StateEffect.mapEffects(asArray(result.effects), mapBy));
2698
+ }
2699
+ return {
2700
+ changes,
2701
+ selection: EditorSelection.create(ranges, sel.mainIndex),
2702
+ effects
2703
+ };
2704
+ }
2705
+ /**
2706
+ Create a [change set](https://codemirror.net/6/docs/ref/#state.ChangeSet) from the given change
2707
+ description, taking the state's document length and line
2708
+ separator into account.
2709
+ */
2710
+ changes(spec = []) {
2711
+ if (spec instanceof ChangeSet)
2712
+ return spec;
2713
+ return ChangeSet.of(spec, this.doc.length, this.facet(EditorState.lineSeparator));
2714
+ }
2715
+ /**
2716
+ Using the state's [line
2717
+ separator](https://codemirror.net/6/docs/ref/#state.EditorState^lineSeparator), create a
2718
+ [`Text`](https://codemirror.net/6/docs/ref/#state.Text) instance from the given string.
2719
+ */
2720
+ toText(string) {
2721
+ return Text.of(string.split(this.facet(EditorState.lineSeparator) || DefaultSplit));
2722
+ }
2723
+ /**
2724
+ Return the given range of the document as a string.
2725
+ */
2726
+ sliceDoc(from = 0, to = this.doc.length) {
2727
+ return this.doc.sliceString(from, to, this.lineBreak);
2728
+ }
2729
+ /**
2730
+ Get the value of a state [facet](https://codemirror.net/6/docs/ref/#state.Facet).
2731
+ */
2732
+ facet(facet) {
2733
+ let addr = this.config.address[facet.id];
2734
+ if (addr == null)
2735
+ return facet.default;
2736
+ ensureAddr(this, addr);
2737
+ return getAddr(this, addr);
2738
+ }
2739
+ /**
2740
+ Convert this state to a JSON-serializable object. When custom
2741
+ fields should be serialized, you can pass them in as an object
2742
+ mapping property names (in the resulting object, which should
2743
+ not use `doc` or `selection`) to fields.
2744
+ */
2745
+ toJSON(fields) {
2746
+ let result = {
2747
+ doc: this.sliceDoc(),
2748
+ selection: this.selection.toJSON()
2749
+ };
2750
+ if (fields)
2751
+ for (let prop in fields) {
2752
+ let value = fields[prop];
2753
+ if (value instanceof StateField && this.config.address[value.id] != null)
2754
+ result[prop] = value.spec.toJSON(this.field(fields[prop]), this);
2755
+ }
2756
+ return result;
2757
+ }
2758
+ /**
2759
+ Deserialize a state from its JSON representation. When custom
2760
+ fields should be deserialized, pass the same object you passed
2761
+ to [`toJSON`](https://codemirror.net/6/docs/ref/#state.EditorState.toJSON) when serializing as
2762
+ third argument.
2763
+ */
2764
+ static fromJSON(json, config = {}, fields) {
2765
+ if (!json || typeof json.doc != "string")
2766
+ throw new RangeError("Invalid JSON representation for EditorState");
2767
+ let fieldInit = [];
2768
+ if (fields)
2769
+ for (let prop in fields) {
2770
+ if (Object.prototype.hasOwnProperty.call(json, prop)) {
2771
+ let field = fields[prop], value = json[prop];
2772
+ fieldInit.push(field.init(state => field.spec.fromJSON(value, state)));
2773
+ }
2774
+ }
2775
+ return EditorState.create({
2776
+ doc: json.doc,
2777
+ selection: EditorSelection.fromJSON(json.selection),
2778
+ extensions: config.extensions ? fieldInit.concat([config.extensions]) : fieldInit
2779
+ });
2780
+ }
2781
+ /**
2782
+ Create a new state. You'll usually only need this when
2783
+ initializing an editor—updated states are created by applying
2784
+ transactions.
2785
+ */
2786
+ static create(config = {}) {
2787
+ let configuration = Configuration.resolve(config.extensions || [], new Map);
2788
+ let doc = config.doc instanceof Text ? config.doc
2789
+ : Text.of((config.doc || "").split(configuration.staticFacet(EditorState.lineSeparator) || DefaultSplit));
2790
+ let selection = !config.selection ? EditorSelection.single(0)
2791
+ : config.selection instanceof EditorSelection ? config.selection
2792
+ : EditorSelection.single(config.selection.anchor, config.selection.head);
2793
+ checkSelection(selection, doc.length);
2794
+ if (!configuration.staticFacet(allowMultipleSelections))
2795
+ selection = selection.asSingle();
2796
+ return new EditorState(configuration, doc, selection, configuration.dynamicSlots.map(() => null), (state, slot) => slot.create(state), null);
2797
+ }
2798
+ /**
2799
+ The size (in columns) of a tab in the document, determined by
2800
+ the [`tabSize`](https://codemirror.net/6/docs/ref/#state.EditorState^tabSize) facet.
2801
+ */
2802
+ get tabSize() { return this.facet(EditorState.tabSize); }
2803
+ /**
2804
+ Get the proper [line-break](https://codemirror.net/6/docs/ref/#state.EditorState^lineSeparator)
2805
+ string for this state.
2806
+ */
2807
+ get lineBreak() { return this.facet(EditorState.lineSeparator) || "\n"; }
2808
+ /**
2809
+ Returns true when the editor is
2810
+ [configured](https://codemirror.net/6/docs/ref/#state.EditorState^readOnly) to be read-only.
2811
+ */
2812
+ get readOnly() { return this.facet(readOnly); }
2813
+ /**
2814
+ Look up a translation for the given phrase (via the
2815
+ [`phrases`](https://codemirror.net/6/docs/ref/#state.EditorState^phrases) facet), or return the
2816
+ original string if no translation is found.
2817
+
2818
+ If additional arguments are passed, they will be inserted in
2819
+ place of markers like `$1` (for the first value) and `$2`, etc.
2820
+ A single `$` is equivalent to `$1`, and `$$` will produce a
2821
+ literal dollar sign.
2822
+ */
2823
+ phrase(phrase, ...insert) {
2824
+ for (let map of this.facet(EditorState.phrases))
2825
+ if (Object.prototype.hasOwnProperty.call(map, phrase)) {
2826
+ phrase = map[phrase];
2827
+ break;
2828
+ }
2829
+ if (insert.length)
2830
+ phrase = phrase.replace(/\$(\$|\d*)/g, (m, i) => {
2831
+ if (i == "$")
2832
+ return "$";
2833
+ let n = +(i || 1);
2834
+ return !n || n > insert.length ? m : insert[n - 1];
2835
+ });
2836
+ return phrase;
2837
+ }
2838
+ /**
2839
+ Find the values for a given language data field, provided by the
2840
+ the [`languageData`](https://codemirror.net/6/docs/ref/#state.EditorState^languageData) facet.
2841
+
2842
+ Examples of language data fields are...
2843
+
2844
+ - [`"commentTokens"`](https://codemirror.net/6/docs/ref/#commands.CommentTokens) for specifying
2845
+ comment syntax.
2846
+ - [`"autocomplete"`](https://codemirror.net/6/docs/ref/#autocomplete.autocompletion^config.override)
2847
+ for providing language-specific completion sources.
2848
+ - [`"wordChars"`](https://codemirror.net/6/docs/ref/#state.EditorState.charCategorizer) for adding
2849
+ characters that should be considered part of words in this
2850
+ language.
2851
+ - [`"closeBrackets"`](https://codemirror.net/6/docs/ref/#autocomplete.CloseBracketConfig) controls
2852
+ bracket closing behavior.
2853
+ */
2854
+ languageDataAt(name, pos, side = -1) {
2855
+ let values = [];
2856
+ for (let provider of this.facet(languageData)) {
2857
+ for (let result of provider(this, pos, side)) {
2858
+ if (Object.prototype.hasOwnProperty.call(result, name))
2859
+ values.push(result[name]);
2860
+ }
2861
+ }
2862
+ return values;
2863
+ }
2864
+ /**
2865
+ Return a function that can categorize strings (expected to
2866
+ represent a single [grapheme cluster](https://codemirror.net/6/docs/ref/#state.findClusterBreak))
2867
+ into one of:
2868
+
2869
+ - Word (contains an alphanumeric character or a character
2870
+ explicitly listed in the local language's `"wordChars"`
2871
+ language data, which should be a string)
2872
+ - Space (contains only whitespace)
2873
+ - Other (anything else)
2874
+ */
2875
+ charCategorizer(at) {
2876
+ return makeCategorizer(this.languageDataAt("wordChars", at).join(""));
2877
+ }
2878
+ /**
2879
+ Find the word at the given position, meaning the range
2880
+ containing all [word](https://codemirror.net/6/docs/ref/#state.CharCategory.Word) characters
2881
+ around it. If no word characters are adjacent to the position,
2882
+ this returns null.
2883
+ */
2884
+ wordAt(pos) {
2885
+ let { text, from, length } = this.doc.lineAt(pos);
2886
+ let cat = this.charCategorizer(pos);
2887
+ let start = pos - from, end = pos - from;
2888
+ while (start > 0) {
2889
+ let prev = findClusterBreak(text, start, false);
2890
+ if (cat(text.slice(prev, start)) != CharCategory.Word)
2891
+ break;
2892
+ start = prev;
2893
+ }
2894
+ while (end < length) {
2895
+ let next = findClusterBreak(text, end);
2896
+ if (cat(text.slice(end, next)) != CharCategory.Word)
2897
+ break;
2898
+ end = next;
2899
+ }
2900
+ return start == end ? null : EditorSelection.range(start + from, end + from);
2901
+ }
2902
+ }
2903
+ /**
2904
+ A facet that, when enabled, causes the editor to allow multiple
2905
+ ranges to be selected. Be careful though, because by default the
2906
+ editor relies on the native DOM selection, which cannot handle
2907
+ multiple selections. An extension like
2908
+ [`drawSelection`](https://codemirror.net/6/docs/ref/#view.drawSelection) can be used to make
2909
+ secondary selections visible to the user.
2910
+ */
2911
+ EditorState.allowMultipleSelections = allowMultipleSelections;
2912
+ /**
2913
+ Configures the tab size to use in this state. The first
2914
+ (highest-precedence) value of the facet is used. If no value is
2915
+ given, this defaults to 4.
2916
+ */
2917
+ EditorState.tabSize = /*@__PURE__*/Facet.define({
2918
+ combine: values => values.length ? values[0] : 4
2919
+ });
2920
+ /**
2921
+ The line separator to use. By default, any of `"\n"`, `"\r\n"`
2922
+ and `"\r"` is treated as a separator when splitting lines, and
2923
+ lines are joined with `"\n"`.
2924
+
2925
+ When you configure a value here, only that precise separator
2926
+ will be used, allowing you to round-trip documents through the
2927
+ editor without normalizing line separators.
2928
+ */
2929
+ EditorState.lineSeparator = lineSeparator;
2930
+ /**
2931
+ This facet controls the value of the
2932
+ [`readOnly`](https://codemirror.net/6/docs/ref/#state.EditorState.readOnly) getter, which is
2933
+ consulted by commands and extensions that implement editing
2934
+ functionality to determine whether they should apply. It
2935
+ defaults to false, but when its highest-precedence value is
2936
+ `true`, such functionality disables itself.
2937
+
2938
+ Not to be confused with
2939
+ [`EditorView.editable`](https://codemirror.net/6/docs/ref/#view.EditorView^editable), which
2940
+ controls whether the editor's DOM is set to be editable (and
2941
+ thus focusable).
2942
+ */
2943
+ EditorState.readOnly = readOnly;
2944
+ /**
2945
+ Registers translation phrases. The
2946
+ [`phrase`](https://codemirror.net/6/docs/ref/#state.EditorState.phrase) method will look through
2947
+ all objects registered with this facet to find translations for
2948
+ its argument.
2949
+ */
2950
+ EditorState.phrases = /*@__PURE__*/Facet.define({
2951
+ compare(a, b) {
2952
+ let kA = Object.keys(a), kB = Object.keys(b);
2953
+ return kA.length == kB.length && kA.every(k => a[k] == b[k]);
2954
+ }
2955
+ });
2956
+ /**
2957
+ A facet used to register [language
2958
+ data](https://codemirror.net/6/docs/ref/#state.EditorState.languageDataAt) providers.
2959
+ */
2960
+ EditorState.languageData = languageData;
2961
+ /**
2962
+ Facet used to register change filters, which are called for each
2963
+ transaction (unless explicitly
2964
+ [disabled](https://codemirror.net/6/docs/ref/#state.TransactionSpec.filter)), and can suppress
2965
+ part of the transaction's changes.
2966
+
2967
+ Such a function can return `true` to indicate that it doesn't
2968
+ want to do anything, `false` to completely stop the changes in
2969
+ the transaction, or a set of ranges in which changes should be
2970
+ suppressed. Such ranges are represented as an array of numbers,
2971
+ with each pair of two numbers indicating the start and end of a
2972
+ range. So for example `[10, 20, 100, 110]` suppresses changes
2973
+ between 10 and 20, and between 100 and 110.
2974
+ */
2975
+ EditorState.changeFilter = changeFilter;
2976
+ /**
2977
+ Facet used to register a hook that gets a chance to update or
2978
+ replace transaction specs before they are applied. This will
2979
+ only be applied for transactions that don't have
2980
+ [`filter`](https://codemirror.net/6/docs/ref/#state.TransactionSpec.filter) set to `false`. You
2981
+ can either return a single transaction spec (possibly the input
2982
+ transaction), or an array of specs (which will be combined in
2983
+ the same way as the arguments to
2984
+ [`EditorState.update`](https://codemirror.net/6/docs/ref/#state.EditorState.update)).
2985
+
2986
+ When possible, it is recommended to avoid accessing
2987
+ [`Transaction.state`](https://codemirror.net/6/docs/ref/#state.Transaction.state) in a filter,
2988
+ since it will force creation of a state that will then be
2989
+ discarded again, if the transaction is actually filtered.
2990
+
2991
+ (This functionality should be used with care. Indiscriminately
2992
+ modifying transaction is likely to break something or degrade
2993
+ the user experience.)
2994
+ */
2995
+ EditorState.transactionFilter = transactionFilter;
2996
+ /**
2997
+ This is a more limited form of
2998
+ [`transactionFilter`](https://codemirror.net/6/docs/ref/#state.EditorState^transactionFilter),
2999
+ which can only add
3000
+ [annotations](https://codemirror.net/6/docs/ref/#state.TransactionSpec.annotations) and
3001
+ [effects](https://codemirror.net/6/docs/ref/#state.TransactionSpec.effects). _But_, this type
3002
+ of filter runs even if the transaction has disabled regular
3003
+ [filtering](https://codemirror.net/6/docs/ref/#state.TransactionSpec.filter), making it suitable
3004
+ for effects that don't need to touch the changes or selection,
3005
+ but do want to process every transaction.
3006
+
3007
+ Extenders run _after_ filters, when both are present.
3008
+ */
3009
+ EditorState.transactionExtender = transactionExtender;
3010
+ Compartment.reconfigure = /*@__PURE__*/StateEffect.define();
3011
+
3012
+ /**
3013
+ Utility function for combining behaviors to fill in a config
3014
+ object from an array of provided configs. `defaults` should hold
3015
+ default values for all optional fields in `Config`.
3016
+
3017
+ The function will, by default, error
3018
+ when a field gets two values that aren't `===`-equal, but you can
3019
+ provide combine functions per field to do something else.
3020
+ */
3021
+ function combineConfig(configs, defaults, // Should hold only the optional properties of Config, but I haven't managed to express that
3022
+ combine = {}) {
3023
+ let result = {};
3024
+ for (let config of configs)
3025
+ for (let key of Object.keys(config)) {
3026
+ let value = config[key], current = result[key];
3027
+ if (current === undefined)
3028
+ result[key] = value;
3029
+ else if (current === value || value === undefined) ; // No conflict
3030
+ else if (Object.hasOwnProperty.call(combine, key))
3031
+ result[key] = combine[key](current, value);
3032
+ else
3033
+ throw new Error("Config merge conflict for field " + key);
3034
+ }
3035
+ for (let key in defaults)
3036
+ if (result[key] === undefined)
3037
+ result[key] = defaults[key];
3038
+ return result;
3039
+ }
3040
+
3041
+ /**
3042
+ Each range is associated with a value, which must inherit from
3043
+ this class.
3044
+ */
3045
+ class RangeValue {
3046
+ /**
3047
+ Compare this value with another value. Used when comparing
3048
+ rangesets. The default implementation compares by identity.
3049
+ Unless you are only creating a fixed number of unique instances
3050
+ of your value type, it is a good idea to implement this
3051
+ properly.
3052
+ */
3053
+ eq(other) { return this == other; }
3054
+ /**
3055
+ Create a [range](https://codemirror.net/6/docs/ref/#state.Range) with this value.
3056
+ */
3057
+ range(from, to = from) { return Range.create(from, to, this); }
3058
+ }
3059
+ RangeValue.prototype.startSide = RangeValue.prototype.endSide = 0;
3060
+ RangeValue.prototype.point = false;
3061
+ RangeValue.prototype.mapMode = MapMode.TrackDel;
3062
+ /**
3063
+ A range associates a value with a range of positions.
3064
+ */
3065
+ class Range {
3066
+ constructor(
3067
+ /**
3068
+ The range's start position.
3069
+ */
3070
+ from,
3071
+ /**
3072
+ Its end position.
3073
+ */
3074
+ to,
3075
+ /**
3076
+ The value associated with this range.
3077
+ */
3078
+ value) {
3079
+ this.from = from;
3080
+ this.to = to;
3081
+ this.value = value;
3082
+ }
3083
+ /**
3084
+ @internal
3085
+ */
3086
+ static create(from, to, value) {
3087
+ return new Range(from, to, value);
3088
+ }
3089
+ }
3090
+ function cmpRange(a, b) {
3091
+ return a.from - b.from || a.value.startSide - b.value.startSide;
3092
+ }
3093
+ class Chunk {
3094
+ constructor(from, to, value,
3095
+ // Chunks are marked with the largest point that occurs
3096
+ // in them (or -1 for no points), so that scans that are
3097
+ // only interested in points (such as the
3098
+ // heightmap-related logic) can skip range-only chunks.
3099
+ maxPoint) {
3100
+ this.from = from;
3101
+ this.to = to;
3102
+ this.value = value;
3103
+ this.maxPoint = maxPoint;
3104
+ }
3105
+ get length() { return this.to[this.to.length - 1]; }
3106
+ // Find the index of the given position and side. Use the ranges'
3107
+ // `from` pos when `end == false`, `to` when `end == true`.
3108
+ findIndex(pos, side, end, startAt = 0) {
3109
+ let arr = end ? this.to : this.from;
3110
+ for (let lo = startAt, hi = arr.length;;) {
3111
+ if (lo == hi)
3112
+ return lo;
3113
+ let mid = (lo + hi) >> 1;
3114
+ let diff = arr[mid] - pos || (end ? this.value[mid].endSide : this.value[mid].startSide) - side;
3115
+ if (mid == lo)
3116
+ return diff >= 0 ? lo : hi;
3117
+ if (diff >= 0)
3118
+ hi = mid;
3119
+ else
3120
+ lo = mid + 1;
3121
+ }
3122
+ }
3123
+ between(offset, from, to, f) {
3124
+ for (let i = this.findIndex(from, -1000000000 /* C.Far */, true), e = this.findIndex(to, 1000000000 /* C.Far */, false, i); i < e; i++)
3125
+ if (f(this.from[i] + offset, this.to[i] + offset, this.value[i]) === false)
3126
+ return false;
3127
+ }
3128
+ map(offset, changes) {
3129
+ let value = [], from = [], to = [], newPos = -1, maxPoint = -1;
3130
+ for (let i = 0; i < this.value.length; i++) {
3131
+ let val = this.value[i], curFrom = this.from[i] + offset, curTo = this.to[i] + offset, newFrom, newTo;
3132
+ if (curFrom == curTo) {
3133
+ let mapped = changes.mapPos(curFrom, val.startSide, val.mapMode);
3134
+ if (mapped == null)
3135
+ continue;
3136
+ newFrom = newTo = mapped;
3137
+ if (val.startSide != val.endSide) {
3138
+ newTo = changes.mapPos(curFrom, val.endSide);
3139
+ if (newTo < newFrom)
3140
+ continue;
3141
+ }
3142
+ }
3143
+ else {
3144
+ newFrom = changes.mapPos(curFrom, val.startSide);
3145
+ newTo = changes.mapPos(curTo, val.endSide);
3146
+ if (newFrom > newTo || newFrom == newTo && val.startSide > 0 && val.endSide <= 0)
3147
+ continue;
3148
+ }
3149
+ if ((newTo - newFrom || val.endSide - val.startSide) < 0)
3150
+ continue;
3151
+ if (newPos < 0)
3152
+ newPos = newFrom;
3153
+ if (val.point)
3154
+ maxPoint = Math.max(maxPoint, newTo - newFrom);
3155
+ value.push(val);
3156
+ from.push(newFrom - newPos);
3157
+ to.push(newTo - newPos);
3158
+ }
3159
+ return { mapped: value.length ? new Chunk(from, to, value, maxPoint) : null, pos: newPos };
3160
+ }
3161
+ }
3162
+ /**
3163
+ A range set stores a collection of [ranges](https://codemirror.net/6/docs/ref/#state.Range) in a
3164
+ way that makes them efficient to [map](https://codemirror.net/6/docs/ref/#state.RangeSet.map) and
3165
+ [update](https://codemirror.net/6/docs/ref/#state.RangeSet.update). This is an immutable data
3166
+ structure.
3167
+ */
3168
+ class RangeSet {
3169
+ constructor(
3170
+ /**
3171
+ @internal
3172
+ */
3173
+ chunkPos,
3174
+ /**
3175
+ @internal
3176
+ */
3177
+ chunk,
3178
+ /**
3179
+ @internal
3180
+ */
3181
+ nextLayer,
3182
+ /**
3183
+ @internal
3184
+ */
3185
+ maxPoint) {
3186
+ this.chunkPos = chunkPos;
3187
+ this.chunk = chunk;
3188
+ this.nextLayer = nextLayer;
3189
+ this.maxPoint = maxPoint;
3190
+ }
3191
+ /**
3192
+ @internal
3193
+ */
3194
+ static create(chunkPos, chunk, nextLayer, maxPoint) {
3195
+ return new RangeSet(chunkPos, chunk, nextLayer, maxPoint);
3196
+ }
3197
+ /**
3198
+ @internal
3199
+ */
3200
+ get length() {
3201
+ let last = this.chunk.length - 1;
3202
+ return last < 0 ? 0 : Math.max(this.chunkEnd(last), this.nextLayer.length);
3203
+ }
3204
+ /**
3205
+ The number of ranges in the set.
3206
+ */
3207
+ get size() {
3208
+ if (this.isEmpty)
3209
+ return 0;
3210
+ let size = this.nextLayer.size;
3211
+ for (let chunk of this.chunk)
3212
+ size += chunk.value.length;
3213
+ return size;
3214
+ }
3215
+ /**
3216
+ @internal
3217
+ */
3218
+ chunkEnd(index) {
3219
+ return this.chunkPos[index] + this.chunk[index].length;
3220
+ }
3221
+ /**
3222
+ Update the range set, optionally adding new ranges or filtering
3223
+ out existing ones.
3224
+
3225
+ (Note: The type parameter is just there as a kludge to work
3226
+ around TypeScript variance issues that prevented `RangeSet<X>`
3227
+ from being a subtype of `RangeSet<Y>` when `X` is a subtype of
3228
+ `Y`.)
3229
+ */
3230
+ update(updateSpec) {
3231
+ let { add = [], sort = false, filterFrom = 0, filterTo = this.length } = updateSpec;
3232
+ let filter = updateSpec.filter;
3233
+ if (add.length == 0 && !filter)
3234
+ return this;
3235
+ if (sort)
3236
+ add = add.slice().sort(cmpRange);
3237
+ if (this.isEmpty)
3238
+ return add.length ? RangeSet.of(add) : this;
3239
+ let cur = new LayerCursor(this, null, -1).goto(0), i = 0, spill = [];
3240
+ let builder = new RangeSetBuilder();
3241
+ while (cur.value || i < add.length) {
3242
+ if (i < add.length && (cur.from - add[i].from || cur.startSide - add[i].value.startSide) >= 0) {
3243
+ let range = add[i++];
3244
+ if (!builder.addInner(range.from, range.to, range.value))
3245
+ spill.push(range);
3246
+ }
3247
+ else if (cur.rangeIndex == 1 && cur.chunkIndex < this.chunk.length &&
3248
+ (i == add.length || this.chunkEnd(cur.chunkIndex) < add[i].from) &&
3249
+ (!filter || filterFrom > this.chunkEnd(cur.chunkIndex) || filterTo < this.chunkPos[cur.chunkIndex]) &&
3250
+ builder.addChunk(this.chunkPos[cur.chunkIndex], this.chunk[cur.chunkIndex])) {
3251
+ cur.nextChunk();
3252
+ }
3253
+ else {
3254
+ if (!filter || filterFrom > cur.to || filterTo < cur.from || filter(cur.from, cur.to, cur.value)) {
3255
+ if (!builder.addInner(cur.from, cur.to, cur.value))
3256
+ spill.push(Range.create(cur.from, cur.to, cur.value));
3257
+ }
3258
+ cur.next();
3259
+ }
3260
+ }
3261
+ return builder.finishInner(this.nextLayer.isEmpty && !spill.length ? RangeSet.empty
3262
+ : this.nextLayer.update({ add: spill, filter, filterFrom, filterTo }));
3263
+ }
3264
+ /**
3265
+ Map this range set through a set of changes, return the new set.
3266
+ */
3267
+ map(changes) {
3268
+ if (changes.empty || this.isEmpty)
3269
+ return this;
3270
+ let chunks = [], chunkPos = [], maxPoint = -1;
3271
+ for (let i = 0; i < this.chunk.length; i++) {
3272
+ let start = this.chunkPos[i], chunk = this.chunk[i];
3273
+ let touch = changes.touchesRange(start, start + chunk.length);
3274
+ if (touch === false) {
3275
+ maxPoint = Math.max(maxPoint, chunk.maxPoint);
3276
+ chunks.push(chunk);
3277
+ chunkPos.push(changes.mapPos(start));
3278
+ }
3279
+ else if (touch === true) {
3280
+ let { mapped, pos } = chunk.map(start, changes);
3281
+ if (mapped) {
3282
+ maxPoint = Math.max(maxPoint, mapped.maxPoint);
3283
+ chunks.push(mapped);
3284
+ chunkPos.push(pos);
3285
+ }
3286
+ }
3287
+ }
3288
+ let next = this.nextLayer.map(changes);
3289
+ return chunks.length == 0 ? next : new RangeSet(chunkPos, chunks, next || RangeSet.empty, maxPoint);
3290
+ }
3291
+ /**
3292
+ Iterate over the ranges that touch the region `from` to `to`,
3293
+ calling `f` for each. There is no guarantee that the ranges will
3294
+ be reported in any specific order. When the callback returns
3295
+ `false`, iteration stops.
3296
+ */
3297
+ between(from, to, f) {
3298
+ if (this.isEmpty)
3299
+ return;
3300
+ for (let i = 0; i < this.chunk.length; i++) {
3301
+ let start = this.chunkPos[i], chunk = this.chunk[i];
3302
+ if (to >= start && from <= start + chunk.length &&
3303
+ chunk.between(start, from - start, to - start, f) === false)
3304
+ return;
3305
+ }
3306
+ this.nextLayer.between(from, to, f);
3307
+ }
3308
+ /**
3309
+ Iterate over the ranges in this set, in order, including all
3310
+ ranges that end at or after `from`.
3311
+ */
3312
+ iter(from = 0) {
3313
+ return HeapCursor.from([this]).goto(from);
3314
+ }
3315
+ /**
3316
+ @internal
3317
+ */
3318
+ get isEmpty() { return this.nextLayer == this; }
3319
+ /**
3320
+ Iterate over the ranges in a collection of sets, in order,
3321
+ starting from `from`.
3322
+ */
3323
+ static iter(sets, from = 0) {
3324
+ return HeapCursor.from(sets).goto(from);
3325
+ }
3326
+ /**
3327
+ Iterate over two groups of sets, calling methods on `comparator`
3328
+ to notify it of possible differences.
3329
+ */
3330
+ static compare(oldSets, newSets,
3331
+ /**
3332
+ This indicates how the underlying data changed between these
3333
+ ranges, and is needed to synchronize the iteration.
3334
+ */
3335
+ textDiff, comparator,
3336
+ /**
3337
+ Can be used to ignore all non-point ranges, and points below
3338
+ the given size. When -1, all ranges are compared.
3339
+ */
3340
+ minPointSize = -1) {
3341
+ let a = oldSets.filter(set => set.maxPoint > 0 || !set.isEmpty && set.maxPoint >= minPointSize);
3342
+ let b = newSets.filter(set => set.maxPoint > 0 || !set.isEmpty && set.maxPoint >= minPointSize);
3343
+ let sharedChunks = findSharedChunks(a, b, textDiff);
3344
+ let sideA = new SpanCursor(a, sharedChunks, minPointSize);
3345
+ let sideB = new SpanCursor(b, sharedChunks, minPointSize);
3346
+ textDiff.iterGaps((fromA, fromB, length) => compare(sideA, fromA, sideB, fromB, length, comparator));
3347
+ if (textDiff.empty && textDiff.length == 0)
3348
+ compare(sideA, 0, sideB, 0, 0, comparator);
3349
+ }
3350
+ /**
3351
+ Compare the contents of two groups of range sets, returning true
3352
+ if they are equivalent in the given range.
3353
+ */
3354
+ static eq(oldSets, newSets, from = 0, to) {
3355
+ if (to == null)
3356
+ to = 1000000000 /* C.Far */ - 1;
3357
+ let a = oldSets.filter(set => !set.isEmpty && newSets.indexOf(set) < 0);
3358
+ let b = newSets.filter(set => !set.isEmpty && oldSets.indexOf(set) < 0);
3359
+ if (a.length != b.length)
3360
+ return false;
3361
+ if (!a.length)
3362
+ return true;
3363
+ let sharedChunks = findSharedChunks(a, b);
3364
+ let sideA = new SpanCursor(a, sharedChunks, 0).goto(from), sideB = new SpanCursor(b, sharedChunks, 0).goto(from);
3365
+ for (;;) {
3366
+ if (sideA.to != sideB.to ||
3367
+ !sameValues(sideA.active, sideB.active) ||
3368
+ sideA.point && (!sideB.point || !sideA.point.eq(sideB.point)))
3369
+ return false;
3370
+ if (sideA.to > to)
3371
+ return true;
3372
+ sideA.next();
3373
+ sideB.next();
3374
+ }
3375
+ }
3376
+ /**
3377
+ Iterate over a group of range sets at the same time, notifying
3378
+ the iterator about the ranges covering every given piece of
3379
+ content. Returns the open count (see
3380
+ [`SpanIterator.span`](https://codemirror.net/6/docs/ref/#state.SpanIterator.span)) at the end
3381
+ of the iteration.
3382
+ */
3383
+ static spans(sets, from, to, iterator,
3384
+ /**
3385
+ When given and greater than -1, only points of at least this
3386
+ size are taken into account.
3387
+ */
3388
+ minPointSize = -1) {
3389
+ let cursor = new SpanCursor(sets, null, minPointSize).goto(from), pos = from;
3390
+ let openRanges = cursor.openStart;
3391
+ for (;;) {
3392
+ let curTo = Math.min(cursor.to, to);
3393
+ if (cursor.point) {
3394
+ let active = cursor.activeForPoint(cursor.to);
3395
+ let openCount = cursor.pointFrom < from ? active.length + 1
3396
+ : cursor.point.startSide < 0 ? active.length
3397
+ : Math.min(active.length, openRanges);
3398
+ iterator.point(pos, curTo, cursor.point, active, openCount, cursor.pointRank);
3399
+ openRanges = Math.min(cursor.openEnd(curTo), active.length);
3400
+ }
3401
+ else if (curTo > pos) {
3402
+ iterator.span(pos, curTo, cursor.active, openRanges);
3403
+ openRanges = cursor.openEnd(curTo);
3404
+ }
3405
+ if (cursor.to > to)
3406
+ return openRanges + (cursor.point && cursor.to > to ? 1 : 0);
3407
+ pos = cursor.to;
3408
+ cursor.next();
3409
+ }
3410
+ }
3411
+ /**
3412
+ Create a range set for the given range or array of ranges. By
3413
+ default, this expects the ranges to be _sorted_ (by start
3414
+ position and, if two start at the same position,
3415
+ `value.startSide`). You can pass `true` as second argument to
3416
+ cause the method to sort them.
3417
+ */
3418
+ static of(ranges, sort = false) {
3419
+ let build = new RangeSetBuilder();
3420
+ for (let range of ranges instanceof Range ? [ranges] : sort ? lazySort(ranges) : ranges)
3421
+ build.add(range.from, range.to, range.value);
3422
+ return build.finish();
3423
+ }
3424
+ /**
3425
+ Join an array of range sets into a single set.
3426
+ */
3427
+ static join(sets) {
3428
+ if (!sets.length)
3429
+ return RangeSet.empty;
3430
+ let result = sets[sets.length - 1];
3431
+ for (let i = sets.length - 2; i >= 0; i--) {
3432
+ for (let layer = sets[i]; layer != RangeSet.empty; layer = layer.nextLayer)
3433
+ result = new RangeSet(layer.chunkPos, layer.chunk, result, Math.max(layer.maxPoint, result.maxPoint));
3434
+ }
3435
+ return result;
3436
+ }
3437
+ }
3438
+ /**
3439
+ The empty set of ranges.
3440
+ */
3441
+ RangeSet.empty = /*@__PURE__*/new RangeSet([], [], null, -1);
3442
+ function lazySort(ranges) {
3443
+ if (ranges.length > 1)
3444
+ for (let prev = ranges[0], i = 1; i < ranges.length; i++) {
3445
+ let cur = ranges[i];
3446
+ if (cmpRange(prev, cur) > 0)
3447
+ return ranges.slice().sort(cmpRange);
3448
+ prev = cur;
3449
+ }
3450
+ return ranges;
3451
+ }
3452
+ RangeSet.empty.nextLayer = RangeSet.empty;
3453
+ /**
3454
+ A range set builder is a data structure that helps build up a
3455
+ [range set](https://codemirror.net/6/docs/ref/#state.RangeSet) directly, without first allocating
3456
+ an array of [`Range`](https://codemirror.net/6/docs/ref/#state.Range) objects.
3457
+ */
3458
+ class RangeSetBuilder {
3459
+ finishChunk(newArrays) {
3460
+ this.chunks.push(new Chunk(this.from, this.to, this.value, this.maxPoint));
3461
+ this.chunkPos.push(this.chunkStart);
3462
+ this.chunkStart = -1;
3463
+ this.setMaxPoint = Math.max(this.setMaxPoint, this.maxPoint);
3464
+ this.maxPoint = -1;
3465
+ if (newArrays) {
3466
+ this.from = [];
3467
+ this.to = [];
3468
+ this.value = [];
3469
+ }
3470
+ }
3471
+ /**
3472
+ Create an empty builder.
3473
+ */
3474
+ constructor() {
3475
+ this.chunks = [];
3476
+ this.chunkPos = [];
3477
+ this.chunkStart = -1;
3478
+ this.last = null;
3479
+ this.lastFrom = -1000000000 /* C.Far */;
3480
+ this.lastTo = -1000000000 /* C.Far */;
3481
+ this.from = [];
3482
+ this.to = [];
3483
+ this.value = [];
3484
+ this.maxPoint = -1;
3485
+ this.setMaxPoint = -1;
3486
+ this.nextLayer = null;
3487
+ }
3488
+ /**
3489
+ Add a range. Ranges should be added in sorted (by `from` and
3490
+ `value.startSide`) order.
3491
+ */
3492
+ add(from, to, value) {
3493
+ if (!this.addInner(from, to, value))
3494
+ (this.nextLayer || (this.nextLayer = new RangeSetBuilder)).add(from, to, value);
3495
+ }
3496
+ /**
3497
+ @internal
3498
+ */
3499
+ addInner(from, to, value) {
3500
+ let diff = from - this.lastTo || value.startSide - this.last.endSide;
3501
+ if (diff <= 0 && (from - this.lastFrom || value.startSide - this.last.startSide) < 0)
3502
+ throw new Error("Ranges must be added sorted by `from` position and `startSide`");
3503
+ if (diff < 0)
3504
+ return false;
3505
+ if (this.from.length == 250 /* C.ChunkSize */)
3506
+ this.finishChunk(true);
3507
+ if (this.chunkStart < 0)
3508
+ this.chunkStart = from;
3509
+ this.from.push(from - this.chunkStart);
3510
+ this.to.push(to - this.chunkStart);
3511
+ this.last = value;
3512
+ this.lastFrom = from;
3513
+ this.lastTo = to;
3514
+ this.value.push(value);
3515
+ if (value.point)
3516
+ this.maxPoint = Math.max(this.maxPoint, to - from);
3517
+ return true;
3518
+ }
3519
+ /**
3520
+ @internal
3521
+ */
3522
+ addChunk(from, chunk) {
3523
+ if ((from - this.lastTo || chunk.value[0].startSide - this.last.endSide) < 0)
3524
+ return false;
3525
+ if (this.from.length)
3526
+ this.finishChunk(true);
3527
+ this.setMaxPoint = Math.max(this.setMaxPoint, chunk.maxPoint);
3528
+ this.chunks.push(chunk);
3529
+ this.chunkPos.push(from);
3530
+ let last = chunk.value.length - 1;
3531
+ this.last = chunk.value[last];
3532
+ this.lastFrom = chunk.from[last] + from;
3533
+ this.lastTo = chunk.to[last] + from;
3534
+ return true;
3535
+ }
3536
+ /**
3537
+ Finish the range set. Returns the new set. The builder can't be
3538
+ used anymore after this has been called.
3539
+ */
3540
+ finish() { return this.finishInner(RangeSet.empty); }
3541
+ /**
3542
+ @internal
3543
+ */
3544
+ finishInner(next) {
3545
+ if (this.from.length)
3546
+ this.finishChunk(false);
3547
+ if (this.chunks.length == 0)
3548
+ return next;
3549
+ let result = RangeSet.create(this.chunkPos, this.chunks, this.nextLayer ? this.nextLayer.finishInner(next) : next, this.setMaxPoint);
3550
+ this.from = null; // Make sure further `add` calls produce errors
3551
+ return result;
3552
+ }
3553
+ }
3554
+ function findSharedChunks(a, b, textDiff) {
3555
+ let inA = new Map();
3556
+ for (let set of a)
3557
+ for (let i = 0; i < set.chunk.length; i++)
3558
+ if (set.chunk[i].maxPoint <= 0)
3559
+ inA.set(set.chunk[i], set.chunkPos[i]);
3560
+ let shared = new Set();
3561
+ for (let set of b)
3562
+ for (let i = 0; i < set.chunk.length; i++) {
3563
+ let known = inA.get(set.chunk[i]);
3564
+ if (known != null && (textDiff ? textDiff.mapPos(known) : known) == set.chunkPos[i] &&
3565
+ !(textDiff === null || textDiff === void 0 ? void 0 : textDiff.touchesRange(known, known + set.chunk[i].length)))
3566
+ shared.add(set.chunk[i]);
3567
+ }
3568
+ return shared;
3569
+ }
3570
+ class LayerCursor {
3571
+ constructor(layer, skip, minPoint, rank = 0) {
3572
+ this.layer = layer;
3573
+ this.skip = skip;
3574
+ this.minPoint = minPoint;
3575
+ this.rank = rank;
3576
+ }
3577
+ get startSide() { return this.value ? this.value.startSide : 0; }
3578
+ get endSide() { return this.value ? this.value.endSide : 0; }
3579
+ goto(pos, side = -1000000000 /* C.Far */) {
3580
+ this.chunkIndex = this.rangeIndex = 0;
3581
+ this.gotoInner(pos, side, false);
3582
+ return this;
3583
+ }
3584
+ gotoInner(pos, side, forward) {
3585
+ while (this.chunkIndex < this.layer.chunk.length) {
3586
+ let next = this.layer.chunk[this.chunkIndex];
3587
+ if (!(this.skip && this.skip.has(next) ||
3588
+ this.layer.chunkEnd(this.chunkIndex) < pos ||
3589
+ next.maxPoint < this.minPoint))
3590
+ break;
3591
+ this.chunkIndex++;
3592
+ forward = false;
3593
+ }
3594
+ if (this.chunkIndex < this.layer.chunk.length) {
3595
+ let rangeIndex = this.layer.chunk[this.chunkIndex].findIndex(pos - this.layer.chunkPos[this.chunkIndex], side, true);
3596
+ if (!forward || this.rangeIndex < rangeIndex)
3597
+ this.setRangeIndex(rangeIndex);
3598
+ }
3599
+ this.next();
3600
+ }
3601
+ forward(pos, side) {
3602
+ if ((this.to - pos || this.endSide - side) < 0)
3603
+ this.gotoInner(pos, side, true);
3604
+ }
3605
+ next() {
3606
+ for (;;) {
3607
+ if (this.chunkIndex == this.layer.chunk.length) {
3608
+ this.from = this.to = 1000000000 /* C.Far */;
3609
+ this.value = null;
3610
+ break;
3611
+ }
3612
+ else {
3613
+ let chunkPos = this.layer.chunkPos[this.chunkIndex], chunk = this.layer.chunk[this.chunkIndex];
3614
+ let from = chunkPos + chunk.from[this.rangeIndex];
3615
+ this.from = from;
3616
+ this.to = chunkPos + chunk.to[this.rangeIndex];
3617
+ this.value = chunk.value[this.rangeIndex];
3618
+ this.setRangeIndex(this.rangeIndex + 1);
3619
+ if (this.minPoint < 0 || this.value.point && this.to - this.from >= this.minPoint)
3620
+ break;
3621
+ }
3622
+ }
3623
+ }
3624
+ setRangeIndex(index) {
3625
+ if (index == this.layer.chunk[this.chunkIndex].value.length) {
3626
+ this.chunkIndex++;
3627
+ if (this.skip) {
3628
+ while (this.chunkIndex < this.layer.chunk.length && this.skip.has(this.layer.chunk[this.chunkIndex]))
3629
+ this.chunkIndex++;
3630
+ }
3631
+ this.rangeIndex = 0;
3632
+ }
3633
+ else {
3634
+ this.rangeIndex = index;
3635
+ }
3636
+ }
3637
+ nextChunk() {
3638
+ this.chunkIndex++;
3639
+ this.rangeIndex = 0;
3640
+ this.next();
3641
+ }
3642
+ compare(other) {
3643
+ return this.from - other.from || this.startSide - other.startSide || this.rank - other.rank ||
3644
+ this.to - other.to || this.endSide - other.endSide;
3645
+ }
3646
+ }
3647
+ class HeapCursor {
3648
+ constructor(heap) {
3649
+ this.heap = heap;
3650
+ }
3651
+ static from(sets, skip = null, minPoint = -1) {
3652
+ let heap = [];
3653
+ for (let i = 0; i < sets.length; i++) {
3654
+ for (let cur = sets[i]; !cur.isEmpty; cur = cur.nextLayer) {
3655
+ if (cur.maxPoint >= minPoint)
3656
+ heap.push(new LayerCursor(cur, skip, minPoint, i));
3657
+ }
3658
+ }
3659
+ return heap.length == 1 ? heap[0] : new HeapCursor(heap);
3660
+ }
3661
+ get startSide() { return this.value ? this.value.startSide : 0; }
3662
+ goto(pos, side = -1000000000 /* C.Far */) {
3663
+ for (let cur of this.heap)
3664
+ cur.goto(pos, side);
3665
+ for (let i = this.heap.length >> 1; i >= 0; i--)
3666
+ heapBubble(this.heap, i);
3667
+ this.next();
3668
+ return this;
3669
+ }
3670
+ forward(pos, side) {
3671
+ for (let cur of this.heap)
3672
+ cur.forward(pos, side);
3673
+ for (let i = this.heap.length >> 1; i >= 0; i--)
3674
+ heapBubble(this.heap, i);
3675
+ if ((this.to - pos || this.value.endSide - side) < 0)
3676
+ this.next();
3677
+ }
3678
+ next() {
3679
+ if (this.heap.length == 0) {
3680
+ this.from = this.to = 1000000000 /* C.Far */;
3681
+ this.value = null;
3682
+ this.rank = -1;
3683
+ }
3684
+ else {
3685
+ let top = this.heap[0];
3686
+ this.from = top.from;
3687
+ this.to = top.to;
3688
+ this.value = top.value;
3689
+ this.rank = top.rank;
3690
+ if (top.value)
3691
+ top.next();
3692
+ heapBubble(this.heap, 0);
3693
+ }
3694
+ }
3695
+ }
3696
+ function heapBubble(heap, index) {
3697
+ for (let cur = heap[index];;) {
3698
+ let childIndex = (index << 1) + 1;
3699
+ if (childIndex >= heap.length)
3700
+ break;
3701
+ let child = heap[childIndex];
3702
+ if (childIndex + 1 < heap.length && child.compare(heap[childIndex + 1]) >= 0) {
3703
+ child = heap[childIndex + 1];
3704
+ childIndex++;
3705
+ }
3706
+ if (cur.compare(child) < 0)
3707
+ break;
3708
+ heap[childIndex] = cur;
3709
+ heap[index] = child;
3710
+ index = childIndex;
3711
+ }
3712
+ }
3713
+ class SpanCursor {
3714
+ constructor(sets, skip, minPoint) {
3715
+ this.minPoint = minPoint;
3716
+ this.active = [];
3717
+ this.activeTo = [];
3718
+ this.activeRank = [];
3719
+ this.minActive = -1;
3720
+ // A currently active point range, if any
3721
+ this.point = null;
3722
+ this.pointFrom = 0;
3723
+ this.pointRank = 0;
3724
+ this.to = -1000000000 /* C.Far */;
3725
+ this.endSide = 0;
3726
+ // The amount of open active ranges at the start of the iterator.
3727
+ // Not including points.
3728
+ this.openStart = -1;
3729
+ this.cursor = HeapCursor.from(sets, skip, minPoint);
3730
+ }
3731
+ goto(pos, side = -1000000000 /* C.Far */) {
3732
+ this.cursor.goto(pos, side);
3733
+ this.active.length = this.activeTo.length = this.activeRank.length = 0;
3734
+ this.minActive = -1;
3735
+ this.to = pos;
3736
+ this.endSide = side;
3737
+ this.openStart = -1;
3738
+ this.next();
3739
+ return this;
3740
+ }
3741
+ forward(pos, side) {
3742
+ while (this.minActive > -1 && (this.activeTo[this.minActive] - pos || this.active[this.minActive].endSide - side) < 0)
3743
+ this.removeActive(this.minActive);
3744
+ this.cursor.forward(pos, side);
3745
+ }
3746
+ removeActive(index) {
3747
+ remove(this.active, index);
3748
+ remove(this.activeTo, index);
3749
+ remove(this.activeRank, index);
3750
+ this.minActive = findMinIndex(this.active, this.activeTo);
3751
+ }
3752
+ addActive(trackOpen) {
3753
+ let i = 0, { value, to, rank } = this.cursor;
3754
+ // Organize active marks by rank first, then by size
3755
+ while (i < this.activeRank.length && (rank - this.activeRank[i] || to - this.activeTo[i]) > 0)
3756
+ i++;
3757
+ insert(this.active, i, value);
3758
+ insert(this.activeTo, i, to);
3759
+ insert(this.activeRank, i, rank);
3760
+ if (trackOpen)
3761
+ insert(trackOpen, i, this.cursor.from);
3762
+ this.minActive = findMinIndex(this.active, this.activeTo);
3763
+ }
3764
+ // After calling this, if `this.point` != null, the next range is a
3765
+ // point. Otherwise, it's a regular range, covered by `this.active`.
3766
+ next() {
3767
+ let from = this.to, wasPoint = this.point;
3768
+ this.point = null;
3769
+ let trackOpen = this.openStart < 0 ? [] : null;
3770
+ for (;;) {
3771
+ let a = this.minActive;
3772
+ if (a > -1 && (this.activeTo[a] - this.cursor.from || this.active[a].endSide - this.cursor.startSide) < 0) {
3773
+ if (this.activeTo[a] > from) {
3774
+ this.to = this.activeTo[a];
3775
+ this.endSide = this.active[a].endSide;
3776
+ break;
3777
+ }
3778
+ this.removeActive(a);
3779
+ if (trackOpen)
3780
+ remove(trackOpen, a);
3781
+ }
3782
+ else if (!this.cursor.value) {
3783
+ this.to = this.endSide = 1000000000 /* C.Far */;
3784
+ break;
3785
+ }
3786
+ else if (this.cursor.from > from) {
3787
+ this.to = this.cursor.from;
3788
+ this.endSide = this.cursor.startSide;
3789
+ break;
3790
+ }
3791
+ else {
3792
+ let nextVal = this.cursor.value;
3793
+ if (!nextVal.point) { // Opening a range
3794
+ this.addActive(trackOpen);
3795
+ this.cursor.next();
3796
+ }
3797
+ else if (wasPoint && this.cursor.to == this.to && this.cursor.from < this.cursor.to) {
3798
+ // Ignore any non-empty points that end precisely at the end of the prev point
3799
+ this.cursor.next();
3800
+ }
3801
+ else { // New point
3802
+ this.point = nextVal;
3803
+ this.pointFrom = this.cursor.from;
3804
+ this.pointRank = this.cursor.rank;
3805
+ this.to = this.cursor.to;
3806
+ this.endSide = nextVal.endSide;
3807
+ this.cursor.next();
3808
+ this.forward(this.to, this.endSide);
3809
+ break;
3810
+ }
3811
+ }
3812
+ }
3813
+ if (trackOpen) {
3814
+ this.openStart = 0;
3815
+ for (let i = trackOpen.length - 1; i >= 0 && trackOpen[i] < from; i--)
3816
+ this.openStart++;
3817
+ }
3818
+ }
3819
+ activeForPoint(to) {
3820
+ if (!this.active.length)
3821
+ return this.active;
3822
+ let active = [];
3823
+ for (let i = this.active.length - 1; i >= 0; i--) {
3824
+ if (this.activeRank[i] < this.pointRank)
3825
+ break;
3826
+ if (this.activeTo[i] > to || this.activeTo[i] == to && this.active[i].endSide >= this.point.endSide)
3827
+ active.push(this.active[i]);
3828
+ }
3829
+ return active.reverse();
3830
+ }
3831
+ openEnd(to) {
3832
+ let open = 0;
3833
+ for (let i = this.activeTo.length - 1; i >= 0 && this.activeTo[i] > to; i--)
3834
+ open++;
3835
+ return open;
3836
+ }
3837
+ }
3838
+ function compare(a, startA, b, startB, length, comparator) {
3839
+ a.goto(startA);
3840
+ b.goto(startB);
3841
+ let endB = startB + length;
3842
+ let pos = startB, dPos = startB - startA;
3843
+ for (;;) {
3844
+ let diff = (a.to + dPos) - b.to || a.endSide - b.endSide;
3845
+ let end = diff < 0 ? a.to + dPos : b.to, clipEnd = Math.min(end, endB);
3846
+ if (a.point || b.point) {
3847
+ if (!(a.point && b.point && (a.point == b.point || a.point.eq(b.point)) &&
3848
+ sameValues(a.activeForPoint(a.to), b.activeForPoint(b.to))))
3849
+ comparator.comparePoint(pos, clipEnd, a.point, b.point);
3850
+ }
3851
+ else {
3852
+ if (clipEnd > pos && !sameValues(a.active, b.active))
3853
+ comparator.compareRange(pos, clipEnd, a.active, b.active);
3854
+ }
3855
+ if (end > endB)
3856
+ break;
3857
+ pos = end;
3858
+ if (diff <= 0)
3859
+ a.next();
3860
+ if (diff >= 0)
3861
+ b.next();
3862
+ }
3863
+ }
3864
+ function sameValues(a, b) {
3865
+ if (a.length != b.length)
3866
+ return false;
3867
+ for (let i = 0; i < a.length; i++)
3868
+ if (a[i] != b[i] && !a[i].eq(b[i]))
3869
+ return false;
3870
+ return true;
3871
+ }
3872
+ function remove(array, index) {
3873
+ for (let i = index, e = array.length - 1; i < e; i++)
3874
+ array[i] = array[i + 1];
3875
+ array.pop();
3876
+ }
3877
+ function insert(array, index, value) {
3878
+ for (let i = array.length - 1; i >= index; i--)
3879
+ array[i + 1] = array[i];
3880
+ array[index] = value;
3881
+ }
3882
+ function findMinIndex(value, array) {
3883
+ let found = -1, foundPos = 1000000000 /* C.Far */;
3884
+ for (let i = 0; i < array.length; i++)
3885
+ if ((array[i] - foundPos || value[i].endSide - value[found].endSide) < 0) {
3886
+ found = i;
3887
+ foundPos = array[i];
3888
+ }
3889
+ return found;
3890
+ }
3891
+
3892
+ /**
3893
+ Count the column position at the given offset into the string,
3894
+ taking extending characters and tab size into account.
3895
+ */
3896
+ function countColumn(string, tabSize, to = string.length) {
3897
+ let n = 0;
3898
+ for (let i = 0; i < to;) {
3899
+ if (string.charCodeAt(i) == 9) {
3900
+ n += tabSize - (n % tabSize);
3901
+ i++;
3902
+ }
3903
+ else {
3904
+ n++;
3905
+ i = findClusterBreak(string, i);
3906
+ }
3907
+ }
3908
+ return n;
3909
+ }
3910
+ /**
3911
+ Find the offset that corresponds to the given column position in a
3912
+ string, taking extending characters and tab size into account. By
3913
+ default, the string length is returned when it is too short to
3914
+ reach the column. Pass `strict` true to make it return -1 in that
3915
+ situation.
3916
+ */
3917
+ function findColumn(string, col, tabSize, strict) {
3918
+ for (let i = 0, n = 0;;) {
3919
+ if (n >= col)
3920
+ return i;
3921
+ if (i == string.length)
3922
+ break;
3923
+ n += string.charCodeAt(i) == 9 ? tabSize - (n % tabSize) : 1;
3924
+ i = findClusterBreak(string, i);
3925
+ }
3926
+ return strict === true ? -1 : string.length;
3927
+ }
3928
+
3929
+ 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 };
3930
+ //# sourceMappingURL=codemirror_state-n1sFh2hh.js.map