@nibling/editor 0.0.0-stage → 0.0.2

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 +66 -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,308 @@
1
+ import { ChangeSet } from "../text/changeset.js";
2
+ /** Lines per block: the unit `SearchIndex` counts in, and the slice a scan reads from the rope. */
3
+ export const BLOCK = 512;
4
+ const WORD = /[\p{L}\p{N}\p{M}_]/u;
5
+ const isWord = (ch) => ch !== undefined && WORD.test(ch);
6
+ /** Compiles a query. null for an empty search; `{ error }` for a regexp that does not parse. */
7
+ export function searcher(query) {
8
+ if (query.search === '')
9
+ return null;
10
+ const flags = 'gm' + (query.caseSensitive ? '' : 'i');
11
+ if (!query.regexp)
12
+ return new Searcher(query, new RegExp(query.search.replace(/[\\^$.*+?()[\]{}|/]/g, '\\$&'), flags + 'u'));
13
+ try {
14
+ return new Searcher(query, new RegExp(query.search, flags + 'u'));
15
+ }
16
+ catch {
17
+ // Unicode mode rejects escapes plain mode allows (`\-`, `\ `), which people type.
18
+ try {
19
+ return new Searcher(query, new RegExp(query.search, flags));
20
+ }
21
+ catch (err) {
22
+ return { error: err.message };
23
+ }
24
+ }
25
+ }
26
+ /**
27
+ * Finds matches of one query in any Text. Stateless over the document: every method takes the
28
+ * Text, so a snapshot's matches never go stale. Reads the rope a block of lines at a time and runs
29
+ * the regexp over each line on its own, so a match cannot cross a line break.
30
+ */
31
+ export class Searcher {
32
+ query;
33
+ #re;
34
+ constructor(query, re) {
35
+ this.query = query;
36
+ this.#re = re;
37
+ }
38
+ /**
39
+ * Calls `f` for each match on lines `fromLine..toLine` (1-based, inclusive), in document order.
40
+ * Stops when `f` returns false.
41
+ */
42
+ scan(text, fromLine, toLine, f) {
43
+ const re = this.#re;
44
+ const word = !!this.query.wholeWord;
45
+ const groups = !!this.query.regexp;
46
+ toLine = Math.min(toLine, text.lines);
47
+ for (let a = Math.max(1, fromLine); a <= toLine; a += BLOCK) {
48
+ const b = Math.min(toLine, a + BLOCK - 1);
49
+ const start = text.line(a).from;
50
+ const chunk = text.slice(start, text.line(b).to);
51
+ let pos = 0;
52
+ for (;;) {
53
+ const nl = chunk.indexOf('\n', pos);
54
+ const s = nl < 0 ? (pos === 0 ? chunk : chunk.slice(pos)) : chunk.slice(pos, nl);
55
+ re.lastIndex = 0;
56
+ for (let m = re.exec(s); m; m = re.exec(s)) {
57
+ const i = m.index;
58
+ const j = i + m[0].length;
59
+ if (j === i) {
60
+ // An empty match (`^`, `x*`) counts for nothing; step past it.
61
+ re.lastIndex = i + (s.codePointAt(i) > 0xffff ? 2 : 1);
62
+ if (re.lastIndex > s.length)
63
+ break;
64
+ continue;
65
+ }
66
+ if (word && !(bounded(s, i) && bounded(s, j))) {
67
+ re.lastIndex = i + 1;
68
+ continue;
69
+ }
70
+ const match = groups ? { from: start + pos + i, to: start + pos + j, groups: m } : { from: start + pos + i, to: start + pos + j };
71
+ if (f(match) === false)
72
+ return;
73
+ }
74
+ if (nl < 0)
75
+ break;
76
+ pos = nl + 1;
77
+ }
78
+ }
79
+ }
80
+ /** Matches on the lines spanning [from, to], at most `limit` of them. */
81
+ matches(text, from, to, limit = Infinity) {
82
+ const out = [];
83
+ if (limit <= 0)
84
+ return out;
85
+ this.scan(text, text.lineAt(from).number, text.lineAt(to).number, (m) => {
86
+ if (m.to < from || m.from > to)
87
+ return;
88
+ out.push(m);
89
+ return out.length < limit;
90
+ });
91
+ return out;
92
+ }
93
+ /** The match exactly covering [from, to), or null. */
94
+ matchAt(text, from, to) {
95
+ if (to <= from)
96
+ return null;
97
+ const line = text.lineAt(from).number;
98
+ let found = null;
99
+ this.scan(text, line, line, (m) => {
100
+ if (m.from === from && m.to === to)
101
+ found = m;
102
+ return m.from < from;
103
+ });
104
+ return found;
105
+ }
106
+ next(text, pos, empty, limit = Infinity) {
107
+ const startLine = text.lineAt(pos).number;
108
+ let found = null;
109
+ const take = (m) => {
110
+ found = m;
111
+ return false;
112
+ };
113
+ // The cursor's own line first, from the cursor on.
114
+ this.scan(text, startLine, startLine, (m) => (m.from >= pos ? take(m) : true));
115
+ if (found)
116
+ return found;
117
+ const blocks = Math.ceil(text.lines / BLOCK);
118
+ const startBlock = Math.floor((startLine - 1) / BLOCK);
119
+ for (let k = 0; k <= blocks && !found; k++) {
120
+ if (k >= limit)
121
+ return undefined;
122
+ const b = (startBlock + k) % blocks;
123
+ if (empty?.(b))
124
+ continue;
125
+ let lo = b * BLOCK + 1;
126
+ let hi = Math.min(text.lines, (b + 1) * BLOCK);
127
+ if (k === 0)
128
+ lo = startLine + 1;
129
+ if (k === blocks)
130
+ hi = startLine; // wrapped all the way: the cursor's line, before the cursor
131
+ if (lo > hi)
132
+ continue;
133
+ this.scan(text, lo, hi, take);
134
+ }
135
+ return found;
136
+ }
137
+ /** The last match starting before `pos`, wrapping to the bottom. */
138
+ prev(text, pos, empty) {
139
+ const startLine = text.lineAt(pos).number;
140
+ let found = null;
141
+ const last = (bound) => (m) => {
142
+ if (m.from < bound)
143
+ found = m;
144
+ else
145
+ return false;
146
+ };
147
+ this.scan(text, startLine, startLine, last(pos));
148
+ if (found)
149
+ return found;
150
+ const blocks = Math.ceil(text.lines / BLOCK);
151
+ const startBlock = Math.floor((startLine - 1) / BLOCK);
152
+ for (let k = 0; k <= blocks && !found; k++) {
153
+ const b = (((startBlock - k) % blocks) + blocks) % blocks;
154
+ if (empty?.(b))
155
+ continue;
156
+ let lo = b * BLOCK + 1;
157
+ let hi = Math.min(text.lines, (b + 1) * BLOCK);
158
+ if (k === 0)
159
+ hi = startLine - 1;
160
+ if (k === blocks)
161
+ lo = startLine; // wrapped: the cursor's line, after the cursor
162
+ if (lo > hi)
163
+ continue;
164
+ this.scan(text, lo, hi, last(Infinity));
165
+ }
166
+ return found;
167
+ }
168
+ /** What a match is replaced with: the text as typed, or with `$&`, `$1`, `$<name>`, `$$`
169
+ * expanded for a regexp. */
170
+ replacement(m, replace) {
171
+ const g = m.groups;
172
+ if (!g)
173
+ return replace;
174
+ return replace.replace(/\$(\$|&|\d{1,2}|<([^>]*)>)/g, (all, ref, name) => {
175
+ if (ref === '$')
176
+ return '$';
177
+ if (ref === '&')
178
+ return g[0];
179
+ if (name !== undefined)
180
+ return g.groups?.[name] ?? '';
181
+ // `$12` with fewer than 12 groups reads as `$1` then "2", as String.prototype.replace does.
182
+ const n = Number(ref);
183
+ if (n > 0 && n < g.length)
184
+ return g[n] ?? '';
185
+ if (ref.length === 2 && Number(ref[0]) > 0 && Number(ref[0]) < g.length)
186
+ return (g[Number(ref[0])] ?? '') + ref[1];
187
+ return all;
188
+ });
189
+ }
190
+ /** One ChangeSet replacing every match in the document: one undo step. Empty when none. */
191
+ replaceAll(text, replace) {
192
+ const specs = [];
193
+ this.scan(text, 1, text.lines, (m) => void specs.push({ from: m.from, to: m.to, insert: this.replacement(m, replace) }));
194
+ return ChangeSet.of(specs, text.length);
195
+ }
196
+ }
197
+ /** Whether offset `i` of `s` is a word boundary, for whole-word matching. */
198
+ function bounded(s, i) {
199
+ return !isWord(s[i - 1]) || !isWord(s[i]);
200
+ }
201
+ /**
202
+ * Match counts per block of lines, filled in a slice at a time off the hot path, so a 100k-line
203
+ * document never scans in one go. Gives the total, the index of a match ("3 of 120"), and lets
204
+ * navigation skip blocks known to be empty. A follower of the document: `map` keeps it current
205
+ * through an edit by forgetting only the blocks the edit touched.
206
+ */
207
+ export class SearchIndex {
208
+ searcher;
209
+ #text;
210
+ /** Matches per block; -1 while uncounted. */
211
+ #counts;
212
+ /** No uncounted block below this one. */
213
+ #low = 0;
214
+ constructor(searcher, text) {
215
+ this.searcher = searcher;
216
+ this.#text = text;
217
+ this.#counts = new Int32Array(Math.ceil(text.lines / BLOCK)).fill(-1);
218
+ }
219
+ get text() {
220
+ return this.#text;
221
+ }
222
+ get complete() {
223
+ return this.#low >= this.#counts.length;
224
+ }
225
+ /** Every match in the document, or null while some block is uncounted. */
226
+ get total() {
227
+ if (!this.complete)
228
+ return null;
229
+ let n = 0;
230
+ for (const c of this.#counts)
231
+ n += c;
232
+ return n;
233
+ }
234
+ /** Count blocks until `stop()` says the slice is spent. True once every block is counted. */
235
+ step(stop) {
236
+ const counts = this.#counts;
237
+ const text = this.#text;
238
+ for (;;) {
239
+ while (this.#low < counts.length && counts[this.#low] >= 0)
240
+ this.#low++;
241
+ if (this.#low >= counts.length)
242
+ return true;
243
+ const b = this.#low;
244
+ let n = 0;
245
+ this.searcher.scan(text, b * BLOCK + 1, (b + 1) * BLOCK, () => void n++);
246
+ counts[b] = n;
247
+ if (stop())
248
+ break;
249
+ }
250
+ while (this.#low < counts.length && counts[this.#low] >= 0)
251
+ this.#low++;
252
+ return this.complete;
253
+ }
254
+ /** Follow an edit from `before` to `after`. Blocks the edit did not touch keep their counts. */
255
+ map(changes, before, after) {
256
+ let lo = Infinity;
257
+ let hi = -Infinity;
258
+ changes.iterChanges((fromA, toA) => {
259
+ lo = Math.min(lo, fromA);
260
+ hi = Math.max(hi, toA);
261
+ });
262
+ this.#text = after;
263
+ if (lo === Infinity)
264
+ return;
265
+ const first = Math.floor((before.lineAt(lo).number - 1) / BLOCK);
266
+ const blocks = Math.ceil(after.lines / BLOCK);
267
+ let last = blocks - 1;
268
+ // Same line count: later lines kept their numbers, so only the touched blocks change.
269
+ if (before.lines === after.lines)
270
+ last = Math.floor((before.lineAt(hi).number - 1) / BLOCK);
271
+ let counts = this.#counts;
272
+ if (counts.length !== blocks) {
273
+ const next = new Int32Array(blocks).fill(-1);
274
+ next.set(counts.subarray(0, Math.min(first, blocks)));
275
+ counts = this.#counts = next;
276
+ }
277
+ counts.fill(-1, first, last + 1);
278
+ this.#low = Math.min(this.#low, first);
279
+ }
280
+ /** Whether block `b` is counted and holds no match. */
281
+ empty = (b) => this.#counts[b] === 0;
282
+ /** The 0-based index of match `m` among all matches, or null while blocks before it are
283
+ * uncounted. Scans at most one block. */
284
+ indexOf(m) {
285
+ const text = this.#text;
286
+ const line = text.lineAt(m.from).number;
287
+ const b = Math.floor((line - 1) / BLOCK);
288
+ let n = 0;
289
+ for (let i = 0; i < b; i++) {
290
+ const c = this.#counts[i];
291
+ if (c < 0)
292
+ return null;
293
+ n += c;
294
+ }
295
+ this.searcher.scan(text, b * BLOCK + 1, line, (x) => {
296
+ if (x.from >= m.from)
297
+ return false;
298
+ n++;
299
+ });
300
+ return n;
301
+ }
302
+ next(pos) {
303
+ return this.searcher.next(this.#text, pos, this.empty);
304
+ }
305
+ prev(pos) {
306
+ return this.searcher.prev(this.#text, pos, this.empty);
307
+ }
308
+ }
@@ -0,0 +1,16 @@
1
+ export { Text, type Line } from './text/text.ts';
2
+ export { ChangeSet, type ChangeSpec } from './text/changeset.ts';
3
+ export { Selection, type SelRange, cursor, rangeFrom, rangeTo, isCursor } from './state/selection.ts';
4
+ export { EditorCore, type Snapshot, type TransactionSpec, type Update, type Origin, type Version } from './state/core.ts';
5
+ export { History } from './state/history.ts';
6
+ export * as commands from './commands/commands.ts';
7
+ export type { Command, CommandContext, Layout } from './commands/commands.ts';
8
+ export { Keymap, defaultKeymap, type KeyBinding } from './commands/keymap.ts';
9
+ export { detectIndentUnit, indentColumns, guideDepth, type IndentUnit } from './state/indent.ts';
10
+ export { markedWhitespace, isWhitespaceMode, type WhitespaceMode } from './state/whitespace.ts';
11
+ export { Projection, liftChanges, lowerChanges, type SliceRange } from './expand/projection.ts';
12
+ export { InlineExpansions, type Expansion, type SourceDocument } from './expand/expansions.ts';
13
+ export { sameDocumentResolver, findDefinition, identifierAt, type Definition, type DefinitionResolver } from './expand/resolver.ts';
14
+ export { rankOptions, acceptOption, type CompletionSource, type CompletionContext, type CompletionItem, type CompletionResult, type Option as CompletionOption } from './complete/source.ts';
15
+ export { WordIndex, wordBefore, completeWords, wordSource } from './complete/words.ts';
16
+ export { searcher, Searcher, SearchIndex, type SearchQuery, type Match as SearchMatch } from './find/search.ts';
@@ -0,0 +1,19 @@
1
+ // `@nibling/editor/headless`: no DOM. Runs in Node, a worker, or tests.
2
+ export { Text } from "./text/text.js";
3
+ export { ChangeSet } from "./text/changeset.js";
4
+ export { Selection, cursor, rangeFrom, rangeTo, isCursor } from "./state/selection.js";
5
+ export { EditorCore } from "./state/core.js";
6
+ export { History } from "./state/history.js";
7
+ export * as commands from "./commands/commands.js";
8
+ export { Keymap, defaultKeymap } from "./commands/keymap.js";
9
+ export { detectIndentUnit, indentColumns, guideDepth } from "./state/indent.js";
10
+ export { markedWhitespace, isWhitespaceMode } from "./state/whitespace.js";
11
+ // Inline expansion (a prototype): projection, call-site tracking, and definition resolvers.
12
+ export { Projection, liftChanges, lowerChanges } from "./expand/projection.js";
13
+ export { InlineExpansions } from "./expand/expansions.js";
14
+ export { sameDocumentResolver, findDefinition, identifierAt } from "./expand/resolver.js";
15
+ // Completion: the source interface, ranking, and the document word index.
16
+ export { rankOptions, acceptOption } from "./complete/source.js";
17
+ export { WordIndex, wordBefore, completeWords, wordSource } from "./complete/words.js";
18
+ // Find and replace: the search behind the element's find bar, over any Text.
19
+ export { searcher, Searcher, SearchIndex } from "./find/search.js";
@@ -0,0 +1,6 @@
1
+ export { NibEditor, editorDefaults, defineEditor, type EditorChangeEvent } from './element.ts';
2
+ export type { Extension, EditorContext, UndoProvider } from './extension.ts';
3
+ export type { Overlay, OverlayKind, OverlaySource, LineClassSource, PresenceSlot, RemoteCursor, RemoteCursorSource } from './view/decorations.ts';
4
+ export { autocomplete, wordCompletions, type AutocompleteOptions } from './complete/autocomplete.ts';
5
+ export type { CompiledTheme, GrammarManifest, ThemePair } from './tokens/protocol.ts';
6
+ export * from './headless.ts';
package/dist/index.js ADDED
@@ -0,0 +1,6 @@
1
+ // `@nibling/editor`: the element. Importing it defines <nib-editor>.
2
+ import { defineEditor } from "./element.js";
3
+ export { NibEditor, editorDefaults, defineEditor } from "./element.js";
4
+ export { autocomplete, wordCompletions } from "./complete/autocomplete.js";
5
+ export * from "./headless.js";
6
+ defineEditor();
@@ -0,0 +1,89 @@
1
+ import type { Command } from './commands/commands.ts';
2
+ import type { KeyBinding } from './commands/keymap.ts';
3
+ import type { EditorCore } from './state/core.ts';
4
+ import { type FoldState } from './state/folds.ts';
5
+ import type { LineTokenSource } from './tokens/bridge.ts';
6
+ import type { BlockWidget, ViewBlocks } from './view/decorations.ts';
7
+ import type { Expansion, InlineExpansions, SourceDocument } from './expand/expansions.ts';
8
+ import type { DefinitionResolver } from './expand/resolver.ts';
9
+ /**
10
+ * How an inline expansion's editor differs from a top-level `<nib-editor>`. The element reads it
11
+ * once, at construction (see `createNestedEditor` in element.ts).
12
+ */
13
+ export interface NestedConfig {
14
+ /** The projection's inner core: the element edits it like its own document. */
15
+ readonly core: EditorCore;
16
+ /** Tokens for each connection, read from the source document's bridge. */
17
+ readonly tokens: (signal: AbortSignal) => LineTokenSource;
18
+ /** Undo and redo of the source document. The inner core has no History of its own. */
19
+ readonly undo: Command;
20
+ readonly redo: Command;
21
+ /** Bound above the defaults: Escape, and arrows at the edges. */
22
+ readonly keymap: readonly KeyBinding[];
23
+ /** Gutter numbers are the source's. */
24
+ readonly gutter: {
25
+ offset(): number;
26
+ digits(): number;
27
+ subscribe(invalidate: () => void): () => void;
28
+ };
29
+ readonly maxLines: number;
30
+ /** Expansions inside this one resolve in its source document. */
31
+ readonly resolver: DefinitionResolver<SourceDocument>;
32
+ }
33
+ /** What the widgets need from the element that hosts them. */
34
+ export interface ExpansionHost {
35
+ readonly core: EditorCore;
36
+ readonly expansions: InlineExpansions;
37
+ readonly folds: FoldState;
38
+ readonly root: ShadowRoot;
39
+ /** A resolver for definitions inside `doc`. */
40
+ resolverIn(doc: SourceDocument): DefinitionResolver<SourceDocument>;
41
+ /** Build a nested editor element for `config`, with the host's language and theme. */
42
+ createEditor(config: NestedConfig): HTMLElement;
43
+ /** Focus the host editor with a cursor at `pos`. */
44
+ focusAt(pos: number): void;
45
+ /** Gutter digits of the host view, so nested gutters line up with it. */
46
+ digits(): number;
47
+ }
48
+ /**
49
+ * The element side of inline expansion: one block widget per open expansion, each a frame holding
50
+ * a nested editor over the expansion's projection. The headless parts (Projection,
51
+ * InlineExpansions, the resolver, SliceTokens) know nothing of this.
52
+ *
53
+ * Lifetime: a widget's nested editor lives as long as its expansion, so its folds and selection
54
+ * survive scrolling. It is in the DOM only while the call site's line is mounted; unmounting
55
+ * disconnects it, which ends its view, its input surface, and its token pin, and reconnecting
56
+ * builds them again. Closing the expansion drops it.
57
+ *
58
+ * Focus and keys: each nested editor has its own textarea, keymap, and selection. The frame stops
59
+ * every surface event at its edge, so nothing typed or clicked inside reaches the host editor's
60
+ * listeners (its EditContext host, its rail, its mousedown) and the host's never reach inside.
61
+ */
62
+ export declare class ExpansionWidgets implements ViewBlocks {
63
+ #private;
64
+ constructor(host: ExpansionHost, signal: AbortSignal);
65
+ widgets(): readonly BlockWidget[];
66
+ subscribe(invalidate: () => void): () => void;
67
+ /**
68
+ * ArrowDown on the call site's line, or ArrowUp from the line below it: put the cursor in the
69
+ * nearest expansion at the same column. True when it moved.
70
+ */
71
+ enterFrom(line: number, dir: -1 | 1, column: number): boolean;
72
+ /** From inside expansion `key`: the next one on its line in `dir`, if any. */
73
+ sibling(key: string, dir: -1 | 1): ExpansionWidget | undefined;
74
+ }
75
+ /** One expansion's frame and nested editor. */
76
+ export declare class ExpansionWidget implements BlockWidget {
77
+ #private;
78
+ readonly key: string;
79
+ readonly editor: HTMLElement;
80
+ constructor(host: ExpansionHost, expansion: Expansion, all: ExpansionWidgets);
81
+ get line(): number;
82
+ estimate(lineHeight: number): number;
83
+ mount(host: HTMLElement, signal: AbortSignal): void;
84
+ /** Focus the nested editor at the first or last line, at `column` or its end. */
85
+ enter(edge: 'first' | 'last', column: number): void;
86
+ /** Close the expansion. With `refocus`, the host editor takes focus at the call site. */
87
+ collapse(refocus: boolean): void;
88
+ dispose(): void;
89
+ }