@rohal12/spindle 0.62.4 → 0.62.6
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 +127 -45
- package/dist/pkg/story-variables.js +207 -86
- package/package.json +1 -1
- package/src/code-check.ts +43 -5
- package/src/completion-context.ts +58 -13
- package/src/components/macros/PassageDisplay.tsx +3 -16
- package/src/components/macros/detached-body.tsx +7 -0
- package/src/format/format-passage.ts +3 -3
- package/src/hooks/use-modal-focus.ts +1 -11
- package/src/interpolation.ts +3 -3
- package/src/js-bindings.ts +7 -4
- package/src/js-lexer.ts +5 -1
- package/src/markup/code-attributes.ts +3 -4
- package/src/markup/code-end.ts +68 -1
- package/src/markup/pair.ts +14 -2
- package/src/markup/parse.ts +22 -12
- package/src/markup/render.tsx +3 -3
- package/src/registry.ts +3 -0
- package/src/saves/save-manager.ts +14 -3
- package/src/store.ts +30 -16
- package/src/story-variables.ts +2 -4
- package/src/tooling.ts +57 -9
- package/src/utils/element-visibility.ts +22 -9
- package/src/widgets/ast-scanner.ts +2 -1
- package/types/tooling.d.ts +18 -1
|
@@ -10,6 +10,46 @@ var __exportAll = (all, no_symbols) => {
|
|
|
10
10
|
return target;
|
|
11
11
|
};
|
|
12
12
|
//#endregion
|
|
13
|
+
//#region src/utils/macro-names.ts
|
|
14
|
+
/**
|
|
15
|
+
* Macro and widget names are case-insensitive: `{StatusBar}` invokes the
|
|
16
|
+
* widget defined as `statusbar`. A NameMap keys its entries by the lowercased
|
|
17
|
+
* name.
|
|
18
|
+
*/
|
|
19
|
+
var NameMap = class extends Map {
|
|
20
|
+
get(name) {
|
|
21
|
+
return super.get(name.toLowerCase());
|
|
22
|
+
}
|
|
23
|
+
set(name, value) {
|
|
24
|
+
return super.set(name.toLowerCase(), value);
|
|
25
|
+
}
|
|
26
|
+
has(name) {
|
|
27
|
+
return super.has(name.toLowerCase());
|
|
28
|
+
}
|
|
29
|
+
delete(name) {
|
|
30
|
+
return super.delete(name.toLowerCase());
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
/** A set of macro or widget names (see NameMap). */
|
|
34
|
+
var NameSet = class {
|
|
35
|
+
constructor(names = []) {
|
|
36
|
+
this.names = new NameMap();
|
|
37
|
+
for (const name of names) this.add(name);
|
|
38
|
+
}
|
|
39
|
+
add(name) {
|
|
40
|
+
this.names.set(name, true);
|
|
41
|
+
}
|
|
42
|
+
has(name) {
|
|
43
|
+
return this.names.has(name);
|
|
44
|
+
}
|
|
45
|
+
delete(name) {
|
|
46
|
+
this.names.delete(name);
|
|
47
|
+
}
|
|
48
|
+
clear() {
|
|
49
|
+
this.names.clear();
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
//#endregion
|
|
13
53
|
//#region src/markup/tokens.ts
|
|
14
54
|
/** Variable sigils: story ($), temporary (_), local (@), transient (%). */
|
|
15
55
|
var SIGIL_SCOPES = {
|
|
@@ -3591,46 +3631,6 @@ function peg$parse(input, options) {
|
|
|
3591
3631
|
else peg$throw();
|
|
3592
3632
|
}
|
|
3593
3633
|
//#endregion
|
|
3594
|
-
//#region src/utils/macro-names.ts
|
|
3595
|
-
/**
|
|
3596
|
-
* Macro and widget names are case-insensitive: `{StatusBar}` invokes the
|
|
3597
|
-
* widget defined as `statusbar`. A NameMap keys its entries by the lowercased
|
|
3598
|
-
* name.
|
|
3599
|
-
*/
|
|
3600
|
-
var NameMap = class extends Map {
|
|
3601
|
-
get(name) {
|
|
3602
|
-
return super.get(name.toLowerCase());
|
|
3603
|
-
}
|
|
3604
|
-
set(name, value) {
|
|
3605
|
-
return super.set(name.toLowerCase(), value);
|
|
3606
|
-
}
|
|
3607
|
-
has(name) {
|
|
3608
|
-
return super.has(name.toLowerCase());
|
|
3609
|
-
}
|
|
3610
|
-
delete(name) {
|
|
3611
|
-
return super.delete(name.toLowerCase());
|
|
3612
|
-
}
|
|
3613
|
-
};
|
|
3614
|
-
/** A set of macro or widget names (see NameMap). */
|
|
3615
|
-
var NameSet = class {
|
|
3616
|
-
constructor(names = []) {
|
|
3617
|
-
this.names = new NameMap();
|
|
3618
|
-
for (const name of names) this.add(name);
|
|
3619
|
-
}
|
|
3620
|
-
add(name) {
|
|
3621
|
-
this.names.set(name, true);
|
|
3622
|
-
}
|
|
3623
|
-
has(name) {
|
|
3624
|
-
return this.names.has(name);
|
|
3625
|
-
}
|
|
3626
|
-
delete(name) {
|
|
3627
|
-
this.names.delete(name);
|
|
3628
|
-
}
|
|
3629
|
-
clear() {
|
|
3630
|
-
this.names.clear();
|
|
3631
|
-
}
|
|
3632
|
-
};
|
|
3633
|
-
//#endregion
|
|
3634
3634
|
//#region src/markup/ast.ts
|
|
3635
3635
|
/**
|
|
3636
3636
|
* The passage AST (built by parse.ts), and the registry of block macros:
|
|
@@ -3825,6 +3825,11 @@ function branchParents(name, macros) {
|
|
|
3825
3825
|
if (!macros) return [...companionParents.get(name) ?? []].filter((parent) => parent !== "listbox");
|
|
3826
3826
|
return macros.filter((macro) => macro.name.toLowerCase() !== "listbox" && macro.subMacros.some((sub) => sub.toLowerCase() === name.toLowerCase())).map((macro) => macro.name.toLowerCase());
|
|
3827
3827
|
}
|
|
3828
|
+
/** The declared parameters of the macro `name`, if it declares any. */
|
|
3829
|
+
function getMacroParameters(name) {
|
|
3830
|
+
return metadataRegistry.get(name)?.parameters;
|
|
3831
|
+
}
|
|
3832
|
+
getMacroParameters.source = (name) => metadataRegistry.get(name)?.source;
|
|
3828
3833
|
function getMacroRegistry() {
|
|
3829
3834
|
return Array.from(metadataRegistry.values());
|
|
3830
3835
|
}
|
|
@@ -3943,6 +3948,8 @@ function pairMarkup(tokens, options = {}) {
|
|
|
3943
3948
|
};
|
|
3944
3949
|
const root = [];
|
|
3945
3950
|
const stack = [];
|
|
3951
|
+
const openPositions = /* @__PURE__ */ new Map();
|
|
3952
|
+
const tagKey = (tag) => `${tag.type}:${lowerName(tag)}`;
|
|
3946
3953
|
const add = (node) => (last$1(stack)?.into ?? root).push(node);
|
|
3947
3954
|
/**
|
|
3948
3955
|
* End the element on top of the stack, with `close` if it has one: its
|
|
@@ -3951,6 +3958,10 @@ function pairMarkup(tokens, options = {}) {
|
|
|
3951
3958
|
const end = (ending, close, owns = ending === "closed") => {
|
|
3952
3959
|
const { node, body } = stack.pop();
|
|
3953
3960
|
const open = node.token;
|
|
3961
|
+
const key = tagKey(open);
|
|
3962
|
+
const positions = openPositions.get(key);
|
|
3963
|
+
positions.pop();
|
|
3964
|
+
if (positions.length === 0) openPositions.delete(key);
|
|
3954
3965
|
const noticedAt = close ? close.end : noticedAtEnd;
|
|
3955
3966
|
for (const { tag } of body.branches) if (open.type !== "macro" || !parentsOf(tag.name).includes(lowerName(open))) misplaced(tag, open, noticedAt);
|
|
3956
3967
|
if (ending === "unclosed") report("unclosed-block", open, `Unclosed ${tagLabel(open)}: no ${closerOf(open)} closes it`, noticedAt, { name: nameOf(open) });
|
|
@@ -3969,8 +3980,7 @@ function pairMarkup(tokens, options = {}) {
|
|
|
3969
3980
|
if (!top) report("stray-closer", token, `${tagLabel(token)} closes nothing: no ${token.type === "html" ? `<${token.tag}>` : `{${token.name}}`} is open here`, token.start, { name: nameOf(token) });
|
|
3970
3981
|
else if (closes(top.node.token, token)) end("closed", token);
|
|
3971
3982
|
else {
|
|
3972
|
-
|
|
3973
|
-
while (outer >= 0 && !closes(stack[outer].node.token, token)) outer--;
|
|
3983
|
+
const outer = last$1(openPositions.get(tagKey(token)) ?? []) ?? -1;
|
|
3974
3984
|
end("mismatch", token, outer === -1);
|
|
3975
3985
|
if (outer !== -1) {
|
|
3976
3986
|
while (stack.length > outer + 1) end("cut", token);
|
|
@@ -4014,6 +4024,10 @@ function pairMarkup(tokens, options = {}) {
|
|
|
4014
4024
|
branches: []
|
|
4015
4025
|
};
|
|
4016
4026
|
node.body = body;
|
|
4027
|
+
const key = tagKey(token);
|
|
4028
|
+
const positions = openPositions.get(key) ?? [];
|
|
4029
|
+
positions.push(stack.length);
|
|
4030
|
+
openPositions.set(key, positions);
|
|
4017
4031
|
stack.push({
|
|
4018
4032
|
node,
|
|
4019
4033
|
body,
|
|
@@ -10211,6 +10225,44 @@ function jsCodeEnd(onOpenEnd) {
|
|
|
10211
10225
|
}
|
|
10212
10226
|
/** The JavaScript reader Spindle uses today: js-lexer, then a lenient scan. */
|
|
10213
10227
|
var defaultCodeEnd = jsCodeEnd();
|
|
10228
|
+
/**
|
|
10229
|
+
* Index possible brace ends for repeated recovery attempts in one template.
|
|
10230
|
+
* Within plain code, nested braces must close first. At a quote, slash or
|
|
10231
|
+
* escape we stop making lexical assumptions, but the remaining raw `}`s
|
|
10232
|
+
* still bound how many pending braces could close. Indexing visits each
|
|
10233
|
+
* opener once, so unmatched plain-code openers can skip suffix scans.
|
|
10234
|
+
*/
|
|
10235
|
+
function indexCodeEnds(input) {
|
|
10236
|
+
const possible = /* @__PURE__ */ new Set();
|
|
10237
|
+
const pending = [];
|
|
10238
|
+
let remaining = 0;
|
|
10239
|
+
let lastBrace = -1;
|
|
10240
|
+
for (let i = 0; i < input.length; i++) if (input[i] === "}") {
|
|
10241
|
+
remaining++;
|
|
10242
|
+
lastBrace = i;
|
|
10243
|
+
}
|
|
10244
|
+
const uncertain = () => {
|
|
10245
|
+
for (let i = Math.max(0, pending.length - remaining); i < pending.length; i++) possible.add(pending[i]);
|
|
10246
|
+
pending.length = 0;
|
|
10247
|
+
};
|
|
10248
|
+
for (let i = 0; i < input.length; i++) {
|
|
10249
|
+
const c = input[i];
|
|
10250
|
+
if (c === "{") pending.push(i);
|
|
10251
|
+
else if (c === "}") {
|
|
10252
|
+
remaining--;
|
|
10253
|
+
const open = pending.pop();
|
|
10254
|
+
if (open !== void 0) possible.add(open);
|
|
10255
|
+
} else if (/['"`/\\]/.test(c)) uncertain();
|
|
10256
|
+
}
|
|
10257
|
+
return (start, codeStart = start, limit = input.length) => {
|
|
10258
|
+
if (start > lastBrace || codeStart >= limit) return -1;
|
|
10259
|
+
const source = limit === input.length ? input : input.slice(0, limit);
|
|
10260
|
+
if (input[start - 1] !== "{" || possible.has(start - 1) || input.slice(start, codeStart).includes("{")) return defaultCodeEnd.closeBrace(source, codeStart, start);
|
|
10261
|
+
const base = Math.max(0, codeStart - 1);
|
|
10262
|
+
const end = parsedCodeEnd(source.slice(base), codeStart - base, "expression");
|
|
10263
|
+
return end < 0 ? -1 : base + end;
|
|
10264
|
+
};
|
|
10265
|
+
}
|
|
10214
10266
|
//#endregion
|
|
10215
10267
|
//#region src/markup/code-attributes.ts
|
|
10216
10268
|
/**
|
|
@@ -10236,6 +10288,7 @@ function splitSigilTemplate(template, incomplete = false) {
|
|
|
10236
10288
|
const parts = [];
|
|
10237
10289
|
let text = "";
|
|
10238
10290
|
let i = 0;
|
|
10291
|
+
const closeBrace = indexCodeEnds(template);
|
|
10239
10292
|
while (i < template.length) {
|
|
10240
10293
|
const brace = template.indexOf("{", i);
|
|
10241
10294
|
if (brace === -1) {
|
|
@@ -10244,7 +10297,7 @@ function splitSigilTemplate(template, incomplete = false) {
|
|
|
10244
10297
|
}
|
|
10245
10298
|
text += template.slice(i, brace);
|
|
10246
10299
|
i = brace + 1;
|
|
10247
|
-
let end = isSigil(template[i]) ?
|
|
10300
|
+
let end = isSigil(template[i]) ? closeBrace(i) : -1;
|
|
10248
10301
|
if (end === -1 && incomplete && isSigil(template[i])) end = template.length;
|
|
10249
10302
|
if (end === -1) {
|
|
10250
10303
|
text += "{";
|
|
@@ -10287,10 +10340,14 @@ var MarkupError = class extends Error {
|
|
|
10287
10340
|
}
|
|
10288
10341
|
};
|
|
10289
10342
|
function run$1(source, startRule, options, completion = false) {
|
|
10290
|
-
const hooks =
|
|
10343
|
+
const hooks = {
|
|
10291
10344
|
...defaultHooks,
|
|
10292
10345
|
...options.hooks
|
|
10293
|
-
}
|
|
10346
|
+
};
|
|
10347
|
+
if (!options.hooks?.closeBrace) {
|
|
10348
|
+
const codeEnd = indexCodeEnds(source);
|
|
10349
|
+
hooks.closeBrace = (_input, codeStart, lenientStart) => codeEnd(lenientStart, codeStart);
|
|
10350
|
+
}
|
|
10294
10351
|
try {
|
|
10295
10352
|
return peg$parse(source, {
|
|
10296
10353
|
startRule,
|
|
@@ -10362,7 +10419,7 @@ function pairingError(position, error) {
|
|
|
10362
10419
|
* macros and elements are no error here. Throws a MarkupError for a
|
|
10363
10420
|
* malformed tag, such as an unclosed `{`, `[[` or attribute value.
|
|
10364
10421
|
*/
|
|
10365
|
-
function tokenizeMarkup(source, options = {}) {
|
|
10422
|
+
function tokenizeMarkup$1(source, options = {}) {
|
|
10366
10423
|
return run$1(source, "Tokens", options);
|
|
10367
10424
|
}
|
|
10368
10425
|
/**
|
|
@@ -10385,7 +10442,7 @@ function parseSelectors(source, at = 0) {
|
|
|
10385
10442
|
* started. Tokens and errors have offsets in `source`; for well-formed
|
|
10386
10443
|
* markup the tokens are those of `tokenizeMarkup` and there are no errors.
|
|
10387
10444
|
*/
|
|
10388
|
-
function tokenizeMarkupTolerant(source, options = {}) {
|
|
10445
|
+
function tokenizeMarkupTolerant$1(source, options = {}) {
|
|
10389
10446
|
const { tokens, errors } = scanMarkupTolerant(source, options);
|
|
10390
10447
|
return {
|
|
10391
10448
|
tokens,
|
|
@@ -10402,16 +10459,19 @@ function scanMarkupTolerant(source, options = {}, completion = false) {
|
|
|
10402
10459
|
const errors = [];
|
|
10403
10460
|
const damage = [];
|
|
10404
10461
|
let position;
|
|
10462
|
+
const codeEnd = indexCodeEnds(source);
|
|
10463
|
+
const customCloseBrace = options.hooks?.closeBrace;
|
|
10405
10464
|
const lastBrace = source.lastIndexOf("}");
|
|
10406
|
-
const { closeBrace } = {
|
|
10407
|
-
...defaultHooks,
|
|
10408
|
-
...options.hooks
|
|
10409
|
-
};
|
|
10410
10465
|
/** `text` shifted by `base`, tokenized: the tokens or the error. */
|
|
10411
10466
|
const attempt = (text, base) => {
|
|
10412
10467
|
const hooks = {
|
|
10413
10468
|
...options.hooks,
|
|
10414
|
-
closeBrace: (input, codeStart, lenientStart) =>
|
|
10469
|
+
closeBrace: (input, codeStart, lenientStart) => {
|
|
10470
|
+
if (base + Math.min(codeStart, lenientStart) > lastBrace) return -1;
|
|
10471
|
+
if (customCloseBrace) return customCloseBrace(input, codeStart, lenientStart);
|
|
10472
|
+
const end = codeEnd(base + lenientStart, base + codeStart, base + text.length);
|
|
10473
|
+
return end < 0 ? -1 : end - base;
|
|
10474
|
+
}
|
|
10415
10475
|
};
|
|
10416
10476
|
try {
|
|
10417
10477
|
return run$1(text, "Tokens", {
|
|
@@ -11155,6 +11215,14 @@ function widgetParameters(params, holds = {}) {
|
|
|
11155
11215
|
...hasOwn(holds, name) ? { holds: holds[name] } : {}
|
|
11156
11216
|
}));
|
|
11157
11217
|
}
|
|
11218
|
+
/** Whether builtin-only interpretation still belongs to this implementation. */
|
|
11219
|
+
function usesBuiltinSemantics(name, parametersOf) {
|
|
11220
|
+
return parametersOf.widget?.(name) === void 0 && parametersOf.source?.(name) !== "user";
|
|
11221
|
+
}
|
|
11222
|
+
/** A raw body is executable only while the builtin implementation owns it. */
|
|
11223
|
+
function executesRawBody(name, parametersOf) {
|
|
11224
|
+
return isRawMacro(name, false) && usesBuiltinSemantics(name, parametersOf);
|
|
11225
|
+
}
|
|
11158
11226
|
/**
|
|
11159
11227
|
* `params` with each `string` and `text` parameter (options too) saying
|
|
11160
11228
|
* what it holds: what it declares, else `holds`.
|
|
@@ -11182,20 +11250,24 @@ function withHolds(params, holds) {
|
|
|
11182
11250
|
function parameterLookup(macros) {
|
|
11183
11251
|
const parameters = new NameMap();
|
|
11184
11252
|
const widgets = new NameMap();
|
|
11185
|
-
|
|
11253
|
+
const sources = new NameMap();
|
|
11254
|
+
for (const { name, parameters: params, interpolate, widget, source } of macros) {
|
|
11255
|
+
sources.set(name, source ?? "user");
|
|
11186
11256
|
if (params) parameters.set(name, withHolds(params, holdsByDefault(interpolate)));
|
|
11257
|
+
else parameters.delete(name);
|
|
11187
11258
|
if (widget) widgets.set(name, widgetParameters(widget.params, widget.holds) ?? []);
|
|
11188
11259
|
}
|
|
11189
11260
|
const lookup = (name) => {
|
|
11190
11261
|
if (widgets.has(name)) return void 0;
|
|
11191
11262
|
let params = parameters.get(name);
|
|
11192
|
-
if (!params) {
|
|
11263
|
+
if (!params && sources.get(name) !== "user") {
|
|
11193
11264
|
const sub = subMacroParameters(name);
|
|
11194
11265
|
if (sub) parameters.set(name, params = withHolds(sub, "text"));
|
|
11195
11266
|
}
|
|
11196
11267
|
return params;
|
|
11197
11268
|
};
|
|
11198
11269
|
if (widgets.size > 0) lookup.widget = (name) => widgets.get(name);
|
|
11270
|
+
lookup.source = (name) => sources.get(name);
|
|
11199
11271
|
return lookup;
|
|
11200
11272
|
}
|
|
11201
11273
|
/** What a `string` or `text` argument holds when its parameter doesn't say. */
|
|
@@ -11261,7 +11333,7 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
11261
11333
|
else if (token.type === "macro" && !token.isClose) {
|
|
11262
11334
|
const name = token.name.toLowerCase();
|
|
11263
11335
|
const widget = parametersOf.widget?.(name);
|
|
11264
|
-
if (name
|
|
11336
|
+
if (executesRawBody(name, parametersOf)) {
|
|
11265
11337
|
const close = closeOfTextBody(t, name);
|
|
11266
11338
|
if (close < 0) continue;
|
|
11267
11339
|
yield {
|
|
@@ -11275,7 +11347,7 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
11275
11347
|
continue;
|
|
11276
11348
|
}
|
|
11277
11349
|
const args = token.rawArgs;
|
|
11278
|
-
if (PASSAGE_BODIES.has(name) &&
|
|
11350
|
+
if (PASSAGE_BODIES.has(name) && usesBuiltinSemantics(name, parametersOf)) {
|
|
11279
11351
|
const close = closeOfTextBody(t, name);
|
|
11280
11352
|
if (close > t + 1) {
|
|
11281
11353
|
const text = src.slice(token.end, tokens[close].start);
|
|
@@ -11293,7 +11365,7 @@ function* codeAndText(src, tokens, parametersOf) {
|
|
|
11293
11365
|
if (!args) continue;
|
|
11294
11366
|
const label = `{${token.name} ${args}}`;
|
|
11295
11367
|
const argsAt = token.argsStart;
|
|
11296
|
-
if (CONDITION_MACROS.has(name) &&
|
|
11368
|
+
if (CONDITION_MACROS.has(name) && usesBuiltinSemantics(name, parametersOf)) {
|
|
11297
11369
|
yield {
|
|
11298
11370
|
kind: "code",
|
|
11299
11371
|
code: args,
|
|
@@ -11653,7 +11725,7 @@ var LITERAL = { kind: "literal" };
|
|
|
11653
11725
|
function completionContext(source, cursor, macros = []) {
|
|
11654
11726
|
if (!Number.isInteger(cursor) || cursor < 0 || cursor > source.length) throw new RangeError("cursor must be a UTF-16 offset within source");
|
|
11655
11727
|
const parametersOf = parameterLookup(macros);
|
|
11656
|
-
const codeContext = (code, goal, map) => {
|
|
11728
|
+
const codeContext = (code, goal, map, containers) => {
|
|
11657
11729
|
const probe = code.length + 1;
|
|
11658
11730
|
let executable = false;
|
|
11659
11731
|
lexJs(code + " __spindle_cursor__", {
|
|
@@ -11673,15 +11745,16 @@ function completionContext(source, cursor, macros = []) {
|
|
|
11673
11745
|
goal,
|
|
11674
11746
|
offset,
|
|
11675
11747
|
cursor: code.length,
|
|
11748
|
+
...containers.length ? { containers } : {},
|
|
11676
11749
|
...offsets.some((at, i) => at !== offset + i) ? { sourceOffsets: offsets } : {}
|
|
11677
11750
|
};
|
|
11678
11751
|
};
|
|
11679
|
-
const read = (src, map, text = false, depth = 0, sigilsOnly = false) => {
|
|
11752
|
+
const read = (src, map, text = false, depth = 0, sigilsOnly = false, containers = []) => {
|
|
11680
11753
|
if (depth >= 64) return LITERAL;
|
|
11681
11754
|
if (sigilsOnly) {
|
|
11682
11755
|
let at = 0;
|
|
11683
11756
|
for (const part of splitSigilTemplate(src, true)) if ("expr" in part) {
|
|
11684
|
-
if (at + 1 + part.expr.length === src.length) return codeContext(part.expr, "expression", (i) => map(at + 1 + i));
|
|
11757
|
+
if (at + 1 + part.expr.length === src.length) return codeContext(part.expr, "expression", (i) => map(at + 1 + i), containers);
|
|
11685
11758
|
at += part.expr.length + 2;
|
|
11686
11759
|
} else at += ("text" in part ? part.text : part.verbatim).length;
|
|
11687
11760
|
return LITERAL;
|
|
@@ -11692,21 +11765,29 @@ function completionContext(source, cursor, macros = []) {
|
|
|
11692
11765
|
});
|
|
11693
11766
|
const token = tokens[tokens.length - 1];
|
|
11694
11767
|
if (!token || token.end !== src.length) return LITERAL;
|
|
11695
|
-
const codeAt = (code, at, goal = "expression") => codeContext(code, goal, (i) => map(at + i));
|
|
11696
|
-
const nested = (value, at, offsets, onlySigils = false) => read(value, (i) => map(offsets?.[i] ?? at + i), true, depth + 1, onlySigils);
|
|
11768
|
+
const codeAt = (code, at, goal = "expression") => codeContext(code, goal, (i) => map(at + i), containers);
|
|
11769
|
+
const nested = (value, at, offsets, onlySigils = false, container) => read(value, (i) => map(offsets?.[i] ?? at + i), true, depth + 1, onlySigils, container ? [...containers, container] : containers);
|
|
11697
11770
|
if (token.type === "variable" && token.nameEnd === src.length) return codeAt(src.slice(token.nameStart - 1), token.nameStart - 1);
|
|
11698
11771
|
if (token.type === "expression" && token.expressionEnd === src.length) return codeAt(token.expression, token.expressionStart);
|
|
11699
11772
|
if (token.type === "html") {
|
|
11700
|
-
for (const { name, value, at } of attributeValues(src, token)) if (at + value.length === src.length)
|
|
11701
|
-
|
|
11773
|
+
for (const { name, value, at } of attributeValues(src, token)) if (at + value.length === src.length) {
|
|
11774
|
+
const span = token.attributeSpans.find((span) => span.valueStart === at);
|
|
11775
|
+
return nested(value, at, void 0, isCodeAttribute(name), {
|
|
11776
|
+
kind: "attribute",
|
|
11777
|
+
name,
|
|
11778
|
+
quote: span?.quote ?? null,
|
|
11779
|
+
code: isCodeAttribute(name)
|
|
11780
|
+
});
|
|
11781
|
+
}
|
|
11782
|
+
} else if (token.type === "link" && token.displayEnd === src.length) return nested(token.display, token.displayStart, void 0, false, { kind: "link-label" });
|
|
11702
11783
|
else if (token.type === "text" && !token.comment) {
|
|
11703
11784
|
const opener = tokens[tokens.length - 2];
|
|
11704
|
-
if (opener?.type === "macro" && !opener.isClose &&
|
|
11785
|
+
if (opener?.type === "macro" && !opener.isClose && executesRawBody(opener.name, parametersOf)) return codeAt(token.value, token.start, "statements");
|
|
11705
11786
|
} else if (token.type === "macro" && !token.isClose && token.argsEnd === src.length) {
|
|
11706
11787
|
const name = token.name.toLowerCase();
|
|
11707
11788
|
const args = token.rawArgs;
|
|
11708
11789
|
const widget = parametersOf.widget?.(name);
|
|
11709
|
-
if (
|
|
11790
|
+
if (usesBuiltinSemantics(name, parametersOf) && [
|
|
11710
11791
|
"if",
|
|
11711
11792
|
"elseif",
|
|
11712
11793
|
"case"
|
|
@@ -11720,9 +11801,14 @@ function completionContext(source, cursor, macros = []) {
|
|
|
11720
11801
|
if (scanStringLiteral(raw, 0).closed) return LITERAL;
|
|
11721
11802
|
const value = escapes === "javascript" ? stringLiteralValue(raw + raw[0]) : quoted.value;
|
|
11722
11803
|
if (value == null) return LITERAL;
|
|
11804
|
+
const container = {
|
|
11805
|
+
kind: "string",
|
|
11806
|
+
quote: raw[0],
|
|
11807
|
+
escapes
|
|
11808
|
+
};
|
|
11723
11809
|
const offsets = escapeOffsets(raw.slice(1), at + 1, value, escapes);
|
|
11724
|
-
if (holds === "expression" || holds === "statements") return codeContext(value, holds, (i) => map(offsets?.[i] ?? at + 1 + i));
|
|
11725
|
-
return holds === "markup" ? nested(value, at + 1, offsets) : LITERAL;
|
|
11810
|
+
if (holds === "expression" || holds === "statements") return codeContext(value, holds, (i) => map(offsets?.[i] ?? at + 1 + i), [...containers, container]);
|
|
11811
|
+
return holds === "markup" ? nested(value, at + 1, offsets, false, container) : LITERAL;
|
|
11726
11812
|
};
|
|
11727
11813
|
if (widget) {
|
|
11728
11814
|
const spans = splitArgSpans(args);
|
|
@@ -11757,6 +11843,7 @@ function completionContext(source, cursor, macros = []) {
|
|
|
11757
11843
|
//#region src/widgets/ast-scanner.ts
|
|
11758
11844
|
/** The declared parameters of the macros registered so far. */
|
|
11759
11845
|
var registeredParameters = (name) => parameterLookup(getMacroRegistry())(name);
|
|
11846
|
+
registeredParameters.source = getMacroParameters.source;
|
|
11760
11847
|
/**
|
|
11761
11848
|
* Whether the text of an attribute value or macro label renders a
|
|
11762
11849
|
* `{@children}`: it is markup, parsed as the renderer parses it.
|
|
@@ -11903,7 +11990,7 @@ function widgetDefinitions$1(passages, parametersOf = registeredParameters) {
|
|
|
11903
11990
|
if (passage.name !== "StoryInit" && !passage.tags?.includes("widget")) continue;
|
|
11904
11991
|
const open = [];
|
|
11905
11992
|
const done = [];
|
|
11906
|
-
for (const token of tokenizeMarkupTolerant(passage.content, { hooks }).tokens) if (token.type === "macro" && token.name.toLowerCase() === "widget") {
|
|
11993
|
+
for (const token of tokenizeMarkupTolerant$1(passage.content, { hooks }).tokens) if (token.type === "macro" && token.name.toLowerCase() === "widget") {
|
|
11907
11994
|
if (!token.isClose) open.push({
|
|
11908
11995
|
token,
|
|
11909
11996
|
isBlock: false
|
|
@@ -12094,7 +12181,7 @@ function validateMarkup(passages, options) {
|
|
|
12094
12181
|
continue;
|
|
12095
12182
|
}
|
|
12096
12183
|
try {
|
|
12097
|
-
const tokens = tokenizeMarkup(passage.content, { hooks });
|
|
12184
|
+
const tokens = tokenizeMarkup$1(passage.content, { hooks });
|
|
12098
12185
|
addWidgets(tokens);
|
|
12099
12186
|
if (!reported) continue;
|
|
12100
12187
|
parseMarkup(passage.content, { hooks });
|
|
@@ -12603,9 +12690,9 @@ function tokensOf(text, textMode, { tolerant, parametersOf } = {}) {
|
|
|
12603
12690
|
text: textMode,
|
|
12604
12691
|
hooks: parametersOf && { isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0) }
|
|
12605
12692
|
};
|
|
12606
|
-
if (tolerant) return tokenizeMarkupTolerant(text, options).tokens;
|
|
12693
|
+
if (tolerant) return tokenizeMarkupTolerant$1(text, options).tokens;
|
|
12607
12694
|
try {
|
|
12608
|
-
return tokenizeMarkup(text, options);
|
|
12695
|
+
return tokenizeMarkup$1(text, options);
|
|
12609
12696
|
} catch (err) {
|
|
12610
12697
|
if (err instanceof MarkupError) return [];
|
|
12611
12698
|
throw err;
|
|
@@ -13031,7 +13118,7 @@ function collectTokenRefs(content, tokens, at, emit, options) {
|
|
|
13031
13118
|
});
|
|
13032
13119
|
} else if (!/^["'`]/.test(first)) scanCode(first, (i) => at(token.argsStart + lead + i), emit);
|
|
13033
13120
|
}
|
|
13034
|
-
if (token.name
|
|
13121
|
+
if (executesRawBody(token.name, parametersOf)) {
|
|
13035
13122
|
let close = t + 1;
|
|
13036
13123
|
while (close < tokens.length) {
|
|
13037
13124
|
const c = tokens[close];
|
|
@@ -13195,6 +13282,11 @@ var Bindings = class {
|
|
|
13195
13282
|
readGroups() {
|
|
13196
13283
|
const { code, groups } = this;
|
|
13197
13284
|
const stack = [];
|
|
13285
|
+
const active = {
|
|
13286
|
+
"{": [],
|
|
13287
|
+
"(": [],
|
|
13288
|
+
"[": []
|
|
13289
|
+
};
|
|
13198
13290
|
for (let i = 0; i < code.length; i++) {
|
|
13199
13291
|
const c = code.charAt(i);
|
|
13200
13292
|
if (c === "{" || c === "(" || c === "[") {
|
|
@@ -13205,15 +13297,16 @@ var Bindings = class {
|
|
|
13205
13297
|
parent: stack[stack.length - 1]
|
|
13206
13298
|
};
|
|
13207
13299
|
groups.push(group);
|
|
13300
|
+
active[c].push(stack.length);
|
|
13208
13301
|
stack.push(group);
|
|
13209
13302
|
this.byOpen.set(i, group);
|
|
13210
13303
|
} else if (CLOSERS[c]) {
|
|
13211
|
-
|
|
13212
|
-
|
|
13213
|
-
if (k
|
|
13304
|
+
const positions = active[CLOSERS[c]];
|
|
13305
|
+
const k = positions[positions.length - 1];
|
|
13306
|
+
if (k !== void 0) {
|
|
13214
13307
|
stack[k].close = i;
|
|
13215
13308
|
this.byClose.set(i, stack[k]);
|
|
13216
|
-
stack.length
|
|
13309
|
+
while (stack.length > k) active[stack.pop().kind].pop();
|
|
13217
13310
|
}
|
|
13218
13311
|
}
|
|
13219
13312
|
}
|
|
@@ -24830,7 +24923,7 @@ function scanSpindleMarkup(text) {
|
|
|
24830
24923
|
if (inAttribute) match.inAttribute = true;
|
|
24831
24924
|
tokens.push(match);
|
|
24832
24925
|
};
|
|
24833
|
-
for (const t of tokenizeMarkupTolerant(text).tokens) if (t.type === "html") {
|
|
24926
|
+
for (const t of tokenizeMarkupTolerant$1(text).tokens) if (t.type === "html") {
|
|
24834
24927
|
tags.push({
|
|
24835
24928
|
start: t.start,
|
|
24836
24929
|
end: t.end
|
|
@@ -24839,7 +24932,7 @@ function scanSpindleMarkup(text) {
|
|
|
24839
24932
|
if (attribute.valueStart === void 0 || attribute.valueEnd === void 0) continue;
|
|
24840
24933
|
const value = text.slice(attribute.valueStart, attribute.valueEnd);
|
|
24841
24934
|
if (!value.includes("{")) continue;
|
|
24842
|
-
for (const inner of tokenizeMarkupTolerant(value, { text: true }).tokens) if (inner.type === "macro" || inner.type === "variable" || inner.type === "expression") push({
|
|
24935
|
+
for (const inner of tokenizeMarkupTolerant$1(value, { text: true }).tokens) if (inner.type === "macro" || inner.type === "variable" || inner.type === "expression") push({
|
|
24843
24936
|
start: attribute.valueStart + inner.start,
|
|
24844
24937
|
end: attribute.valueStart + inner.end
|
|
24845
24938
|
}, true);
|
|
@@ -25038,7 +25131,7 @@ var DEFAULT_DEDENTING = /* @__PURE__ */ new Set([
|
|
|
25038
25131
|
/** The names of the macros the passage closes with `{/name}`: block macros it defines itself. */
|
|
25039
25132
|
function closedMacroNames(text) {
|
|
25040
25133
|
const found = /* @__PURE__ */ new Set();
|
|
25041
|
-
for (const t of tokenizeMarkupTolerant(text).tokens) if (t.type === "macro" && t.isClose) found.add(t.name.toLowerCase());
|
|
25134
|
+
for (const t of tokenizeMarkupTolerant$1(text).tokens) if (t.type === "macro" && t.isClose) found.add(t.name.toLowerCase());
|
|
25042
25135
|
return found;
|
|
25043
25136
|
}
|
|
25044
25137
|
/** The built-in block macros (and the block widgets the runtime has defined) plus those the passage closes. */
|
|
@@ -25211,8 +25304,8 @@ async function formatLf(text, options) {
|
|
|
25211
25304
|
output = output.replace(/\n*$/, "\n");
|
|
25212
25305
|
return replaceAll(output.split("\n").map((line) => literalLines.get(line.trim()) ?? line).join("\n"), sentinel, "\n");
|
|
25213
25306
|
}
|
|
25214
|
-
/** HTML
|
|
25215
|
-
var WHITESPACE_AS_WRITTEN = /<(?:pre|textarea)[\s>]|white-space\s*:/i;
|
|
25307
|
+
/** Literal HTML and opaque SVG, whose text can inherit significant whitespace. */
|
|
25308
|
+
var WHITESPACE_AS_WRITTEN = /<(?:pre|textarea|svg)[\s>]|white-space\s*:/i;
|
|
25216
25309
|
/** Preserve whole literal elements, including openers beside nested tags. */
|
|
25217
25310
|
function whitespaceSensitiveLines(source, isBlock) {
|
|
25218
25311
|
const lines = /* @__PURE__ */ new Set();
|
|
@@ -25222,7 +25315,11 @@ function whitespaceSensitiveLines(source, isBlock) {
|
|
|
25222
25315
|
function visit(nodes) {
|
|
25223
25316
|
for (const node of nodes) {
|
|
25224
25317
|
const { token, body } = node;
|
|
25225
|
-
if (token.type === "html" && ([
|
|
25318
|
+
if (token.type === "html" && ([
|
|
25319
|
+
"pre",
|
|
25320
|
+
"textarea",
|
|
25321
|
+
"svg"
|
|
25322
|
+
].includes(token.tag.toLowerCase()) || Object.entries(token.attributes).some(([name, value]) => name.toLowerCase() === "style" && /white-space\s*:/i.test(value)))) {
|
|
25226
25323
|
const first = lineAt(node.start);
|
|
25227
25324
|
const last = lineAt(node.end - 1);
|
|
25228
25325
|
for (let line = first; line <= last; line++) lines.add(line);
|
|
@@ -25357,7 +25454,7 @@ function macroTokensByLine(lines) {
|
|
|
25357
25454
|
offset += line.length + 1;
|
|
25358
25455
|
}
|
|
25359
25456
|
let line = 0;
|
|
25360
|
-
for (const token of tokenizeMarkupTolerant(lines.join("\n")).tokens) {
|
|
25457
|
+
for (const token of tokenizeMarkupTolerant$1(lines.join("\n")).tokens) {
|
|
25361
25458
|
if (token.type !== "macro") continue;
|
|
25362
25459
|
while (line + 1 < lines.length && starts[line + 1] <= token.start) line++;
|
|
25363
25460
|
const lineStart = starts[line];
|
|
@@ -25450,7 +25547,7 @@ function wordWrap(text, maxLen, indent) {
|
|
|
25450
25547
|
const lines = [];
|
|
25451
25548
|
/** Whether the character is inside a token whose text must stay as written. */
|
|
25452
25549
|
const inToken = new Array(text.length).fill(false);
|
|
25453
|
-
for (const token of tokenizeMarkupTolerant(text).tokens) {
|
|
25550
|
+
for (const token of tokenizeMarkupTolerant$1(text).tokens) {
|
|
25454
25551
|
if (token.type === "text") continue;
|
|
25455
25552
|
inToken.fill(true, token.start, token.end);
|
|
25456
25553
|
}
|
|
@@ -25577,6 +25674,28 @@ var transformCache = /* @__PURE__ */ new Map();
|
|
|
25577
25674
|
* validator, with no Preact or DOM dependencies. pkg/tooling.js re-exports
|
|
25578
25675
|
* them, binding the validator to its macro metadata.
|
|
25579
25676
|
*/
|
|
25677
|
+
function tokenizationOptions(options) {
|
|
25678
|
+
if (!options.macros) return options;
|
|
25679
|
+
const macros = [...options.macros];
|
|
25680
|
+
const parametersOf = parameterLookup(macros);
|
|
25681
|
+
return {
|
|
25682
|
+
...options,
|
|
25683
|
+
hooks: {
|
|
25684
|
+
isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0),
|
|
25685
|
+
isBlock: blockLookup(macros),
|
|
25686
|
+
branchParents: (name) => branchParents(name, macros),
|
|
25687
|
+
...options.hooks
|
|
25688
|
+
}
|
|
25689
|
+
};
|
|
25690
|
+
}
|
|
25691
|
+
/** Flat tokens with optional effective macro/widget metadata. */
|
|
25692
|
+
function tokenizeMarkup(source, options = {}) {
|
|
25693
|
+
return tokenizeMarkup$1(source, tokenizationOptions(options));
|
|
25694
|
+
}
|
|
25695
|
+
/** Metadata-aware flat tokens and recovery errors, in original source offsets. */
|
|
25696
|
+
function tokenizeMarkupTolerant(source, options = {}) {
|
|
25697
|
+
return tokenizeMarkupTolerant$1(source, tokenizationOptions(options));
|
|
25698
|
+
}
|
|
25580
25699
|
/**
|
|
25581
25700
|
* Validate the markup of a story's passages as Spindle does when the story
|
|
25582
25701
|
* starts, against the given macros (built-in and user-defined): malformed
|
|
@@ -25672,10 +25791,12 @@ var NOT_MARKUP_TAGS = ["script", "stylesheet"];
|
|
|
25672
25791
|
*/
|
|
25673
25792
|
function referenceOptions(macros, all = false, passages = []) {
|
|
25674
25793
|
const list = [...macros];
|
|
25794
|
+
const effective = new NameMap();
|
|
25795
|
+
for (const macro of list) effective.set(macro.name, macro);
|
|
25675
25796
|
const widgets = effectiveWidgetDefinitions(widgetDefinitions$1(passages, parameterLookup(list)));
|
|
25676
25797
|
return {
|
|
25677
25798
|
all,
|
|
25678
|
-
storeVarMacros: new Set(
|
|
25799
|
+
storeVarMacros: new Set([...effective.values()].filter((m) => m.storeVar).map((m) => m.name.toLowerCase())),
|
|
25679
25800
|
parametersOf: parameterLookup([...list, ...definedWidgetMacros(widgets)]),
|
|
25680
25801
|
tolerant: true
|
|
25681
25802
|
};
|