@vuu-ui/vuu-codemirror 0.8.34 → 0.8.36

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