prettier-plugin-wolfram 0.7.1 → 0.7.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prettier-plugin-wolfram",
3
- "version": "0.7.1",
3
+ "version": "0.7.3",
4
4
  "description": "Prettier plugin for Wolfram Language using tree-sitter",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -17,6 +17,9 @@
17
17
  ],
18
18
  "type": "module",
19
19
  "main": "src/index.js",
20
+ "exports": {
21
+ ".": "./src/index.js"
22
+ },
20
23
  "bin": {
21
24
  "prettier-wolfram": "bin/prettier-wolfram.js"
22
25
  },
@@ -7,12 +7,18 @@ const GROUP_KIND = { "{": "List", "(": "GroupParen", "[": "Group", "<|": "Associ
7
7
  const GROUP_OPEN_LEAF = { "{": "Token`OpenCurly", "(": "Token`OpenParen", "[": "Token`OpenSquare", "<|": "Token`LessBar" };
8
8
  const GROUP_CLOSE_LEAF = { "}": "Token`CloseCurly", ")": "Token`CloseParen", "]": "Token`CloseSquare", "|>": "Token`BarGreater" };
9
9
 
10
- export function adapt(tree, source) {
11
- const lineIndex = makeLineIndex(source);
12
- const ctx = { source, lineIndex };
10
+ // preprocessedSource is the version passed to tree-sitter (may have ⁢ for InvisibleTimes);
11
+ // source is the original — used only for the unformattable fallback.
12
+ export function adapt(tree, source, preprocessedSource) {
13
+ const ps = preprocessedSource ?? source;
14
+ const lineIndex = makeLineIndex(ps);
15
+ const ctx = { source: ps, lineIndex };
13
16
  const root = tree.rootNode;
14
17
  if (subtreeHasError(root)) {
15
- const src = nodeSource(root, lineIndex);
18
+ // Use original source positions for the error fallback so the printer can
19
+ // emit the unmodified source text verbatim.
20
+ const errLineIndex = makeLineIndex(source);
21
+ const src = nodeSource(root, errLineIndex);
16
22
  return { type: "ContainerNode", kind: "String", children: [{ type: "Unknown", kind: "SyntaxErrorNode[]", source: src }], source: src };
17
23
  }
18
24
  // Hoist top-level semicolon chains that end with a trailing ";" (MISSING rhs) into separate
@@ -289,17 +295,34 @@ function delimLeaf(node, kind, value, ctx) {
289
295
  return { type: "LeafNode", kind, value, source: nodeSource(node, ctx.lineIndex) };
290
296
  }
291
297
 
292
- // Produce trivia (whitespace/newline) LeafNodes for the gap between two character offsets.
293
- // Splits on newlines: each run of spaces produces Token`Whitespace, each "\n" produces Token`Newline.
298
+ // Produce trivia LeafNodes for the gap between two character offsets.
299
+ // Splits on newlines and (*...*) comments (which may appear between tokens in right-associative
300
+ // binary nodes like :=). Each whitespace run → Token`Whitespace, each "\n" → Token`Newline,
301
+ // each (*...*) comment → Token`Comment.
294
302
  function triviaLeaves(fromIdx, toIdx, ctx) {
295
303
  if (fromIdx >= toIdx) return [];
296
304
  const gap = ctx.source.slice(fromIdx, toIdx);
297
- if (!/[\s]/.test(gap)) return [];
305
+ if (gap.length === 0) return [];
298
306
  const leaves = [];
299
307
  let i = 0;
300
308
  while (i < gap.length) {
301
309
  const ch = gap[i];
302
- if (ch === "\n") {
310
+ // Nested WL comment (*...*)
311
+ if (ch === "(" && gap[i + 1] === "*") {
312
+ const start = i;
313
+ i += 2;
314
+ let depth = 1;
315
+ while (i < gap.length && depth > 0) {
316
+ if (gap[i] === "(" && gap[i + 1] === "*") { depth++; i += 2; }
317
+ else if (gap[i] === "*" && gap[i + 1] === ")") { depth--; i += 2; }
318
+ else i++;
319
+ }
320
+ const commentText = gap.slice(start, i);
321
+ const startChar = fromIdx + start;
322
+ const endChar = fromIdx + i;
323
+ const src = [offsetToLineCol(ctx.lineIndex, startChar), offsetToLineCol(ctx.lineIndex, endChar)];
324
+ leaves.push({ type: "LeafNode", kind: "Token`Comment", value: commentText, source: src });
325
+ } else if (ch === "\n") {
303
326
  const endChar = fromIdx + i + 1;
304
327
  const src = [offsetToLineCol(ctx.lineIndex, fromIdx + i), offsetToLineCol(ctx.lineIndex, endChar)];
305
328
  leaves.push({ type: "LeafNode", kind: "Token`Newline", value: "\n", source: src });
@@ -311,13 +334,19 @@ function triviaLeaves(fromIdx, toIdx, ctx) {
311
334
  leaves.push({ type: "LeafNode", kind: "Token`Newline", value: nl, source: src });
312
335
  i += nl.length;
313
336
  } else {
314
- // Collect run of whitespace (non-newline)
337
+ // Collect run of non-comment, non-newline chars
315
338
  let j = i;
316
- while (j < gap.length && gap[j] !== "\n" && gap[j] !== "\r") j++;
339
+ while (j < gap.length && gap[j] !== "\n" && gap[j] !== "\r" && !(gap[j] === "(" && gap[j + 1] === "*")) j++;
340
+ if (j === i) { i++; continue; } // safety: skip single unknown char
317
341
  const ws = gap.slice(i, j);
318
342
  const startChar = fromIdx + i;
319
343
  const endChar = fromIdx + j;
320
344
  const src = [offsetToLineCol(ctx.lineIndex, startChar), offsetToLineCol(ctx.lineIndex, endChar)];
345
+ if (/\S/.test(ws)) {
346
+ // Non-whitespace that isn't a comment — shouldn't happen in valid WL, skip it
347
+ i = j;
348
+ continue;
349
+ }
321
350
  leaves.push({ type: "LeafNode", kind: "Token`Whitespace", value: ws, source: src });
322
351
  i = j;
323
352
  }
@@ -361,6 +390,8 @@ const TOKEN_KIND_NAME = {
361
390
  // additional infix operators missing from original table
362
391
  "**": "StarStar", "|": "Bar", "||": "BarBar", "&&": "AmpAmp",
363
392
  "@*": "AtStar", "/*": "SlashStar",
393
+ // InvisibleTimes: WL space-multiplication, encoded as U+2062 during preprocessing
394
+ "⁢": "InvisibleTimes",
364
395
  };
365
396
 
366
397
  const INEQUALITY_OPS = new Set(["<", "<=", ">", ">=", "==", "!=", "===", "=!="]);
@@ -18,12 +18,64 @@ async function getLanguage() {
18
18
  return _langPromise;
19
19
  }
20
20
 
21
+ // Replace space-based implicit multiplication (a b) with U+2062 (InvisibleTimes)
22
+ // so the grammar can parse it. Skip content inside strings and nested comments.
23
+ export function preprocessInvisibleTimes(src) {
24
+ let result = "";
25
+ let i = 0;
26
+ const n = src.length;
27
+ while (i < n) {
28
+ // Skip quoted string
29
+ if (src[i] === '"') {
30
+ const start = i++;
31
+ while (i < n && src[i] !== '"') {
32
+ if (src[i] === "\\") i++;
33
+ i++;
34
+ }
35
+ if (i < n) i++;
36
+ result += src.slice(start, i);
37
+ continue;
38
+ }
39
+ // Skip nested WL comment (* ... *)
40
+ if (src[i] === "(" && src[i + 1] === "*") {
41
+ const start = i;
42
+ i += 2;
43
+ let depth = 1;
44
+ while (i < n && depth > 0) {
45
+ if (src[i] === "(" && src[i + 1] === "*") { depth++; i += 2; }
46
+ else if (src[i] === "*" && src[i + 1] === ")") { depth--; i += 2; }
47
+ else i++;
48
+ }
49
+ result += src.slice(start, i);
50
+ continue;
51
+ }
52
+ // Two or more spaces between word chars on same line → InvisibleTimes
53
+ if (src[i] === " " && src[i + 1] === " ") {
54
+ // Check previous meaningful char is a word char
55
+ const prevChar = result.length > 0 ? result[result.length - 1] : "";
56
+ if (/\w/.test(prevChar)) {
57
+ // Consume all spaces and peek at next non-space char
58
+ let j = i;
59
+ while (j < n && src[j] === " ") j++;
60
+ if (j < n && /\w/.test(src[j])) {
61
+ result += "⁢"; // InvisibleTimes, spaces stripped (they're extras)
62
+ i = j;
63
+ continue;
64
+ }
65
+ }
66
+ }
67
+ result += src[i++];
68
+ }
69
+ return result;
70
+ }
71
+
21
72
  export class WolframParser {
22
73
  async getCST(sourceText) {
23
74
  const lang = await getLanguage();
24
75
  const parser = new Parser();
25
76
  parser.setLanguage(lang);
26
- const tree = parser.parse(sourceText);
27
- return adapt(tree, sourceText);
77
+ const preprocessed = preprocessInvisibleTimes(sourceText);
78
+ const tree = parser.parse(preprocessed);
79
+ return adapt(tree, sourceText, preprocessed);
28
80
  }
29
81
  }
@@ -8,6 +8,8 @@ export const INFIX_OPS = {
8
8
  "==": "Equal", "!=": "Unequal", "<": "Less", "<=": "LessEqual",
9
9
  ">": "Greater", ">=": "GreaterEqual",
10
10
  "@*": "Composition", "/*": "RightComposition",
11
+ // U+2062: WL InvisibleTimes (space-multiplication), inserted by preprocessor
12
+ "⁢": "InvisibleTimes",
11
13
  };
12
14
  export const BINARY_OPS = {
13
15
  "=": "Set", ":=": "SetDelayed", "^=": "UpSet", "^:=": "UpSetDelayed",
Binary file
@@ -331,6 +331,15 @@ export function printInfix(node, options, print) {
331
331
  return printOriginalSource(node, options);
332
332
  }
333
333
 
334
+ if (node.op === "InvisibleTimes") {
335
+ const terms = operands(node);
336
+ const parts = [print(terms[0])];
337
+ for (let i = 1; i < terms.length; i++) {
338
+ parts.push(" ", print(terms[i]));
339
+ }
340
+ return group(parts);
341
+ }
342
+
334
343
  if (node.op === "MessageName") {
335
344
  const parts = operands(node);
336
345
  return group(