@tsrx/content-mapper 0.1.0

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.
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Constants of the TypeScript 7 content-mapper protocol, kept in sync with
3
+ * `tsc/internal/contentmapper` and `tsc/internal/spanmap` in microsoft/TypeScript.
4
+ */
5
+
6
+ /** Span kinds. */
7
+ export const SpanMapKind = /** @type {const} */ ({
8
+ /** Same length and identical text; the only kind that supports text edits. */
9
+ Verbatim: 0,
10
+ /** Same entity, different text or length. */
11
+ Atom: 1,
12
+ /** Different text; diagnostics covering the whole span exactly show the original text. */
13
+ Alias: 2,
14
+ });
15
+
16
+ /** Feature bits a span opts into. Diagnostics ignore them. */
17
+ export const SpanMapFeature = /** @type {const} */ ({
18
+ Hover: 1 << 0,
19
+ SignatureHelp: 1 << 1,
20
+ Completion: 1 << 2,
21
+ Definition: 1 << 3,
22
+ TypeDefinition: 1 << 4,
23
+ Implementation: 1 << 5,
24
+ References: 1 << 6,
25
+ DocumentHighlights: 1 << 7,
26
+ Rename: 1 << 8,
27
+ CallHierarchy: 1 << 9,
28
+ CodeActions: 1 << 10,
29
+ Formatting: 1 << 11,
30
+ InlayHints: 1 << 12,
31
+ SemanticTokens: 1 << 13,
32
+ FoldingRanges: 1 << 14,
33
+ SelectionRanges: 1 << 15,
34
+ LinkedEditing: 1 << 16,
35
+ AutoInsert: 1 << 17,
36
+ DocumentSymbols: 1 << 18,
37
+ CodeLens: 1 << 19,
38
+ None: 0,
39
+ All: (1 << 20) - 1,
40
+ });
41
+
42
+ /** Diagnostic-directive policies (unused by TSRX today; kept for completeness). */
43
+ export const DiagnosticDirectivePolicy = /** @type {const} */ ({ Ignore: 0, Expect: 1 });
44
+
45
+ /** Prefix for mapper-authored diagnostics: `error tsrx(1000): ...`. */
46
+ export const DIAGNOSTIC_SOURCE = 'tsrx';
47
+
48
+ /** Numeric code for a fatal compile error (the file could not be transformed). */
49
+ export const DIAGNOSTIC_CODE_COMPILE_ERROR = 1000;
50
+ /** Numeric code for a usage error that carries no string code. */
51
+ export const DIAGNOSTIC_CODE_USAGE_ERROR = 1001;
52
+ /** Numeric code when no TSRX compiler can be resolved for a file. */
53
+ export const DIAGNOSTIC_CODE_NO_COMPILER = 1002;
54
+ /** Numeric code when the project's TSRX configuration is invalid. */
55
+ export const DIAGNOSTIC_CODE_INVALID_CONFIG = 1003;
56
+
57
+ /**
58
+ * @typedef {[
59
+ * generatedStart: number,
60
+ * generatedLength: number,
61
+ * originalStart: number,
62
+ * originalLength: number,
63
+ * kind: 0 | 1 | 2,
64
+ * features: number,
65
+ * ]} SpanMapping
66
+ */
67
+
68
+ /**
69
+ * @typedef {object} MapperDiagnostic
70
+ * @property {number} start Offset into the original text (UTF-16 code units).
71
+ * @property {number} length
72
+ * @property {number} code
73
+ * @property {string} messageText
74
+ */
75
+
76
+ /**
77
+ * @typedef {object} MappedOutput
78
+ * @property {string} text
79
+ * @property {'.js' | '.jsx' | '.mjs' | '.cjs' | '.ts' | '.tsx' | '.mts' | '.cts' | '.json'} extension
80
+ * @property {SpanMapping[]} mappings
81
+ */
82
+
83
+ /**
84
+ * @typedef {MappedOutput & {
85
+ * diagnostics?: MapperDiagnostic[],
86
+ * supplemental?: MappedOutput[],
87
+ * }} TransformResult
88
+ */
89
+
90
+ /**
91
+ * @typedef {object} InitializeParams
92
+ * @property {string} [locale]
93
+ * @property {Array<'utf-8' | 'utf-16'>} positionEncodings
94
+ */
95
+
96
+ /**
97
+ * @typedef {object} InitializeResult
98
+ * @property {'utf-8' | 'utf-16'} positionEncoding
99
+ * @property {string} diagnosticSource
100
+ */
101
+
102
+ /**
103
+ * @typedef {object} OpenProjectParams
104
+ * @property {string} configFileName Empty for inferred projects.
105
+ * @property {string} projectHandle
106
+ * @property {Record<string, unknown>} [options] The mapper entry's `options` object.
107
+ * @property {Record<string, unknown>} compilerOptions Effective compiler options (wire format).
108
+ */
109
+
110
+ /**
111
+ * @typedef {object} OpenProjectResult
112
+ * @property {string} configIdentity
113
+ * @property {string[]} [watchedFiles]
114
+ * @property {Array<{ path: Array<string | number>, messageText: string, code: number }>} [optionDiagnostics]
115
+ */
116
+
117
+ /**
118
+ * @typedef {object} TransformParams
119
+ * @property {string} fileName
120
+ * @property {string} content
121
+ * @property {string} [projectHandle]
122
+ */
package/src/rpc.js ADDED
@@ -0,0 +1,126 @@
1
+ /**
2
+ * Content-mapper transport: `Content-Length`-framed JSON-RPC 2.0 over stdio.
3
+ * TypeScript is the only requester; the mapper never sends requests or
4
+ * notifications. stdout is reserved for protocol messages, so everything
5
+ * else (including `console.log`) goes to stderr.
6
+ *
7
+ * Kept separate from the executable entry (`server.js`) so hosts that bundle
8
+ * the mapper (the VS Code extension ships it for inferred projects) can start
9
+ * it from their own entry file.
10
+ */
11
+
12
+ /**
13
+ * @typedef {{ jsonrpc: '2.0', id?: number | string | null, method: string, params?: unknown }} RequestMessage
14
+ */
15
+
16
+ /**
17
+ * Run the mapper server on the given streams. Installs the stream handlers
18
+ * and returns; the process exits (with `process.exitCode`) once the input
19
+ * stream ends, which is how TypeScript shuts a mapper down.
20
+ * @param {import('./mapper.js').ContentMapper} mapper
21
+ * @param {{ input?: NodeJS.ReadableStream, output?: NodeJS.WritableStream }} [streams]
22
+ */
23
+ export function run_mapper_server(mapper, streams = {}) {
24
+ const input = streams.input ?? process.stdin;
25
+ const output = streams.output ?? process.stdout;
26
+ let buffered = Buffer.alloc(0);
27
+
28
+ input.on('data', (chunk) => {
29
+ buffered = Buffer.concat([buffered, typeof chunk === 'string' ? Buffer.from(chunk) : chunk]);
30
+ read_messages();
31
+ });
32
+ input.on('error', (error) => {
33
+ console.error(error);
34
+ process.exitCode = 1;
35
+ });
36
+ input.on('end', () => {
37
+ process.exit(process.exitCode ?? 0);
38
+ });
39
+
40
+ function read_messages() {
41
+ while (true) {
42
+ const header_end = buffered.indexOf('\r\n\r\n');
43
+ if (header_end < 0) return;
44
+ const header = buffered.subarray(0, header_end).toString('ascii');
45
+ const length_match = /^Content-Length:\s*(\d+)\s*$/im.exec(header);
46
+ if (!length_match) {
47
+ console.error('@tsrx/content-mapper: missing Content-Length header');
48
+ process.exit(1);
49
+ }
50
+ const content_length = Number(length_match[1]);
51
+ const message_start = header_end + 4;
52
+ const message_end = message_start + content_length;
53
+ if (buffered.length < message_end) return;
54
+ const body = buffered.subarray(message_start, message_end).toString('utf8');
55
+ buffered = buffered.subarray(message_end);
56
+ /** @type {RequestMessage} */
57
+ let message;
58
+ try {
59
+ message = JSON.parse(body);
60
+ } catch (error) {
61
+ console.error('@tsrx/content-mapper: invalid JSON-RPC message', error);
62
+ continue;
63
+ }
64
+ handle_message(message);
65
+ }
66
+ }
67
+
68
+ /** @param {RequestMessage} message */
69
+ function handle_message(message) {
70
+ if (message.id === undefined || message.id === null) {
71
+ // Notifications need no response.
72
+ return;
73
+ }
74
+ try {
75
+ /** @type {unknown} */
76
+ let result;
77
+ switch (message.method) {
78
+ case 'initialize':
79
+ result = mapper.initialize(/** @type {any} */ (message.params));
80
+ break;
81
+ case 'openProject':
82
+ result = mapper.openProject(/** @type {any} */ (message.params));
83
+ break;
84
+ case 'closeProject':
85
+ mapper.closeProject(/** @type {any} */ (message.params));
86
+ result = null;
87
+ break;
88
+ case 'transform':
89
+ result = mapper.transform(/** @type {any} */ (message.params));
90
+ break;
91
+ default:
92
+ write({
93
+ jsonrpc: '2.0',
94
+ id: message.id,
95
+ error: { code: -32601, message: `Unknown method ${message.method}` },
96
+ });
97
+ return;
98
+ }
99
+ write({ jsonrpc: '2.0', id: message.id, result });
100
+ } catch (error) {
101
+ write({
102
+ jsonrpc: '2.0',
103
+ id: message.id,
104
+ error: {
105
+ code: -32603,
106
+ message: error instanceof Error ? (error.stack ?? error.message) : String(error),
107
+ },
108
+ });
109
+ }
110
+ }
111
+
112
+ /** @param {unknown} message */
113
+ function write(message) {
114
+ const body = Buffer.from(JSON.stringify(message), 'utf8');
115
+ output.write(`Content-Length: ${body.length}\r\n\r\n`);
116
+ output.write(body);
117
+ }
118
+ }
119
+
120
+ /**
121
+ * Route every console method to stderr so stray logging (from a target
122
+ * compiler, for example) can never corrupt the protocol stream on stdout.
123
+ */
124
+ export function redirect_console_to_stderr() {
125
+ console.log = console.info = console.warn = console.debug = (...args) => console.error(...args);
126
+ }
package/src/server.js ADDED
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Executable entry of the content mapper: TypeScript spawns it through the
4
+ * `typescript.contentMapper.exec` manifest entry and talks JSON-RPC over stdio.
5
+ */
6
+
7
+ import { create_tsrx_content_mapper } from './mapper.js';
8
+ import { redirect_console_to_stderr, run_mapper_server } from './rpc.js';
9
+
10
+ redirect_console_to_stderr();
11
+ run_mapper_server(create_tsrx_content_mapper());
@@ -0,0 +1,370 @@
1
+ /** @import {CodeMapping} from '@tsrx/core/types' */
2
+ /** @import {SpanMapping} from './protocol.js' */
3
+
4
+ import { SpanMapFeature, SpanMapKind } from './protocol.js';
5
+
6
+ const semantic_features =
7
+ SpanMapFeature.Hover |
8
+ SpanMapFeature.SignatureHelp |
9
+ SpanMapFeature.InlayHints |
10
+ SpanMapFeature.SemanticTokens;
11
+ const completion_features = SpanMapFeature.Completion | SpanMapFeature.AutoInsert;
12
+ const navigation_features =
13
+ SpanMapFeature.Definition |
14
+ SpanMapFeature.TypeDefinition |
15
+ SpanMapFeature.Implementation |
16
+ SpanMapFeature.References |
17
+ SpanMapFeature.DocumentHighlights |
18
+ SpanMapFeature.Rename |
19
+ SpanMapFeature.CallHierarchy |
20
+ SpanMapFeature.CodeActions |
21
+ SpanMapFeature.LinkedEditing;
22
+ const structure_features =
23
+ SpanMapFeature.FoldingRanges |
24
+ SpanMapFeature.SelectionRanges |
25
+ SpanMapFeature.DocumentSymbols |
26
+ SpanMapFeature.CodeLens;
27
+
28
+ const identifier_pattern = /^[\p{ID_Start}_$][\p{ID_Continue}$‌‍]*$/u;
29
+
30
+ /**
31
+ * @typedef {object} Candidate
32
+ * @property {number} generatedStart
33
+ * @property {number} generatedEnd
34
+ * @property {number} originalStart
35
+ * @property {number} originalEnd
36
+ * @property {0 | 1 | 2} kind
37
+ * @property {number} features
38
+ */
39
+
40
+ /**
41
+ * Translate a Volar `CodeMapping['data']` into content-mapper feature bits.
42
+ *
43
+ * `verification` has no bit: diagnostics always apply. Spans that carry CSS
44
+ * class hover/definition metadata (`customData.hover` / `customData.definition`)
45
+ * leave Hover and Definition off so the TSRX language server owns those
46
+ * features. `semantic.shouldHighlight()` returning `false` (string-literal
47
+ * spans) drops SemanticTokens.
48
+ * @param {CodeMapping['data'] | undefined} data
49
+ * @returns {number}
50
+ */
51
+ export function features_from_mapping_data(data) {
52
+ if (!data || typeof data !== 'object') return SpanMapFeature.None;
53
+ let features = SpanMapFeature.None;
54
+ if (data.semantic) {
55
+ features |= semantic_features;
56
+ if (typeof data.semantic === 'object' && data.semantic.shouldHighlight?.() === false) {
57
+ features &= ~SpanMapFeature.SemanticTokens;
58
+ }
59
+ }
60
+ if (data.completion) {
61
+ features |= completion_features;
62
+ }
63
+ if (data.navigation) {
64
+ features |= navigation_features;
65
+ if (typeof data.navigation === 'object') {
66
+ if (data.navigation.shouldHighlight?.() === false) {
67
+ features &= ~SpanMapFeature.DocumentHighlights;
68
+ }
69
+ if (data.navigation.shouldRename?.() === false) {
70
+ features &= ~SpanMapFeature.Rename;
71
+ }
72
+ }
73
+ }
74
+ if (data.structure) {
75
+ features |= structure_features;
76
+ }
77
+ if (data.format) {
78
+ features |= SpanMapFeature.Formatting;
79
+ }
80
+ const custom = data.customData;
81
+ if (custom && (custom.hover !== undefined || custom.definition !== undefined)) {
82
+ features &= ~(SpanMapFeature.Hover | SpanMapFeature.Definition);
83
+ }
84
+ return features;
85
+ }
86
+
87
+ /**
88
+ * Convert the type-only transform's Volar mappings into a content-mapper span
89
+ * map: an ordered, disjoint (in generated space) list of
90
+ * `[generatedStart, generatedLength, originalStart, originalLength, kind, features]`.
91
+ *
92
+ * - Equal length and identical text → `Verbatim` (the only edit-safe kind).
93
+ * - An identifier whose generated spelling differs (obfuscated `_$_` names,
94
+ * renamed or capitalized identifiers) → `Alias`, so diagnostics that cover
95
+ * the span exactly show the authored name.
96
+ * - Anything else → `Atom`.
97
+ *
98
+ * The transform emits container spans (statements, control-flow bodies) that
99
+ * enclose token spans. TypeScript requires generated spans to be disjoint, so
100
+ * candidates are ranked Verbatim first, then shorter before longer, and a
101
+ * candidate that overlaps an already selected span in generated space is
102
+ * dropped. Overlap in original space is allowed by the protocol (one source
103
+ * range may be projected several times) and is kept.
104
+ *
105
+ * Neighbouring Verbatim spans with equal feature bits whose gap is the same
106
+ * text in both files are then coalesced into one span (see
107
+ * {@link coalesce_verbatim_spans}), so a statement such as
108
+ * `import { a } from './x';` is one edit-safe span rather than four tokens
109
+ * with unmapped punctuation between them. TypeScript only applies text edits
110
+ * (auto-import, rename, code actions) that fit inside a single Verbatim span.
111
+ * @param {readonly CodeMapping[]} mappings
112
+ * @param {string} generated_text
113
+ * @param {string} original_text
114
+ * @param {{ languageFeatures?: boolean }} [options] `languageFeatures: false` clears every feature bit (CLI-only projects).
115
+ * @returns {SpanMapping[]}
116
+ */
117
+ export function to_span_mappings(mappings, generated_text, original_text, options = {}) {
118
+ const language_features = options.languageFeatures !== false;
119
+ /** @type {Candidate[]} */
120
+ const candidates = [];
121
+
122
+ for (const mapping of mappings) {
123
+ const count = Math.min(mapping.sourceOffsets.length, mapping.generatedOffsets.length);
124
+ for (let index = 0; index < count; index++) {
125
+ const original_start = mapping.sourceOffsets[index];
126
+ const generated_start = mapping.generatedOffsets[index];
127
+ const original_length = mapping.lengths[index] ?? mapping.lengths[0];
128
+ const generated_length =
129
+ mapping.generatedLengths?.[index] ?? mapping.generatedLengths?.[0] ?? original_length;
130
+ if (
131
+ !Number.isInteger(original_start) ||
132
+ !Number.isInteger(generated_start) ||
133
+ !Number.isInteger(original_length) ||
134
+ !Number.isInteger(generated_length) ||
135
+ original_start < 0 ||
136
+ generated_start < 0 ||
137
+ original_length <= 0 ||
138
+ generated_length <= 0
139
+ ) {
140
+ continue;
141
+ }
142
+ const generated_end = generated_start + generated_length;
143
+ const original_end = original_start + original_length;
144
+ if (generated_end > generated_text.length || original_end > original_text.length) {
145
+ continue;
146
+ }
147
+ const generated_slice = generated_text.slice(generated_start, generated_end);
148
+ const original_slice = original_text.slice(original_start, original_end);
149
+ /** @type {0 | 1 | 2} */
150
+ let kind;
151
+ if (generated_length === original_length && generated_slice === original_slice) {
152
+ kind = SpanMapKind.Verbatim;
153
+ } else if (
154
+ is_whole_identifier(generated_text, generated_start, generated_end) &&
155
+ is_whole_token(original_text, original_start, original_end)
156
+ ) {
157
+ kind = SpanMapKind.Alias;
158
+ } else if (is_partial_identifier(original_text, original_start, original_end)) {
159
+ // A fragment of an identifier (the transform's one-character file-start
160
+ // anchor, `e` of `export` → `c` of a hoisted `const`) is not an entity.
161
+ // Volar needed the anchor; under the content-mapper protocol it would
162
+ // only make TypeScript project requests at the file start into
163
+ // synthesized code.
164
+ continue;
165
+ } else {
166
+ kind = SpanMapKind.Atom;
167
+ }
168
+ candidates.push({
169
+ generatedStart: generated_start,
170
+ generatedEnd: generated_end,
171
+ originalStart: original_start,
172
+ originalEnd: original_end,
173
+ kind,
174
+ features: language_features
175
+ ? features_from_mapping_data(mapping.data)
176
+ : SpanMapFeature.None,
177
+ });
178
+ }
179
+ }
180
+
181
+ candidates.sort(
182
+ (left, right) =>
183
+ Number(left.kind !== SpanMapKind.Verbatim) - Number(right.kind !== SpanMapKind.Verbatim) ||
184
+ left.generatedEnd - left.generatedStart - (right.generatedEnd - right.generatedStart) ||
185
+ left.generatedStart - right.generatedStart ||
186
+ left.originalStart - right.originalStart,
187
+ );
188
+
189
+ /** @type {Candidate[]} */
190
+ const selected = [];
191
+ const occupied = new IntervalSet();
192
+ for (const candidate of candidates) {
193
+ if (!occupied.add(candidate.generatedStart, candidate.generatedEnd)) {
194
+ continue;
195
+ }
196
+ selected.push(candidate);
197
+ }
198
+
199
+ selected.sort((left, right) => left.generatedStart - right.generatedStart);
200
+ return extend_verbatim_spans_over_whitespace(
201
+ coalesce_verbatim_spans(selected, generated_text, original_text),
202
+ generated_text,
203
+ original_text,
204
+ ).map((candidate) => [
205
+ candidate.generatedStart,
206
+ candidate.generatedEnd - candidate.generatedStart,
207
+ candidate.originalStart,
208
+ candidate.originalEnd - candidate.originalStart,
209
+ candidate.kind,
210
+ candidate.features,
211
+ ]);
212
+ }
213
+
214
+ /**
215
+ * Merge runs of Verbatim spans, sorted by generated start, when the two spans
216
+ * carry the same feature bits and the text between them is identical (and of
217
+ * equal length) in the generated and the original file. The merged span is
218
+ * Verbatim by construction: every position inside it maps with the same
219
+ * offset and the same text.
220
+ * @param {Candidate[]} spans
221
+ * @param {string} generated_text
222
+ * @param {string} original_text
223
+ * @returns {Candidate[]}
224
+ */
225
+ export function coalesce_verbatim_spans(spans, generated_text, original_text) {
226
+ /** @type {Candidate[]} */
227
+ const merged = [];
228
+ for (const span of spans) {
229
+ const previous = merged[merged.length - 1];
230
+ if (
231
+ previous &&
232
+ previous.kind === SpanMapKind.Verbatim &&
233
+ span.kind === SpanMapKind.Verbatim &&
234
+ previous.features === span.features &&
235
+ span.originalStart >= previous.originalEnd &&
236
+ span.generatedStart - previous.generatedEnd === span.originalStart - previous.originalEnd &&
237
+ generated_text.slice(previous.generatedEnd, span.generatedStart) ===
238
+ original_text.slice(previous.originalEnd, span.originalStart)
239
+ ) {
240
+ previous.generatedEnd = span.generatedEnd;
241
+ previous.originalEnd = span.originalEnd;
242
+ continue;
243
+ }
244
+ merged.push({ ...span });
245
+ }
246
+ return merged;
247
+ }
248
+
249
+ /**
250
+ * Extend every Verbatim span forward over whitespace that is identical in the
251
+ * generated and the original file, up to the next span in either file.
252
+ *
253
+ * TypeScript's edits routinely end at the start of the line after a statement
254
+ * (an organize-imports edit for the import block ends after the last import's
255
+ * newline), and TypeScript 7 discards a whole edit when any of its ranges does
256
+ * not map. A span that stops at a statement's last character therefore made
257
+ * "Organize Imports" a no-op on `.tsrx` files; covering the newline that follows
258
+ * fixes that at no cost, since identical whitespace maps verbatim.
259
+ * @param {Candidate[]} spans Sorted by generated start, non-overlapping.
260
+ * @param {string} generated_text
261
+ * @param {string} original_text
262
+ * @returns {Candidate[]}
263
+ */
264
+ export function extend_verbatim_spans_over_whitespace(spans, generated_text, original_text) {
265
+ for (let index = 0; index < spans.length; index++) {
266
+ const span = spans[index];
267
+ if (span.kind !== SpanMapKind.Verbatim) continue;
268
+ const next = spans[index + 1];
269
+ const generated_limit = next ? next.generatedStart : generated_text.length;
270
+ const original_limit = next ? next.originalStart : original_text.length;
271
+ while (
272
+ span.generatedEnd < generated_limit &&
273
+ span.originalEnd < original_limit &&
274
+ generated_text[span.generatedEnd] === original_text[span.originalEnd] &&
275
+ /[ \t\r\n]/.test(generated_text[span.generatedEnd])
276
+ ) {
277
+ span.generatedEnd++;
278
+ span.originalEnd++;
279
+ }
280
+ }
281
+ return spans;
282
+ }
283
+
284
+ const identifier_part = /[\p{ID_Continue}$\u200c\u200d]/u;
285
+ const token_part = /[\p{ID_Continue}$#@\u200c\u200d]/u;
286
+
287
+ /**
288
+ * Whether `[start, end)` is a complete identifier in `text`: identifier
289
+ * syntax inside and no identifier character touching either edge.
290
+ * @param {string} text
291
+ * @param {number} start
292
+ * @param {number} end
293
+ */
294
+ function is_whole_identifier(text, start, end) {
295
+ return (
296
+ identifier_pattern.test(text.slice(start, end)) &&
297
+ (start === 0 || !identifier_part.test(text[start - 1])) &&
298
+ (end === text.length || !identifier_part.test(text[end]))
299
+ );
300
+ }
301
+
302
+ /**
303
+ * Whether `[start, end)` is a piece of a longer identifier in `text`:
304
+ * identifier characters inside and an identifier character touching an edge.
305
+ * @param {string} text
306
+ * @param {number} start
307
+ * @param {number} end
308
+ */
309
+ function is_partial_identifier(text, start, end) {
310
+ const slice = text.slice(start, end);
311
+ return (
312
+ slice.length > 0 &&
313
+ [...slice].every((character) => identifier_part.test(character)) &&
314
+ ((start > 0 && identifier_part.test(text[start - 1])) ||
315
+ (end < text.length && identifier_part.test(text[end])))
316
+ );
317
+ }
318
+
319
+ /**
320
+ * Whether `[start, end)` is a complete authored token: no whitespace inside
321
+ * and no token character (identifier characters plus the TSRX `#`/`@`
322
+ * sigils) touching either edge.
323
+ * @param {string} text
324
+ * @param {number} start
325
+ * @param {number} end
326
+ */
327
+ function is_whole_token(text, start, end) {
328
+ const slice = text.slice(start, end);
329
+ return (
330
+ slice.length > 0 &&
331
+ !/\s/.test(slice) &&
332
+ (start === 0 || !token_part.test(text[start - 1])) &&
333
+ (end === text.length || !token_part.test(text[end]))
334
+ );
335
+ }
336
+
337
+ /**
338
+ * Sorted set of half-open intervals supporting an "add if disjoint" query.
339
+ * Intervals are kept sorted by start in a plain array; a binary search finds
340
+ * the neighbours to test, so adding is O(n) in the worst case, which is fine
341
+ * for the few thousand spans a file produces.
342
+ */
343
+ class IntervalSet {
344
+ /** @type {number[]} */
345
+ #starts = [];
346
+ /** @type {number[]} */
347
+ #ends = [];
348
+
349
+ /**
350
+ * @param {number} start
351
+ * @param {number} end
352
+ * @returns {boolean} Whether the interval was added (it overlapped nothing).
353
+ */
354
+ add(start, end) {
355
+ const starts = this.#starts;
356
+ let low = 0;
357
+ let high = starts.length;
358
+ while (low < high) {
359
+ const middle = (low + high) >>> 1;
360
+ if (starts[middle] < start) low = middle + 1;
361
+ else high = middle;
362
+ }
363
+ // `low` is the first interval starting at or after `start`.
364
+ if (low < starts.length && starts[low] < end) return false;
365
+ if (low > 0 && this.#ends[low - 1] > start) return false;
366
+ starts.splice(low, 0, start);
367
+ this.#ends.splice(low, 0, end);
368
+ return true;
369
+ }
370
+ }