@rohal12/spindle 0.64.9 → 0.64.11
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.
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +412 -241
- package/dist/pkg/play.js +2 -1
- package/dist/pkg/story-variables.js +489 -271
- package/dist/pkg/tooling.js +2 -0
- package/package.json +1 -1
- package/src/completion-context.ts +111 -23
- package/src/js-lexer.ts +178 -63
- package/src/markup/grammar-shared.ts +2 -1
- package/src/markup/html-elements.ts +16 -0
- package/src/markup/html-integration.tsx +101 -0
- package/src/markup/render.tsx +70 -32
- package/src/markup/spindle.peggy +16 -8
- package/src/parser.ts +2 -1
- package/src/play/session.ts +9 -1
- package/src/store.ts +6 -0
- package/src/story-api.ts +9 -3
- package/src/tooling.ts +63 -19
- package/src/twine-editor/highlight.ts +17 -20
- package/src/utils/stable-key.ts +5 -6
- package/types/index.d.ts +5 -2
- package/types/tooling.d.ts +42 -0
package/dist/pkg/tooling.js
CHANGED
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
/** Cursor context from the runtime grammar and argument metadata, without executing code. */
|
|
2
2
|
import { completionTokens } from './markup/parse';
|
|
3
|
+
import type { Token } from './markup/tokens';
|
|
3
4
|
import { isRawMacro } from './markup/ast';
|
|
4
5
|
import { isCodeAttribute, splitSigilTemplate } from './markup/code-attributes';
|
|
5
6
|
import {
|
|
@@ -14,6 +15,8 @@ import { macroArgumentSpans } from './components/macros/macro-args';
|
|
|
14
15
|
import { readQuoted, splitArgSpans } from './components/macros/arg-utils';
|
|
15
16
|
import {
|
|
16
17
|
lexJs,
|
|
18
|
+
completionIdentifier,
|
|
19
|
+
type CompletionIdentifier,
|
|
17
20
|
scanStringLiteral,
|
|
18
21
|
stringLiteralValue,
|
|
19
22
|
type JsGoal,
|
|
@@ -37,6 +40,13 @@ export type CompletionContext =
|
|
|
37
40
|
containers?: readonly CompletionContainer[];
|
|
38
41
|
});
|
|
39
42
|
|
|
43
|
+
/** Full code and identifier boundaries for an edit at the cursor. */
|
|
44
|
+
export type CompletionTarget =
|
|
45
|
+
| { kind: 'literal' }
|
|
46
|
+
| (Extract<CompletionContext, { kind: 'code' }> & {
|
|
47
|
+
identifier?: CompletionIdentifier;
|
|
48
|
+
});
|
|
49
|
+
|
|
40
50
|
const LITERAL = { kind: 'literal' } as const;
|
|
41
51
|
|
|
42
52
|
/**
|
|
@@ -49,7 +59,46 @@ export function completionContext(
|
|
|
49
59
|
cursor: number,
|
|
50
60
|
macros: Iterable<ToolingMacro> = [],
|
|
51
61
|
): CompletionContext {
|
|
52
|
-
|
|
62
|
+
return readCompletion(
|
|
63
|
+
source.slice(0, cursor),
|
|
64
|
+
cursor,
|
|
65
|
+
macros,
|
|
66
|
+
false,
|
|
67
|
+
source.length,
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** Includes the identifier suffix and its mappings through enclosing markup. */
|
|
72
|
+
export function completionTarget(
|
|
73
|
+
source: string,
|
|
74
|
+
cursor: number,
|
|
75
|
+
macros: Iterable<ToolingMacro> = [],
|
|
76
|
+
): CompletionTarget {
|
|
77
|
+
return readCompletion(source, cursor, macros, true);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Visit the markup scopes enclosing a cursor, from outermost to innermost. */
|
|
81
|
+
export function completionScopes(
|
|
82
|
+
source: string,
|
|
83
|
+
cursor: number,
|
|
84
|
+
macros: Iterable<ToolingMacro>,
|
|
85
|
+
visit: (tokens: readonly Token[], map: (offset: number) => number) => boolean,
|
|
86
|
+
): void {
|
|
87
|
+
readCompletion(source, cursor, macros, true, source.length, visit);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function readCompletion(
|
|
91
|
+
source: string,
|
|
92
|
+
cursor: number,
|
|
93
|
+
macros: Iterable<ToolingMacro>,
|
|
94
|
+
full: boolean,
|
|
95
|
+
sourceLength = source.length,
|
|
96
|
+
visit?: (
|
|
97
|
+
tokens: readonly Token[],
|
|
98
|
+
map: (offset: number) => number,
|
|
99
|
+
) => boolean,
|
|
100
|
+
): CompletionTarget {
|
|
101
|
+
if (!Number.isInteger(cursor) || cursor < 0 || cursor > sourceLength) {
|
|
53
102
|
throw new RangeError('cursor must be a UTF-16 offset within source');
|
|
54
103
|
}
|
|
55
104
|
const parametersOf = parameterLookup(macros);
|
|
@@ -64,10 +113,13 @@ export function completionContext(
|
|
|
64
113
|
// A following identifier probes the lexer's state at EOF. It cannot
|
|
65
114
|
// close a string, regex, comment or template, and needs no guessed
|
|
66
115
|
// delimiters. A dot before it still counts as executable property code.
|
|
67
|
-
|
|
116
|
+
let position = 0;
|
|
117
|
+
while (position < code.length && map(position) < cursor) position++;
|
|
118
|
+
const prefix = code.slice(0, position);
|
|
119
|
+
const probe = prefix.length + 1;
|
|
68
120
|
let executable = false;
|
|
69
121
|
lexJs(
|
|
70
|
-
|
|
122
|
+
prefix + ' __spindle_cursor__',
|
|
71
123
|
{
|
|
72
124
|
code(_ch, at) {
|
|
73
125
|
if (at >= probe) executable = true;
|
|
@@ -79,6 +131,9 @@ export function completionContext(
|
|
|
79
131
|
goal,
|
|
80
132
|
);
|
|
81
133
|
if (!executable) return LITERAL;
|
|
134
|
+
const identifier = full
|
|
135
|
+
? completionIdentifier(code, position, goal)
|
|
136
|
+
: undefined;
|
|
82
137
|
const offset = map(0);
|
|
83
138
|
const offsets = Array.from({ length: code.length + 1 }, (_, i) => map(i));
|
|
84
139
|
return {
|
|
@@ -86,7 +141,8 @@ export function completionContext(
|
|
|
86
141
|
code,
|
|
87
142
|
goal,
|
|
88
143
|
offset,
|
|
89
|
-
cursor:
|
|
144
|
+
cursor: position,
|
|
145
|
+
...(identifier && { identifier }),
|
|
90
146
|
...(containers.length ? { containers } : {}),
|
|
91
147
|
...(offsets.some((at, i) => at !== offset + i)
|
|
92
148
|
? { sourceOffsets: offsets }
|
|
@@ -107,7 +163,10 @@ export function completionContext(
|
|
|
107
163
|
let at = 0;
|
|
108
164
|
for (const part of splitSigilTemplate(src, true)) {
|
|
109
165
|
if ('expr' in part) {
|
|
110
|
-
if (
|
|
166
|
+
if (
|
|
167
|
+
map(at + 1) <= cursor &&
|
|
168
|
+
cursor <= map(at + 1 + part.expr.length)
|
|
169
|
+
) {
|
|
111
170
|
return codeContext(
|
|
112
171
|
part.expr,
|
|
113
172
|
'expression',
|
|
@@ -127,8 +186,14 @@ export function completionContext(
|
|
|
127
186
|
isRawMacro(name, parametersOf.widget?.(name) !== undefined),
|
|
128
187
|
},
|
|
129
188
|
});
|
|
130
|
-
|
|
131
|
-
|
|
189
|
+
if (visit && !visit(tokens, map)) return LITERAL;
|
|
190
|
+
const contains = (start: number, end: number) =>
|
|
191
|
+
map(start) <= cursor && cursor <= map(end);
|
|
192
|
+
const index = tokens.findIndex(
|
|
193
|
+
(token) => map(token.start) < cursor && cursor <= map(token.end),
|
|
194
|
+
);
|
|
195
|
+
const token = tokens[index];
|
|
196
|
+
if (!token) return LITERAL;
|
|
132
197
|
const codeAt = (code: string, at: number, goal: JsGoal = 'expression') =>
|
|
133
198
|
codeContext(code, goal, (i) => map(at + i), containers);
|
|
134
199
|
const nested = (
|
|
@@ -147,15 +212,24 @@ export function completionContext(
|
|
|
147
212
|
container ? [...containers, container] : containers,
|
|
148
213
|
);
|
|
149
214
|
|
|
150
|
-
if (
|
|
151
|
-
|
|
215
|
+
if (
|
|
216
|
+
token.type === 'variable' &&
|
|
217
|
+
contains(token.nameStart - 1, token.nameEnd)
|
|
218
|
+
) {
|
|
219
|
+
return codeAt(
|
|
220
|
+
src.slice(token.nameStart - 1, token.nameEnd),
|
|
221
|
+
token.nameStart - 1,
|
|
222
|
+
);
|
|
152
223
|
}
|
|
153
|
-
if (
|
|
224
|
+
if (
|
|
225
|
+
token.type === 'expression' &&
|
|
226
|
+
contains(token.expressionStart, token.expressionEnd)
|
|
227
|
+
) {
|
|
154
228
|
return codeAt(token.expression, token.expressionStart);
|
|
155
229
|
}
|
|
156
230
|
if (token.type === 'html') {
|
|
157
231
|
for (const { name, value, at } of attributeValues(src, token)) {
|
|
158
|
-
if (at + value.length
|
|
232
|
+
if (contains(at, at + value.length)) {
|
|
159
233
|
const span = token.attributeSpans.find(
|
|
160
234
|
(span) => span.valueStart === at,
|
|
161
235
|
);
|
|
@@ -167,12 +241,15 @@ export function completionContext(
|
|
|
167
241
|
});
|
|
168
242
|
}
|
|
169
243
|
}
|
|
170
|
-
} else if (
|
|
244
|
+
} else if (
|
|
245
|
+
token.type === 'link' &&
|
|
246
|
+
contains(token.displayStart, token.displayEnd)
|
|
247
|
+
) {
|
|
171
248
|
return nested(token.display, token.displayStart, undefined, false, {
|
|
172
249
|
kind: 'link-label',
|
|
173
250
|
});
|
|
174
251
|
} else if (token.type === 'text' && !token.comment) {
|
|
175
|
-
const opener = tokens[
|
|
252
|
+
const opener = tokens[index - 1];
|
|
176
253
|
if (
|
|
177
254
|
opener?.type === 'macro' &&
|
|
178
255
|
!opener.isClose &&
|
|
@@ -183,7 +260,7 @@ export function completionContext(
|
|
|
183
260
|
} else if (
|
|
184
261
|
token.type === 'macro' &&
|
|
185
262
|
!token.isClose &&
|
|
186
|
-
token.
|
|
263
|
+
contains(token.argsStart, token.argsEnd)
|
|
187
264
|
) {
|
|
188
265
|
const name = token.name.toLowerCase();
|
|
189
266
|
const args = token.rawArgs;
|
|
@@ -206,10 +283,13 @@ export function completionContext(
|
|
|
206
283
|
return holds === 'markup' ? nested(raw, at) : LITERAL;
|
|
207
284
|
}
|
|
208
285
|
const scan = scanStringLiteral(raw, 0);
|
|
209
|
-
|
|
286
|
+
const bodyEnd = scan.closed ? scan.end - 1 : scan.end;
|
|
287
|
+
if (!contains(at + 1, at + bodyEnd)) return LITERAL;
|
|
210
288
|
const value =
|
|
211
289
|
escapes === 'javascript'
|
|
212
|
-
? stringLiteralValue(
|
|
290
|
+
? stringLiteralValue(
|
|
291
|
+
raw.slice(0, scan.end) + (scan.closed ? '' : raw[0]),
|
|
292
|
+
)
|
|
213
293
|
: quoted.value;
|
|
214
294
|
if (value == null) return LITERAL;
|
|
215
295
|
const container: CompletionContainer = {
|
|
@@ -217,7 +297,12 @@ export function completionContext(
|
|
|
217
297
|
quote: raw[0] as '"' | "'",
|
|
218
298
|
escapes,
|
|
219
299
|
};
|
|
220
|
-
const offsets = escapeOffsets(
|
|
300
|
+
const offsets = escapeOffsets(
|
|
301
|
+
raw.slice(1, bodyEnd),
|
|
302
|
+
at + 1,
|
|
303
|
+
value,
|
|
304
|
+
escapes,
|
|
305
|
+
);
|
|
221
306
|
if (holds === 'expression' || holds === 'statements') {
|
|
222
307
|
return codeContext(
|
|
223
308
|
value,
|
|
@@ -232,10 +317,13 @@ export function completionContext(
|
|
|
232
317
|
};
|
|
233
318
|
if (widget) {
|
|
234
319
|
const spans = splitArgSpans(args);
|
|
235
|
-
const
|
|
320
|
+
const which = spans.findIndex(([start, end]) =>
|
|
321
|
+
contains(token.argsStart + start, token.argsStart + end),
|
|
322
|
+
);
|
|
323
|
+
const span = spans[which];
|
|
236
324
|
if (!span) return LITERAL;
|
|
237
|
-
const raw = args.slice(span
|
|
238
|
-
const holds = widget[
|
|
325
|
+
const raw = args.slice(...span);
|
|
326
|
+
const holds = widget[which]?.holds;
|
|
239
327
|
return /^["']/.test(raw) && holds
|
|
240
328
|
? stringContext(raw, token.argsStart + span[0], holds, 'javascript')
|
|
241
329
|
: codeAt(raw, token.argsStart + span[0]);
|
|
@@ -245,7 +333,7 @@ export function completionContext(
|
|
|
245
333
|
const spans = macroArgumentSpans(args, params).filter(
|
|
246
334
|
(span) =>
|
|
247
335
|
span.parameter.type !== 'options' &&
|
|
248
|
-
span.
|
|
336
|
+
contains(token.argsStart + span.start, token.argsStart + span.end),
|
|
249
337
|
);
|
|
250
338
|
const span = spans[spans.length - 1];
|
|
251
339
|
if (!span) return LITERAL;
|
|
@@ -256,7 +344,7 @@ export function completionContext(
|
|
|
256
344
|
at += param.name.length;
|
|
257
345
|
while (/\s/.test(args.charAt(at))) at++;
|
|
258
346
|
}
|
|
259
|
-
const raw = args.slice(at);
|
|
347
|
+
const raw = args.slice(at, span.end);
|
|
260
348
|
const offset = token.argsStart + at;
|
|
261
349
|
if (param.type === 'expression' || param.type === 'passage')
|
|
262
350
|
return codeAt(raw, offset);
|
|
@@ -266,5 +354,5 @@ export function completionContext(
|
|
|
266
354
|
}
|
|
267
355
|
return LITERAL;
|
|
268
356
|
};
|
|
269
|
-
return read(source
|
|
357
|
+
return read(source, (i) => i);
|
|
270
358
|
}
|
package/src/js-lexer.ts
CHANGED
|
@@ -97,6 +97,7 @@ interface ParserState {
|
|
|
97
97
|
start: number;
|
|
98
98
|
end: number;
|
|
99
99
|
exprAllowed: boolean;
|
|
100
|
+
operandHere: boolean;
|
|
100
101
|
lastTokEnd: number;
|
|
101
102
|
context: unknown[];
|
|
102
103
|
nextToken(): void;
|
|
@@ -162,14 +163,6 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
162
163
|
typeBeforeLast: TokenType = tt.eof;
|
|
163
164
|
/** Look ahead after a `%name` starting a line (off in look-aheads). */
|
|
164
165
|
lookahead = true;
|
|
165
|
-
/**
|
|
166
|
-
* Where a `%` is a transient (true) or the modulo operator (false)
|
|
167
|
-
* whatever the tokens before it (see parseCode).
|
|
168
|
-
*/
|
|
169
|
-
percentAt?: ReadonlyMap<number, boolean>;
|
|
170
|
-
/** Where it read a `%name` as a transient. */
|
|
171
|
-
transientsRead?: Set<number>;
|
|
172
|
-
|
|
173
166
|
readToken(code: number): void {
|
|
174
167
|
const self = this as unknown as ParserState;
|
|
175
168
|
// acorn updates it only when parsing; its guesses read it when
|
|
@@ -191,15 +184,12 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
191
184
|
const afterPrefix =
|
|
192
185
|
self.type === tt.incDec &&
|
|
193
186
|
(this.operandBeforeLast || this.breakBeforeLast);
|
|
194
|
-
const forced = code === 37 ? this.percentAt?.get(at) : undefined;
|
|
195
187
|
if (
|
|
196
188
|
code === 64 ||
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
(this.lookahead && transientAssignment(self, end))))
|
|
189
|
+
self.exprAllowed ||
|
|
190
|
+
afterPrefix ||
|
|
191
|
+
(this.lookahead && transientAssignment(self, end))
|
|
201
192
|
) {
|
|
202
|
-
if (code === 37) this.transientsRead?.add(at);
|
|
203
193
|
self.pos = end;
|
|
204
194
|
self.finishToken(tt.name, self.input.charAt(at) + name);
|
|
205
195
|
return;
|
|
@@ -230,6 +220,7 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
230
220
|
// or an object key (`class D { function; }`, `{ class: 1 }`)
|
|
231
221
|
const self = this as unknown as ParserState;
|
|
232
222
|
const prevType = self.type;
|
|
223
|
+
const beforePreviousValue = this.prevValue;
|
|
233
224
|
// The word before, unless it is a member name (`a.static`, `a?.let`),
|
|
234
225
|
// which no contextual keyword is
|
|
235
226
|
this.prevValue = isMemberDot(this.typeBeforeLast) ? undefined : self.value;
|
|
@@ -240,7 +231,8 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
240
231
|
// acorn takes a `function` or `class` for a statement's (whose `}` a
|
|
241
232
|
// regex may follow) after the `:` of a conditional, and `async function`
|
|
242
233
|
// always. Both are expressions where an operand is (`a ? b : class {} / 2`,
|
|
243
|
-
// `x = async function () {} / 2`)
|
|
234
|
+
// `x = async function () {} / 2`), including after contextual operators
|
|
235
|
+
// such as `of` and `await`: read them as after what comes before.
|
|
244
236
|
if (!property && (type === tt._function || type === tt._class)) {
|
|
245
237
|
const afterAsync =
|
|
246
238
|
type === tt._function &&
|
|
@@ -248,8 +240,19 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
248
240
|
this.prevValue === 'async' &&
|
|
249
241
|
!this.breakHere;
|
|
250
242
|
const before = afterAsync ? this.typeBeforeLast : prevType;
|
|
243
|
+
// `yield` ends at a line break; the following declaration then
|
|
244
|
+
// closes a statement, so a `%name` after it starts a new expression.
|
|
245
|
+
// For `async function`, retain the operand context from before
|
|
246
|
+
// `async`: parseIdent has already consumed it as an identifier.
|
|
247
|
+
const contextualOperand =
|
|
248
|
+
before === tt.name &&
|
|
249
|
+
(afterAsync ? this.operandBeforeLast : self.exprAllowed) &&
|
|
250
|
+
((afterAsync ? beforePreviousValue : this.prevValue) !== 'yield' ||
|
|
251
|
+
!(afterAsync ? this.breakBeforeLast : this.breakHere));
|
|
251
252
|
self.type =
|
|
252
|
-
before === tt.colon && this.colonEndsTernary
|
|
253
|
+
(before === tt.colon && this.colonEndsTernary) || contextualOperand
|
|
254
|
+
? tt.parenL
|
|
255
|
+
: before;
|
|
253
256
|
}
|
|
254
257
|
this.typeBeforeLast = prevType;
|
|
255
258
|
// @ts-expect-error acorn internals
|
|
@@ -270,6 +273,31 @@ const SigilParser = class extends (Parser as unknown as Base) {
|
|
|
270
273
|
}
|
|
271
274
|
}
|
|
272
275
|
|
|
276
|
+
parseIdent(liberal?: boolean): AnyNode {
|
|
277
|
+
// The parser knows this token is an identifier, even when the
|
|
278
|
+
// tokenizer guessed `of` was a for-of separator. Set its context
|
|
279
|
+
// before consuming it, so the following `%` reads as modulo.
|
|
280
|
+
(this as unknown as ParserState).exprAllowed = false;
|
|
281
|
+
// @ts-expect-error acorn internals
|
|
282
|
+
return super.parseIdent(liberal);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
parseForIn(node: AnyNode, init: AnyNode): AnyNode {
|
|
286
|
+
// Conversely, this `of` really is a separator. In `const of of
|
|
287
|
+
// %list`, the preceding identifier can mislead the token-only guess.
|
|
288
|
+
(this as unknown as ParserState).exprAllowed = true;
|
|
289
|
+
// @ts-expect-error acorn internals
|
|
290
|
+
return super.parseForIn(node, init);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
parseAwait(forInit?: boolean): AnyNode {
|
|
294
|
+
// `await` is also tokenized as a name; here the parser knows an
|
|
295
|
+
// operand follows, including a transient such as `await %result`.
|
|
296
|
+
(this as unknown as ParserState).exprAllowed = true;
|
|
297
|
+
// @ts-expect-error acorn internals
|
|
298
|
+
return super.parseAwait(forInit);
|
|
299
|
+
}
|
|
300
|
+
|
|
273
301
|
braceIsBlock(prevType: TokenType): boolean {
|
|
274
302
|
if (prevType === tt.colon && this.colonEndsTernary) return false;
|
|
275
303
|
if (prevType === tt.braceR && this.closedBlock) return true;
|
|
@@ -533,27 +561,11 @@ function parseWithAst(
|
|
|
533
561
|
src: string,
|
|
534
562
|
goal: JsGoal,
|
|
535
563
|
): { out: ParsedCode; ast: AnyNode } {
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
for (;;) {
|
|
542
|
-
const transientsRead = new Set<number>();
|
|
543
|
-
try {
|
|
544
|
-
const ast = parseAst(src, goal, percentAt, transientsRead);
|
|
545
|
-
const out: ParsedCode = { refs: [], strings: [] };
|
|
546
|
-
walk(ast, src, out, false, false);
|
|
547
|
-
out.refs.sort((a, b) => a.start - b.start);
|
|
548
|
-
return { out, ast };
|
|
549
|
-
} catch (error) {
|
|
550
|
-
const pos = (error as { pos?: number }).pos ?? -1;
|
|
551
|
-
if (!(error instanceof SyntaxError) || percentAt.has(pos)) throw error;
|
|
552
|
-
TRANS_NAME_RE.lastIndex = pos + 1;
|
|
553
|
-
if (src.charAt(pos) !== '%' || !TRANS_NAME_RE.test(src)) throw error;
|
|
554
|
-
percentAt.set(pos, !transientsRead.has(pos));
|
|
555
|
-
}
|
|
556
|
-
}
|
|
564
|
+
const ast = parseAst(src, goal);
|
|
565
|
+
const out: ParsedCode = { refs: [], strings: [] };
|
|
566
|
+
walk(ast, src, out, false, false);
|
|
567
|
+
out.refs.sort((a, b) => a.start - b.start);
|
|
568
|
+
return { out, ast };
|
|
557
569
|
}
|
|
558
570
|
|
|
559
571
|
/** The variable at the root of a reference path, and whether it has members. */
|
|
@@ -633,24 +645,12 @@ export function readMemberPath(
|
|
|
633
645
|
return { path, end };
|
|
634
646
|
}
|
|
635
647
|
|
|
636
|
-
/**
|
|
637
|
-
|
|
638
|
-
* noting where it read a transient in `transientsRead`.
|
|
639
|
-
*/
|
|
640
|
-
function parseAst(
|
|
641
|
-
src: string,
|
|
642
|
-
goal: JsGoal,
|
|
643
|
-
percentAt: ReadonlyMap<number, boolean>,
|
|
644
|
-
transientsRead: Set<number>,
|
|
645
|
-
): AnyNode {
|
|
648
|
+
/** Parse `src` as `goal`, using the parser's context for `%name`. */
|
|
649
|
+
function parseAst(src: string, goal: JsGoal): AnyNode {
|
|
646
650
|
try {
|
|
647
651
|
const p = tokenizerAt(src, 0, goal) as ParserState & {
|
|
648
652
|
parse(): AnyNode;
|
|
649
|
-
percentAt?: ReadonlyMap<number, boolean>;
|
|
650
|
-
transientsRead?: Set<number>;
|
|
651
653
|
};
|
|
652
|
-
p.percentAt = percentAt;
|
|
653
|
-
p.transientsRead = transientsRead;
|
|
654
654
|
if (goal === 'statements') return p.parse();
|
|
655
655
|
// As acorn's parseExpressionAt, and then the code must end
|
|
656
656
|
p.nextToken();
|
|
@@ -940,36 +940,58 @@ export function findCodeEnd(
|
|
|
940
940
|
start: number,
|
|
941
941
|
{ goal = 'expression', stop, onRunOut }: FindCodeEndOptions = {},
|
|
942
942
|
): number {
|
|
943
|
-
|
|
944
|
-
|
|
943
|
+
let verifyStop = false;
|
|
944
|
+
const end = scanCodeEnd(src, start, goal, stop, onRunOut, () => {
|
|
945
|
+
verifyStop = true;
|
|
946
|
+
});
|
|
947
|
+
if (end < 0 || verifyStop) {
|
|
948
|
+
const parsed = parsedCodeEnd(src, start, goal, stop);
|
|
949
|
+
if (parsed >= 0) return parsed;
|
|
950
|
+
}
|
|
951
|
+
return end;
|
|
945
952
|
}
|
|
946
953
|
|
|
947
954
|
/**
|
|
948
955
|
* Where the code from `start` ends, as the parser finds it: the `}` it
|
|
949
|
-
* stops at, or -1. The token scan of
|
|
950
|
-
* opens a regex from the tokens before
|
|
951
|
-
*
|
|
952
|
-
*
|
|
956
|
+
* stops at (or the `{` matching `stop`), or -1. The token scan of
|
|
957
|
+
* `findCodeEnd` guesses whether a `/` opens a regex from the tokens before
|
|
958
|
+
* it, and class fields inside template literals can mislead it. The parser
|
|
959
|
+
* knows the context. Asked when the scan finds no end or a stop that might
|
|
960
|
+
* still be template text.
|
|
953
961
|
*/
|
|
954
962
|
export function parsedCodeEnd(
|
|
955
963
|
src: string,
|
|
956
964
|
start: number,
|
|
957
965
|
goal: JsGoal,
|
|
966
|
+
stop?: (index: number) => boolean,
|
|
958
967
|
): number {
|
|
959
968
|
const p = tokenizerAt(src, start, goal) as ParserState & {
|
|
960
969
|
parse(): unknown;
|
|
961
970
|
};
|
|
971
|
+
const stopped = {};
|
|
972
|
+
let stoppedAt = -1;
|
|
973
|
+
if (stop) {
|
|
974
|
+
const nextToken = p.nextToken;
|
|
975
|
+
p.nextToken = () => {
|
|
976
|
+
nextToken.call(p);
|
|
977
|
+
if (p.type === tt.braceL && stop(p.start)) {
|
|
978
|
+
stoppedAt = p.start;
|
|
979
|
+
throw stopped;
|
|
980
|
+
}
|
|
981
|
+
};
|
|
982
|
+
}
|
|
962
983
|
try {
|
|
963
984
|
if (goal === 'statements') {
|
|
964
985
|
p.parse();
|
|
965
986
|
} else {
|
|
966
987
|
p.nextToken();
|
|
967
988
|
p.parseExpression();
|
|
968
|
-
if (p.type === tt.braceR) return p.start;
|
|
989
|
+
if (!stop && p.type === tt.braceR) return p.start;
|
|
969
990
|
}
|
|
970
991
|
} catch (error) {
|
|
992
|
+
if (error === stopped) return stoppedAt;
|
|
971
993
|
const pos = (error as { pos?: number }).pos ?? -1;
|
|
972
|
-
if (src.charAt(pos) === '}') return pos;
|
|
994
|
+
if (!stop && src.charAt(pos) === '}') return pos;
|
|
973
995
|
}
|
|
974
996
|
return -1;
|
|
975
997
|
}
|
|
@@ -981,6 +1003,7 @@ function scanCodeEnd(
|
|
|
981
1003
|
goal: JsGoal,
|
|
982
1004
|
stop: ((index: number) => boolean) | undefined,
|
|
983
1005
|
onRunOut: (() => void) | undefined,
|
|
1006
|
+
onTemplateStop: () => void,
|
|
984
1007
|
): number {
|
|
985
1008
|
/** No end: the code runs to the end of `src`. */
|
|
986
1009
|
const ranOut = () => {
|
|
@@ -993,6 +1016,7 @@ function scanCodeEnd(
|
|
|
993
1016
|
let from = start;
|
|
994
1017
|
let afterOperand = false;
|
|
995
1018
|
let templateSlip = false;
|
|
1019
|
+
let interpolated = false;
|
|
996
1020
|
for (;;) {
|
|
997
1021
|
let resumeAt = -1;
|
|
998
1022
|
for (const tok of tokens(src, from, goal, { afterOperand })) {
|
|
@@ -1044,10 +1068,17 @@ function scanCodeEnd(
|
|
|
1044
1068
|
t === tt.num;
|
|
1045
1069
|
prevEnd = tok.end;
|
|
1046
1070
|
if (t === tt.braceL) {
|
|
1047
|
-
if (stop?.(tok.start))
|
|
1071
|
+
if (stop?.(tok.start)) {
|
|
1072
|
+
// A misleading keyword can leave the tokenizer's template
|
|
1073
|
+
// context early. Let the parser verify this possible stop once.
|
|
1074
|
+
if (interpolated) onTemplateStop();
|
|
1075
|
+
return tok.start;
|
|
1076
|
+
}
|
|
1048
1077
|
stack.push('{');
|
|
1049
|
-
} else if (t === tt.dollarBraceL)
|
|
1050
|
-
|
|
1078
|
+
} else if (t === tt.dollarBraceL) {
|
|
1079
|
+
interpolated = true;
|
|
1080
|
+
stack.push('${');
|
|
1081
|
+
} else if (t === tt.parenL) stack.push('(');
|
|
1051
1082
|
else if (t === tt.bracketL) stack.push('[');
|
|
1052
1083
|
else if (t === tt.parenR || t === tt.bracketR) {
|
|
1053
1084
|
if (last(stack) === (t === tt.parenR ? '(' : '[')) stack.pop();
|
|
@@ -1063,6 +1094,8 @@ function scanCodeEnd(
|
|
|
1063
1094
|
}
|
|
1064
1095
|
|
|
1065
1096
|
interface Tok {
|
|
1097
|
+
/** Tokenizer expected an operand before this token. */
|
|
1098
|
+
operand?: boolean;
|
|
1066
1099
|
type: TokenType;
|
|
1067
1100
|
start: number;
|
|
1068
1101
|
end: number;
|
|
@@ -1146,7 +1179,13 @@ function* tokens(
|
|
|
1146
1179
|
yield { error: error as Error, pos };
|
|
1147
1180
|
return;
|
|
1148
1181
|
}
|
|
1149
|
-
yield {
|
|
1182
|
+
yield {
|
|
1183
|
+
type: p.type,
|
|
1184
|
+
start: p.start,
|
|
1185
|
+
end: p.end,
|
|
1186
|
+
value: p.value,
|
|
1187
|
+
operand: p.operandHere,
|
|
1188
|
+
};
|
|
1150
1189
|
if (p.type === tt.eof) return;
|
|
1151
1190
|
}
|
|
1152
1191
|
}
|
|
@@ -1376,3 +1415,79 @@ export function scanStringLiteral(
|
|
|
1376
1415
|
}
|
|
1377
1416
|
return { end: src.length, closed: false };
|
|
1378
1417
|
}
|
|
1418
|
+
|
|
1419
|
+
/** Identifier replacement boundaries in decoded code (UTF-16). */
|
|
1420
|
+
export interface CompletionIdentifier extends Span {
|
|
1421
|
+
kind: 'root' | 'member';
|
|
1422
|
+
/** Decoded name, excluding a root's sigil. */
|
|
1423
|
+
name: string;
|
|
1424
|
+
/** Start of the preceding `.` or `?.`, for bracket-access edits. */
|
|
1425
|
+
dotStart?: number;
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
/** Read completion boundaries with the same tokenizer as runtime code. */
|
|
1429
|
+
export function completionIdentifier(
|
|
1430
|
+
source: string,
|
|
1431
|
+
cursor: number,
|
|
1432
|
+
goal: JsGoal = 'expression',
|
|
1433
|
+
): CompletionIdentifier | undefined {
|
|
1434
|
+
let previous: Tok | undefined;
|
|
1435
|
+
for (const token of tokens(source, 0, goal)) {
|
|
1436
|
+
if ('error' in token) {
|
|
1437
|
+
// A bare local sigil is temporarily invalid JavaScript while typing.
|
|
1438
|
+
if (source[token.pos] === '@' && cursor === token.pos + 1)
|
|
1439
|
+
return { kind: 'root', name: '', start: token.pos, end: cursor };
|
|
1440
|
+
return;
|
|
1441
|
+
}
|
|
1442
|
+
const member =
|
|
1443
|
+
previous && isMemberDot(previous.type) ? previous : undefined;
|
|
1444
|
+
if (token.start <= cursor && cursor <= token.end) {
|
|
1445
|
+
if (member && (token.type === tt.name || token.type.keyword)) {
|
|
1446
|
+
return {
|
|
1447
|
+
kind: 'member',
|
|
1448
|
+
name: token.value as string,
|
|
1449
|
+
start: token.start,
|
|
1450
|
+
end: token.end,
|
|
1451
|
+
dotStart: member.start,
|
|
1452
|
+
};
|
|
1453
|
+
}
|
|
1454
|
+
if (token.type === tt.name && !member) {
|
|
1455
|
+
const value = token.value as string;
|
|
1456
|
+
const sigil = sigilAt(source, token.start, value);
|
|
1457
|
+
if (
|
|
1458
|
+
(sigil || value === '$' || value === '_') &&
|
|
1459
|
+
!keyPosition(source, token.end, previous?.type)
|
|
1460
|
+
)
|
|
1461
|
+
return {
|
|
1462
|
+
kind: 'root',
|
|
1463
|
+
name: value.slice(1),
|
|
1464
|
+
start: token.start,
|
|
1465
|
+
end: token.end,
|
|
1466
|
+
};
|
|
1467
|
+
}
|
|
1468
|
+
if (
|
|
1469
|
+
token.type === tt.modulo &&
|
|
1470
|
+
source.slice(token.start, token.end) === '%' &&
|
|
1471
|
+
cursor === token.end &&
|
|
1472
|
+
token.operand
|
|
1473
|
+
)
|
|
1474
|
+
return { kind: 'root', name: '', start: token.start, end: token.end };
|
|
1475
|
+
}
|
|
1476
|
+
if (isMemberDot(token.type) && token.end === cursor) {
|
|
1477
|
+
// Prefer a following identifier's entire suffix, even at its start.
|
|
1478
|
+
previous = token;
|
|
1479
|
+
continue;
|
|
1480
|
+
}
|
|
1481
|
+
if (member && member.end === cursor && token.start >= cursor)
|
|
1482
|
+
return {
|
|
1483
|
+
kind: 'member',
|
|
1484
|
+
name: '',
|
|
1485
|
+
start: cursor,
|
|
1486
|
+
end: cursor,
|
|
1487
|
+
dotStart: member.start,
|
|
1488
|
+
};
|
|
1489
|
+
if (token.start > cursor || token.type === tt.eof) return;
|
|
1490
|
+
previous = token;
|
|
1491
|
+
}
|
|
1492
|
+
return undefined;
|
|
1493
|
+
}
|
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
* it holds no second copy: the generated parser imports this module's
|
|
4
4
|
* default export as `shared` (see scripts/peggy.ts).
|
|
5
5
|
*/
|
|
6
|
+
import { HTML_TEXT_ELEMENTS } from './html-elements';
|
|
6
7
|
import { SIGIL_SCOPES, withSelectors } from './tokens';
|
|
7
8
|
|
|
8
|
-
export default { SIGIL_SCOPES, withSelectors };
|
|
9
|
+
export default { SIGIL_SCOPES, withSelectors, HTML_TEXT_ELEMENTS };
|