@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,585 @@
1
+ const IDENT_CHAR = /[\p{L}\p{N}_$]/u;
2
+ const IDENT_START = /[\p{L}_$]/u;
3
+ // Reserved words identifierAt refuses to treat as a usable identifier. Not exhaustive (there is
4
+ // no value in rejecting every contextual TS keyword), but covers the statement/operator keywords
5
+ // that are never legitimate "go to definition" targets.
6
+ const KEYWORDS = new Set([
7
+ 'break', 'case', 'catch', 'class', 'const', 'continue', 'debugger', 'default', 'delete', 'do',
8
+ 'else', 'export', 'extends', 'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof',
9
+ 'new', 'return', 'super', 'switch', 'this', 'throw', 'try', 'typeof', 'var', 'void', 'while',
10
+ 'with', 'yield', 'let', 'static', 'enum', 'await', 'async', 'implements', 'interface', 'package',
11
+ 'private', 'protected', 'public', 'abstract', 'as', 'from', 'of', 'namespace', 'declare', 'type',
12
+ 'readonly', 'get', 'set', 'module', 'is', 'keyof', 'infer', 'null', 'undefined', 'true', 'false',
13
+ ]);
14
+ /**
15
+ * The identifier touching `pos`, scanned within its line only (so this stays cheap on huge docs).
16
+ * `pos` may sit anywhere inside the token, including either edge. Rejects tokens that start with
17
+ * a digit (number literals) and JS/TS reserved words, since neither is a useful jump target.
18
+ */
19
+ export function identifierAt(text, pos) {
20
+ const line = text.lineAt(pos);
21
+ const s = text.slice(line.from, line.to);
22
+ const rel = pos - line.from;
23
+ let start = rel;
24
+ while (start > 0 && IDENT_CHAR.test(s[start - 1]))
25
+ start--;
26
+ let end = rel;
27
+ while (end < s.length && IDENT_CHAR.test(s[end]))
28
+ end++;
29
+ if (start === end)
30
+ return null;
31
+ if (!IDENT_START.test(s[start]))
32
+ return null;
33
+ const name = s.slice(start, end);
34
+ if (KEYWORDS.has(name))
35
+ return null;
36
+ return { from: line.from + start, to: line.from + end, name };
37
+ }
38
+ // --- low-level scanning helpers shared by all three definition kinds ---
39
+ function skipString(src, i, quote) {
40
+ let j = i + 1;
41
+ while (j < src.length) {
42
+ const c = src[j];
43
+ if (c === '\\') {
44
+ j += 2;
45
+ continue;
46
+ }
47
+ if (c === quote)
48
+ return j + 1;
49
+ if (c === '\n')
50
+ return j; // unterminated string on this line: bail without consuming the newline
51
+ j++;
52
+ }
53
+ return src.length;
54
+ }
55
+ // Template literals nest: `${ ... }` can contain braces, strings, and further templates, each of
56
+ // which must be matched before the outer backtick can close.
57
+ function skipTemplate(src, i) {
58
+ let j = i + 1;
59
+ while (j < src.length) {
60
+ const c = src[j];
61
+ if (c === '\\') {
62
+ j += 2;
63
+ continue;
64
+ }
65
+ if (c === '`')
66
+ return j + 1;
67
+ if (c === '$' && src[j + 1] === '{') {
68
+ const close = scanBalanced(src, j + 1, '{', '}');
69
+ if (close === null)
70
+ return src.length;
71
+ j = close + 1;
72
+ continue;
73
+ }
74
+ j++;
75
+ }
76
+ return src.length;
77
+ }
78
+ // Heuristic only (documented limitation): a '/' starts a regex when the nearest non-space char
79
+ // before it is one that can't end an expression (an operator, opener, or statement separator).
80
+ function isRegexStart(src, i) {
81
+ let j = i - 1;
82
+ while (j >= 0 && (src[j] === ' ' || src[j] === '\t'))
83
+ j--;
84
+ if (j < 0)
85
+ return true;
86
+ const c = src[j];
87
+ if (c === '\n')
88
+ return true;
89
+ return '(,=:[!&|?{};'.includes(c);
90
+ }
91
+ function skipRegex(src, i) {
92
+ let j = i + 1;
93
+ let inClass = false;
94
+ while (j < src.length) {
95
+ const c = src[j];
96
+ if (c === '\\') {
97
+ j += 2;
98
+ continue;
99
+ }
100
+ if (c === '\n')
101
+ return j; // unterminated: bail without consuming the newline
102
+ if (c === '[')
103
+ inClass = true;
104
+ else if (c === ']')
105
+ inClass = false;
106
+ else if (c === '/' && !inClass) {
107
+ j++;
108
+ break;
109
+ }
110
+ j++;
111
+ }
112
+ while (j < src.length && /[a-zA-Z]/.test(src[j]))
113
+ j++;
114
+ return j;
115
+ }
116
+ /**
117
+ * Finds the index of the `close` char matching the `open` char at `openIdx`, skipping over string
118
+ * and template literals, line/block comments, and (heuristically) regex literals, so that braces
119
+ * or parens inside those never throw off the depth count. Returns null when EOF is hit unbalanced.
120
+ */
121
+ function scanBalanced(src, openIdx, open, close) {
122
+ let depth = 1;
123
+ let i = openIdx + 1;
124
+ const n = src.length;
125
+ while (i < n) {
126
+ const c = src[i];
127
+ if (c === open) {
128
+ depth++;
129
+ i++;
130
+ continue;
131
+ }
132
+ if (c === close) {
133
+ depth--;
134
+ i++;
135
+ if (depth === 0)
136
+ return i - 1;
137
+ continue;
138
+ }
139
+ if (c === '"' || c === "'") {
140
+ i = skipString(src, i, c);
141
+ continue;
142
+ }
143
+ if (c === '`') {
144
+ i = skipTemplate(src, i);
145
+ continue;
146
+ }
147
+ if (c === '/' && src[i + 1] === '/') {
148
+ const nl = src.indexOf('\n', i);
149
+ i = nl < 0 ? n : nl;
150
+ continue;
151
+ }
152
+ if (c === '/' && src[i + 1] === '*') {
153
+ const end = src.indexOf('*/', i + 2);
154
+ i = end < 0 ? n : end + 2;
155
+ continue;
156
+ }
157
+ if (c === '/' && isRegexStart(src, i)) {
158
+ i = skipRegex(src, i);
159
+ continue;
160
+ }
161
+ i++;
162
+ }
163
+ return null;
164
+ }
165
+ function skipTrivia(src, i) {
166
+ while (i < src.length) {
167
+ const c = src[i];
168
+ if (c === ' ' || c === '\t' || c === '\n' || c === '\r') {
169
+ i++;
170
+ continue;
171
+ }
172
+ if (c === '/' && src[i + 1] === '/') {
173
+ const nl = src.indexOf('\n', i);
174
+ i = nl < 0 ? src.length : nl;
175
+ continue;
176
+ }
177
+ if (c === '/' && src[i + 1] === '*') {
178
+ const end = src.indexOf('*/', i + 2);
179
+ i = end < 0 ? src.length : end + 2;
180
+ continue;
181
+ }
182
+ break;
183
+ }
184
+ return i;
185
+ }
186
+ // `<T>` between a name and its parameter list. Not a full type-expression parser: just balances
187
+ // angle brackets while hopping over strings/templates, which is enough for generic parameter lists.
188
+ function skipGenerics(src, i) {
189
+ i = skipTrivia(src, i);
190
+ if (src[i] !== '<')
191
+ return i;
192
+ let depth = 0;
193
+ while (i < src.length) {
194
+ const c = src[i];
195
+ if (c === '<') {
196
+ depth++;
197
+ i++;
198
+ continue;
199
+ }
200
+ if (c === '>') {
201
+ depth--;
202
+ i++;
203
+ if (depth === 0)
204
+ break;
205
+ continue;
206
+ }
207
+ if (c === '"' || c === "'") {
208
+ i = skipString(src, i, c);
209
+ continue;
210
+ }
211
+ if (c === '`') {
212
+ i = skipTemplate(src, i);
213
+ continue;
214
+ }
215
+ i++;
216
+ }
217
+ return i;
218
+ }
219
+ /**
220
+ * Scans a type expression starting at `i`, tracking `(`, `[`, `{`, `<`/`>` depth (and hopping over
221
+ * strings/templates/comments) until `isStop` is true at depth 0. Used to find where a type
222
+ * annotation ends — e.g. the `{` of a function body, or the `=` of a variable initializer —
223
+ * without needing a real type-syntax parser.
224
+ */
225
+ function scanTypeExpr(src, i, isStop) {
226
+ let depth = 0;
227
+ while (i < src.length) {
228
+ if (depth === 0 && isStop(i))
229
+ return i;
230
+ const c = src[i];
231
+ if (c === '(' || c === '[' || c === '{' || c === '<') {
232
+ depth++;
233
+ i++;
234
+ continue;
235
+ }
236
+ if (c === ')' || c === ']' || c === '}') {
237
+ depth = Math.max(0, depth - 1);
238
+ i++;
239
+ continue;
240
+ }
241
+ if (c === '>') {
242
+ if (depth > 0)
243
+ depth--;
244
+ i++;
245
+ continue;
246
+ }
247
+ if (c === '"' || c === "'") {
248
+ i = skipString(src, i, c);
249
+ continue;
250
+ }
251
+ if (c === '`') {
252
+ i = skipTemplate(src, i);
253
+ continue;
254
+ }
255
+ if (c === '/' && src[i + 1] === '/') {
256
+ const nl = src.indexOf('\n', i);
257
+ i = nl < 0 ? src.length : nl;
258
+ continue;
259
+ }
260
+ if (c === '/' && src[i + 1] === '*') {
261
+ const end = src.indexOf('*/', i + 2);
262
+ i = end < 0 ? src.length : end + 2;
263
+ continue;
264
+ }
265
+ i++;
266
+ }
267
+ return null;
268
+ }
269
+ function atStop(src, i, stopAt) {
270
+ for (const tok of stopAt)
271
+ if (src.slice(i, i + tok.length) === tok)
272
+ return true;
273
+ return false;
274
+ }
275
+ // After a function/arrow's `)`, either a `{` body, a `: Type` then `{`/`;`, or (for arrows) a
276
+ // `: Type` then `=>`. `stopAt` names the tokens (one or two chars) that end the type, besides EOF.
277
+ function scanTypeAndFindBoundary(src, i, stopAt) {
278
+ i = skipTrivia(src, i);
279
+ if (atStop(src, i, stopAt))
280
+ return i;
281
+ if (src[i] !== ':')
282
+ return null;
283
+ return scanTypeExpr(src, i + 1, (p) => atStop(src, p, stopAt));
284
+ }
285
+ function nameTokenRegex(name) {
286
+ const escaped = name.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
287
+ return new RegExp(`(?<![\\p{L}\\p{N}_$])${escaped}(?![\\p{L}\\p{N}_$])`, 'gu');
288
+ }
289
+ // Sentinel distinguishing "found the real header but its body never closes" (abort the whole
290
+ // search — there is no other candidate to fall back to) from "this occurrence isn't a match"
291
+ // (keep scanning for the next occurrence of the name).
292
+ const UNTERMINATED = Symbol('unterminated');
293
+ function buildSpan(text, src, headerFromRaw, toIdx, name, nameIdx, kind) {
294
+ const fromLine = text.lineAt(headerFromRaw);
295
+ const toLine = text.lineAt(toIdx);
296
+ const from = extendForJsDoc(text, src, fromLine);
297
+ return { from, to: toLine.to, name, kind, nameFrom: nameIdx };
298
+ }
299
+ // If a `/** ... */` block ends on the line directly above the header (no blank line between),
300
+ // the definition's range grows to start at that comment's first line.
301
+ function extendForJsDoc(text, src, headerLine) {
302
+ if (headerLine.number <= 1)
303
+ return headerLine.from;
304
+ const prevLine = text.line(headerLine.number - 1);
305
+ const prevText = src.slice(prevLine.from, prevLine.to);
306
+ const trimmed = prevText.trimEnd();
307
+ if (!trimmed.endsWith('*/'))
308
+ return headerLine.from;
309
+ const closeIdx = prevLine.from + prevText.lastIndexOf('*/');
310
+ const startIdx = src.lastIndexOf('/**', closeIdx);
311
+ if (startIdx < 0)
312
+ return headerLine.from;
313
+ // Make sure `closeIdx` is actually the close for this `/**` (no earlier unrelated `*/` between).
314
+ const firstClose = src.indexOf('*/', startIdx + 3);
315
+ if (firstClose !== closeIdx)
316
+ return headerLine.from;
317
+ return text.lineAt(startIdx).from;
318
+ }
319
+ function peelModifiers(src, pos) {
320
+ const mods = ['export', 'default', 'declare', 'async'];
321
+ let changed = true;
322
+ while (changed) {
323
+ changed = false;
324
+ for (const m of mods) {
325
+ const start = pos - m.length;
326
+ if (start >= 0 && src.slice(start, pos) === m && (start === 0 || !IDENT_CHAR.test(src[start - 1]))) {
327
+ pos = start;
328
+ while (pos > 0 && /\s/.test(src[pos - 1]))
329
+ pos--;
330
+ changed = true;
331
+ break;
332
+ }
333
+ }
334
+ }
335
+ return pos;
336
+ }
337
+ // `function` / `function*` right before `name`, with optional `export`/`export default`/`declare`/
338
+ // `async` modifiers in front of it (any order, matching real-world TS). Returns the offset of the
339
+ // first modifier token (or of `function` itself) for line alignment.
340
+ function matchFunctionHeader(src, nameIdx) {
341
+ let j = nameIdx;
342
+ while (j > 0 && /\s/.test(src[j - 1]))
343
+ j--;
344
+ if (src[j - 1] === '*') {
345
+ j--;
346
+ while (j > 0 && /\s/.test(src[j - 1]))
347
+ j--;
348
+ }
349
+ const kw = 'function';
350
+ if (src.slice(j - kw.length, j) !== kw)
351
+ return null;
352
+ const k = j - kw.length;
353
+ if (k > 0 && IDENT_CHAR.test(src[k - 1]))
354
+ return null;
355
+ return peelModifiers(src, k);
356
+ }
357
+ function tryFunction(src, text, name) {
358
+ const re = nameTokenRegex(name);
359
+ let m;
360
+ while ((m = re.exec(src))) {
361
+ const nameIdx = m.index;
362
+ const headerFrom = matchFunctionHeader(src, nameIdx);
363
+ if (headerFrom === null)
364
+ continue;
365
+ let i = skipGenerics(src, nameIdx + name.length);
366
+ i = skipTrivia(src, i);
367
+ if (src[i] !== '(')
368
+ continue;
369
+ const closeParen = scanBalanced(src, i, '(', ')');
370
+ if (closeParen === null)
371
+ return UNTERMINATED;
372
+ const boundary = scanTypeAndFindBoundary(src, closeParen + 1, ['{', ';']);
373
+ if (boundary === null)
374
+ continue;
375
+ let toIdx;
376
+ if (src[boundary] === ';') {
377
+ toIdx = boundary;
378
+ }
379
+ else {
380
+ const closeBrace = scanBalanced(src, boundary, '{', '}');
381
+ if (closeBrace === null)
382
+ return UNTERMINATED;
383
+ toIdx = closeBrace;
384
+ }
385
+ return buildSpan(text, src, headerFrom, toIdx, name, nameIdx, 'function');
386
+ }
387
+ return null;
388
+ }
389
+ const MOD_RE = /^(?:\s*(?:public|private|protected|static|async|override|readonly|get|set)\b|\s*\*)*\s*$/;
390
+ function tryMethod(src, text, name) {
391
+ const re = nameTokenRegex(name);
392
+ let m;
393
+ while ((m = re.exec(src))) {
394
+ const nameIdx = m.index;
395
+ const lineStart = src.lastIndexOf('\n', nameIdx - 1) + 1;
396
+ const prefix = src.slice(lineStart, nameIdx);
397
+ if (!MOD_RE.test(prefix))
398
+ continue;
399
+ let i = skipGenerics(src, nameIdx + name.length);
400
+ i = skipTrivia(src, i);
401
+ if (src[i] !== '(')
402
+ continue;
403
+ const closeParen = scanBalanced(src, i, '(', ')');
404
+ if (closeParen === null)
405
+ return UNTERMINATED;
406
+ const boundary = scanTypeAndFindBoundary(src, closeParen + 1, ['{', ';']);
407
+ if (boundary === null)
408
+ continue;
409
+ if (src[boundary] !== '{')
410
+ continue; // call site or bodyless signature, not a method definition
411
+ const closeBrace = scanBalanced(src, boundary, '{', '}');
412
+ if (closeBrace === null)
413
+ return UNTERMINATED;
414
+ return buildSpan(text, src, lineStart, closeBrace, name, nameIdx, 'method');
415
+ }
416
+ return null;
417
+ }
418
+ const IDENT_TOKEN_RE = /^[\p{L}_$][\p{L}\p{N}_$]*/u;
419
+ // Expression-bodied arrow: ends at the first top-level `;`, or (if none) at the end of the current
420
+ // line once brackets are balanced — matching how `const x = a + b` reads without a semicolon.
421
+ function scanExpressionBody(src, i) {
422
+ let depth = 0;
423
+ let j = i;
424
+ while (j < src.length) {
425
+ const c = src[j];
426
+ if (c === '(' || c === '[' || c === '{') {
427
+ depth++;
428
+ j++;
429
+ continue;
430
+ }
431
+ if (c === ')' || c === ']' || c === '}') {
432
+ depth--;
433
+ if (depth < 0)
434
+ return null;
435
+ j++;
436
+ continue;
437
+ }
438
+ if (c === '"' || c === "'") {
439
+ j = skipString(src, j, c);
440
+ continue;
441
+ }
442
+ if (c === '`') {
443
+ j = skipTemplate(src, j);
444
+ continue;
445
+ }
446
+ if (c === '/' && src[j + 1] === '/') {
447
+ const nl = src.indexOf('\n', j);
448
+ j = nl < 0 ? src.length : nl;
449
+ continue;
450
+ }
451
+ if (c === '/' && src[j + 1] === '*') {
452
+ const end = src.indexOf('*/', j + 2);
453
+ j = end < 0 ? src.length : end + 2;
454
+ continue;
455
+ }
456
+ if (depth === 0 && c === ';')
457
+ return j;
458
+ if (depth === 0 && c === '\n')
459
+ return j;
460
+ j++;
461
+ }
462
+ return depth === 0 ? j : null;
463
+ }
464
+ function matchArrowDeclKeyword(src, nameIdx) {
465
+ let j = nameIdx;
466
+ while (j > 0 && /\s/.test(src[j - 1]))
467
+ j--;
468
+ let kw = null;
469
+ for (const cand of ['const', 'let', 'var']) {
470
+ if (src.slice(j - cand.length, j) === cand) {
471
+ kw = cand;
472
+ break;
473
+ }
474
+ }
475
+ if (kw === null)
476
+ return null;
477
+ const k = j - kw.length;
478
+ if (k > 0 && IDENT_CHAR.test(src[k - 1]))
479
+ return null;
480
+ return peelModifiers(src, k);
481
+ }
482
+ function tryArrow(src, text, name) {
483
+ const re = nameTokenRegex(name);
484
+ let m;
485
+ while ((m = re.exec(src))) {
486
+ const nameIdx = m.index;
487
+ const headerFrom = matchArrowDeclKeyword(src, nameIdx);
488
+ if (headerFrom === null)
489
+ continue;
490
+ let i = skipTrivia(src, nameIdx + name.length);
491
+ if (src[i] === ':') {
492
+ const r = scanTypeExpr(src, i + 1, (p) => src[p] === '=');
493
+ if (r === null)
494
+ continue;
495
+ i = r;
496
+ }
497
+ i = skipTrivia(src, i);
498
+ if (src[i] !== '=')
499
+ continue;
500
+ i = skipTrivia(src, i + 1);
501
+ if (src.slice(i, i + 5) === 'async' && !IDENT_CHAR.test(src[i + 5] ?? ' ')) {
502
+ i = skipTrivia(src, i + 5);
503
+ }
504
+ i = skipGenerics(src, i);
505
+ i = skipTrivia(src, i);
506
+ let afterParams;
507
+ if (src[i] === '(') {
508
+ const closeParen = scanBalanced(src, i, '(', ')');
509
+ if (closeParen === null)
510
+ return UNTERMINATED;
511
+ afterParams = closeParen + 1;
512
+ }
513
+ else {
514
+ const idm = IDENT_TOKEN_RE.exec(src.slice(i));
515
+ if (!idm)
516
+ continue;
517
+ afterParams = i + idm[0].length;
518
+ }
519
+ const boundary = scanTypeAndFindBoundary(src, afterParams, ['=>']);
520
+ if (boundary === null)
521
+ continue;
522
+ let j = boundary;
523
+ if (src[j] !== '=' || src[j + 1] !== '>')
524
+ continue;
525
+ j = skipTrivia(src, j + 2);
526
+ let toIdx;
527
+ if (src[j] === '{') {
528
+ const close = scanBalanced(src, j, '{', '}');
529
+ if (close === null)
530
+ return UNTERMINATED;
531
+ toIdx = close;
532
+ }
533
+ else {
534
+ const end = scanExpressionBody(src, j);
535
+ if (end === null)
536
+ return UNTERMINATED;
537
+ toIdx = end;
538
+ }
539
+ return buildSpan(text, src, headerFrom, toIdx, name, nameIdx, 'arrow');
540
+ }
541
+ return null;
542
+ }
543
+ /**
544
+ * Finds where `name` is defined in `text`, preferring (in order) a `function` declaration, a
545
+ * class/object method, then a `const`/`let`/`var` arrow — and within a kind, the first in the
546
+ * document. Scans `text.toString()` once; a hand-rolled bracket scanner (not per-line arrays)
547
+ * keeps this cheap even on very large documents.
548
+ */
549
+ export function findDefinition(text, name) {
550
+ if (!name)
551
+ return null;
552
+ const src = text.toString();
553
+ const byFn = tryFunction(src, text, name);
554
+ if (byFn === UNTERMINATED)
555
+ return null;
556
+ if (byFn)
557
+ return byFn;
558
+ const byMethod = tryMethod(src, text, name);
559
+ if (byMethod === UNTERMINATED)
560
+ return null;
561
+ if (byMethod)
562
+ return byMethod;
563
+ const byArrow = tryArrow(src, text, name);
564
+ if (byArrow === UNTERMINATED)
565
+ return null;
566
+ return byArrow;
567
+ }
568
+ /**
569
+ * Resolves `pos` to a definition of the identifier under it within the same document. Returns
570
+ * null when there's no identifier, no definition, or the caret is already on the definition's own
571
+ * name (nothing useful to jump to).
572
+ */
573
+ export function sameDocumentResolver(doc) {
574
+ return (state, pos) => {
575
+ const id = identifierAt(state.text, pos);
576
+ if (!id)
577
+ return null;
578
+ const def = findDefinition(state.text, id.name);
579
+ if (!def)
580
+ return null;
581
+ if (pos >= def.nameFrom && pos <= def.nameFrom + def.name.length)
582
+ return null;
583
+ return { doc, from: def.from, to: def.to, name: def.name };
584
+ };
585
+ }
@@ -0,0 +1,25 @@
1
+ import type { LineTokenSource, TokenChange } from '../tokens/bridge.ts';
2
+ import type { LineTokens, StyleDef } from '../tokens/protocol.ts';
3
+ import type { Projection } from './projection.ts';
4
+ /** A source document's tokens as an expansion needs them: a bridge it can pin a range of. */
5
+ export interface PinnableTokens extends LineTokenSource {
6
+ pin(owner: object, from: number, to: number): void;
7
+ readonly styles: readonly StyleDef[];
8
+ }
9
+ /**
10
+ * An inline expansion's tokens, read from its source's bridge. The slice is whole lines, so inner
11
+ * line n is source line `first + n - 1` with the same text, and its tokens are the source's own,
12
+ * computed with the real context above the definition (a method's class body, say). No second
13
+ * worker, and no colors that differ between the file and its expansion.
14
+ *
15
+ * `setViewport` pins the inner view's mounted lines in the source bridge, so the worker sends them
16
+ * wherever the source view is scrolled. The pin lives as long as `signal`, which is the inner
17
+ * view's: an expansion scrolled out of the outer view unmounts and costs the worker nothing.
18
+ */
19
+ export declare class SliceTokens implements LineTokenSource {
20
+ #private;
21
+ constructor(source: PinnableTokens, projection: Projection, signal: AbortSignal);
22
+ lineTokens(line: number): LineTokens | undefined;
23
+ setViewport(from: number, to: number): void;
24
+ onChange(listener: (c: TokenChange) => void): () => void;
25
+ }
@@ -0,0 +1,79 @@
1
+ /**
2
+ * An inline expansion's tokens, read from its source's bridge. The slice is whole lines, so inner
3
+ * line n is source line `first + n - 1` with the same text, and its tokens are the source's own,
4
+ * computed with the real context above the definition (a method's class body, say). No second
5
+ * worker, and no colors that differ between the file and its expansion.
6
+ *
7
+ * `setViewport` pins the inner view's mounted lines in the source bridge, so the worker sends them
8
+ * wherever the source view is scrolled. The pin lives as long as `signal`, which is the inner
9
+ * view's: an expansion scrolled out of the outer view unmounts and costs the worker nothing.
10
+ */
11
+ export class SliceTokens {
12
+ #source;
13
+ #projection;
14
+ #first;
15
+ #mounted = { from: 1, to: 1 };
16
+ #listeners = [];
17
+ constructor(source, projection, signal) {
18
+ this.#source = source;
19
+ this.#projection = projection;
20
+ this.#first = projection.firstLine;
21
+ const offSource = source.onChange((c) => this.#onSource(c));
22
+ const offMoved = projection.subscribe((reason) => {
23
+ if (reason === 'detached')
24
+ return source.pin(this, 0, 0);
25
+ const first = projection.firstLine;
26
+ if (first === this.#first)
27
+ return;
28
+ this.#first = first;
29
+ this.#pin();
30
+ // Rows moved with their lines in the bridge, so the same lines still have the same rows;
31
+ // re-check them all, which renders nothing that did not change.
32
+ this.#emit({ lines: this.#allLines(), styles: [], reset: false });
33
+ });
34
+ signal.addEventListener('abort', () => {
35
+ offSource();
36
+ offMoved();
37
+ source.pin(this, 0, 0);
38
+ }, { once: true });
39
+ }
40
+ lineTokens(line) {
41
+ return this.#source.lineTokens(this.#first + line - 1);
42
+ }
43
+ setViewport(from, to) {
44
+ this.#mounted = { from, to };
45
+ this.#pin();
46
+ }
47
+ onChange(listener) {
48
+ this.#listeners.push(listener);
49
+ // A view built after the source's first batch has no styles yet: give it all of them.
50
+ queueMicrotask(() => {
51
+ if (this.#listeners.includes(listener))
52
+ listener({ lines: this.#allLines(), styles: this.#source.styles, reset: true });
53
+ });
54
+ return () => (this.#listeners = this.#listeners.filter((l) => l !== listener));
55
+ }
56
+ #pin() {
57
+ if (this.#projection.detached)
58
+ return;
59
+ this.#source.pin(this, this.#first + this.#mounted.from - 1, this.#first + this.#mounted.to - 1);
60
+ }
61
+ #allLines() {
62
+ return Array.from({ length: this.#projection.inner.state.text.lines }, (_, i) => i + 1);
63
+ }
64
+ #onSource(c) {
65
+ const first = this.#first;
66
+ const last = first + this.#projection.inner.state.text.lines;
67
+ const lines = [];
68
+ for (const n of c.lines)
69
+ if (n >= first && n < last)
70
+ lines.push(n - first + 1);
71
+ if (lines.length === 0 && c.styles.length === 0 && !c.reset)
72
+ return;
73
+ this.#emit({ lines, styles: c.styles, reset: c.reset });
74
+ }
75
+ #emit(c) {
76
+ for (const l of this.#listeners)
77
+ l(c);
78
+ }
79
+ }