@rohal12/spindle 0.59.33 → 0.60.1
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/README.md +1 -0
- package/dist/pkg/format.js +1 -1
- package/dist/pkg/headless.js +99 -49
- package/dist/pkg/story-variables.js +830 -54
- package/dist/pkg/tooling.js +1 -0
- package/package.json +4 -2
- package/src/format/format-passage.ts +626 -0
- package/src/format/placeholders.ts +316 -0
- package/src/format/prettier.ts +62 -0
- package/src/format/segment.ts +126 -0
- package/src/js-lexer.ts +36 -6
- package/src/markup/code-end.ts +97 -41
- package/src/markup/parse.ts +6 -6
- package/src/tooling.ts +23 -1
- package/src/twine-editor/builtin-macros.ts +11 -0
- package/src/twine-editor/commands.ts +173 -0
- package/src/twine-editor/highlight.ts +471 -0
- package/src/twine-editor/icons.ts +35 -0
- package/src/twine-editor/index.ts +19 -0
- package/src/twine-editor/mode.ts +137 -0
- package/src/twine-editor/references.ts +34 -0
- package/src/twine-editor/snippets.ts +74 -0
- package/src/twine-editor/toolbar.ts +85 -0
- package/src/types-drift-check.ts +12 -0
- package/types/tooling.d.ts +68 -0
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SIGIL_SCOPES,
|
|
3
|
+
lexJs,
|
|
4
|
+
passagePiecesOfTokens,
|
|
5
|
+
pieceOffset,
|
|
6
|
+
type HtmlToken,
|
|
7
|
+
type MacroToken,
|
|
8
|
+
type MarkupError,
|
|
9
|
+
type PieceBase,
|
|
10
|
+
type Token,
|
|
11
|
+
type VariableScope,
|
|
12
|
+
} from '../tooling';
|
|
13
|
+
import { scanMarkupTolerant, tokenizeMarkupTolerant } from '../markup/parse';
|
|
14
|
+
|
|
15
|
+
type ScannedMarkup = ReturnType<typeof scanMarkupTolerant>;
|
|
16
|
+
import { jsCodeEnd } from '../markup/code-end';
|
|
17
|
+
import { builtinMacros } from './builtin-macros';
|
|
18
|
+
|
|
19
|
+
/** The CodeMirror 5 token names Twine themes (see the design spec's table). */
|
|
20
|
+
export type CmStyle =
|
|
21
|
+
| 'keyword'
|
|
22
|
+
| 'variable-2'
|
|
23
|
+
| 'variable-3'
|
|
24
|
+
| 'string'
|
|
25
|
+
| 'number'
|
|
26
|
+
| 'atom'
|
|
27
|
+
| 'operator'
|
|
28
|
+
| 'link'
|
|
29
|
+
| 'attribute'
|
|
30
|
+
| 'tag'
|
|
31
|
+
| 'comment'
|
|
32
|
+
| 'error';
|
|
33
|
+
|
|
34
|
+
export interface StyledSpan {
|
|
35
|
+
start: number;
|
|
36
|
+
end: number;
|
|
37
|
+
style: CmStyle;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const SCOPE_STYLES: Record<VariableScope, CmStyle> = {
|
|
41
|
+
variable: 'variable-2',
|
|
42
|
+
transient: 'variable-2',
|
|
43
|
+
temporary: 'variable-3',
|
|
44
|
+
local: 'variable-3',
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const ATOMS = new Set(['true', 'false', 'null']);
|
|
48
|
+
|
|
49
|
+
/** The words, numbers and runs of punctuation of code with literals blanked. */
|
|
50
|
+
const CODE_TOKEN_RE =
|
|
51
|
+
/([\p{L}_$][\p{L}\p{N}_$]*)|((?:\d+\.?\d*|\.\d+)[eE][+-]\d+|(?:\d|\.\d)[\p{L}\p{N}_.]*)|([^\s\p{L}\p{N}_$]+)/gu;
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What is painted on each character of the text. A later paint covers an
|
|
55
|
+
* earlier one, so the innermost construct wins when they are painted
|
|
56
|
+
* outside-in; every paint is one span, even beside an equal style.
|
|
57
|
+
*/
|
|
58
|
+
class Canvas {
|
|
59
|
+
private readonly styles: (CmStyle | undefined)[];
|
|
60
|
+
private readonly ids: number[];
|
|
61
|
+
private nextId = 1;
|
|
62
|
+
|
|
63
|
+
constructor(length: number) {
|
|
64
|
+
this.styles = new Array<CmStyle | undefined>(length).fill(undefined);
|
|
65
|
+
this.ids = new Array<number>(length).fill(0);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Paint [start, end) with `style`; without one, leave it unstyled. */
|
|
69
|
+
paint(start: number, end: number, style?: CmStyle): void {
|
|
70
|
+
const id = this.nextId++;
|
|
71
|
+
const last = Math.min(end, this.styles.length);
|
|
72
|
+
for (let i = Math.max(start, 0); i < last; i++) {
|
|
73
|
+
this.styles[i] = style;
|
|
74
|
+
this.ids[i] = id;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
spans(): StyledSpan[] {
|
|
79
|
+
const spans: StyledSpan[] = [];
|
|
80
|
+
let open: StyledSpan | undefined;
|
|
81
|
+
let openId = 0;
|
|
82
|
+
for (let i = 0; i < this.styles.length; i++) {
|
|
83
|
+
const style = this.styles[i];
|
|
84
|
+
if (style === undefined) {
|
|
85
|
+
open = undefined;
|
|
86
|
+
continue;
|
|
87
|
+
}
|
|
88
|
+
if (open && this.ids[i] === openId) {
|
|
89
|
+
open.end = i + 1;
|
|
90
|
+
continue;
|
|
91
|
+
}
|
|
92
|
+
open = { start: i, end: i + 1, style };
|
|
93
|
+
openId = this.ids[i]!;
|
|
94
|
+
spans.push(open);
|
|
95
|
+
}
|
|
96
|
+
return spans;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The code of a piece: `code` at `offset` (see pieceOffset), read as `goal`. */
|
|
101
|
+
interface CodeRange extends PieceBase {
|
|
102
|
+
code: string;
|
|
103
|
+
goal: 'expression' | 'statements';
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Style the code of `range`: variables, literals, numbers, atoms and
|
|
108
|
+
* operators, unstyled where the code is plain JavaScript words.
|
|
109
|
+
*/
|
|
110
|
+
function paintCode(canvas: Canvas, range: CodeRange): void {
|
|
111
|
+
const { code } = range;
|
|
112
|
+
const at = (i: number) => pieceOffset(range, i);
|
|
113
|
+
canvas.paint(at(0), at(code.length));
|
|
114
|
+
// The code with everything that is not plain code blanked, to read the
|
|
115
|
+
// words and punctuation of what is left
|
|
116
|
+
const plain = new Array<string>(code.length).fill(' ');
|
|
117
|
+
lexJs(
|
|
118
|
+
code,
|
|
119
|
+
{
|
|
120
|
+
code(ch, index) {
|
|
121
|
+
for (let k = 0; k < ch.length; k++) plain[index + k] = ch[k]!;
|
|
122
|
+
},
|
|
123
|
+
literal(text, index) {
|
|
124
|
+
const comment = text.startsWith('/*') || text.startsWith('//');
|
|
125
|
+
canvas.paint(
|
|
126
|
+
at(index),
|
|
127
|
+
at(index + text.length),
|
|
128
|
+
comment ? 'comment' : 'string',
|
|
129
|
+
);
|
|
130
|
+
},
|
|
131
|
+
variable(sigil, _name, index, _nesting, end) {
|
|
132
|
+
canvas.paint(at(index), at(end), SCOPE_STYLES[SIGIL_SCOPES[sigil]]);
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
range.goal,
|
|
136
|
+
);
|
|
137
|
+
for (const m of plain.join('').matchAll(CODE_TOKEN_RE)) {
|
|
138
|
+
const style: CmStyle | undefined = m[1]
|
|
139
|
+
? ATOMS.has(m[1])
|
|
140
|
+
? 'atom'
|
|
141
|
+
: undefined
|
|
142
|
+
: m[2]
|
|
143
|
+
? 'number'
|
|
144
|
+
: 'operator';
|
|
145
|
+
if (style) canvas.paint(at(m.index), at(m.index + m[0].length), style);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Paint a construct written as an opening up to `nameEnd`, what it holds, and
|
|
151
|
+
* a closer (`}`, `>`, `/>`) of `closerLength`: the frame in `style`.
|
|
152
|
+
*/
|
|
153
|
+
function paintFrame(
|
|
154
|
+
canvas: Canvas,
|
|
155
|
+
token: { start: number; end: number },
|
|
156
|
+
nameEnd: number,
|
|
157
|
+
closerLength: number,
|
|
158
|
+
style: CmStyle,
|
|
159
|
+
): void {
|
|
160
|
+
const closer = token.end - closerLength;
|
|
161
|
+
if (nameEnd >= closer) return canvas.paint(token.start, token.end, style);
|
|
162
|
+
canvas.paint(token.start, nameEnd, style);
|
|
163
|
+
canvas.paint(closer, token.end, style);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
function paintHtml(canvas: Canvas, text: string, token: HtmlToken): void {
|
|
167
|
+
const closerLength = text.slice(token.end - 2, token.end) === '/>' ? 2 : 1;
|
|
168
|
+
paintFrame(canvas, token, token.tagNameEnd, closerLength, 'tag');
|
|
169
|
+
for (const attribute of token.attributeSpans) {
|
|
170
|
+
canvas.paint(attribute.nameStart, attribute.nameEnd, 'attribute');
|
|
171
|
+
if (attribute.valueStart === undefined) continue;
|
|
172
|
+
const quoted = attribute.quote ? 1 : 0;
|
|
173
|
+
canvas.paint(
|
|
174
|
+
attribute.valueStart - quoted,
|
|
175
|
+
attribute.valueEnd! + quoted,
|
|
176
|
+
'string',
|
|
177
|
+
);
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function paintToken(canvas: Canvas, text: string, token: Token): void {
|
|
182
|
+
switch (token.type) {
|
|
183
|
+
case 'text':
|
|
184
|
+
if (token.comment) canvas.paint(token.start, token.end, 'comment');
|
|
185
|
+
return;
|
|
186
|
+
case 'link':
|
|
187
|
+
canvas.paint(token.start, token.end, 'link');
|
|
188
|
+
break;
|
|
189
|
+
case 'macro':
|
|
190
|
+
paintFrame(canvas, token, token.nameEnd, 1, 'keyword');
|
|
191
|
+
break;
|
|
192
|
+
case 'variable':
|
|
193
|
+
canvas.paint(
|
|
194
|
+
token.nameStart - 1,
|
|
195
|
+
token.nameEnd,
|
|
196
|
+
SCOPE_STYLES[token.scope],
|
|
197
|
+
);
|
|
198
|
+
break;
|
|
199
|
+
case 'html':
|
|
200
|
+
return paintHtml(canvas, text, token);
|
|
201
|
+
case 'expression':
|
|
202
|
+
break;
|
|
203
|
+
}
|
|
204
|
+
if (token.selectorsStart !== undefined) {
|
|
205
|
+
canvas.paint(token.selectorsStart, token.selectorsEnd!, 'attribute');
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** The string literals among a macro's arguments, which are not all code. */
|
|
210
|
+
function paintArgumentStrings(canvas: Canvas, token: MacroToken): void {
|
|
211
|
+
lexJs(token.rawArgs, {
|
|
212
|
+
literal(text, index) {
|
|
213
|
+
if (!/^["'`]/.test(text)) return;
|
|
214
|
+
canvas.paint(
|
|
215
|
+
token.argsStart + index,
|
|
216
|
+
token.argsStart + index + text.length,
|
|
217
|
+
'string',
|
|
218
|
+
);
|
|
219
|
+
},
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
/** The `{do}` that no `{/do}` follows, if any: its body is code to the end. */
|
|
224
|
+
function openDo(tokens: readonly Token[]): MacroToken | undefined {
|
|
225
|
+
let open: MacroToken | undefined;
|
|
226
|
+
for (const token of tokens) {
|
|
227
|
+
if (token.type !== 'macro' || token.name.toLowerCase() !== 'do') continue;
|
|
228
|
+
open = token.isClose ? undefined : (open ?? token);
|
|
229
|
+
}
|
|
230
|
+
return open;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Elements whose content is code, where `<!--` is not an HTML comment. */
|
|
234
|
+
const CODE_ELEMENTS = new Set(['script', 'style']);
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* The text tokens that are markup, each with the code element (`<script>`
|
|
238
|
+
* or `<style>`) it is in, if any. The text of a `{do}` body is code, and is
|
|
239
|
+
* left out. (Strings and expressions in macros are not text tokens.)
|
|
240
|
+
*/
|
|
241
|
+
function* markupText(
|
|
242
|
+
tokens: readonly Token[],
|
|
243
|
+
): Generator<{ token: Token & { type: 'text' }; element?: string }> {
|
|
244
|
+
let inDo = false;
|
|
245
|
+
let element: string | undefined;
|
|
246
|
+
for (const token of tokens) {
|
|
247
|
+
if (token.type === 'macro' && token.name.toLowerCase() === 'do') {
|
|
248
|
+
inDo = !token.isClose;
|
|
249
|
+
} else if (token.type === 'html') {
|
|
250
|
+
const tag = token.tag.toLowerCase();
|
|
251
|
+
if (token.isClose) {
|
|
252
|
+
if (tag === element) element = undefined;
|
|
253
|
+
} else if (!token.isSelfClose && CODE_ELEMENTS.has(tag)) {
|
|
254
|
+
element ??= tag;
|
|
255
|
+
}
|
|
256
|
+
} else if (token.type === 'text' && !token.comment && !inDo) {
|
|
257
|
+
yield { token, element };
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Where the `<!--` that no `-->` closes starts, or -1. In a `<script>` or
|
|
264
|
+
* `<style>`, `<!--` is code, no comment.
|
|
265
|
+
*/
|
|
266
|
+
function openComment(tokens: readonly Token[]): number {
|
|
267
|
+
for (const { token, element } of markupText(tokens)) {
|
|
268
|
+
const at = element ? -1 : token.value.indexOf('<!--');
|
|
269
|
+
if (at >= 0) return token.start + at;
|
|
270
|
+
}
|
|
271
|
+
return -1;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** `<name` or `</name`, where a tag may start. */
|
|
275
|
+
const TAG_START = /<(\/?)([a-zA-Z][a-zA-Z0-9-]*)/g;
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Whether the text from `at` is a tag that a `>` on the next line would
|
|
279
|
+
* close (on the same line, `<a@b>` would be an email autolink).
|
|
280
|
+
*/
|
|
281
|
+
function closesWithGt(text: string, at: number): boolean {
|
|
282
|
+
const rest = `${text.slice(at)}\n>`;
|
|
283
|
+
const [first] = tokenizeMarkupTolerant(rest).tokens;
|
|
284
|
+
return first?.type === 'html' && first.end === rest.length;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Where a tag starts that is text only for want of its `>`, or -1: `</b`,
|
|
289
|
+
* or `<b_r x=` (no error: `<b` is no tag before `_`), which a `>` on a later
|
|
290
|
+
* line makes a tag. In a code element only its end tag counts.
|
|
291
|
+
*/
|
|
292
|
+
function openTag(text: string, tokens: readonly Token[]): number {
|
|
293
|
+
for (const { token, element } of markupText(tokens)) {
|
|
294
|
+
for (const m of text.slice(token.start, token.end).matchAll(TAG_START)) {
|
|
295
|
+
const at = token.start + m.index;
|
|
296
|
+
if (element && !(m[1] && m[2]!.toLowerCase() === element)) continue;
|
|
297
|
+
if (closesWithGt(text, at)) return at;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return -1;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Whether `error` is a construct left open (an unclosed tag, link, …). */
|
|
304
|
+
function isUnclosed(error: MarkupError): boolean {
|
|
305
|
+
return error.code.startsWith('unclosed-');
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Where each construct left open starts: where the tokenizer's damage
|
|
310
|
+
* starts, which for an error inside a tag (an attribute value, code in
|
|
311
|
+
* one) is the tag. An unclosed attribute value is in a tag that starts
|
|
312
|
+
* before it, even where the tag reads as text without it (`<u:9 ="`).
|
|
313
|
+
*/
|
|
314
|
+
function unclosedStarts(text: string, scan: ScannedMarkup): number[] {
|
|
315
|
+
return scan.errors.flatMap((error, i) => {
|
|
316
|
+
if (!isUnclosed(error)) return [];
|
|
317
|
+
const damage = scan.damage[i]!;
|
|
318
|
+
return error.code === 'unclosed-attribute'
|
|
319
|
+
? [Math.min(damage, Math.max(text.lastIndexOf('<', error.offset), 0))]
|
|
320
|
+
: [damage];
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* The tolerant tokens of `text`, and where code starts whose end may move
|
|
326
|
+
* once text follows (see `jsCodeEnd`).
|
|
327
|
+
*/
|
|
328
|
+
interface Reading extends ScannedMarkup {
|
|
329
|
+
openEnds: number[];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
function read(text: string): Reading {
|
|
333
|
+
const ranOut: [input: string, codeStart: number][] = [];
|
|
334
|
+
const scan = scanMarkupTolerant(text, {
|
|
335
|
+
hooks: jsCodeEnd((input, codeStart) => ranOut.push([input, codeStart])),
|
|
336
|
+
});
|
|
337
|
+
// The tokenizer reads the text from its start and from just past each
|
|
338
|
+
// error's damage: to the end, and up to where the damage starts. Code
|
|
339
|
+
// that runs to the end, or into an unclosed construct, may end elsewhere
|
|
340
|
+
// once a line follows; not so code cut off by any other error.
|
|
341
|
+
const bases = [0, ...scan.damage.map((at) => at + 1)];
|
|
342
|
+
const ends = new Set([
|
|
343
|
+
text.length,
|
|
344
|
+
...scan.damage.filter((_, i) => isUnclosed(scan.errors[i]!)),
|
|
345
|
+
]);
|
|
346
|
+
const openEnds: number[] = [];
|
|
347
|
+
for (const [input, codeStart] of ranOut) {
|
|
348
|
+
const base = bases.find(
|
|
349
|
+
(at) => ends.has(at + input.length) && text.startsWith(input, at),
|
|
350
|
+
);
|
|
351
|
+
if (base !== undefined) openEnds.push(base + codeStart);
|
|
352
|
+
}
|
|
353
|
+
// The tokenizer reads no code after the last `}`, as none can end there:
|
|
354
|
+
// a `{` after it may start code once a `}` follows
|
|
355
|
+
for (let i = text.indexOf('{', text.lastIndexOf('}') + 1); i >= 0;) {
|
|
356
|
+
openEnds.push(i + 1);
|
|
357
|
+
i = text.indexOf('{', i + 1);
|
|
358
|
+
}
|
|
359
|
+
return { ...scan, openEnds };
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function spansOf(text: string, reading: Reading): StyledSpan[] {
|
|
363
|
+
const { tokens, errors } = reading;
|
|
364
|
+
const canvas = new Canvas(text.length);
|
|
365
|
+
const comment = openComment(tokens);
|
|
366
|
+
if (comment >= 0) canvas.paint(comment, text.length, 'comment');
|
|
367
|
+
for (const start of unclosedStarts(text, reading)) {
|
|
368
|
+
canvas.paint(start, text.length, 'error');
|
|
369
|
+
}
|
|
370
|
+
for (const error of errors) {
|
|
371
|
+
if (!isUnclosed(error)) {
|
|
372
|
+
canvas.paint(
|
|
373
|
+
error.offset,
|
|
374
|
+
Math.max(error.end, error.offset + 1),
|
|
375
|
+
'error',
|
|
376
|
+
);
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
for (const token of tokens) {
|
|
380
|
+
if (token.type === 'macro') paintArgumentStrings(canvas, token);
|
|
381
|
+
}
|
|
382
|
+
for (const token of tokens) paintToken(canvas, text, token);
|
|
383
|
+
const body = openDo(tokens);
|
|
384
|
+
for (const piece of passagePiecesOfTokens(text, builtinMacros, tokens)) {
|
|
385
|
+
if (piece.kind === 'code') paintCode(canvas, piece);
|
|
386
|
+
if (piece.kind === 'text') {
|
|
387
|
+
for (const token of piece.tokens) paintToken(canvas, text, token);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
if (body) {
|
|
391
|
+
paintCode(canvas, {
|
|
392
|
+
code: text.slice(body.end),
|
|
393
|
+
goal: 'statements',
|
|
394
|
+
offset: body.end,
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
return canvas.spans();
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
/**
|
|
401
|
+
* Where the construct starts whose code, starting at `at`, may end
|
|
402
|
+
* elsewhere once text follows: the damage of an error the code is in, or
|
|
403
|
+
* the tag holding the code (a raw body starts where its opener ends).
|
|
404
|
+
*/
|
|
405
|
+
function openCodeStart(scan: ScannedMarkup, at: number): number {
|
|
406
|
+
const i = scan.errors.findIndex(
|
|
407
|
+
(e, i) => scan.damage[i]! < at && at <= Math.max(e.offset, e.end),
|
|
408
|
+
);
|
|
409
|
+
if (i >= 0) return scan.damage[i]!;
|
|
410
|
+
const token = scan.tokens.find(
|
|
411
|
+
(t) => t.type !== 'text' && t.start < at && at <= t.end,
|
|
412
|
+
);
|
|
413
|
+
return token ? token.start : -1;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
function openStartOf(text: string, reading: Reading): number {
|
|
417
|
+
const { tokens } = reading;
|
|
418
|
+
const starts = [
|
|
419
|
+
...unclosedStarts(text, reading),
|
|
420
|
+
...reading.openEnds.map((at) => openCodeStart(reading, at)),
|
|
421
|
+
openComment(tokens),
|
|
422
|
+
openDo(tokens)?.start ?? -1,
|
|
423
|
+
openTag(text, tokens),
|
|
424
|
+
].filter((start) => start >= 0);
|
|
425
|
+
return starts.length > 0 ? Math.min(...starts) : -1;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Non-overlapping spans in source order, offsets into `text`; unstyled text
|
|
430
|
+
* has no span. Half-typed markup is styled as far as it reads, and a
|
|
431
|
+
* construct left open is an `error` (an unclosed `<!--` a `comment`).
|
|
432
|
+
*/
|
|
433
|
+
export function highlightSpans(text: string): StyledSpan[] {
|
|
434
|
+
try {
|
|
435
|
+
return spansOf(text, read(text));
|
|
436
|
+
} catch {
|
|
437
|
+
return [];
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Where a construct still open at the end of `text` starts, or -1: an
|
|
443
|
+
* unclosed tag (the tokenizer's `unclosed-*` errors), an unclosed `<!--`, a
|
|
444
|
+
* `{do}` body without `{/do}`, a tag read as text for want of its `>`
|
|
445
|
+
* (`</b`), or code whose end text after `text` could move: the tokenizer
|
|
446
|
+
* ended it leniently, or found none, because its JavaScript runs to the end
|
|
447
|
+
* (a `//` comment, an open string or template literal). The earliest wins.
|
|
448
|
+
*/
|
|
449
|
+
export function openConstructStart(text: string): number {
|
|
450
|
+
try {
|
|
451
|
+
return openStartOf(text, read(text));
|
|
452
|
+
} catch {
|
|
453
|
+
return -1;
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
/** `highlightSpans` and `openConstructStart` of `text`, reading it once. */
|
|
458
|
+
export function highlightWithOpenStart(text: string): {
|
|
459
|
+
spans: StyledSpan[];
|
|
460
|
+
openStart: number;
|
|
461
|
+
} {
|
|
462
|
+
try {
|
|
463
|
+
const reading = read(text);
|
|
464
|
+
return {
|
|
465
|
+
spans: spansOf(text, reading),
|
|
466
|
+
openStart: openStartOf(text, reading),
|
|
467
|
+
};
|
|
468
|
+
} catch {
|
|
469
|
+
return { spans: highlightSpans(text), openStart: openConstructStart(text) };
|
|
470
|
+
}
|
|
471
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export type IconName =
|
|
2
|
+
'links' | 'logic' | 'variables' | 'inputs' | 'ui' | 'check' | 'format';
|
|
3
|
+
|
|
4
|
+
/** The shapes of each 24x24 icon, drawn as outlines. */
|
|
5
|
+
const shapes: Record<IconName, string> = {
|
|
6
|
+
links:
|
|
7
|
+
'<path d="M10 14a4 4 0 0 0 5.7 0l3-3a4 4 0 0 0-5.7-5.7l-1 1"/>' +
|
|
8
|
+
'<path d="M14 10a4 4 0 0 0-5.7 0l-3 3a4 4 0 0 0 5.7 5.7l1-1"/>',
|
|
9
|
+
logic:
|
|
10
|
+
'<path d="M6 3v12"/><circle cx="18" cy="6" r="3"/>' +
|
|
11
|
+
'<circle cx="6" cy="18" r="3"/><path d="M18 9a9 9 0 0 1-9 9"/>',
|
|
12
|
+
variables:
|
|
13
|
+
'<path d="M8 4C5 8 5 16 8 20"/><path d="M16 4c3 4 3 12 0 16"/>' +
|
|
14
|
+
'<path d="M9.5 9.5l5 5M14.5 9.5l-5 5"/>',
|
|
15
|
+
inputs:
|
|
16
|
+
'<rect x="3" y="6" width="18" height="12" rx="2"/><path d="M8 10v4"/>',
|
|
17
|
+
ui: '<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M3 9h18"/>',
|
|
18
|
+
check: '<path d="M5 13l4 4L19 7"/>',
|
|
19
|
+
format: '<path d="M4 6h16M4 10h10M4 14h16M4 18h10"/>',
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
/** `color` as the value of an XML attribute written in double quotes. */
|
|
23
|
+
function attributeValue(color: string): string {
|
|
24
|
+
return color.replace(/[&<>"]/g, (c) => `&#${c.charCodeAt(0)};`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** A toolbar icon: an SVG drawn in `color`, as a `data:` URL. */
|
|
28
|
+
export function icon(name: IconName, color: string): string {
|
|
29
|
+
const paint = attributeValue(color);
|
|
30
|
+
const svg =
|
|
31
|
+
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" ' +
|
|
32
|
+
`fill="none" stroke="${paint}" stroke-width="2" ` +
|
|
33
|
+
`stroke-linecap="round" stroke-linejoin="round">${shapes[name]}</svg>`;
|
|
34
|
+
return `data:image/svg+xml,${encodeURIComponent(svg)}`;
|
|
35
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { mode } from './mode';
|
|
2
|
+
import { toolbar } from './toolbar';
|
|
3
|
+
import { commands } from './commands';
|
|
4
|
+
import { parsePassageText } from './references';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* What the format's `hydrate` hands Twine 2.4+: the passage editor's
|
|
8
|
+
* CodeMirror mode, toolbar and commands, and the passage references Twine
|
|
9
|
+
* draws as connections. Twine ignores `hydrate` when it compiles a story, so
|
|
10
|
+
* none of this reaches a published story.
|
|
11
|
+
*/
|
|
12
|
+
export const editorExtensions = {
|
|
13
|
+
twine: {
|
|
14
|
+
'^2.4.0': {
|
|
15
|
+
codeMirror: { mode, toolbar, commands },
|
|
16
|
+
references: { parsePassageText },
|
|
17
|
+
},
|
|
18
|
+
},
|
|
19
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import type { Mode, StringStream } from 'codemirror';
|
|
2
|
+
import {
|
|
3
|
+
highlightSpans,
|
|
4
|
+
highlightWithOpenStart,
|
|
5
|
+
openConstructStart,
|
|
6
|
+
type StyledSpan,
|
|
7
|
+
} from './highlight';
|
|
8
|
+
|
|
9
|
+
/** How many lines the mode looks ahead for a construct to close. */
|
|
10
|
+
export const LOOK_AHEAD_LINES = 200;
|
|
11
|
+
|
|
12
|
+
export interface ModeState {
|
|
13
|
+
/**
|
|
14
|
+
* The text of a construct still open at the end of the previous line,
|
|
15
|
+
* from its start, ending in a line break; `''` when nothing is open.
|
|
16
|
+
*/
|
|
17
|
+
tail: string;
|
|
18
|
+
/** The spans left to emit on the current line, in window offsets. */
|
|
19
|
+
spans: StyledSpan[];
|
|
20
|
+
/** Where the current line starts in the window its spans came from. */
|
|
21
|
+
lineStart: number;
|
|
22
|
+
/**
|
|
23
|
+
* The open construct did not close within the look-ahead: it is treated
|
|
24
|
+
* as unclosed, `tail` keeps only its first line and lines no longer look
|
|
25
|
+
* ahead until it closes.
|
|
26
|
+
*/
|
|
27
|
+
capped: boolean;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** True when a construct open at the end of `text` started before `end`. */
|
|
31
|
+
function openAcross(text: string, end: number): boolean {
|
|
32
|
+
const start = openConstructStart(text);
|
|
33
|
+
return start >= 0 && start < end;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* `head` (the tail and the line) with as many following lines as it takes
|
|
38
|
+
* to close the construct open at the line's end. If it does not close
|
|
39
|
+
* within the look-ahead, the result is `head` alone and `capped`. Lines are
|
|
40
|
+
* read only as needed: CodeMirror re-reads a line when any line it looked
|
|
41
|
+
* ahead to changes.
|
|
42
|
+
*/
|
|
43
|
+
function lookAhead(
|
|
44
|
+
stream: StringStream,
|
|
45
|
+
head: string,
|
|
46
|
+
): { text: string; capped: boolean } {
|
|
47
|
+
const lines: string[] = [];
|
|
48
|
+
/** Read up to `count` lines ahead; false where the passage ends first. */
|
|
49
|
+
const readAhead = (count: number) => {
|
|
50
|
+
while (lines.length < count) {
|
|
51
|
+
const line = stream.lookAhead(lines.length + 1);
|
|
52
|
+
if (line === undefined) return false;
|
|
53
|
+
lines.push(line);
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
};
|
|
57
|
+
// Grow the window by doubling: the cost stays within twice the final one.
|
|
58
|
+
for (let count = 1; ; count = Math.min(count * 2, LOOK_AHEAD_LINES)) {
|
|
59
|
+
const more = readAhead(count);
|
|
60
|
+
const text = [head, ...lines].join('\n');
|
|
61
|
+
// Closed, or open to the end of the passage: as the whole passage is.
|
|
62
|
+
if (!openAcross(text, head.length) || !more) {
|
|
63
|
+
return { text, capped: false };
|
|
64
|
+
}
|
|
65
|
+
if (count >= LOOK_AHEAD_LINES) return { text: head, capped: true };
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Read the current line's spans and the tail for the next line. */
|
|
70
|
+
function startLine(stream: StringStream, state: ModeState): void {
|
|
71
|
+
const lineStart = state.tail.length;
|
|
72
|
+
const head = state.tail + stream.string;
|
|
73
|
+
const line = highlightWithOpenStart(head);
|
|
74
|
+
const open = line.openStart;
|
|
75
|
+
let spans = line.spans;
|
|
76
|
+
// A capped construct still open: no look-ahead until it closes.
|
|
77
|
+
let capped = state.capped && open === 0;
|
|
78
|
+
if (open >= 0 && !capped) {
|
|
79
|
+
const window = lookAhead(stream, head);
|
|
80
|
+
capped = window.capped;
|
|
81
|
+
if (window.text !== head) spans = highlightSpans(window.text);
|
|
82
|
+
}
|
|
83
|
+
state.spans = spans
|
|
84
|
+
.filter((span) => span.end > lineStart && span.start < head.length)
|
|
85
|
+
.map((span) => ({
|
|
86
|
+
start: Math.max(span.start, lineStart),
|
|
87
|
+
end: Math.min(span.end, head.length),
|
|
88
|
+
style: span.style,
|
|
89
|
+
}));
|
|
90
|
+
state.lineStart = lineStart;
|
|
91
|
+
state.capped = capped;
|
|
92
|
+
const rest = open < 0 ? '' : head.slice(open);
|
|
93
|
+
// A capped tail keeps the construct's first line only, so the cost per
|
|
94
|
+
// line stays flat however long it stays open.
|
|
95
|
+
const kept = capped ? rest.split('\n', 1)[0]! : rest;
|
|
96
|
+
state.tail = open < 0 ? '' : `${kept}\n`;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* The CodeMirror 5 mode for Spindle passages. Each line is styled as
|
|
101
|
+
* `highlightSpans` styles the whole passage, for constructs that close
|
|
102
|
+
* within `LOOK_AHEAD_LINES` lines.
|
|
103
|
+
*/
|
|
104
|
+
export function mode(): Mode<ModeState> {
|
|
105
|
+
return {
|
|
106
|
+
name: 'spindle',
|
|
107
|
+
startState: () => ({ tail: '', spans: [], lineStart: 0, capped: false }),
|
|
108
|
+
copyState: (state) => ({ ...state, spans: [...state.spans] }),
|
|
109
|
+
blankLine: (state) => {
|
|
110
|
+
if (state.tail) state.tail += '\n';
|
|
111
|
+
},
|
|
112
|
+
token: (stream, state) => {
|
|
113
|
+
try {
|
|
114
|
+
if (stream.sol() && state.spans.length === 0) startLine(stream, state);
|
|
115
|
+
const span = state.spans[0];
|
|
116
|
+
if (!span) {
|
|
117
|
+
stream.skipToEnd();
|
|
118
|
+
return null;
|
|
119
|
+
}
|
|
120
|
+
const pos = state.lineStart + stream.pos;
|
|
121
|
+
if (pos < span.start) {
|
|
122
|
+
stream.pos = span.start - state.lineStart;
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
state.spans.shift();
|
|
126
|
+
stream.pos = span.end - state.lineStart;
|
|
127
|
+
return span.style;
|
|
128
|
+
} catch {
|
|
129
|
+
stream.skipToEnd();
|
|
130
|
+
state.spans = [];
|
|
131
|
+
state.tail = '';
|
|
132
|
+
state.capped = false;
|
|
133
|
+
return null;
|
|
134
|
+
}
|
|
135
|
+
},
|
|
136
|
+
};
|
|
137
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { collectStoryPassageReferences } from '../tooling';
|
|
2
|
+
import { tokenizeMarkupTolerant } from '../markup/parse';
|
|
3
|
+
import { builtinMacros } from './builtin-macros';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* The names of the passages `text` names with macros (`{goto}`, `{include}`,
|
|
7
|
+
* `{link}`, `{dialog}`, `{watch}` and the like), once each, in source order:
|
|
8
|
+
* what Twine draws as connections. `[[…]]` links are left out, Twine reads
|
|
9
|
+
* those itself, as are names that are expressions. Half-typed markup is read
|
|
10
|
+
* as far as it goes, and anything that cannot be read gives no references.
|
|
11
|
+
*/
|
|
12
|
+
export function parsePassageText(text: string): string[] {
|
|
13
|
+
try {
|
|
14
|
+
// Both `[[…]]` and `{link}` report `macro: 'link'`; only the link token
|
|
15
|
+
// tells them apart.
|
|
16
|
+
const links = tokenizeMarkupTolerant(text).tokens.filter(
|
|
17
|
+
(token) => token.type === 'link',
|
|
18
|
+
);
|
|
19
|
+
const names = new Set<string>();
|
|
20
|
+
for (const ref of collectStoryPassageReferences(text, builtinMacros)) {
|
|
21
|
+
if (ref.target.kind !== 'name') continue;
|
|
22
|
+
if (
|
|
23
|
+
links.some(
|
|
24
|
+
(link) => ref.start >= link.targetStart && ref.start < link.targetEnd,
|
|
25
|
+
)
|
|
26
|
+
)
|
|
27
|
+
continue;
|
|
28
|
+
names.add(ref.target.name);
|
|
29
|
+
}
|
|
30
|
+
return [...names];
|
|
31
|
+
} catch {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
}
|