@rohal12/spindle 0.33.0 → 0.35.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,282 @@
1
+ [
2
+ {
3
+ "name": "set",
4
+ "block": false,
5
+ "subMacros": [],
6
+ "source": "builtin"
7
+ },
8
+ {
9
+ "name": "print",
10
+ "block": false,
11
+ "subMacros": [],
12
+ "interpolate": true,
13
+ "merged": true,
14
+ "source": "builtin"
15
+ },
16
+ {
17
+ "name": "computed",
18
+ "block": false,
19
+ "subMacros": [],
20
+ "merged": true,
21
+ "source": "builtin"
22
+ },
23
+ {
24
+ "name": "meter",
25
+ "block": false,
26
+ "subMacros": [],
27
+ "interpolate": true,
28
+ "merged": true,
29
+ "source": "builtin"
30
+ },
31
+ {
32
+ "name": "if",
33
+ "block": true,
34
+ "subMacros": [],
35
+ "interpolate": true,
36
+ "merged": true,
37
+ "source": "builtin"
38
+ },
39
+ {
40
+ "name": "for",
41
+ "block": true,
42
+ "subMacros": [],
43
+ "interpolate": true,
44
+ "merged": true,
45
+ "source": "builtin"
46
+ },
47
+ {
48
+ "name": "do",
49
+ "block": true,
50
+ "subMacros": [],
51
+ "source": "builtin"
52
+ },
53
+ {
54
+ "name": "button",
55
+ "block": true,
56
+ "subMacros": [],
57
+ "interpolate": true,
58
+ "source": "builtin"
59
+ },
60
+ {
61
+ "name": "link",
62
+ "block": true,
63
+ "subMacros": [],
64
+ "interpolate": true,
65
+ "source": "builtin"
66
+ },
67
+ {
68
+ "name": "switch",
69
+ "block": true,
70
+ "subMacros": [
71
+ "case",
72
+ "default"
73
+ ],
74
+ "merged": true,
75
+ "source": "builtin"
76
+ },
77
+ {
78
+ "name": "timed",
79
+ "block": true,
80
+ "subMacros": [
81
+ "next"
82
+ ],
83
+ "interpolate": true,
84
+ "source": "builtin"
85
+ },
86
+ {
87
+ "name": "repeat",
88
+ "block": true,
89
+ "subMacros": [],
90
+ "interpolate": true,
91
+ "source": "builtin"
92
+ },
93
+ {
94
+ "name": "stop",
95
+ "block": false,
96
+ "subMacros": [],
97
+ "source": "builtin"
98
+ },
99
+ {
100
+ "name": "type",
101
+ "block": true,
102
+ "subMacros": [],
103
+ "interpolate": true,
104
+ "source": "builtin"
105
+ },
106
+ {
107
+ "name": "widget",
108
+ "block": true,
109
+ "subMacros": [],
110
+ "source": "builtin"
111
+ },
112
+ {
113
+ "name": "story-title",
114
+ "block": false,
115
+ "subMacros": [],
116
+ "source": "builtin"
117
+ },
118
+ {
119
+ "name": "back",
120
+ "block": false,
121
+ "subMacros": [],
122
+ "source": "builtin"
123
+ },
124
+ {
125
+ "name": "forward",
126
+ "block": false,
127
+ "subMacros": [],
128
+ "source": "builtin"
129
+ },
130
+ {
131
+ "name": "quicksave",
132
+ "block": false,
133
+ "subMacros": [],
134
+ "source": "builtin"
135
+ },
136
+ {
137
+ "name": "quickload",
138
+ "block": false,
139
+ "subMacros": [],
140
+ "source": "builtin"
141
+ },
142
+ {
143
+ "name": "restart",
144
+ "block": false,
145
+ "subMacros": [],
146
+ "source": "builtin"
147
+ },
148
+ {
149
+ "name": "saves",
150
+ "block": false,
151
+ "subMacros": [],
152
+ "source": "builtin"
153
+ },
154
+ {
155
+ "name": "settings",
156
+ "block": false,
157
+ "subMacros": [],
158
+ "source": "builtin"
159
+ },
160
+ {
161
+ "name": "dialog",
162
+ "block": true,
163
+ "subMacros": [],
164
+ "interpolate": true,
165
+ "source": "builtin"
166
+ },
167
+ {
168
+ "name": "save-manager",
169
+ "block": false,
170
+ "subMacros": [],
171
+ "source": "builtin"
172
+ },
173
+ {
174
+ "name": "settings-controls",
175
+ "block": false,
176
+ "subMacros": [],
177
+ "source": "builtin"
178
+ },
179
+ {
180
+ "name": "include",
181
+ "block": false,
182
+ "subMacros": [],
183
+ "interpolate": true,
184
+ "merged": true,
185
+ "source": "builtin"
186
+ },
187
+ {
188
+ "name": "nobr",
189
+ "block": true,
190
+ "subMacros": [],
191
+ "source": "builtin"
192
+ },
193
+ {
194
+ "name": "goto",
195
+ "block": false,
196
+ "subMacros": [],
197
+ "merged": true,
198
+ "source": "builtin"
199
+ },
200
+ {
201
+ "name": "unset",
202
+ "block": false,
203
+ "subMacros": [],
204
+ "source": "builtin"
205
+ },
206
+ {
207
+ "name": "textbox",
208
+ "block": false,
209
+ "subMacros": [],
210
+ "storeVar": true,
211
+ "source": "builtin"
212
+ },
213
+ {
214
+ "name": "numberbox",
215
+ "block": false,
216
+ "subMacros": [],
217
+ "storeVar": true,
218
+ "source": "builtin"
219
+ },
220
+ {
221
+ "name": "textarea",
222
+ "block": false,
223
+ "subMacros": [],
224
+ "storeVar": true,
225
+ "source": "builtin"
226
+ },
227
+ {
228
+ "name": "checkbox",
229
+ "block": false,
230
+ "subMacros": [],
231
+ "storeVar": true,
232
+ "source": "builtin"
233
+ },
234
+ {
235
+ "name": "radiobutton",
236
+ "block": false,
237
+ "subMacros": [],
238
+ "storeVar": true,
239
+ "source": "builtin"
240
+ },
241
+ {
242
+ "name": "listbox",
243
+ "block": true,
244
+ "subMacros": [
245
+ "option"
246
+ ],
247
+ "storeVar": true,
248
+ "source": "builtin"
249
+ },
250
+ {
251
+ "name": "cycle",
252
+ "block": true,
253
+ "subMacros": [],
254
+ "storeVar": true,
255
+ "source": "builtin"
256
+ },
257
+ {
258
+ "name": "span",
259
+ "block": true,
260
+ "subMacros": [],
261
+ "source": "builtin"
262
+ },
263
+ {
264
+ "name": "passage",
265
+ "block": false,
266
+ "subMacros": [],
267
+ "interpolate": true,
268
+ "source": "builtin"
269
+ },
270
+ {
271
+ "name": "watch",
272
+ "block": false,
273
+ "subMacros": [],
274
+ "source": "builtin"
275
+ },
276
+ {
277
+ "name": "unwatch",
278
+ "block": false,
279
+ "subMacros": [],
280
+ "source": "builtin"
281
+ }
282
+ ]
@@ -0,0 +1,44 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { dirname, join } from 'node:path';
3
+ import { fileURLToPath } from 'node:url';
4
+
5
+ const __dirname = dirname(fileURLToPath(import.meta.url));
6
+
7
+ // Load built-in macro metadata generated at build time
8
+ const registryPath = join(__dirname, 'macro-registry.json');
9
+ let builtins = [];
10
+ try {
11
+ builtins = JSON.parse(readFileSync(registryPath, 'utf-8'));
12
+ } catch {
13
+ // macro-registry.json not yet built — empty builtins
14
+ }
15
+
16
+ const metadata = new Map();
17
+ for (const m of builtins) metadata.set(m.name.toLowerCase(), m);
18
+
19
+ /**
20
+ * Metadata-only defineMacro for tooling.
21
+ * Captures macro metadata without creating Preact components.
22
+ * LSP servers call this to register user-defined macros discovered in story scripts.
23
+ */
24
+ export function defineMacro(config) {
25
+ const name = config.name.toLowerCase();
26
+ metadata.set(name, {
27
+ name: config.name,
28
+ block: config.block ?? (config.subMacros?.length > 0 ? true : false),
29
+ subMacros: config.subMacros ?? [],
30
+ storeVar: config.storeVar,
31
+ interpolate: config.interpolate,
32
+ merged: config.merged,
33
+ description: config.description,
34
+ parameters: config.parameters,
35
+ source: 'user',
36
+ });
37
+ }
38
+
39
+ /**
40
+ * Return metadata for all registered macros (built-in + user-defined).
41
+ */
42
+ export function getMacroRegistry() {
43
+ return Array.from(metadata.values());
44
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rohal12/spindle",
3
- "version": "0.33.0",
3
+ "version": "0.35.0",
4
4
  "type": "module",
5
5
  "description": "A Preact-based story format for Twine 2.",
6
6
  "license": "Unlicense",
@@ -25,16 +25,22 @@
25
25
  ".": {
26
26
  "types": "./types/index.d.ts",
27
27
  "import": "./dist/pkg/index.js"
28
+ },
29
+ "./tooling": {
30
+ "types": "./types/tooling.d.ts",
31
+ "import": "./dist/pkg/tooling.js"
28
32
  }
29
33
  },
30
34
  "files": [
31
35
  "dist/pkg/index.js",
32
36
  "dist/pkg/format.js",
37
+ "dist/pkg/tooling.js",
38
+ "dist/pkg/macro-registry.json",
33
39
  "types",
34
40
  "src"
35
41
  ],
36
42
  "scripts": {
37
- "build": "vite build && bun run scripts/build-format.ts",
43
+ "build": "vite build && bun run scripts/build-format.ts && bun run scripts/dump-macro-registry.ts",
38
44
  "compile": "bun run scripts/compile-story.ts",
39
45
  "preview": "bun run build && bun run compile",
40
46
  "test": "vitest run",
@@ -10,6 +10,7 @@ import {
10
10
 
11
11
  defineMacro({
12
12
  name: 'button',
13
+ block: true,
13
14
  interpolate: true,
14
15
  render({ rawArgs, children = [] }, ctx) {
15
16
  const label = ctx.resolve?.(rawArgs.replace(/^["']|["']$/g, '')) ?? rawArgs;
@@ -2,6 +2,7 @@ import { defineMacro } from '../../define-macro';
2
2
 
3
3
  defineMacro({
4
4
  name: 'cycle',
5
+ block: true,
5
6
  storeVar: true,
6
7
  render({ children = [] }, ctx) {
7
8
  const options = ctx.extractOptions(children);
@@ -2,6 +2,7 @@ import { defineMacro } from '../../define-macro';
2
2
 
3
3
  defineMacro({
4
4
  name: 'do',
5
+ block: true,
5
6
  render({ children = [] }, ctx) {
6
7
  const code = ctx.collectText(children);
7
8
 
@@ -91,6 +91,7 @@ function ForIteration({
91
91
 
92
92
  defineMacro({
93
93
  name: 'for',
94
+ block: true,
94
95
  interpolate: true,
95
96
  merged: true,
96
97
  render({ rawArgs, children = [] }, ctx) {
@@ -4,6 +4,7 @@ import type { Branch } from '../../markup/ast';
4
4
 
5
5
  defineMacro({
6
6
  name: 'if',
7
+ block: true,
7
8
  interpolate: true,
8
9
  merged: true,
9
10
  render({ branches = [] }, ctx) {
@@ -56,6 +56,7 @@ function renderChildrenDetached(
56
56
 
57
57
  defineMacro({
58
58
  name: 'link',
59
+ block: true,
59
60
  interpolate: true,
60
61
  render({ rawArgs, children = [] }, ctx) {
61
62
  const { display, passage } = parseArgs(rawArgs);
@@ -3,6 +3,7 @@ import { NobrContext } from '../../markup/render';
3
3
 
4
4
  defineMacro({
5
5
  name: 'nobr',
6
+ block: true,
6
7
  render({ children = [] }, ctx) {
7
8
  return ctx.wrap(
8
9
  ctx.h(
@@ -8,6 +8,7 @@ export const RepeatContext = createContext<{ stop: () => void }>({
8
8
 
9
9
  defineMacro({
10
10
  name: 'repeat',
11
+ block: true,
11
12
  interpolate: true,
12
13
  render({ rawArgs, children = [] }, ctx) {
13
14
  const { useState, useEffect, useCallback } = ctx.hooks;
@@ -2,6 +2,7 @@ import { defineMacro } from '../../define-macro';
2
2
 
3
3
  defineMacro({
4
4
  name: 'span',
5
+ block: true,
5
6
  render({ children = [] }, ctx) {
6
7
  return ctx.wrap(ctx.renderNodes(children));
7
8
  },
@@ -3,6 +3,7 @@ import { defineMacro } from '../../define-macro';
3
3
 
4
4
  defineMacro({
5
5
  name: 'type',
6
+ block: true,
6
7
  interpolate: true,
7
8
  render({ rawArgs, children = [] }, ctx) {
8
9
  const { useState, useEffect, useRef } = ctx.hooks;
@@ -10,6 +10,7 @@ function parseWidgetDef(rawArgs: string): { name: string; params: string[] } {
10
10
 
11
11
  defineMacro({
12
12
  name: 'widget',
13
+ block: true,
13
14
  render({ rawArgs, children = [] }, ctx) {
14
15
  const { name, params } = parseWidgetDef(rawArgs);
15
16
 
@@ -24,40 +24,93 @@ interface WidgetInvocationProps {
24
24
  }
25
25
 
26
26
  /**
27
- * Try to split a raw string into adjacent quoted string literals separated by
28
- * whitespace. Returns the individual quoted strings if the entire input is
29
- * consumed, or `null` if any non-quote/non-whitespace content is found.
27
+ * Check whether a whitespace-delimited token looks like a standalone value
28
+ * (not an operator or partial expression).
30
29
  */
31
- function trySplitAdjacentQuotes(raw: string): string[] | null {
30
+ function isStandaloneValue(token: string): boolean {
31
+ const first = token[0]!;
32
+ // Quoted string
33
+ if (first === '"' || first === "'" || first === '`') return true;
34
+ // Variable ($var, _var, @var)
35
+ if (first === '$' || first === '_' || first === '@') return true;
36
+ // Number literal
37
+ if (/\d/.test(first)) return true;
38
+ // Signed number (-1, +2)
39
+ if (
40
+ (first === '-' || first === '+') &&
41
+ token.length > 1 &&
42
+ /\d/.test(token[1]!)
43
+ )
44
+ return true;
45
+ // Grouped expression or collection literal
46
+ if (first === '(' || first === '[' || first === '{') return true;
47
+ // Boolean / null / undefined
48
+ if (/^(true|false|null|undefined)$/.test(token)) return true;
49
+ // Negation (!$flag, !true)
50
+ if (first === '!' && token.length > 1) return true;
51
+ return false;
52
+ }
53
+
54
+ /**
55
+ * Try to split a raw string on whitespace at depth 0 (respecting quotes,
56
+ * parentheses, brackets, and braces). Each resulting token must pass
57
+ * `isStandaloneValue()` or the split is rejected and `null` is returned.
58
+ */
59
+ function trySplitOnWhitespace(raw: string): string[] | null {
32
60
  const args: string[] = [];
33
- let i = 0;
34
-
35
- while (i < raw.length) {
36
- // skip whitespace between quoted strings
37
- while (i < raw.length && /\s/.test(raw[i]!)) i++;
38
- if (i >= raw.length) break;
39
-
40
- const quote = raw[i]!;
41
- if (quote !== '"' && quote !== "'") return null;
42
-
43
- // consume the quoted string
44
- let str = quote;
45
- i++;
46
- while (i < raw.length) {
47
- const ch = raw[i]!;
48
- str += ch;
49
- i++;
50
- if (ch === quote && raw[i - 2] !== '\\') break;
61
+ let current = '';
62
+ let depth = 0;
63
+ let inString: string | null = null;
64
+
65
+ for (let i = 0; i < raw.length; i++) {
66
+ const ch = raw[i]!;
67
+
68
+ if (inString) {
69
+ current += ch;
70
+ if (ch === inString && raw[i - 1] !== '\\') inString = null;
71
+ continue;
72
+ }
73
+
74
+ if (ch === '"' || ch === "'" || ch === '`') {
75
+ inString = ch;
76
+ current += ch;
77
+ continue;
51
78
  }
52
79
 
53
- // must be properly closed
54
- if (str.length < 2 || str[str.length - 1] !== quote) return null;
80
+ if (ch === '(' || ch === '[' || ch === '{') {
81
+ depth++;
82
+ current += ch;
83
+ continue;
84
+ }
85
+
86
+ if (ch === ')' || ch === ']' || ch === '}') {
87
+ depth--;
88
+ current += ch;
89
+ continue;
90
+ }
55
91
 
56
- args.push(str);
92
+ if (/\s/.test(ch) && depth === 0) {
93
+ if (current) {
94
+ args.push(current);
95
+ current = '';
96
+ }
97
+ continue;
98
+ }
99
+
100
+ current += ch;
57
101
  }
58
102
 
59
- // only use this path when there are 2+ adjacent quoted strings
60
- return args.length > 1 ? args : null;
103
+ if (current) args.push(current);
104
+
105
+ // Need 2+ tokens
106
+ if (args.length < 2) return null;
107
+
108
+ // Every token must be a standalone value (not an operator)
109
+ for (const arg of args) {
110
+ if (!isStandaloneValue(arg)) return null;
111
+ }
112
+
113
+ return args;
61
114
  }
62
115
 
63
116
  /**
@@ -113,10 +166,10 @@ export function splitArgs(raw: string): string[] {
113
166
  if (last) args.push(last);
114
167
 
115
168
  // If no commas were found and we got a single expression, try splitting
116
- // it as adjacent quoted string literals (e.g. "Label" "target").
169
+ // on whitespace at depth 0 (e.g. "Label" "target", $var "text", $x $y).
117
170
  if (!hasComma && args.length === 1) {
118
- const quoted = trySplitAdjacentQuotes(args[0]!);
119
- if (quoted) return quoted;
171
+ const split = trySplitOnWhitespace(args[0]!);
172
+ if (split) return split;
120
173
  }
121
174
 
122
175
  return args;
@@ -27,8 +27,12 @@ import type { UseActionOptions } from './hooks/use-action';
27
27
  import { collectText } from './utils/extract-text';
28
28
  import { currentSourceLocation } from './utils/source-location';
29
29
  import { parseVarArgs, extractOptions } from './components/macros/option-utils';
30
- import { registerMacro, registerSubMacro } from './registry';
31
- import type { MacroProps } from './registry';
30
+ import {
31
+ registerMacro,
32
+ registerSubMacro,
33
+ registerMacroMetadata,
34
+ } from './registry';
35
+ import type { MacroProps, ParameterDef } from './registry';
32
36
  import { registerBlockMacro } from './markup/ast';
33
37
 
34
38
  export function macroClass(type: string, className?: string): string {
@@ -81,6 +85,8 @@ export interface MacroDefinition {
81
85
  interpolate?: boolean;
82
86
  merged?: boolean;
83
87
  storeVar?: boolean;
88
+ description?: string;
89
+ parameters?: ParameterDef[];
84
90
  render: (props: MacroProps, ctx: MacroContext) => VNode | null;
85
91
  }
86
92
 
@@ -121,7 +127,10 @@ function setByPath(
121
127
  target[segments[segments.length - 1]!] = value;
122
128
  }
123
129
 
124
- export function defineMacro(config: MacroDefinition): void {
130
+ export function defineMacro(
131
+ config: MacroDefinition,
132
+ source: 'builtin' | 'user' = 'builtin',
133
+ ): void {
125
134
  function Wrapper(props: MacroProps) {
126
135
  // className/id resolved first (interpolate may transform them)
127
136
  let className = props.className;
@@ -196,13 +205,27 @@ export function defineMacro(config: MacroDefinition): void {
196
205
  }
197
206
 
198
207
  registerMacro(config.name, Wrapper);
208
+
209
+ // Store metadata for tooling API
210
+ const isBlock =
211
+ config.block === true ||
212
+ (config.block !== false && (config.subMacros?.length ?? 0) > 0);
213
+ registerMacroMetadata(config.name, {
214
+ name: config.name,
215
+ block: isBlock,
216
+ subMacros: config.subMacros ?? [],
217
+ storeVar: config.storeVar,
218
+ interpolate: config.interpolate,
219
+ merged: config.merged,
220
+ description: config.description,
221
+ parameters: config.parameters,
222
+ source,
223
+ });
224
+
199
225
  if (config.subMacros) {
200
226
  for (const sub of config.subMacros) registerSubMacro(sub);
201
227
  }
202
- if (
203
- config.block === true ||
204
- (config.block !== false && config.subMacros && config.subMacros.length > 0)
205
- ) {
228
+ if (isBlock) {
206
229
  registerBlockMacro(config.name);
207
230
  }
208
231
  }