@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,68 @@
1
+ import type { Command } from '../commands/commands.ts';
2
+ import type { EditorCore } from '../state/core.ts';
3
+ import { type DefinitionResolver } from './resolver.ts';
4
+ import { Projection } from './projection.ts';
5
+ import type { PinnableTokens } from './slice-tokens.ts';
6
+ /**
7
+ * A document an expansion can project from: the host editor's own (same-document definitions), or
8
+ * another open file's (cross-document, later through LSP's textDocument/definition). The resolver
9
+ * hands one out; the expansion never owns it.
10
+ */
11
+ export interface SourceDocument {
12
+ readonly core: EditorCore;
13
+ /** Its undo. An edit inside an expansion lands in this document, so its undo runs here too.
14
+ * null when something else owns undo (a collaboration adapter's UndoManager, D5). */
15
+ readonly history: {
16
+ readonly undo: Command;
17
+ readonly redo: Command;
18
+ } | null;
19
+ /** Its tokens while a view has them, for the expansion to read its slice from. */
20
+ tokens(): PinnableTokens | null;
21
+ /** Shown in the expansion's header when it is not the host document, e.g. a file name. */
22
+ readonly label?: string;
23
+ }
24
+ /** One open expansion. Positions are the host document's, mapped through every edit. */
25
+ export interface Expansion {
26
+ /** Stable for the expansion's life; the view keys its widget by it. */
27
+ readonly key: string;
28
+ readonly name: string;
29
+ readonly doc: SourceDocument;
30
+ readonly projection: Projection;
31
+ /** Start of the call site's identifier. */
32
+ readonly anchor: number;
33
+ /** 1-based line of the call site, where the expansion sits below. */
34
+ readonly line: number;
35
+ }
36
+ /**
37
+ * Which call sites are expanded, as a follower of the host core with no DOM. It keeps one anchor
38
+ * per expansion and maps it through each Update in one sweep, O(expansions + log lines), the way
39
+ * FoldState maps folds; it must listen before the view, so the view's geometry reads anchors that
40
+ * are already mapped. An edit that turns the call site into something else closes its expansion.
41
+ * So does the end of its projection (its definition deleted, or its document replaced).
42
+ *
43
+ * Opening asks a pluggable resolver where the identifier at a position is defined. The default
44
+ * resolver is same-document; one backed by LSP's textDocument/definition returns another file's
45
+ * SourceDocument through the same interface.
46
+ */
47
+ export declare class InlineExpansions {
48
+ #private;
49
+ constructor(host: EditorCore, resolver: DefinitionResolver<SourceDocument> | null, signal?: AbortSignal);
50
+ set resolver(r: DefinitionResolver<SourceDocument> | null);
51
+ /** Sorted by line, then by when they opened. */
52
+ get list(): readonly Expansion[];
53
+ /** Expansions below `line`, oldest first. */
54
+ at(line: number): readonly Expansion[];
55
+ get(key: string): Expansion | undefined;
56
+ subscribe(listener: () => void): () => void;
57
+ /**
58
+ * Open the definition of the identifier at `pos` below its line, or return the one already open
59
+ * for that call site. null when nothing resolves.
60
+ */
61
+ open(pos: number): Expansion | null;
62
+ /** The expansion for the call site at `pos`, if one is open. */
63
+ openAt(pos: number): Expansion | undefined;
64
+ /** Close the call site's expansion if open, else open one. 'none' when nothing resolves. */
65
+ toggle(pos: number): 'opened' | 'closed' | 'none';
66
+ close(key: string): void;
67
+ closeAll(): void;
68
+ }
@@ -0,0 +1,143 @@
1
+ import { identifierAt } from "./resolver.js";
2
+ import { Projection } from "./projection.js";
3
+ let seq = 0;
4
+ /**
5
+ * Which call sites are expanded, as a follower of the host core with no DOM. It keeps one anchor
6
+ * per expansion and maps it through each Update in one sweep, O(expansions + log lines), the way
7
+ * FoldState maps folds; it must listen before the view, so the view's geometry reads anchors that
8
+ * are already mapped. An edit that turns the call site into something else closes its expansion.
9
+ * So does the end of its projection (its definition deleted, or its document replaced).
10
+ *
11
+ * Opening asks a pluggable resolver where the identifier at a position is defined. The default
12
+ * resolver is same-document; one backed by LSP's textDocument/definition returns another file's
13
+ * SourceDocument through the same interface.
14
+ */
15
+ export class InlineExpansions {
16
+ #host;
17
+ #resolver;
18
+ #list = [];
19
+ #listeners = [];
20
+ constructor(host, resolver, signal) {
21
+ this.#host = host;
22
+ this.#resolver = resolver;
23
+ host.listen((u) => this.#onUpdate(u), signal);
24
+ signal?.addEventListener('abort', () => this.closeAll(), { once: true });
25
+ }
26
+ set resolver(r) {
27
+ this.#resolver = r;
28
+ }
29
+ /** Sorted by line, then by when they opened. */
30
+ get list() {
31
+ return this.#list;
32
+ }
33
+ /** Expansions below `line`, oldest first. */
34
+ at(line) {
35
+ return this.#list.filter((e) => e.line === line);
36
+ }
37
+ get(key) {
38
+ return this.#list.find((e) => e.key === key);
39
+ }
40
+ subscribe(listener) {
41
+ this.#listeners.push(listener);
42
+ return () => (this.#listeners = this.#listeners.filter((l) => l !== listener));
43
+ }
44
+ /**
45
+ * Open the definition of the identifier at `pos` below its line, or return the one already open
46
+ * for that call site. null when nothing resolves.
47
+ */
48
+ open(pos) {
49
+ const state = this.#host.state;
50
+ const id = identifierAt(state.text, pos);
51
+ if (!id || !this.#resolver)
52
+ return null;
53
+ const line = state.text.lineAt(id.from).number;
54
+ const open = this.#list.find((e) => e.line === line && e.name === id.name);
55
+ if (open)
56
+ return open;
57
+ const def = this.#resolver(state, pos);
58
+ if (!def)
59
+ return null;
60
+ const projection = new Projection(def.doc.core, { from: def.from, to: def.to });
61
+ const entry = { key: `x${++seq}`, name: def.name, doc: def.doc, projection, anchor: id.from, line };
62
+ projection.subscribe((reason) => {
63
+ if (reason === 'detached')
64
+ this.#remove(entry.key);
65
+ });
66
+ const i = this.#list.findIndex((e) => e.line > line);
67
+ this.#list.splice(i < 0 ? this.#list.length : i, 0, entry);
68
+ this.#emit();
69
+ return entry;
70
+ }
71
+ /** The expansion for the call site at `pos`, if one is open. */
72
+ openAt(pos) {
73
+ const state = this.#host.state;
74
+ const id = identifierAt(state.text, pos);
75
+ if (!id)
76
+ return undefined;
77
+ const line = state.text.lineAt(id.from).number;
78
+ return this.#list.find((e) => e.line === line && e.name === id.name);
79
+ }
80
+ /** Close the call site's expansion if open, else open one. 'none' when nothing resolves. */
81
+ toggle(pos) {
82
+ const open = this.openAt(pos);
83
+ if (open) {
84
+ this.close(open.key);
85
+ return 'closed';
86
+ }
87
+ return this.open(pos) ? 'opened' : 'none';
88
+ }
89
+ close(key) {
90
+ const e = this.#list.find((x) => x.key === key);
91
+ if (!e)
92
+ return;
93
+ e.projection.dispose();
94
+ this.#remove(key);
95
+ }
96
+ closeAll() {
97
+ for (const e of this.#list)
98
+ e.projection.dispose();
99
+ if (this.#list.length === 0)
100
+ return;
101
+ this.#list = [];
102
+ this.#emit();
103
+ }
104
+ #remove(key) {
105
+ const before = this.#list.length;
106
+ this.#list = this.#list.filter((e) => e.key !== key);
107
+ if (this.#list.length !== before)
108
+ this.#emit();
109
+ }
110
+ #emit() {
111
+ for (const l of this.#listeners)
112
+ l();
113
+ }
114
+ #onUpdate(u) {
115
+ if (!u.docChanged || this.#list.length === 0)
116
+ return;
117
+ if (u.origin === 'host.set')
118
+ return this.closeAll();
119
+ const text = u.after.text;
120
+ let changed = false;
121
+ const keep = [];
122
+ for (const e of this.#list) {
123
+ const anchor = u.changes.mapPos(e.anchor, 1);
124
+ const line = text.lineAt(anchor).number;
125
+ // Only an edit on the call site's own line can turn it into something else.
126
+ const l = u.before.text.lineAt(e.anchor);
127
+ if (u.changes.touches(l.from, l.to) && identifierAt(text, anchor)?.name !== e.name) {
128
+ e.projection.dispose();
129
+ changed = true;
130
+ continue;
131
+ }
132
+ if (line !== e.line)
133
+ changed = true;
134
+ e.anchor = anchor;
135
+ e.line = line;
136
+ keep.push(e);
137
+ }
138
+ // Mapping keeps the order: anchors never cross.
139
+ this.#list = keep;
140
+ if (changed)
141
+ this.#emit();
142
+ }
143
+ }
@@ -0,0 +1,73 @@
1
+ import { ChangeSet, type ChangeSpec } from '../text/changeset.ts';
2
+ import type { Text } from '../text/text.ts';
3
+ import { EditorCore } from '../state/core.ts';
4
+ /** A range of whole lines in a source document: `from` is a line start, `to` a line end. */
5
+ export interface SliceRange {
6
+ readonly from: number;
7
+ readonly to: number;
8
+ }
9
+ /**
10
+ * Pure. Shift a ChangeSet over the slice [from, from + inner.length] into the source's coordinates:
11
+ * retain everything before and after it. O(changes).
12
+ */
13
+ export declare function liftChanges(inner: ChangeSet, from: number, sourceLength: number): ChangeSet;
14
+ /** What a source ChangeSet does to a slice. `changes` are in the slice's old coordinates. */
15
+ export interface Lowered {
16
+ readonly changes: readonly ChangeSpec[];
17
+ /** The slice in the source's new coordinates, grown to whole lines. */
18
+ readonly range: SliceRange;
19
+ /** Every character of the slice was deleted: there is nothing left to project. */
20
+ readonly gone: boolean;
21
+ }
22
+ /**
23
+ * Pure. Restrict a source ChangeSet to the slice [from, to] and move the slice through it.
24
+ *
25
+ * Gravity is outside: text inserted exactly at either edge, or by a change that reaches across an
26
+ * edge, lands outside the slice. Then the slice grows to whole lines of `after`, and whatever it
27
+ * grew over is inserted into it. So typing at column 0 of the first line, or after the closing
28
+ * brace on the last one, still shows up inside, while a line inserted above or below does not.
29
+ * Whole lines are what keep inner line i and source line `first + i - 1` the same text, which is
30
+ * what lets the inner editor reuse the source's tokens. O(changes + log lines).
31
+ */
32
+ export declare function lowerChanges(changes: ChangeSet, range: SliceRange, after: Text): Lowered;
33
+ /**
34
+ * Snap a range to whole lines of `text`. Resolvers already return line-aligned ranges; this is for
35
+ * callers that do not.
36
+ */
37
+ export declare function wholeLines(text: Text, range: SliceRange): SliceRange;
38
+ /**
39
+ * Projection core (option A in research/inline-expansion/REPORT.md). `inner` is a second
40
+ * EditorCore whose document is the slice [from, to] of `source`, always whole lines. It is a
41
+ * follower of both cores and the only writer that crosses between them:
42
+ *
43
+ * inner Update (local edit) -> liftChanges -> source.apply, with the inner's origin, so the
44
+ * source's History records and groups it as if typed there.
45
+ * source Update touching the slice -> lowerChanges -> inner.apply with origin 'remote'.
46
+ *
47
+ * Reentrancy: each direction applies only to the *other* core, never to the one dispatching to
48
+ * it, and marks itself while it does so it ignores the echo. A chain of projections (an expansion
49
+ * inside an expansion) and a diamond (two expansions over one source) stay acyclic for the same
50
+ * reason. The one way to hit `apply`'s reentrancy guard is an inner edit made synchronously from
51
+ * a source listener; the guard throws before the source changes, and the projection detaches
52
+ * rather than let the two documents disagree.
53
+ *
54
+ * The inner core has no History: undo belongs to the document an edit lands in (the source), so
55
+ * the inner editor's undo keys run the source's History (see `InlineExpansions`).
56
+ *
57
+ * DOM-free: a terminal view can project the same way.
58
+ */
59
+ export declare class Projection {
60
+ #private;
61
+ readonly source: EditorCore;
62
+ readonly inner: EditorCore;
63
+ constructor(source: EditorCore, range: SliceRange, signal?: AbortSignal);
64
+ /** The slice in the source's current coordinates. */
65
+ get range(): SliceRange;
66
+ get detached(): boolean;
67
+ /** 1-based source line of the inner document's first line. */
68
+ get firstLine(): number;
69
+ /** 'moved' after the range changed; 'detached' once, when the projection ends. */
70
+ subscribe(listener: (reason: 'moved' | 'detached') => void): () => void;
71
+ /** End the projection. Safe inside a notification loop, like detaching. */
72
+ dispose(): void;
73
+ }
@@ -0,0 +1,189 @@
1
+ import { ChangeSet } from "../text/changeset.js";
2
+ import { EditorCore } from "../state/core.js";
3
+ import { Selection } from "../state/selection.js";
4
+ /**
5
+ * Pure. Shift a ChangeSet over the slice [from, from + inner.length] into the source's coordinates:
6
+ * retain everything before and after it. O(changes).
7
+ */
8
+ export function liftChanges(inner, from, sourceLength) {
9
+ const specs = [];
10
+ inner.iterChanges((fromA, toA, _fromB, _toB, insert) => specs.push({ from: from + fromA, to: from + toA, insert }));
11
+ return ChangeSet.of(specs, sourceLength);
12
+ }
13
+ /**
14
+ * Pure. Restrict a source ChangeSet to the slice [from, to] and move the slice through it.
15
+ *
16
+ * Gravity is outside: text inserted exactly at either edge, or by a change that reaches across an
17
+ * edge, lands outside the slice. Then the slice grows to whole lines of `after`, and whatever it
18
+ * grew over is inserted into it. So typing at column 0 of the first line, or after the closing
19
+ * brace on the last one, still shows up inside, while a line inserted above or below does not.
20
+ * Whole lines are what keep inner line i and source line `first + i - 1` the same text, which is
21
+ * what lets the inner editor reuse the source's tokens. O(changes + log lines).
22
+ */
23
+ export function lowerChanges(changes, range, after) {
24
+ const { from, to } = range;
25
+ const specs = [];
26
+ let shift = 0;
27
+ let straddled = -1;
28
+ let delta = 0;
29
+ changes.iterChanges((fromA, toA, _fromB, toB, insert) => {
30
+ // Wholly before (including a pure insertion at `from`), or wholly after (at `to`).
31
+ if (toA <= from) {
32
+ shift += insert.length - (toA - fromA);
33
+ return;
34
+ }
35
+ if (fromA >= to)
36
+ return;
37
+ if (fromA < from)
38
+ straddled = toB;
39
+ const inside = fromA >= from && toA <= to;
40
+ const a = Math.max(fromA, from) - from;
41
+ const b = Math.min(toA, to) - from;
42
+ const ins = inside ? insert : '';
43
+ if (b > a || ins.length > 0)
44
+ specs.push({ from: a, to: b, insert: ins });
45
+ delta += ins.length - (b - a);
46
+ });
47
+ const newFrom = straddled >= 0 ? straddled : from + shift;
48
+ const newTo = newFrom + (to - from) + delta;
49
+ const gone = to > from && newTo === newFrom;
50
+ if (gone)
51
+ return { changes: specs, range: { from: newFrom, to: newTo }, gone };
52
+ // Grow to whole lines. The text grown over sits just outside the slice in `after`.
53
+ const lineFrom = after.lineAt(newFrom).from;
54
+ const lineTo = after.lineAt(newTo).to;
55
+ const oldLength = to - from;
56
+ const out = [];
57
+ if (lineFrom < newFrom)
58
+ out.push({ from: 0, insert: after.slice(lineFrom, newFrom) });
59
+ out.push(...specs);
60
+ if (lineTo > newTo)
61
+ out.push({ from: oldLength, insert: after.slice(newTo, lineTo) });
62
+ return { changes: out, range: { from: lineFrom, to: lineTo }, gone: false };
63
+ }
64
+ /**
65
+ * Snap a range to whole lines of `text`. Resolvers already return line-aligned ranges; this is for
66
+ * callers that do not.
67
+ */
68
+ export function wholeLines(text, range) {
69
+ return { from: text.lineAt(range.from).from, to: text.lineAt(Math.max(range.from, range.to)).to };
70
+ }
71
+ /**
72
+ * Projection core (option A in research/inline-expansion/REPORT.md). `inner` is a second
73
+ * EditorCore whose document is the slice [from, to] of `source`, always whole lines. It is a
74
+ * follower of both cores and the only writer that crosses between them:
75
+ *
76
+ * inner Update (local edit) -> liftChanges -> source.apply, with the inner's origin, so the
77
+ * source's History records and groups it as if typed there.
78
+ * source Update touching the slice -> lowerChanges -> inner.apply with origin 'remote'.
79
+ *
80
+ * Reentrancy: each direction applies only to the *other* core, never to the one dispatching to
81
+ * it, and marks itself while it does so it ignores the echo. A chain of projections (an expansion
82
+ * inside an expansion) and a diamond (two expansions over one source) stay acyclic for the same
83
+ * reason. The one way to hit `apply`'s reentrancy guard is an inner edit made synchronously from
84
+ * a source listener; the guard throws before the source changes, and the projection detaches
85
+ * rather than let the two documents disagree.
86
+ *
87
+ * The inner core has no History: undo belongs to the document an edit lands in (the source), so
88
+ * the inner editor's undo keys run the source's History (see `InlineExpansions`).
89
+ *
90
+ * DOM-free: a terminal view can project the same way.
91
+ */
92
+ export class Projection {
93
+ source;
94
+ inner;
95
+ #range;
96
+ /** Applying a lowered source change to the inner core. */
97
+ #pushing = false;
98
+ /** Applying a lifted inner change to the source core. */
99
+ #lifting = false;
100
+ #detached = false;
101
+ #listeners = [];
102
+ #lifetime = new AbortController();
103
+ constructor(source, range, signal) {
104
+ this.source = source;
105
+ this.#range = wholeLines(source.state.text, range);
106
+ this.inner = new EditorCore(source.state.text.slice(this.#range.from, this.#range.to), Selection.single(0));
107
+ const life = signal ? AbortSignal.any([signal, this.#lifetime.signal]) : this.#lifetime.signal;
108
+ source.listen((u) => this.#onSource(u), life);
109
+ this.inner.listen((u) => this.#onInner(u), life);
110
+ life.addEventListener('abort', () => this.#detach(), { once: true });
111
+ }
112
+ /** The slice in the source's current coordinates. */
113
+ get range() {
114
+ return this.#range;
115
+ }
116
+ get detached() {
117
+ return this.#detached;
118
+ }
119
+ /** 1-based source line of the inner document's first line. */
120
+ get firstLine() {
121
+ return this.source.state.text.lineAt(this.#range.from).number;
122
+ }
123
+ /** 'moved' after the range changed; 'detached' once, when the projection ends. */
124
+ subscribe(listener) {
125
+ this.#listeners.push(listener);
126
+ return () => (this.#listeners = this.#listeners.filter((l) => l !== listener));
127
+ }
128
+ /** End the projection. Safe inside a notification loop, like detaching. */
129
+ dispose() {
130
+ this.#detach();
131
+ }
132
+ #emit(reason) {
133
+ for (const l of this.#listeners)
134
+ l(reason);
135
+ }
136
+ /** Listeners turn into no-ops at once; unsubscribing waits for a microtask, because this can run
137
+ * inside a core's notification loop, and removing a listener there would skip the next one. */
138
+ #detach() {
139
+ if (this.#detached)
140
+ return;
141
+ this.#detached = true;
142
+ queueMicrotask(() => this.#lifetime.abort());
143
+ this.#emit('detached');
144
+ }
145
+ #onSource(u) {
146
+ if (!u.docChanged || this.#lifting || this.#detached)
147
+ return;
148
+ if (u.origin === 'host.set')
149
+ return this.#detach();
150
+ const before = this.#range;
151
+ const lowered = lowerChanges(u.changes, before, u.after.text);
152
+ if (lowered.gone)
153
+ return this.#detach();
154
+ this.#range = lowered.range;
155
+ if (lowered.changes.length > 0) {
156
+ this.#pushing = true;
157
+ try {
158
+ this.inner.apply({ changes: lowered.changes, origin: 'remote' });
159
+ }
160
+ finally {
161
+ this.#pushing = false;
162
+ }
163
+ }
164
+ if (before.from !== this.#range.from || before.to !== this.#range.to)
165
+ this.#emit('moved');
166
+ }
167
+ #onInner(u) {
168
+ if (!u.docChanged || this.#pushing || this.#detached)
169
+ return;
170
+ const { from } = this.#range;
171
+ const changes = liftChanges(u.changes, from, this.source.state.text.length);
172
+ this.#lifting = true;
173
+ try {
174
+ this.source.apply({ changes, origin: u.origin });
175
+ }
176
+ catch (err) {
177
+ // The source refused (it was dispatching). The inner already changed, so the two would now
178
+ // disagree: end the projection instead, loudly.
179
+ console.error('inline expansion detached: its source could not take the edit', err);
180
+ this.#detach();
181
+ return;
182
+ }
183
+ finally {
184
+ this.#lifting = false;
185
+ }
186
+ this.#range = { from, to: from + this.inner.state.text.length };
187
+ this.#emit('moved');
188
+ }
189
+ }
@@ -0,0 +1,42 @@
1
+ import { Text } from '../text/text.ts';
2
+ import type { Snapshot } from '../state/core.ts';
3
+ /** A definition found in some text: line-aligned [from, to]. */
4
+ export interface DefinitionSpan {
5
+ readonly from: number;
6
+ readonly to: number;
7
+ readonly name: string;
8
+ readonly kind: 'function' | 'method' | 'arrow';
9
+ /** Offset of the name token inside the definition header. */
10
+ readonly nameFrom: number;
11
+ }
12
+ /** What a resolver returns: the document holding the definition and its line-aligned range. */
13
+ export interface Definition<D> {
14
+ readonly doc: D;
15
+ readonly from: number;
16
+ readonly to: number;
17
+ readonly name: string;
18
+ }
19
+ export type DefinitionResolver<D> = (state: Snapshot, pos: number) => Definition<D> | null;
20
+ /**
21
+ * The identifier touching `pos`, scanned within its line only (so this stays cheap on huge docs).
22
+ * `pos` may sit anywhere inside the token, including either edge. Rejects tokens that start with
23
+ * a digit (number literals) and JS/TS reserved words, since neither is a useful jump target.
24
+ */
25
+ export declare function identifierAt(text: Text, pos: number): {
26
+ from: number;
27
+ to: number;
28
+ name: string;
29
+ } | null;
30
+ /**
31
+ * Finds where `name` is defined in `text`, preferring (in order) a `function` declaration, a
32
+ * class/object method, then a `const`/`let`/`var` arrow — and within a kind, the first in the
33
+ * document. Scans `text.toString()` once; a hand-rolled bracket scanner (not per-line arrays)
34
+ * keeps this cheap even on very large documents.
35
+ */
36
+ export declare function findDefinition(text: Text, name: string): DefinitionSpan | null;
37
+ /**
38
+ * Resolves `pos` to a definition of the identifier under it within the same document. Returns
39
+ * null when there's no identifier, no definition, or the caret is already on the definition's own
40
+ * name (nothing useful to jump to).
41
+ */
42
+ export declare function sameDocumentResolver<D>(doc: D): DefinitionResolver<D>;