@rohal12/spindle 0.64.10 → 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 +169 -84
- package/dist/pkg/play.js +2 -1
- package/dist/pkg/story-variables.js +256 -128
- 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/html-integration.tsx +101 -0
- package/src/markup/render.tsx +67 -30
- package/src/play/session.ts +9 -1
- package/src/tooling.ts +63 -19
- package/src/twine-editor/highlight.ts +10 -11
- package/types/tooling.d.ts +42 -0
|
@@ -9,51 +9,6 @@ var __exportAll = (all, no_symbols) => {
|
|
|
9
9
|
if (!no_symbols) __defProp(target, Symbol.toStringTag, { value: "Module" });
|
|
10
10
|
return target;
|
|
11
11
|
};
|
|
12
|
-
// src/utils/macro-names.ts
|
|
13
|
-
/**
|
|
14
|
-
* Macro and widget names are case-insensitive: `{StatusBar}` invokes the
|
|
15
|
-
* widget defined as `statusbar`. A NameMap keys its entries by the lowercased
|
|
16
|
-
* name.
|
|
17
|
-
*/
|
|
18
|
-
var NameMap = class extends Map {
|
|
19
|
-
get(name) {
|
|
20
|
-
return super.get(name.toLowerCase());
|
|
21
|
-
}
|
|
22
|
-
set(name, value) {
|
|
23
|
-
return super.set(name.toLowerCase(), value);
|
|
24
|
-
}
|
|
25
|
-
has(name) {
|
|
26
|
-
return super.has(name.toLowerCase());
|
|
27
|
-
}
|
|
28
|
-
delete(name) {
|
|
29
|
-
return super.delete(name.toLowerCase());
|
|
30
|
-
}
|
|
31
|
-
};
|
|
32
|
-
/** The latest definition of each name, using runtime replacement precedence. */
|
|
33
|
-
function effectiveNamedDefinitions(definitions) {
|
|
34
|
-
const latest = new NameMap();
|
|
35
|
-
for (const definition of definitions) latest.set(definition.name, definition);
|
|
36
|
-
return [...latest.values()];
|
|
37
|
-
}
|
|
38
|
-
/** A set of macro or widget names (see NameMap). */
|
|
39
|
-
var NameSet = class {
|
|
40
|
-
constructor(names = []) {
|
|
41
|
-
this.names = new NameMap();
|
|
42
|
-
for (const name of names) this.add(name);
|
|
43
|
-
}
|
|
44
|
-
add(name) {
|
|
45
|
-
this.names.set(name, true);
|
|
46
|
-
}
|
|
47
|
-
has(name) {
|
|
48
|
-
return this.names.has(name);
|
|
49
|
-
}
|
|
50
|
-
delete(name) {
|
|
51
|
-
this.names.delete(name);
|
|
52
|
-
}
|
|
53
|
-
clear() {
|
|
54
|
-
this.names.clear();
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
12
|
/** Text-only bodies recognize their own end tag, never nested HTML tags. */
|
|
58
13
|
var HTML_TEXT_ELEMENTS$1 = /* @__PURE__ */ new Set([
|
|
59
14
|
.../* @__PURE__ */ new Set([
|
|
@@ -3739,6 +3694,51 @@ function peg$parse(input, options) {
|
|
|
3739
3694
|
if (peg$success) return peg$result;
|
|
3740
3695
|
else peg$throw();
|
|
3741
3696
|
}
|
|
3697
|
+
// src/utils/macro-names.ts
|
|
3698
|
+
/**
|
|
3699
|
+
* Macro and widget names are case-insensitive: `{StatusBar}` invokes the
|
|
3700
|
+
* widget defined as `statusbar`. A NameMap keys its entries by the lowercased
|
|
3701
|
+
* name.
|
|
3702
|
+
*/
|
|
3703
|
+
var NameMap = class extends Map {
|
|
3704
|
+
get(name) {
|
|
3705
|
+
return super.get(name.toLowerCase());
|
|
3706
|
+
}
|
|
3707
|
+
set(name, value) {
|
|
3708
|
+
return super.set(name.toLowerCase(), value);
|
|
3709
|
+
}
|
|
3710
|
+
has(name) {
|
|
3711
|
+
return super.has(name.toLowerCase());
|
|
3712
|
+
}
|
|
3713
|
+
delete(name) {
|
|
3714
|
+
return super.delete(name.toLowerCase());
|
|
3715
|
+
}
|
|
3716
|
+
};
|
|
3717
|
+
/** The latest definition of each name, using runtime replacement precedence. */
|
|
3718
|
+
function effectiveNamedDefinitions(definitions) {
|
|
3719
|
+
const latest = new NameMap();
|
|
3720
|
+
for (const definition of definitions) latest.set(definition.name, definition);
|
|
3721
|
+
return [...latest.values()];
|
|
3722
|
+
}
|
|
3723
|
+
/** A set of macro or widget names (see NameMap). */
|
|
3724
|
+
var NameSet = class {
|
|
3725
|
+
constructor(names = []) {
|
|
3726
|
+
this.names = new NameMap();
|
|
3727
|
+
for (const name of names) this.add(name);
|
|
3728
|
+
}
|
|
3729
|
+
add(name) {
|
|
3730
|
+
this.names.set(name, true);
|
|
3731
|
+
}
|
|
3732
|
+
has(name) {
|
|
3733
|
+
return this.names.has(name);
|
|
3734
|
+
}
|
|
3735
|
+
delete(name) {
|
|
3736
|
+
this.names.delete(name);
|
|
3737
|
+
}
|
|
3738
|
+
clear() {
|
|
3739
|
+
this.names.clear();
|
|
3740
|
+
}
|
|
3741
|
+
};
|
|
3742
3742
|
// src/components/macros/option-clauses.ts
|
|
3743
3743
|
/** The direct value clauses read by the built-in choice controls. */
|
|
3744
3744
|
var OPTION_CLAUSE = "option";
|
|
@@ -9489,9 +9489,7 @@ var SigilParser = class extends Parser {
|
|
|
9489
9489
|
if (name) {
|
|
9490
9490
|
const end = at + 1 + name.length;
|
|
9491
9491
|
const afterPrefix = self.type === types$1.incDec && (this.operandBeforeLast || this.breakBeforeLast);
|
|
9492
|
-
|
|
9493
|
-
if (code === 64 || (forced ?? (self.exprAllowed || afterPrefix || this.lookahead && transientAssignment(self, end)))) {
|
|
9494
|
-
if (code === 37) this.transientsRead?.add(at);
|
|
9492
|
+
if (code === 64 || self.exprAllowed || afterPrefix || this.lookahead && transientAssignment(self, end)) {
|
|
9495
9493
|
self.pos = end;
|
|
9496
9494
|
self.finishToken(types$1.name, self.input.charAt(at) + name);
|
|
9497
9495
|
return;
|
|
@@ -9514,16 +9512,31 @@ var SigilParser = class extends Parser {
|
|
|
9514
9512
|
}
|
|
9515
9513
|
const self = this;
|
|
9516
9514
|
const prevType = self.type;
|
|
9515
|
+
const beforePreviousValue = this.prevValue;
|
|
9517
9516
|
this.prevValue = isMemberDot(this.typeBeforeLast) ? void 0 : self.value;
|
|
9518
9517
|
const property = type.keyword !== void 0 && isMemberDot(prevType) || (type === types$1._function || type === types$1._class) && notABody(self.input, self.pos);
|
|
9519
9518
|
if (!property && (type === types$1._function || type === types$1._class)) {
|
|
9520
|
-
const
|
|
9521
|
-
|
|
9519
|
+
const afterAsync = type === types$1._function && prevType === types$1.name && this.prevValue === "async" && !this.breakHere;
|
|
9520
|
+
const before = afterAsync ? this.typeBeforeLast : prevType;
|
|
9521
|
+
const contextualOperand = before === types$1.name && (afterAsync ? this.operandBeforeLast : self.exprAllowed) && ((afterAsync ? beforePreviousValue : this.prevValue) !== "yield" || !(afterAsync ? this.breakBeforeLast : this.breakHere));
|
|
9522
|
+
self.type = before === types$1.colon && this.colonEndsTernary || contextualOperand ? types$1.parenL : before;
|
|
9522
9523
|
}
|
|
9523
9524
|
this.typeBeforeLast = prevType;
|
|
9524
9525
|
super.finishToken(property ? types$1.name : type, value);
|
|
9525
9526
|
if (type === types$1.name && (isMemberDot(prevType) || value === "of" && (prevType === types$1._const || prevType === types$1._var || this.prevValue === "let"))) self.exprAllowed = false;
|
|
9526
9527
|
}
|
|
9528
|
+
parseIdent(liberal) {
|
|
9529
|
+
this.exprAllowed = false;
|
|
9530
|
+
return super.parseIdent(liberal);
|
|
9531
|
+
}
|
|
9532
|
+
parseForIn(node, init) {
|
|
9533
|
+
this.exprAllowed = true;
|
|
9534
|
+
return super.parseForIn(node, init);
|
|
9535
|
+
}
|
|
9536
|
+
parseAwait(forInit) {
|
|
9537
|
+
this.exprAllowed = true;
|
|
9538
|
+
return super.parseAwait(forInit);
|
|
9539
|
+
}
|
|
9527
9540
|
braceIsBlock(prevType) {
|
|
9528
9541
|
if (prevType === types$1.colon && this.colonEndsTernary) return false;
|
|
9529
9542
|
if (prevType === types$1.braceR && this.closedBlock) return true;
|
|
@@ -9708,29 +9721,17 @@ function parseCode(src, goal = "expression") {
|
|
|
9708
9721
|
}
|
|
9709
9722
|
/** What parseCode finds in `src`, and the syntax tree it is in. */
|
|
9710
9723
|
function parseWithAst(src, goal) {
|
|
9711
|
-
const
|
|
9712
|
-
|
|
9713
|
-
|
|
9714
|
-
|
|
9715
|
-
|
|
9716
|
-
|
|
9717
|
-
|
|
9718
|
-
|
|
9719
|
-
|
|
9720
|
-
|
|
9721
|
-
|
|
9722
|
-
return {
|
|
9723
|
-
out,
|
|
9724
|
-
ast
|
|
9725
|
-
};
|
|
9726
|
-
} catch (error) {
|
|
9727
|
-
const pos = error.pos ?? -1;
|
|
9728
|
-
if (!(error instanceof SyntaxError) || percentAt.has(pos)) throw error;
|
|
9729
|
-
TRANS_NAME_RE.lastIndex = pos + 1;
|
|
9730
|
-
if (src.charAt(pos) !== "%" || !TRANS_NAME_RE.test(src)) throw error;
|
|
9731
|
-
percentAt.set(pos, !transientsRead.has(pos));
|
|
9732
|
-
}
|
|
9733
|
-
}
|
|
9724
|
+
const ast = parseAst(src, goal);
|
|
9725
|
+
const out = {
|
|
9726
|
+
refs: [],
|
|
9727
|
+
strings: []
|
|
9728
|
+
};
|
|
9729
|
+
walk(ast, src, out, false, false);
|
|
9730
|
+
out.refs.sort((a, b) => a.start - b.start);
|
|
9731
|
+
return {
|
|
9732
|
+
out,
|
|
9733
|
+
ast
|
|
9734
|
+
};
|
|
9734
9735
|
}
|
|
9735
9736
|
/**
|
|
9736
9737
|
* Read `src` as a reference path: a variable with any number of `.name` and
|
|
@@ -9788,15 +9789,10 @@ function readMemberPath(src, end) {
|
|
|
9788
9789
|
end
|
|
9789
9790
|
};
|
|
9790
9791
|
}
|
|
9791
|
-
/**
|
|
9792
|
-
|
|
9793
|
-
* noting where it read a transient in `transientsRead`.
|
|
9794
|
-
*/
|
|
9795
|
-
function parseAst(src, goal, percentAt, transientsRead) {
|
|
9792
|
+
/** Parse `src` as `goal`, using the parser's context for `%name`. */
|
|
9793
|
+
function parseAst(src, goal) {
|
|
9796
9794
|
try {
|
|
9797
9795
|
const p = tokenizerAt(src, 0, goal);
|
|
9798
|
-
p.percentAt = percentAt;
|
|
9799
|
-
p.transientsRead = transientsRead;
|
|
9800
9796
|
if (goal === "statements") return p.parse();
|
|
9801
9797
|
p.nextToken();
|
|
9802
9798
|
const ast = p.parseExpression();
|
|
@@ -10009,33 +10005,54 @@ var WORDS_BEFORE_STRING = /* @__PURE__ */ new Set([
|
|
|
10009
10005
|
* what in it is no JavaScript.
|
|
10010
10006
|
*/
|
|
10011
10007
|
function findCodeEnd(src, start, { goal = "expression", stop, onRunOut } = {}) {
|
|
10012
|
-
|
|
10013
|
-
|
|
10008
|
+
let verifyStop = false;
|
|
10009
|
+
const end = scanCodeEnd(src, start, goal, stop, onRunOut, () => {
|
|
10010
|
+
verifyStop = true;
|
|
10011
|
+
});
|
|
10012
|
+
if (end < 0 || verifyStop) {
|
|
10013
|
+
const parsed = parsedCodeEnd(src, start, goal, stop);
|
|
10014
|
+
if (parsed >= 0) return parsed;
|
|
10015
|
+
}
|
|
10016
|
+
return end;
|
|
10014
10017
|
}
|
|
10015
10018
|
/**
|
|
10016
10019
|
* Where the code from `start` ends, as the parser finds it: the `}` it
|
|
10017
|
-
* stops at, or -1. The token scan of
|
|
10018
|
-
* opens a regex from the tokens before
|
|
10019
|
-
*
|
|
10020
|
-
*
|
|
10020
|
+
* stops at (or the `{` matching `stop`), or -1. The token scan of
|
|
10021
|
+
* `findCodeEnd` guesses whether a `/` opens a regex from the tokens before
|
|
10022
|
+
* it, and class fields inside template literals can mislead it. The parser
|
|
10023
|
+
* knows the context. Asked when the scan finds no end or a stop that might
|
|
10024
|
+
* still be template text.
|
|
10021
10025
|
*/
|
|
10022
|
-
function parsedCodeEnd(src, start, goal) {
|
|
10026
|
+
function parsedCodeEnd(src, start, goal, stop) {
|
|
10023
10027
|
const p = tokenizerAt(src, start, goal);
|
|
10028
|
+
const stopped = {};
|
|
10029
|
+
let stoppedAt = -1;
|
|
10030
|
+
if (stop) {
|
|
10031
|
+
const nextToken = p.nextToken;
|
|
10032
|
+
p.nextToken = () => {
|
|
10033
|
+
nextToken.call(p);
|
|
10034
|
+
if (p.type === types$1.braceL && stop(p.start)) {
|
|
10035
|
+
stoppedAt = p.start;
|
|
10036
|
+
throw stopped;
|
|
10037
|
+
}
|
|
10038
|
+
};
|
|
10039
|
+
}
|
|
10024
10040
|
try {
|
|
10025
10041
|
if (goal === "statements") p.parse();
|
|
10026
10042
|
else {
|
|
10027
10043
|
p.nextToken();
|
|
10028
10044
|
p.parseExpression();
|
|
10029
|
-
if (p.type === types$1.braceR) return p.start;
|
|
10045
|
+
if (!stop && p.type === types$1.braceR) return p.start;
|
|
10030
10046
|
}
|
|
10031
10047
|
} catch (error) {
|
|
10048
|
+
if (error === stopped) return stoppedAt;
|
|
10032
10049
|
const pos = error.pos ?? -1;
|
|
10033
|
-
if (src.charAt(pos) === "}") return pos;
|
|
10050
|
+
if (!stop && src.charAt(pos) === "}") return pos;
|
|
10034
10051
|
}
|
|
10035
10052
|
return -1;
|
|
10036
10053
|
}
|
|
10037
10054
|
/** `findCodeEnd` by the tokens: see there. */
|
|
10038
|
-
function scanCodeEnd(src, start, goal, stop, onRunOut) {
|
|
10055
|
+
function scanCodeEnd(src, start, goal, stop, onRunOut, onTemplateStop) {
|
|
10039
10056
|
/** No end: the code runs to the end of `src`. */
|
|
10040
10057
|
const ranOut = () => {
|
|
10041
10058
|
onRunOut?.();
|
|
@@ -10047,6 +10064,7 @@ function scanCodeEnd(src, start, goal, stop, onRunOut) {
|
|
|
10047
10064
|
let from = start;
|
|
10048
10065
|
let afterOperand = false;
|
|
10049
10066
|
let templateSlip = false;
|
|
10067
|
+
let interpolated = false;
|
|
10050
10068
|
for (;;) {
|
|
10051
10069
|
let resumeAt = -1;
|
|
10052
10070
|
for (const tok of tokens(src, from, goal, { afterOperand })) {
|
|
@@ -10075,10 +10093,15 @@ function scanCodeEnd(src, start, goal, stop, onRunOut) {
|
|
|
10075
10093
|
prevWord = t === types$1.name && !WORDS_BEFORE_STRING.has(tok.value) || t === types$1.num;
|
|
10076
10094
|
prevEnd = tok.end;
|
|
10077
10095
|
if (t === types$1.braceL) {
|
|
10078
|
-
if (stop?.(tok.start))
|
|
10096
|
+
if (stop?.(tok.start)) {
|
|
10097
|
+
if (interpolated) onTemplateStop();
|
|
10098
|
+
return tok.start;
|
|
10099
|
+
}
|
|
10079
10100
|
stack.push("{");
|
|
10080
|
-
} else if (t === types$1.dollarBraceL)
|
|
10081
|
-
|
|
10101
|
+
} else if (t === types$1.dollarBraceL) {
|
|
10102
|
+
interpolated = true;
|
|
10103
|
+
stack.push("${");
|
|
10104
|
+
} else if (t === types$1.parenL) stack.push("(");
|
|
10082
10105
|
else if (t === types$1.bracketL) stack.push("[");
|
|
10083
10106
|
else if (t === types$1.parenR || t === types$1.bracketR) {
|
|
10084
10107
|
if (last(stack) === (t === types$1.parenR ? "(" : "[")) stack.pop();
|
|
@@ -10156,7 +10179,8 @@ function* tokens(src, start, goal, options = {}) {
|
|
|
10156
10179
|
type: p.type,
|
|
10157
10180
|
start: p.start,
|
|
10158
10181
|
end: p.end,
|
|
10159
|
-
value: p.value
|
|
10182
|
+
value: p.value,
|
|
10183
|
+
operand: p.operandHere
|
|
10160
10184
|
};
|
|
10161
10185
|
if (p.type === types$1.eof) return;
|
|
10162
10186
|
}
|
|
@@ -10335,6 +10359,59 @@ function scanStringLiteral(src, start) {
|
|
|
10335
10359
|
closed: false
|
|
10336
10360
|
};
|
|
10337
10361
|
}
|
|
10362
|
+
/** Read completion boundaries with the same tokenizer as runtime code. */
|
|
10363
|
+
function completionIdentifier(source, cursor, goal = "expression") {
|
|
10364
|
+
let previous;
|
|
10365
|
+
for (const token of tokens(source, 0, goal)) {
|
|
10366
|
+
if ("error" in token) {
|
|
10367
|
+
if (source[token.pos] === "@" && cursor === token.pos + 1) return {
|
|
10368
|
+
kind: "root",
|
|
10369
|
+
name: "",
|
|
10370
|
+
start: token.pos,
|
|
10371
|
+
end: cursor
|
|
10372
|
+
};
|
|
10373
|
+
return;
|
|
10374
|
+
}
|
|
10375
|
+
const member = previous && isMemberDot(previous.type) ? previous : void 0;
|
|
10376
|
+
if (token.start <= cursor && cursor <= token.end) {
|
|
10377
|
+
if (member && (token.type === types$1.name || token.type.keyword)) return {
|
|
10378
|
+
kind: "member",
|
|
10379
|
+
name: token.value,
|
|
10380
|
+
start: token.start,
|
|
10381
|
+
end: token.end,
|
|
10382
|
+
dotStart: member.start
|
|
10383
|
+
};
|
|
10384
|
+
if (token.type === types$1.name && !member) {
|
|
10385
|
+
const value = token.value;
|
|
10386
|
+
if ((sigilAt(source, token.start, value) || value === "$" || value === "_") && !keyPosition(source, token.end, previous?.type)) return {
|
|
10387
|
+
kind: "root",
|
|
10388
|
+
name: value.slice(1),
|
|
10389
|
+
start: token.start,
|
|
10390
|
+
end: token.end
|
|
10391
|
+
};
|
|
10392
|
+
}
|
|
10393
|
+
if (token.type === types$1.modulo && source.slice(token.start, token.end) === "%" && cursor === token.end && token.operand) return {
|
|
10394
|
+
kind: "root",
|
|
10395
|
+
name: "",
|
|
10396
|
+
start: token.start,
|
|
10397
|
+
end: token.end
|
|
10398
|
+
};
|
|
10399
|
+
}
|
|
10400
|
+
if (isMemberDot(token.type) && token.end === cursor) {
|
|
10401
|
+
previous = token;
|
|
10402
|
+
continue;
|
|
10403
|
+
}
|
|
10404
|
+
if (member && member.end === cursor && token.start >= cursor) return {
|
|
10405
|
+
kind: "member",
|
|
10406
|
+
name: "",
|
|
10407
|
+
start: cursor,
|
|
10408
|
+
end: cursor,
|
|
10409
|
+
dotStart: member.start
|
|
10410
|
+
};
|
|
10411
|
+
if (token.start > cursor || token.type === types$1.eof) return;
|
|
10412
|
+
previous = token;
|
|
10413
|
+
}
|
|
10414
|
+
}
|
|
10338
10415
|
// src/markup/code-end.ts
|
|
10339
10416
|
/**
|
|
10340
10417
|
* A quote directly after a letter/digit is an apostrophe (don't), not a
|
|
@@ -12099,12 +12176,26 @@ var LITERAL = { kind: "literal" };
|
|
|
12099
12176
|
* kind is literal when the cursor itself is inside one, a comment, or prose.
|
|
12100
12177
|
*/
|
|
12101
12178
|
function completionContext(source, cursor, macros = []) {
|
|
12102
|
-
|
|
12179
|
+
return readCompletion(source.slice(0, cursor), cursor, macros, false, source.length);
|
|
12180
|
+
}
|
|
12181
|
+
/** Includes the identifier suffix and its mappings through enclosing markup. */
|
|
12182
|
+
function completionTarget(source, cursor, macros = []) {
|
|
12183
|
+
return readCompletion(source, cursor, macros, true);
|
|
12184
|
+
}
|
|
12185
|
+
/** Visit the markup scopes enclosing a cursor, from outermost to innermost. */
|
|
12186
|
+
function completionScopes(source, cursor, macros, visit) {
|
|
12187
|
+
readCompletion(source, cursor, macros, true, source.length, visit);
|
|
12188
|
+
}
|
|
12189
|
+
function readCompletion(source, cursor, macros, full, sourceLength = source.length, visit) {
|
|
12190
|
+
if (!Number.isInteger(cursor) || cursor < 0 || cursor > sourceLength) throw new RangeError("cursor must be a UTF-16 offset within source");
|
|
12103
12191
|
const parametersOf = parameterLookup(macros);
|
|
12104
12192
|
const codeContext = (code, goal, map, containers) => {
|
|
12105
|
-
|
|
12193
|
+
let position = 0;
|
|
12194
|
+
while (position < code.length && map(position) < cursor) position++;
|
|
12195
|
+
const prefix = code.slice(0, position);
|
|
12196
|
+
const probe = prefix.length + 1;
|
|
12106
12197
|
let executable = false;
|
|
12107
|
-
lexJs(
|
|
12198
|
+
lexJs(prefix + " __spindle_cursor__", {
|
|
12108
12199
|
code(_ch, at) {
|
|
12109
12200
|
if (at >= probe) executable = true;
|
|
12110
12201
|
},
|
|
@@ -12113,6 +12204,7 @@ function completionContext(source, cursor, macros = []) {
|
|
|
12113
12204
|
}
|
|
12114
12205
|
}, goal);
|
|
12115
12206
|
if (!executable) return LITERAL;
|
|
12207
|
+
const identifier = full ? completionIdentifier(code, position, goal) : void 0;
|
|
12116
12208
|
const offset = map(0);
|
|
12117
12209
|
const offsets = Array.from({ length: code.length + 1 }, (_, i) => map(i));
|
|
12118
12210
|
return {
|
|
@@ -12120,7 +12212,8 @@ function completionContext(source, cursor, macros = []) {
|
|
|
12120
12212
|
code,
|
|
12121
12213
|
goal,
|
|
12122
12214
|
offset,
|
|
12123
|
-
cursor:
|
|
12215
|
+
cursor: position,
|
|
12216
|
+
...identifier && { identifier },
|
|
12124
12217
|
...containers.length ? { containers } : {},
|
|
12125
12218
|
...offsets.some((at, i) => at !== offset + i) ? { sourceOffsets: offsets } : {}
|
|
12126
12219
|
};
|
|
@@ -12130,7 +12223,7 @@ function completionContext(source, cursor, macros = []) {
|
|
|
12130
12223
|
if (sigilsOnly) {
|
|
12131
12224
|
let at = 0;
|
|
12132
12225
|
for (const part of splitSigilTemplate(src, true)) if ("expr" in part) {
|
|
12133
|
-
if (at + 1 + part.expr.length
|
|
12226
|
+
if (map(at + 1) <= cursor && cursor <= map(at + 1 + part.expr.length)) return codeContext(part.expr, "expression", (i) => map(at + 1 + i), containers);
|
|
12134
12227
|
at += part.expr.length + 2;
|
|
12135
12228
|
} else at += ("text" in part ? part.text : part.verbatim).length;
|
|
12136
12229
|
return LITERAL;
|
|
@@ -12139,14 +12232,17 @@ function completionContext(source, cursor, macros = []) {
|
|
|
12139
12232
|
text,
|
|
12140
12233
|
hooks: { isRaw: (name) => isRawMacro(name, parametersOf.widget?.(name) !== void 0) }
|
|
12141
12234
|
});
|
|
12142
|
-
|
|
12143
|
-
|
|
12235
|
+
if (visit && !visit(tokens, map)) return LITERAL;
|
|
12236
|
+
const contains = (start, end) => map(start) <= cursor && cursor <= map(end);
|
|
12237
|
+
const index = tokens.findIndex((token) => map(token.start) < cursor && cursor <= map(token.end));
|
|
12238
|
+
const token = tokens[index];
|
|
12239
|
+
if (!token) return LITERAL;
|
|
12144
12240
|
const codeAt = (code, at, goal = "expression") => codeContext(code, goal, (i) => map(at + i), containers);
|
|
12145
12241
|
const nested = (value, at, offsets, onlySigils = false, container) => read(value, (i) => map(offsets?.[i] ?? at + i), true, depth + 1, onlySigils, container ? [...containers, container] : containers);
|
|
12146
|
-
if (token.type === "variable" && token.
|
|
12147
|
-
if (token.type === "expression" && token.
|
|
12242
|
+
if (token.type === "variable" && contains(token.nameStart - 1, token.nameEnd)) return codeAt(src.slice(token.nameStart - 1, token.nameEnd), token.nameStart - 1);
|
|
12243
|
+
if (token.type === "expression" && contains(token.expressionStart, token.expressionEnd)) return codeAt(token.expression, token.expressionStart);
|
|
12148
12244
|
if (token.type === "html") {
|
|
12149
|
-
for (const { name, value, at } of attributeValues(src, token)) if (at + value.length
|
|
12245
|
+
for (const { name, value, at } of attributeValues(src, token)) if (contains(at, at + value.length)) {
|
|
12150
12246
|
const span = token.attributeSpans.find((span) => span.valueStart === at);
|
|
12151
12247
|
return nested(value, at, void 0, isCodeAttribute(name), {
|
|
12152
12248
|
kind: "attribute",
|
|
@@ -12155,11 +12251,11 @@ function completionContext(source, cursor, macros = []) {
|
|
|
12155
12251
|
code: isCodeAttribute(name)
|
|
12156
12252
|
});
|
|
12157
12253
|
}
|
|
12158
|
-
} else if (token.type === "link" && token.
|
|
12254
|
+
} else if (token.type === "link" && contains(token.displayStart, token.displayEnd)) return nested(token.display, token.displayStart, void 0, false, { kind: "link-label" });
|
|
12159
12255
|
else if (token.type === "text" && !token.comment) {
|
|
12160
|
-
const opener = tokens[
|
|
12256
|
+
const opener = tokens[index - 1];
|
|
12161
12257
|
if (opener?.type === "macro" && !opener.isClose && executesRawBody(opener.name, parametersOf)) return codeAt(token.value, token.start, "statements");
|
|
12162
|
-
} else if (token.type === "macro" && !token.isClose && token.
|
|
12258
|
+
} else if (token.type === "macro" && !token.isClose && contains(token.argsStart, token.argsEnd)) {
|
|
12163
12259
|
const name = token.name.toLowerCase();
|
|
12164
12260
|
const args = token.rawArgs;
|
|
12165
12261
|
const widget = parametersOf.widget?.(name);
|
|
@@ -12172,29 +12268,32 @@ function completionContext(source, cursor, macros = []) {
|
|
|
12172
12268
|
if (holds === "expression" || holds === "statements") return codeAt(raw, at, holds);
|
|
12173
12269
|
return holds === "markup" ? nested(raw, at) : LITERAL;
|
|
12174
12270
|
}
|
|
12175
|
-
|
|
12176
|
-
const
|
|
12271
|
+
const scan = scanStringLiteral(raw, 0);
|
|
12272
|
+
const bodyEnd = scan.closed ? scan.end - 1 : scan.end;
|
|
12273
|
+
if (!contains(at + 1, at + bodyEnd)) return LITERAL;
|
|
12274
|
+
const value = escapes === "javascript" ? stringLiteralValue(raw.slice(0, scan.end) + (scan.closed ? "" : raw[0])) : quoted.value;
|
|
12177
12275
|
if (value == null) return LITERAL;
|
|
12178
12276
|
const container = {
|
|
12179
12277
|
kind: "string",
|
|
12180
12278
|
quote: raw[0],
|
|
12181
12279
|
escapes
|
|
12182
12280
|
};
|
|
12183
|
-
const offsets = escapeOffsets(raw.slice(1), at + 1, value, escapes);
|
|
12281
|
+
const offsets = escapeOffsets(raw.slice(1, bodyEnd), at + 1, value, escapes);
|
|
12184
12282
|
if (holds === "expression" || holds === "statements") return codeContext(value, holds, (i) => map(offsets?.[i] ?? at + 1 + i), [...containers, container]);
|
|
12185
12283
|
return holds === "markup" ? nested(value, at + 1, offsets, false, container) : LITERAL;
|
|
12186
12284
|
};
|
|
12187
12285
|
if (widget) {
|
|
12188
12286
|
const spans = splitArgSpans(args);
|
|
12189
|
-
const
|
|
12287
|
+
const which = spans.findIndex(([start, end]) => contains(token.argsStart + start, token.argsStart + end));
|
|
12288
|
+
const span = spans[which];
|
|
12190
12289
|
if (!span) return LITERAL;
|
|
12191
|
-
const raw = args.slice(span
|
|
12192
|
-
const holds = widget[
|
|
12290
|
+
const raw = args.slice(...span);
|
|
12291
|
+
const holds = widget[which]?.holds;
|
|
12193
12292
|
return /^["']/.test(raw) && holds ? stringContext(raw, token.argsStart + span[0], holds, "javascript") : codeAt(raw, token.argsStart + span[0]);
|
|
12194
12293
|
}
|
|
12195
12294
|
const params = parametersOf(name);
|
|
12196
12295
|
if (!params) return LITERAL;
|
|
12197
|
-
const spans = macroArgumentSpans(args, params).filter((span) => span.parameter.type !== "options" && span.
|
|
12296
|
+
const spans = macroArgumentSpans(args, params).filter((span) => span.parameter.type !== "options" && contains(token.argsStart + span.start, token.argsStart + span.end));
|
|
12198
12297
|
const span = spans[spans.length - 1];
|
|
12199
12298
|
if (!span) return LITERAL;
|
|
12200
12299
|
const param = span.parameter;
|
|
@@ -12203,7 +12302,7 @@ function completionContext(source, cursor, macros = []) {
|
|
|
12203
12302
|
at += param.name.length;
|
|
12204
12303
|
while (/\s/.test(args.charAt(at))) at++;
|
|
12205
12304
|
}
|
|
12206
|
-
const raw = args.slice(at);
|
|
12305
|
+
const raw = args.slice(at, span.end);
|
|
12207
12306
|
const offset = token.argsStart + at;
|
|
12208
12307
|
if (param.type === "expression" || param.type === "passage") return codeAt(raw, offset);
|
|
12209
12308
|
if (param.type === "statements") return codeAt(raw, offset, "statements");
|
|
@@ -12211,7 +12310,7 @@ function completionContext(source, cursor, macros = []) {
|
|
|
12211
12310
|
}
|
|
12212
12311
|
return LITERAL;
|
|
12213
12312
|
};
|
|
12214
|
-
return read(source
|
|
12313
|
+
return read(source, (i) => i);
|
|
12215
12314
|
}
|
|
12216
12315
|
// src/widgets/ast-scanner.ts
|
|
12217
12316
|
/** The declared parameters of the macros registered so far. */
|
|
@@ -26275,7 +26374,6 @@ function blockLookup(macros, includeRuntime = true) {
|
|
|
26275
26374
|
*/
|
|
26276
26375
|
function macroCalls(source, macros = []) {
|
|
26277
26376
|
const all = effectiveToolingMacros(macros);
|
|
26278
|
-
const definitions = new NameMap(all.map((macro) => [macro.name, macro]));
|
|
26279
26377
|
const parametersOf = parameterLookup(all);
|
|
26280
26378
|
const options = tokenizationOptions({
|
|
26281
26379
|
macros: all,
|
|
@@ -26284,7 +26382,31 @@ function macroCalls(source, macros = []) {
|
|
|
26284
26382
|
const hooks = options.hooks;
|
|
26285
26383
|
const tokens = tokenizeMarkupTolerant$1(source, options).tokens;
|
|
26286
26384
|
const calls = [];
|
|
26385
|
+
const classifier = macroClassifier(all);
|
|
26287
26386
|
const classify = (tokens) => {
|
|
26387
|
+
for (const call of classifier(tokens)) calls.push(call);
|
|
26388
|
+
};
|
|
26389
|
+
classify(tokens);
|
|
26390
|
+
for (const piece of passagePiecesOf(source, tokens, parametersOf, { isBlock: hooks.isBlock })) {
|
|
26391
|
+
if (piece.kind !== "text" || !piece.tokens.length) continue;
|
|
26392
|
+
classify(piece.tokens);
|
|
26393
|
+
}
|
|
26394
|
+
let literalEnd = -1;
|
|
26395
|
+
return calls.sort((a, b) => a.token.start - b.token.start).filter((call) => {
|
|
26396
|
+
if (call.token.start < literalEnd) return false;
|
|
26397
|
+
if (call.kind === "value" || call.kind === "branch") literalEnd = call.token.argsEnd;
|
|
26398
|
+
return true;
|
|
26399
|
+
});
|
|
26400
|
+
}
|
|
26401
|
+
/** Shared contextual dispatch for complete and unfinished grammar tokens. */
|
|
26402
|
+
function macroClassifier(all) {
|
|
26403
|
+
const definitions = new NameMap(all.map((macro) => [macro.name, macro]));
|
|
26404
|
+
const hooks = tokenizationOptions({
|
|
26405
|
+
macros: all,
|
|
26406
|
+
hooks: { isBlock: blockLookup(all, false) }
|
|
26407
|
+
}).hooks;
|
|
26408
|
+
return (tokens) => {
|
|
26409
|
+
const calls = [];
|
|
26288
26410
|
const found = /* @__PURE__ */ new Map();
|
|
26289
26411
|
const pending = pairMarkup(tokens, hooks).nodes.map((node) => ({ node }));
|
|
26290
26412
|
while (pending.length) {
|
|
@@ -26325,18 +26447,24 @@ function macroCalls(source, macros = []) {
|
|
|
26325
26447
|
};
|
|
26326
26448
|
calls.push(call);
|
|
26327
26449
|
}
|
|
26450
|
+
return calls;
|
|
26328
26451
|
};
|
|
26329
|
-
|
|
26330
|
-
|
|
26331
|
-
|
|
26332
|
-
|
|
26333
|
-
|
|
26334
|
-
let
|
|
26335
|
-
|
|
26336
|
-
|
|
26337
|
-
if (call
|
|
26338
|
-
|
|
26452
|
+
}
|
|
26453
|
+
/** The opening macro head containing a cursor, including unfinished heads. */
|
|
26454
|
+
function macroCallAt(source, cursor, macros = []) {
|
|
26455
|
+
const all = effectiveToolingMacros(macros);
|
|
26456
|
+
const classify = macroClassifier(all);
|
|
26457
|
+
let selected;
|
|
26458
|
+
completionScopes(source, cursor, all, (tokens, map) => {
|
|
26459
|
+
const call = classify(tokens).find(({ token }) => map(token.start) < cursor && cursor <= map(token.end));
|
|
26460
|
+
if (call) selected = {
|
|
26461
|
+
...call,
|
|
26462
|
+
token: mapOffsets(call.token, map),
|
|
26463
|
+
...call.owner && { owner: mapOffsets(call.owner, map) }
|
|
26464
|
+
};
|
|
26465
|
+
return !call || call.kind === "invocation";
|
|
26339
26466
|
});
|
|
26467
|
+
return selected;
|
|
26340
26468
|
}
|
|
26341
26469
|
/**
|
|
26342
26470
|
* What a passage's markup runs and names, and where: the pieces of code
|
|
@@ -26434,4 +26562,4 @@ function validateVariableReferences(passages, declarations, macros = []) {
|
|
|
26434
26562
|
const list = [...passages];
|
|
26435
26563
|
return checkVariableReferences(list.filter((p) => !p.tags?.some((t) => NOT_MARKUP_TAGS.includes(t))), declarations, referenceOptions(macros, false, list));
|
|
26436
26564
|
}
|
|
26437
|
-
export { MarkupError, SIGIL_SCOPES, checkParameterTypes, collectStoryPassageReferences, completionContext, discoverMacros, effectiveWidgetDefinitions, endsWithOperator, evaluatePassageName, findCodeEnd, formatDiagnostic, formatPassage, isBlockMacro, isSigil, isSpecialPassage, lexJs, lexTemplate, macroArgumentSpans, macroCalls, pairMarkup, parseBoundVariable, parseCode, parseDeclarations, parseSelectors, parseStoryVariables, parseWidgetDef, passagePieces, passagePiecesOfTokens, passageTarget, pieceOffset, readQuoted, scanStringLiteral, splitArgs, splitIncludeFlag, splitTopLevel, stripLooseQuotes, tokenizeMarkup, tokenizeMarkupTolerant, transform, unescapeQuoted, validateStoryMarkup, validateVariableReferences, variableReferences, widgetDefinitions };
|
|
26565
|
+
export { MarkupError, SIGIL_SCOPES, checkParameterTypes, collectStoryPassageReferences, completionContext, completionTarget, discoverMacros, effectiveWidgetDefinitions, endsWithOperator, evaluatePassageName, findCodeEnd, formatDiagnostic, formatPassage, isBlockMacro, isSigil, isSpecialPassage, lexJs, lexTemplate, macroArgumentSpans, macroCallAt, macroCalls, pairMarkup, parseBoundVariable, parseCode, parseDeclarations, parseSelectors, parseStoryVariables, parseWidgetDef, passagePieces, passagePiecesOfTokens, passageTarget, pieceOffset, readQuoted, scanStringLiteral, splitArgs, splitIncludeFlag, splitTopLevel, stripLooseQuotes, tokenizeMarkup, tokenizeMarkupTolerant, transform, unescapeQuoted, validateStoryMarkup, validateVariableReferences, variableReferences, widgetDefinitions };
|
package/dist/pkg/tooling.js
CHANGED