@nibling/editor 0.0.0-stage → 0.0.1

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 (146) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -2
  3. package/dist/commands/autoclose.d.ts +13 -0
  4. package/dist/commands/autoclose.js +78 -0
  5. package/dist/commands/commands.d.ts +104 -0
  6. package/dist/commands/commands.js +442 -0
  7. package/dist/commands/comments.d.ts +3 -0
  8. package/dist/commands/comments.js +9 -0
  9. package/dist/commands/fold.d.ts +18 -0
  10. package/dist/commands/fold.js +75 -0
  11. package/dist/commands/keymap.d.ts +40 -0
  12. package/dist/commands/keymap.js +118 -0
  13. package/dist/complete/autocomplete.d.ts +35 -0
  14. package/dist/complete/autocomplete.js +386 -0
  15. package/dist/complete/source.d.ts +72 -0
  16. package/dist/complete/source.js +52 -0
  17. package/dist/complete/words.d.ts +53 -0
  18. package/dist/complete/words.js +203 -0
  19. package/dist/element.d.ts +119 -0
  20. package/dist/element.js +1011 -0
  21. package/dist/expand/expansions.d.ts +68 -0
  22. package/dist/expand/expansions.js +143 -0
  23. package/dist/expand/projection.d.ts +73 -0
  24. package/dist/expand/projection.js +189 -0
  25. package/dist/expand/resolver.d.ts +42 -0
  26. package/dist/expand/resolver.js +585 -0
  27. package/dist/expand/slice-tokens.d.ts +25 -0
  28. package/dist/expand/slice-tokens.js +79 -0
  29. package/dist/extension.d.ts +58 -0
  30. package/dist/extension.js +1 -0
  31. package/dist/find/find.d.ts +12 -0
  32. package/dist/find/find.js +469 -0
  33. package/dist/find/search.d.ts +81 -0
  34. package/dist/find/search.js +308 -0
  35. package/dist/headless.d.ts +16 -0
  36. package/dist/headless.js +19 -0
  37. package/dist/index.d.ts +6 -0
  38. package/dist/index.js +6 -0
  39. package/dist/inline-expansion.d.ts +89 -0
  40. package/dist/inline-expansion.js +324 -0
  41. package/dist/input/edit-context.d.ts +23 -0
  42. package/dist/input/edit-context.js +102 -0
  43. package/dist/input/surface.d.ts +101 -0
  44. package/dist/input/surface.js +84 -0
  45. package/dist/input/textarea.d.ts +26 -0
  46. package/dist/input/textarea.js +125 -0
  47. package/dist/sheet.d.ts +6 -0
  48. package/dist/sheet.js +12 -0
  49. package/dist/state/core.d.ts +73 -0
  50. package/dist/state/core.js +86 -0
  51. package/dist/state/fold-ranges.d.ts +41 -0
  52. package/dist/state/fold-ranges.js +167 -0
  53. package/dist/state/folds.d.ts +45 -0
  54. package/dist/state/folds.js +240 -0
  55. package/dist/state/history.d.ts +25 -0
  56. package/dist/state/history.js +127 -0
  57. package/dist/state/indent.d.ts +33 -0
  58. package/dist/state/indent.js +121 -0
  59. package/dist/state/selection.d.ts +45 -0
  60. package/dist/state/selection.js +106 -0
  61. package/dist/state/whitespace.d.ts +16 -0
  62. package/dist/state/whitespace.js +54 -0
  63. package/dist/text/changeset.d.ts +56 -0
  64. package/dist/text/changeset.js +321 -0
  65. package/dist/text/text.d.ts +30 -0
  66. package/dist/text/text.js +321 -0
  67. package/dist/tokens/bridge.d.ts +107 -0
  68. package/dist/tokens/bridge.js +461 -0
  69. package/dist/tokens/engine.d.ts +39 -0
  70. package/dist/tokens/engine.js +360 -0
  71. package/dist/tokens/protocol.d.ts +113 -0
  72. package/dist/tokens/protocol.js +38 -0
  73. package/dist/tokens/textmate/ascii-regex.d.ts +5 -0
  74. package/dist/tokens/textmate/ascii-regex.js +156 -0
  75. package/dist/tokens/textmate/index.d.ts +7 -0
  76. package/dist/tokens/textmate/index.js +19 -0
  77. package/dist/tokens/textmate/linker.d.ts +67 -0
  78. package/dist/tokens/textmate/linker.js +238 -0
  79. package/dist/tokens/textmate/scopes.d.ts +14 -0
  80. package/dist/tokens/textmate/scopes.js +54 -0
  81. package/dist/tokens/textmate/theme.d.ts +54 -0
  82. package/dist/tokens/textmate/theme.js +138 -0
  83. package/dist/tokens/textmate/tokenizer.d.ts +29 -0
  84. package/dist/tokens/textmate/tokenizer.js +684 -0
  85. package/dist/tokens/textmate.d.ts +49 -0
  86. package/dist/tokens/textmate.js +4 -0
  87. package/dist/view/decorations.d.ts +183 -0
  88. package/dist/view/decorations.js +51 -0
  89. package/dist/view/guides.d.ts +58 -0
  90. package/dist/view/guides.js +174 -0
  91. package/dist/view/heightmap.d.ts +97 -0
  92. package/dist/view/heightmap.js +229 -0
  93. package/dist/view/line-dom.d.ts +54 -0
  94. package/dist/view/line-dom.js +168 -0
  95. package/dist/view/measure.d.ts +37 -0
  96. package/dist/view/measure.js +136 -0
  97. package/dist/view/overlays.d.ts +32 -0
  98. package/dist/view/overlays.js +135 -0
  99. package/dist/view/rail.d.ts +27 -0
  100. package/dist/view/rail.js +232 -0
  101. package/dist/view/remote.d.ts +24 -0
  102. package/dist/view/remote.js +143 -0
  103. package/dist/view/scope-focus.d.ts +27 -0
  104. package/dist/view/scope-focus.js +255 -0
  105. package/dist/view/view.d.ts +114 -0
  106. package/dist/view/view.js +1231 -0
  107. package/dist/vite.d.ts +12 -0
  108. package/dist/vite.js +64 -0
  109. package/dist/worker.d.ts +1 -0
  110. package/dist/worker.js +64 -0
  111. package/grammars/astro.mjs +2 -0
  112. package/grammars/chunks/r-90cb351dba.mjs +1 -0
  113. package/grammars/chunks/r-b92a500aab.mjs +1 -0
  114. package/grammars/chunks/r-ced5311992.mjs +1 -0
  115. package/grammars/chunks/r-ebae91d4ec.mjs +1 -0
  116. package/grammars/chunks/s-1821e66170.mjs +1 -0
  117. package/grammars/css.mjs +1 -0
  118. package/grammars/diff.mjs +1 -0
  119. package/grammars/docker.mjs +1 -0
  120. package/grammars/dotenv.mjs +1 -0
  121. package/grammars/graphql.mjs +1 -0
  122. package/grammars/html.mjs +2 -0
  123. package/grammars/ini.mjs +1 -0
  124. package/grammars/javascript.mjs +5 -0
  125. package/grammars/json.mjs +1 -0
  126. package/grammars/json5.mjs +1 -0
  127. package/grammars/jsonc.mjs +1 -0
  128. package/grammars/less.mjs +1 -0
  129. package/grammars/manifest.json +58 -0
  130. package/grammars/markdown.mjs +1 -0
  131. package/grammars/mdx.mjs +1 -0
  132. package/grammars/pug.mjs +1 -0
  133. package/grammars/scss.mjs +1 -0
  134. package/grammars/shellscript.mjs +1 -0
  135. package/grammars/sql.mjs +1 -0
  136. package/grammars/stylus.mjs +1 -0
  137. package/grammars/svelte.mjs +1 -0
  138. package/grammars/toml.mjs +1 -0
  139. package/grammars/tsx.mjs +5 -0
  140. package/grammars/typescript.mjs +2 -0
  141. package/grammars/vue.mjs +1 -0
  142. package/grammars/yaml.mjs +1 -0
  143. package/package.json +71 -3
  144. package/themes/dark-plus.json +1 -0
  145. package/themes/light-plus.json +1 -0
  146. package/themes/monokai.json +1 -0
@@ -0,0 +1,106 @@
1
+ export const rangeFrom = (r) => Math.min(r.anchor, r.head);
2
+ export const rangeTo = (r) => Math.max(r.anchor, r.head);
3
+ export const isCursor = (r) => r.anchor === r.head;
4
+ export const cursor = (pos, goalX) => goalX === undefined ? { anchor: pos, head: pos } : { anchor: pos, head: pos, goalX };
5
+ /**
6
+ * A normalized multi-range selection (D4).
7
+ *
8
+ * Invariants, established by `create` and nothing else (the constructor is private):
9
+ * - `ranges` is non-empty, sorted by `rangeFrom`.
10
+ * - No two ranges overlap. Touching non-empty ranges merge; two cursors at the same offset merge.
11
+ * A cursor touching a non-empty range does not merge with it.
12
+ * - `primaryIndex` indexes `ranges`. When ranges merge, the merged range is primary if any input
13
+ * to it was.
14
+ */
15
+ export class Selection {
16
+ ranges;
17
+ primaryIndex;
18
+ constructor(ranges, primaryIndex) {
19
+ this.ranges = ranges;
20
+ this.primaryIndex = primaryIndex;
21
+ }
22
+ static single(anchor, head = anchor) {
23
+ return new Selection([{ anchor, head }], 0);
24
+ }
25
+ /** Sorts, merges, and picks the primary. Total: any non-empty input yields a valid Selection. */
26
+ static create(ranges, primaryIndex) {
27
+ const tagged = ranges.map((r, i) => ({ r, isPrimary: i === primaryIndex }));
28
+ tagged.sort((a, b) => rangeFrom(a.r) - rangeFrom(b.r) || rangeTo(a.r) - rangeTo(b.r));
29
+ const out = [];
30
+ for (const t of tagged) {
31
+ const last = out[out.length - 1];
32
+ if (last !== undefined && rangeFrom(t.r) <= rangeTo(last.r)) {
33
+ // Touching non-empty ranges merge; two cursors at the same offset merge. A cursor
34
+ // touching a non-empty range does not merge with it.
35
+ const lastEmpty = isCursor(last.r);
36
+ const tEmpty = isCursor(t.r);
37
+ const sameOffset = rangeFrom(t.r) === rangeTo(last.r);
38
+ const shouldMerge = !(lastEmpty !== tEmpty && sameOffset);
39
+ if (shouldMerge) {
40
+ const from = Math.min(rangeFrom(last.r), rangeFrom(t.r));
41
+ const to = Math.max(rangeTo(last.r), rangeTo(t.r));
42
+ const forward = last.r.head >= last.r.anchor;
43
+ const merged = forward ? { anchor: from, head: to } : { anchor: to, head: from };
44
+ out[out.length - 1] = { r: merged, isPrimary: last.isPrimary || t.isPrimary };
45
+ continue;
46
+ }
47
+ }
48
+ out.push(t);
49
+ }
50
+ let newPrimary = 0;
51
+ for (let i = 0; i < out.length; i++) {
52
+ if (out[i].isPrimary) {
53
+ newPrimary = i;
54
+ break;
55
+ }
56
+ }
57
+ const merged = out.map((o) => o.r);
58
+ return new Selection(merged, newPrimary);
59
+ }
60
+ get primary() {
61
+ const r = this.ranges[this.primaryIndex];
62
+ if (!r)
63
+ throw new Error('unreachable: primaryIndex out of range');
64
+ return r;
65
+ }
66
+ /**
67
+ * Map every range through `changes`, then normalize. Text inserted at a range's edge, by another
68
+ * cursor or a remote user, stays outside it, and a cursor stays before text inserted at it.
69
+ * Commands that type at a cursor set the selection explicitly.
70
+ */
71
+ map(changes) {
72
+ const mapped = this.ranges.map((r) => {
73
+ if (isCursor(r)) {
74
+ const p = changes.mapPos(r.head, -1);
75
+ return { anchor: p, head: p };
76
+ }
77
+ const from = changes.mapPos(rangeFrom(r), 1);
78
+ const to = Math.max(from, changes.mapPos(rangeTo(r), -1));
79
+ return r.anchor <= r.head ? { anchor: from, head: to } : { anchor: to, head: from };
80
+ });
81
+ return Selection.create(mapped, this.primaryIndex);
82
+ }
83
+ /** Alt-click. Adds a cursor and makes it primary. */
84
+ addCursor(pos) {
85
+ const ranges = [...this.ranges, cursor(pos)];
86
+ return Selection.create(ranges, ranges.length - 1);
87
+ }
88
+ /** Escape. Keeps only the primary range, as a cursor at its head when `collapse` is set. */
89
+ toPrimary(collapse) {
90
+ const p = this.primary;
91
+ return collapse ? Selection.single(p.head) : Selection.single(p.anchor, p.head);
92
+ }
93
+ eq(other) {
94
+ if (this.primaryIndex !== other.primaryIndex)
95
+ return false;
96
+ if (this.ranges.length !== other.ranges.length)
97
+ return false;
98
+ for (let i = 0; i < this.ranges.length; i++) {
99
+ const a = this.ranges[i];
100
+ const b = other.ranges[i];
101
+ if (a.anchor !== b.anchor || a.head !== b.head)
102
+ return false;
103
+ }
104
+ return true;
105
+ }
106
+ }
@@ -0,0 +1,16 @@
1
+ /**
2
+ * Which whitespace the view marks, after VS Code's `editor.renderWhitespace`, with one change:
3
+ * only whitespace between characters is marked, never indentation (indent guides show that) and
4
+ * never trailing whitespace, except in 'trailing', whose whole job is to show it. 'selection'
5
+ * marks interior whitespace inside a non-empty selection, 'boundary' interior whitespace except
6
+ * single spaces between words, 'all' every interior run.
7
+ */
8
+ export type WhitespaceMode = 'none' | 'selection' | 'boundary' | 'trailing' | 'all';
9
+ export declare const isWhitespaceMode: (v: unknown) => v is WhitespaceMode;
10
+ /**
11
+ * The runs of spaces and tabs in `line` that `mode` marks, as flat `[from, to)` column pairs in
12
+ * ascending order. Only spaces and tabs are ever inside a run. `selected` is the line's selected
13
+ * columns in the same flat form, sorted and disjoint, and only 'selection' reads it. Pure, so a
14
+ * terminal view marks the same runs.
15
+ */
16
+ export declare function markedWhitespace(line: string, mode: WhitespaceMode, selected?: readonly number[]): number[];
@@ -0,0 +1,54 @@
1
+ const MODES = new Set(['none', 'selection', 'boundary', 'trailing', 'all']);
2
+ export const isWhitespaceMode = (v) => typeof v === 'string' && MODES.has(v);
3
+ const isSpace = (c) => c === 32 || c === 9;
4
+ /**
5
+ * The runs of spaces and tabs in `line` that `mode` marks, as flat `[from, to)` column pairs in
6
+ * ascending order. Only spaces and tabs are ever inside a run. `selected` is the line's selected
7
+ * columns in the same flat form, sorted and disjoint, and only 'selection' reads it. Pure, so a
8
+ * terminal view marks the same runs.
9
+ */
10
+ export function markedWhitespace(line, mode, selected = []) {
11
+ const out = [];
12
+ if (mode === 'none')
13
+ return out;
14
+ let end = line.length;
15
+ if (end > 0 && line.charCodeAt(end - 1) === 13)
16
+ end--;
17
+ // [lead, tail) spans the first through the last non-whitespace character; interior runs lie inside.
18
+ let lead = 0;
19
+ while (lead < end && isSpace(line.charCodeAt(lead)))
20
+ lead++;
21
+ let tail = end;
22
+ while (tail > lead && isSpace(line.charCodeAt(tail - 1)))
23
+ tail--;
24
+ if (mode === 'selection') {
25
+ for (let k = 0; k < selected.length; k += 2) {
26
+ const to = Math.min(selected[k + 1], tail);
27
+ for (let i = Math.max(lead, selected[k]); i < to;) {
28
+ if (!isSpace(line.charCodeAt(i))) {
29
+ i++;
30
+ continue;
31
+ }
32
+ const a = i;
33
+ while (i < to && isSpace(line.charCodeAt(i)))
34
+ i++;
35
+ out.push(a, i);
36
+ }
37
+ }
38
+ return out;
39
+ }
40
+ for (let i = 0; i < end;) {
41
+ if (!isSpace(line.charCodeAt(i))) {
42
+ i++;
43
+ continue;
44
+ }
45
+ const a = i;
46
+ while (i < end && isSpace(line.charCodeAt(i)))
47
+ i++;
48
+ const interior = a >= lead && i <= tail;
49
+ const marked = mode === 'trailing' ? i === end : interior && (mode === 'all' || !(i - a === 1 && line.charCodeAt(a) === 32));
50
+ if (marked)
51
+ out.push(a, i);
52
+ }
53
+ return out;
54
+ }
@@ -0,0 +1,56 @@
1
+ import type { Text } from './text.ts';
2
+ /** One edit in the coordinates of the document *before* the ChangeSet. */
3
+ export interface ChangeSpec {
4
+ readonly from: number;
5
+ readonly to?: number;
6
+ readonly insert?: string;
7
+ }
8
+ /** Wire form, structured-cloneable. Same layout as the in-memory representation. */
9
+ export interface SerializedChangeSet {
10
+ readonly sections: readonly number[];
11
+ readonly inserted: readonly string[];
12
+ }
13
+ /**
14
+ * CodeMirror 6 representation: `sections` is [len, insLen] pairs over the old document, with
15
+ * insLen === -1 marking a retained span. Invariant: `apply(t)` requires `t.length === length`.
16
+ */
17
+ export declare class ChangeSet {
18
+ readonly sections: readonly number[];
19
+ readonly inserted: readonly string[];
20
+ private touchIndex;
21
+ private constructor();
22
+ static of(changes: readonly ChangeSpec[], length: number): ChangeSet;
23
+ /** NEW. The identity change over a document of `length`. */
24
+ static empty(length: number): ChangeSet;
25
+ /** NEW. Rebuild from the wire form. The worker is the only caller. */
26
+ static fromJSON(json: SerializedChangeSet): ChangeSet;
27
+ get length(): number;
28
+ get newLength(): number;
29
+ /** NEW. True when the set retains everything. */
30
+ get isEmpty(): boolean;
31
+ apply(text: Text): Text;
32
+ mapPos(pos: number, assoc?: -1 | 1): number;
33
+ compose(other: ChangeSet): ChangeSet;
34
+ invert(original: Text): ChangeSet;
35
+ /**
36
+ * NEW. True when any deletion or insertion falls inside [from, to] (inclusive at both ends, so
37
+ * an insertion at a line's end counts as touching it). Old-document coordinates.
38
+ * The token bridge uses this to decide whether a stale line's tokens still describe its text,
39
+ * once per row of its mounted window and once per cached row: on a remote burst (a few hundred
40
+ * scattered single-character edits in one ChangeSet) that is hundreds of calls against the same
41
+ * instance, so this binary-searches a memoized index instead of rescanning every section.
42
+ */
43
+ touches(from: number, to: number): boolean;
44
+ /**
45
+ * NEW, not needed by the first slice. Rebase `this` over `other` (both against the same
46
+ * document), CM6's `ChangeSet.map`. Per-user undo maps its stack over remote changes with it.
47
+ * `before` breaks ties for insertions at the same position.
48
+ */
49
+ map(other: ChangeSet, before?: boolean): ChangeSet;
50
+ /**
51
+ * NEW. Calls `f(fromA, toA, fromB, toB, inserted)` for each changed section, in order.
52
+ * History grouping, LSP incremental sync, and line-level cache mapping all read changes this way.
53
+ */
54
+ iterChanges(f: (fromA: number, toA: number, fromB: number, toB: number, inserted: string) => void): void;
55
+ toJSON(): SerializedChangeSet;
56
+ }
@@ -0,0 +1,321 @@
1
+ /** Behind `ChangeSet.touches`: the old-document [start, end) of every ins >= 0 section, in order. */
2
+ function buildTouchIndex(sections) {
3
+ const starts = [];
4
+ const ends = [];
5
+ let pos = 0;
6
+ for (let i = 0; i < sections.length; i += 2) {
7
+ const len = sections[i];
8
+ if (sections[i + 1] >= 0) {
9
+ starts.push(pos);
10
+ ends.push(pos + len);
11
+ }
12
+ pos += len;
13
+ }
14
+ return { starts, ends };
15
+ }
16
+ // `sections` holds [len, ins] pairs over the old document; ins === -1 marks an unchanged span.
17
+ // `inserted[i]` is the text of section i ('' for unchanged sections and deletions).
18
+ class Builder {
19
+ sections = [];
20
+ inserted = [];
21
+ add(len, ins, text, join) {
22
+ if (len === 0 && ins <= 0)
23
+ return;
24
+ const last = this.sections.length - 2;
25
+ if (last >= 0 && ins <= 0 && ins === this.sections[last + 1]) {
26
+ this.sections[last] = this.sections[last] + len;
27
+ }
28
+ else if (last >= 0 && len === 0 && this.sections[last] === 0) {
29
+ this.sections[last + 1] = this.sections[last + 1] + ins;
30
+ }
31
+ else if (join) {
32
+ this.sections[last] = this.sections[last] + len;
33
+ this.sections[last + 1] = this.sections[last + 1] + ins;
34
+ }
35
+ else {
36
+ this.sections.push(len, ins);
37
+ this.inserted.push(text);
38
+ return;
39
+ }
40
+ if (text.length > 0)
41
+ this.inserted[this.inserted.length - 1] = this.inserted[this.inserted.length - 1] + text;
42
+ }
43
+ }
44
+ // Walks one set's sections. `off` counts what has been consumed of the current section.
45
+ class Cursor {
46
+ set;
47
+ i = 0;
48
+ len = 0;
49
+ ins = -2;
50
+ off = 0;
51
+ constructor(set) {
52
+ this.set = set;
53
+ this.next();
54
+ }
55
+ get done() {
56
+ return this.ins === -2;
57
+ }
58
+ get outLen() {
59
+ return this.ins < 0 ? this.len : this.ins;
60
+ }
61
+ next() {
62
+ const s = this.set.sections;
63
+ if (this.i < s.length) {
64
+ this.len = s[this.i];
65
+ this.ins = s[this.i + 1];
66
+ this.i += 2;
67
+ }
68
+ else {
69
+ this.len = 0;
70
+ this.ins = -2;
71
+ }
72
+ this.off = 0;
73
+ }
74
+ text() {
75
+ return this.set.inserted[(this.i - 2) >> 1] ?? '';
76
+ }
77
+ textPart(n) {
78
+ return this.text().slice(this.off, this.off + n);
79
+ }
80
+ advanceIn(n) {
81
+ if (n === this.len)
82
+ this.next();
83
+ else {
84
+ this.len -= n;
85
+ this.off += n;
86
+ }
87
+ }
88
+ advanceOut(n) {
89
+ if (this.ins < 0)
90
+ this.advanceIn(n);
91
+ else if (n === this.ins)
92
+ this.next();
93
+ else {
94
+ this.ins -= n;
95
+ this.off += n;
96
+ }
97
+ }
98
+ }
99
+ /**
100
+ * CodeMirror 6 representation: `sections` is [len, insLen] pairs over the old document, with
101
+ * insLen === -1 marking a retained span. Invariant: `apply(t)` requires `t.length === length`.
102
+ */
103
+ export class ChangeSet {
104
+ sections;
105
+ inserted;
106
+ // Lazily built, memoized index behind `touches` (old-document [start, end) of each ins >= 0
107
+ // section, both arrays sorted ascending since sections are walked in position order). A caller
108
+ // that asks `touches` about many ranges against the same ChangeSet (the token bridge re-keying
109
+ // its window one row at a time, or its cache one entry at a time) would otherwise rescan every
110
+ // section per call; building this once per instance turns that into one O(sections) pass plus a
111
+ // binary search per call.
112
+ touchIndex = null;
113
+ constructor(sections, inserted) {
114
+ this.sections = sections;
115
+ this.inserted = inserted;
116
+ }
117
+ static of(changes, length) {
118
+ const sorted = changes
119
+ .map((c, i) => ({ from: c.from, to: c.to ?? c.from, insert: c.insert ?? '', i }))
120
+ .sort((a, b) => a.from - b.from || a.i - b.i);
121
+ const b = new Builder();
122
+ let pos = 0;
123
+ for (const c of sorted) {
124
+ if (c.from > c.to || c.from < 0 || c.to > length)
125
+ throw new RangeError(`Invalid change ${c.from}-${c.to} in ${length}`);
126
+ if (c.from < pos)
127
+ throw new RangeError(`Overlapping change at ${c.from}`);
128
+ if (c.from === c.to && c.insert.length === 0)
129
+ continue;
130
+ if (c.from > pos)
131
+ b.add(c.from - pos, -1, '', false);
132
+ b.add(c.to - c.from, c.insert.length, c.insert, false);
133
+ pos = c.to;
134
+ }
135
+ if (pos < length)
136
+ b.add(length - pos, -1, '', false);
137
+ return new ChangeSet(b.sections, b.inserted);
138
+ }
139
+ /** NEW. The identity change over a document of `length`. */
140
+ static empty(length) {
141
+ return length > 0 ? new ChangeSet([length, -1], ['']) : new ChangeSet([], []);
142
+ }
143
+ /** NEW. Rebuild from the wire form. The worker is the only caller. */
144
+ static fromJSON(json) {
145
+ return new ChangeSet(json.sections, json.inserted);
146
+ }
147
+ get length() {
148
+ let n = 0;
149
+ for (let i = 0; i < this.sections.length; i += 2)
150
+ n += this.sections[i];
151
+ return n;
152
+ }
153
+ get newLength() {
154
+ let n = 0;
155
+ for (let i = 0; i < this.sections.length; i += 2) {
156
+ const ins = this.sections[i + 1];
157
+ n += ins < 0 ? this.sections[i] : ins;
158
+ }
159
+ return n;
160
+ }
161
+ /** NEW. True when the set retains everything. */
162
+ get isEmpty() {
163
+ for (let i = 0; i < this.sections.length; i += 2)
164
+ if (this.sections[i + 1] >= 0)
165
+ return false;
166
+ return true;
167
+ }
168
+ apply(text) {
169
+ if (text.length !== this.length)
170
+ throw new RangeError(`Applying change set of length ${this.length} to text of length ${text.length}`);
171
+ const ends = [];
172
+ let pos = 0;
173
+ for (let i = 0; i < this.sections.length; i += 2) {
174
+ const len = this.sections[i];
175
+ if (this.sections[i + 1] >= 0)
176
+ ends.push(pos, pos + len, i >> 1);
177
+ pos += len;
178
+ }
179
+ for (let k = ends.length - 3; k >= 0; k -= 3)
180
+ text = text.replace(ends[k], ends[k + 1], this.inserted[ends[k + 2]] ?? '');
181
+ return text;
182
+ }
183
+ mapPos(pos, assoc = -1) {
184
+ let posA = 0;
185
+ let posB = 0;
186
+ for (let i = 0; i < this.sections.length; i += 2) {
187
+ const len = this.sections[i];
188
+ const ins = this.sections[i + 1];
189
+ const endA = posA + len;
190
+ if (ins < 0) {
191
+ if (endA > pos)
192
+ return posB + (pos - posA);
193
+ posB += len;
194
+ }
195
+ else {
196
+ // Inside a replaced span, or exactly at a pure insertion with assoc < 0.
197
+ if (endA > pos || (endA === pos && len === 0 && assoc < 0))
198
+ return pos === posA || assoc < 0 ? posB : posB + ins;
199
+ posB += ins;
200
+ }
201
+ posA = endA;
202
+ }
203
+ if (pos > posA)
204
+ throw new RangeError(`Position ${pos} out of range for change set of length ${posA}`);
205
+ return posB;
206
+ }
207
+ // `other` must start in the document this set produces.
208
+ compose(other) {
209
+ if (this.newLength !== other.length)
210
+ throw new RangeError('Mismatched change set lengths');
211
+ const b = new Builder();
212
+ const a = new Cursor(this);
213
+ const c = new Cursor(other);
214
+ let open = false;
215
+ for (;;) {
216
+ if (a.done && c.done)
217
+ return new ChangeSet(b.sections, b.inserted);
218
+ if (a.ins === 0) {
219
+ // Deletion in the first set: nothing of it reaches the second.
220
+ b.add(a.len, 0, '', open);
221
+ a.next();
222
+ }
223
+ else if (c.len === 0 && !c.done) {
224
+ // Insertion in the second set.
225
+ b.add(0, c.ins, c.text(), open);
226
+ c.next();
227
+ }
228
+ else {
229
+ if (a.done || c.done)
230
+ throw new Error('Mismatched change set lengths');
231
+ const n = Math.min(a.outLen, c.len);
232
+ const before = b.sections.length;
233
+ if (a.ins < 0) {
234
+ const ins = c.ins < 0 ? -1 : c.off > 0 ? 0 : c.ins;
235
+ b.add(n, ins, ins > 0 ? c.text() : '', open);
236
+ }
237
+ else if (c.ins < 0) {
238
+ b.add(a.off > 0 ? 0 : a.len, n, a.textPart(n), open);
239
+ }
240
+ else {
241
+ b.add(a.off > 0 ? 0 : a.len, c.off > 0 ? 0 : c.ins, c.off > 0 ? '' : c.text(), open);
242
+ }
243
+ open = (a.ins > n || (c.ins >= 0 && c.len > n)) && (open || b.sections.length > before);
244
+ a.advanceOut(n);
245
+ c.advanceIn(n);
246
+ }
247
+ }
248
+ }
249
+ invert(original) {
250
+ const sections = this.sections.slice();
251
+ const inserted = [];
252
+ let pos = 0;
253
+ for (let i = 0; i < sections.length; i += 2) {
254
+ const len = sections[i];
255
+ const ins = sections[i + 1];
256
+ if (ins >= 0) {
257
+ sections[i] = ins;
258
+ sections[i + 1] = len;
259
+ inserted.push(len > 0 ? original.slice(pos, pos + len) : '');
260
+ }
261
+ else {
262
+ inserted.push('');
263
+ }
264
+ pos += len;
265
+ }
266
+ return new ChangeSet(sections, inserted);
267
+ }
268
+ /**
269
+ * NEW. True when any deletion or insertion falls inside [from, to] (inclusive at both ends, so
270
+ * an insertion at a line's end counts as touching it). Old-document coordinates.
271
+ * The token bridge uses this to decide whether a stale line's tokens still describe its text,
272
+ * once per row of its mounted window and once per cached row: on a remote burst (a few hundred
273
+ * scattered single-character edits in one ChangeSet) that is hundreds of calls against the same
274
+ * instance, so this binary-searches a memoized index instead of rescanning every section.
275
+ */
276
+ touches(from, to) {
277
+ const { starts, ends } = this.touchIndex ?? (this.touchIndex = buildTouchIndex(this.sections));
278
+ let lo = 0;
279
+ let hi = ends.length;
280
+ while (lo < hi) {
281
+ const mid = (lo + hi) >> 1;
282
+ if (ends[mid] < from)
283
+ lo = mid + 1;
284
+ else
285
+ hi = mid;
286
+ }
287
+ return lo < starts.length && starts[lo] <= to;
288
+ }
289
+ /**
290
+ * NEW, not needed by the first slice. Rebase `this` over `other` (both against the same
291
+ * document), CM6's `ChangeSet.map`. Per-user undo maps its stack over remote changes with it.
292
+ * `before` breaks ties for insertions at the same position.
293
+ */
294
+ map(other, before) {
295
+ void other, before;
296
+ throw new Error('not implemented');
297
+ }
298
+ /**
299
+ * NEW. Calls `f(fromA, toA, fromB, toB, inserted)` for each changed section, in order.
300
+ * History grouping, LSP incremental sync, and line-level cache mapping all read changes this way.
301
+ */
302
+ iterChanges(f) {
303
+ let posA = 0;
304
+ let posB = 0;
305
+ for (let i = 0; i < this.sections.length; i += 2) {
306
+ const len = this.sections[i];
307
+ const ins = this.sections[i + 1];
308
+ if (ins >= 0) {
309
+ f(posA, posA + len, posB, posB + ins, this.inserted[i >> 1] ?? '');
310
+ posB += ins;
311
+ }
312
+ else {
313
+ posB += len;
314
+ }
315
+ posA += len;
316
+ }
317
+ }
318
+ toJSON() {
319
+ return { sections: this.sections, inserted: this.inserted };
320
+ }
321
+ }
@@ -0,0 +1,30 @@
1
+ /** A line of a Text. `number` is 1-based. `to` excludes the "\n". */
2
+ export interface Line {
3
+ readonly number: number;
4
+ readonly from: number;
5
+ readonly to: number;
6
+ }
7
+ /**
8
+ * Immutable persistent rope. Positions are UTF-16 code units. Only "\n" breaks lines.
9
+ * `replace` shares untouched subtrees, so keeping old versions alive is cheap; the token bridge
10
+ * relies on that to hold the Text of every in-flight version.
11
+ */
12
+ export declare class Text {
13
+ private readonly root;
14
+ private constructor();
15
+ static readonly empty: Text;
16
+ static of(s: string): Text;
17
+ get length(): number;
18
+ get lines(): number;
19
+ /**
20
+ * Length in code units of the longest line. Nodes cache their part, so after an edit this costs
21
+ * O(log n) node combines on the edited path; the first call on a fresh document scans it once.
22
+ */
23
+ get maxLineLength(): number;
24
+ replace(from: number, to: number, insert: string): Text;
25
+ slice(from: number, to?: number): string;
26
+ private breakOffset;
27
+ lineAt(pos: number): Line;
28
+ line(n: number): Line;
29
+ toString(): string;
30
+ }