prettier-plugin-wolfram 0.7.12 → 0.7.14

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,11 +1,15 @@
1
1
  {
2
2
  "name": "prettier-plugin-wolfram",
3
- "version": "0.7.12",
3
+ "version": "0.7.14",
4
4
  "description": "Prettier plugin for Wolfram Language using tree-sitter",
5
5
  "license": "MIT",
6
6
  "repository": {
7
7
  "type": "git",
8
- "url": "git://github.com/ToneAr/prettier-plugin-wolfram.git"
8
+ "url": "https://github.com/ToneAr/prettier-plugin-wolfram.git"
9
+ },
10
+ "homepage": "https://github.com/ToneAr/prettier-plugin-wolfram#readme",
11
+ "bugs": {
12
+ "url": "https://github.com/ToneAr/prettier-plugin-wolfram/issues"
9
13
  },
10
14
  "keywords": [
11
15
  "prettier",
@@ -55,8 +59,5 @@
55
59
  },
56
60
  "workspaces": [
57
61
  "vscode-extension"
58
- ],
59
- "engines": {
60
- "vscode": "^1.105.0"
61
- }
62
+ ]
62
63
  }
package/src/options.js CHANGED
@@ -15,6 +15,9 @@ function wolframOptionDefinition(definition) {
15
15
  return {
16
16
  type: definition.type,
17
17
  category: "Wolfram",
18
+ ...(hasOwn(definition, "exception")
19
+ ? { exception: definition.exception }
20
+ : {}),
18
21
  ...(hasOwn(definition, "default")
19
22
  ? { default: definition.default }
20
23
  : {}),
@@ -22,42 +25,61 @@ function wolframOptionDefinition(definition) {
22
25
  };
23
26
  }
24
27
 
28
+ function isBlankLineRange(value) {
29
+ return (
30
+ isPlainObject(value) &&
31
+ Number.isInteger(value.min) &&
32
+ Number.isInteger(value.max) &&
33
+ value.min >= 0 &&
34
+ value.max >= value.min
35
+ );
36
+ }
37
+
38
+ function blankLineRangeOption(defaultValue, description) {
39
+ return {
40
+ type: "int",
41
+ ...(defaultValue == null ? {} : { default: defaultValue }),
42
+ exception: (value) =>
43
+ (Number.isInteger(value) && value >= 0) || isBlankLineRange(value),
44
+ description,
45
+ };
46
+ }
47
+
25
48
  export const wolframOptions = {
26
49
  newlinesBetweenDefinitions: {
27
- type: "int",
28
- default: 1,
29
- minimum: 0,
50
+ ...blankLineRangeOption(
51
+ { min: 1, max: 1 },
52
+ "Allowed blank lines between adjacent definitions.",
53
+ ),
30
54
  legacyName: "wolframNewlinesBetweenDefinitions",
31
- description: "Blank lines inserted between adjacent definitions.",
32
55
  },
33
56
  newlinesBetweenSetDefinitions: {
34
- type: "int",
35
- minimum: 0,
57
+ ...blankLineRangeOption(
58
+ null,
59
+ "Allowed blank lines between adjacent Set-family definitions. Inherits newlinesBetweenDefinitions when unset.",
60
+ ),
36
61
  legacyName: "wolframNewlinesBetweenSetDefinitions",
37
- description:
38
- "Blank lines inserted between adjacent Set-family definitions. Inherits newlinesBetweenDefinitions when unset.",
39
62
  },
40
63
  newlinesBetweenSetDelayedDefinitions: {
41
- type: "int",
42
- minimum: 0,
64
+ ...blankLineRangeOption(
65
+ null,
66
+ "Allowed blank lines between adjacent SetDelayed-family definitions. Inherits newlinesBetweenDefinitions when unset.",
67
+ ),
43
68
  legacyName: "wolframNewlinesBetweenSetDelayedDefinitions",
44
- description:
45
- "Blank lines inserted between adjacent SetDelayed-family definitions. Inherits newlinesBetweenDefinitions when unset.",
46
69
  },
47
70
  newlinesBetweenSetAndSetDelayedDefinitions: {
48
- type: "int",
49
- minimum: 0,
71
+ ...blankLineRangeOption(
72
+ null,
73
+ "Allowed blank lines between mixed Set-family and SetDelayed-family definitions. Inherits newlinesBetweenDefinitions when unset.",
74
+ ),
50
75
  legacyName: "wolframNewlinesBetweenSetAndSetDelayedDefinitions",
51
- description:
52
- "Blank lines inserted between mixed Set-family and SetDelayed-family definitions. Inherits newlinesBetweenDefinitions when unset.",
53
76
  },
54
77
  newlinesBetweenSameNameDefinitions: {
55
- type: "int",
56
- default: 0,
57
- minimum: 0,
78
+ ...blankLineRangeOption(
79
+ { min: 0, max: 0 },
80
+ "Allowed blank lines between adjacent definitions that belong to the same symbol.",
81
+ ),
58
82
  legacyName: "wolframNewlinesBetweenSameNameDefinitions",
59
- description:
60
- "Blank lines inserted between adjacent definitions that belong to the same symbol.",
61
83
  },
62
84
  maxBlankLinesBetweenCode: {
63
85
  type: "int",
@@ -1,5 +1,6 @@
1
1
  import { makeLineIndex, nodeSource, offsetToLineCol } from "./position.js";
2
2
  import { INFIX_OPS, BINARY_OPS, PREFIX_OPS, POSTFIX_OPS, opName } from "./operators.js";
3
+ import { IMPLICIT_NULL_SYMBOL } from "./sentinels.js";
3
4
 
4
5
  const LEAF_KIND = { symbol: "Symbol", integer: "Integer", real: "Real", string: "String", comment: "Token`Comment" };
5
6
 
@@ -47,13 +48,24 @@ export function adapt(tree, source, preprocessedSource, map) {
47
48
 
48
49
  function shouldHoistTopLevelSemicolonChain(node, ctx) {
49
50
  if (node.type !== "infix" || operatorLiteral(node, ctx) !== ";") return false;
50
- return hasTrailingSemicolon(node) || spansMultipleLines(node, ctx);
51
- }
52
-
53
- // Returns true if the infix node ends with a MISSING node (i.e. has a trailing ";").
54
- function hasTrailingSemicolon(node) {
55
- const last = node.child(node.childCount - 1);
56
- return last !== null && last.isMissing;
51
+ return hasTrailingSemicolon(node, ctx) || spansMultipleLines(node, ctx);
52
+ }
53
+
54
+ // Returns true if the infix node ends with ";" and has no real RHS. Raw trailing
55
+ // semicolons may be represented as a MISSING node, while preprocessed ones use
56
+ // the internal fake-null symbol.
57
+ function hasTrailingSemicolon(node, ctx) {
58
+ for (let i = node.childCount - 1; i >= 0; i--) {
59
+ const child = node.child(i);
60
+ if (child.isMissing) return true;
61
+ if (child.isNamed) {
62
+ if (child.type === "comment") continue;
63
+ if (isImplicitNullSymbol(child, ctx)) return true;
64
+ return false;
65
+ }
66
+ return child.type === ";";
67
+ }
68
+ return false;
57
69
  }
58
70
 
59
71
  function spansMultipleLines(node, ctx) {
@@ -61,6 +73,13 @@ function spansMultipleLines(node, ctx) {
61
73
  return source?.[0]?.[0] !== source?.[1]?.[0];
62
74
  }
63
75
 
76
+ function isImplicitNullSymbol(node, ctx) {
77
+ return (
78
+ node?.type === "symbol" &&
79
+ ctx.source.slice(node.startIndex, node.endIndex) === IMPLICIT_NULL_SYMBOL
80
+ );
81
+ }
82
+
64
83
  // Collect all leaf statements from a semicolon infix chain (possibly left-recursive),
65
84
  // flattening the left-associative structure into a linear list of segments.
66
85
  // Each segment is { nodes: TSNode[], semiToken: TSNode|null } where semiToken is the ";" that follows.
@@ -76,7 +95,7 @@ function collectSemicolonSegments(node, ctx, segments) {
76
95
  if (!c.isNamed) {
77
96
  // ";" operator token
78
97
  semiToken = c;
79
- } else if (c.isMissing) {
98
+ } else if (c.isMissing || isImplicitNullSymbol(c, ctx)) {
80
99
  // trailing implicit null — rhs is nothing (trailing semicolon)
81
100
  } else if (c.type === "comment") {
82
101
  if (lhs === null) leadingComments.push(c);
@@ -138,7 +157,10 @@ function isLeadingCommentForNextSegment(comment, semiToken, ctx) {
138
157
  function hoistSemicolonChildren(node, ctx, out) {
139
158
  const segments = [];
140
159
  collectSemicolonSegments(node, ctx, segments);
160
+ emitSemicolonSegments(segments, ctx, out);
161
+ }
141
162
 
163
+ function emitSemicolonSegments(segments, ctx, out) {
142
164
  for (const seg of segments) {
143
165
  for (const c of seg.leadingComments ?? []) out.push(leaf(c, ctx));
144
166
  if (seg.nodes.length === 0) continue;
@@ -195,6 +217,9 @@ function namedChildren(node) {
195
217
  }
196
218
 
197
219
  function leaf(node, ctx, kind = LEAF_KIND[node.type]) {
220
+ if (isImplicitNullSymbol(node, ctx)) {
221
+ return { type: "LeafNode", kind: "Token`Fake`ImplicitNull", value: "", source: nodeSource(node, ctx.lineIndex) };
222
+ }
198
223
  return { type: "LeafNode", kind, value: ctx.source.slice(node.startIndex, node.endIndex), source: nodeSource(node, ctx.lineIndex) };
199
224
  }
200
225
 
@@ -3,6 +3,7 @@ import { dirname, resolve } from "path";
3
3
  import { fileURLToPath } from "url";
4
4
  import { readFileSync } from "fs";
5
5
  import { adapt } from "./adapter.js";
6
+ import { IMPLICIT_NULL_SYMBOL } from "./sentinels.js";
6
7
 
7
8
  const here = dirname(fileURLToPath(import.meta.url));
8
9
  const WASM_PATH = resolve(here, "tree-sitter-wolfram.wasm");
@@ -24,10 +25,11 @@ async function getLanguage() {
24
25
  // Returns { text, map } where `text` is the preprocessed source and `map` is an
25
26
  // index translation table: map[i] is the original-source character offset that
26
27
  // corresponds to preprocessed-text offset i (map[text.length] === src.length).
27
- // Because the only length-changing transform collapses a run of spaces into a
28
- // single InvisibleTimes char, this map lets callers translate tree-sitter node
29
- // positions (computed on the preprocessed text) back to exact offsets in the
30
- // original source, without lossy line/col round-trips.
28
+ // Length-changing transforms collapse a run of spaces into a single
29
+ // InvisibleTimes char or insert an internal fake Null symbol after terminating
30
+ // semicolons. This map lets callers translate tree-sitter node positions
31
+ // (computed on the preprocessed text) back to exact offsets in the original
32
+ // source, without lossy line/col round-trips.
31
33
  export function preprocess(src) {
32
34
  let result = "";
33
35
  const map = [];
@@ -37,6 +39,37 @@ export function preprocess(src) {
37
39
  for (let k = start; k < end; k++) map.push(k);
38
40
  result += src.slice(start, end);
39
41
  };
42
+ const appendSynthetic = (text, originalOffset) => {
43
+ for (let k = 0; k < text.length; k++) map.push(originalOffset);
44
+ result += text;
45
+ };
46
+ const skipTrivia = (start) => {
47
+ let j = start;
48
+ while (j < n) {
49
+ if (/\s/.test(src[j])) {
50
+ j++;
51
+ continue;
52
+ }
53
+ if (src[j] === "(" && src[j + 1] === "*") {
54
+ j += 2;
55
+ let depth = 1;
56
+ while (j < n && depth > 0) {
57
+ if (src[j] === "(" && src[j + 1] === "*") { depth++; j += 2; }
58
+ else if (src[j] === "*" && src[j + 1] === ")") { depth--; j += 2; }
59
+ else j++;
60
+ }
61
+ continue;
62
+ }
63
+ break;
64
+ }
65
+ return j;
66
+ };
67
+ const isTerminatorBoundary = (offset) =>
68
+ offset >= n ||
69
+ src[offset] === "]" ||
70
+ src[offset] === "}" ||
71
+ src[offset] === ")" ||
72
+ (src[offset] === "|" && src[offset + 1] === ">");
40
73
  let i = 0;
41
74
  while (i < n) {
42
75
  // Skip quoted string
@@ -63,6 +96,15 @@ export function preprocess(src) {
63
96
  copyVerbatim(start, i);
64
97
  continue;
65
98
  }
99
+ if (src[i] === ";" && src[i - 1] !== ";" && src[i + 1] !== ";") {
100
+ const boundary = skipTrivia(i + 1);
101
+ if (isTerminatorBoundary(boundary)) {
102
+ copyVerbatim(i, boundary);
103
+ appendSynthetic(IMPLICIT_NULL_SYMBOL, boundary);
104
+ i = boundary;
105
+ continue;
106
+ }
107
+ }
66
108
  // Two or more spaces between word chars on same line → InvisibleTimes
67
109
  if (src[i] === " " && src[i + 1] === " ") {
68
110
  // Check previous meaningful char is a word char
@@ -0,0 +1 @@
1
+ export const IMPLICIT_NULL_SYMBOL = "$$PrettierWolframImplicitNull$$";
@@ -35,6 +35,27 @@ export function nonNegativeIntegerOption(value, fallback) {
35
35
  return Math.max(0, Math.floor(numeric));
36
36
  }
37
37
 
38
+ function blankLineRangeOption(value, fallback) {
39
+ if (value == null) return fallback;
40
+ if (typeof value === "object" && !Array.isArray(value)) {
41
+ const min = nonNegativeIntegerOption(value.min, fallback.min);
42
+ const max = nonNegativeIntegerOption(value.max, fallback.max);
43
+ return { min, max: Math.max(min, max) };
44
+ }
45
+
46
+ const exact = nonNegativeIntegerOption(value, fallback.min);
47
+ return { min: exact, max: exact };
48
+ }
49
+
50
+ function exactBlankLineRange(value) {
51
+ return { min: value, max: value };
52
+ }
53
+
54
+ function blankLinesFromRange(observedBlankLines, range) {
55
+ const observed = nonNegativeIntegerOption(observedBlankLines, 0);
56
+ return Math.min(range.max, Math.max(range.min, observed));
57
+ }
58
+
38
59
  export function maxBlankLinesBetweenCode(options = {}) {
39
60
  options = normalizeWolframOptions(options);
40
61
  return nonNegativeIntegerOption(
@@ -44,47 +65,72 @@ export function maxBlankLinesBetweenCode(options = {}) {
44
65
  }
45
66
 
46
67
  export function blankLinesBetweenDefinitions(options = {}) {
68
+ const range = blankLineRangeBetweenDefinitions(options);
69
+ return range.min;
70
+ }
71
+
72
+ function blankLineRangeBetweenDefinitions(options = {}) {
47
73
  options = normalizeWolframOptions(options);
48
- return nonNegativeIntegerOption(
74
+ return blankLineRangeOption(
49
75
  options.wolframNewlinesBetweenDefinitions,
50
- DEFAULT_BLANK_LINES_BETWEEN_DEFINITIONS,
76
+ exactBlankLineRange(DEFAULT_BLANK_LINES_BETWEEN_DEFINITIONS),
51
77
  );
52
78
  }
53
79
 
54
- function optionalBlankLinesOption(value, fallback) {
80
+ function optionalBlankLineRangeOption(value, fallback) {
55
81
  if (value == null) return fallback;
56
- return nonNegativeIntegerOption(value, fallback);
82
+ return blankLineRangeOption(value, fallback);
57
83
  }
58
84
 
59
85
  export function blankLinesBetweenSetDefinitions(options = {}) {
86
+ const range = blankLineRangeBetweenSetDefinitions(options);
87
+ return range.min;
88
+ }
89
+
90
+ function blankLineRangeBetweenSetDefinitions(options = {}) {
60
91
  options = normalizeWolframOptions(options);
61
- return optionalBlankLinesOption(
92
+ return optionalBlankLineRangeOption(
62
93
  options.wolframNewlinesBetweenSetDefinitions,
63
- blankLinesBetweenDefinitions(options),
94
+ blankLineRangeBetweenDefinitions(options),
64
95
  );
65
96
  }
66
97
 
67
98
  export function blankLinesBetweenSetDelayedDefinitions(options = {}) {
99
+ const range = blankLineRangeBetweenSetDelayedDefinitions(options);
100
+ return range.min;
101
+ }
102
+
103
+ function blankLineRangeBetweenSetDelayedDefinitions(options = {}) {
68
104
  options = normalizeWolframOptions(options);
69
- return optionalBlankLinesOption(
105
+ return optionalBlankLineRangeOption(
70
106
  options.wolframNewlinesBetweenSetDelayedDefinitions,
71
- blankLinesBetweenDefinitions(options),
107
+ blankLineRangeBetweenDefinitions(options),
72
108
  );
73
109
  }
74
110
 
75
111
  export function blankLinesBetweenSetAndSetDelayedDefinitions(options = {}) {
112
+ const range = blankLineRangeBetweenSetAndSetDelayedDefinitions(options);
113
+ return range.min;
114
+ }
115
+
116
+ function blankLineRangeBetweenSetAndSetDelayedDefinitions(options = {}) {
76
117
  options = normalizeWolframOptions(options);
77
- return optionalBlankLinesOption(
118
+ return optionalBlankLineRangeOption(
78
119
  options.wolframNewlinesBetweenSetAndSetDelayedDefinitions,
79
- blankLinesBetweenDefinitions(options),
120
+ blankLineRangeBetweenDefinitions(options),
80
121
  );
81
122
  }
82
123
 
83
124
  export function blankLinesBetweenSameNameDefinitions(options = {}) {
125
+ const range = blankLineRangeBetweenSameNameDefinitions(options);
126
+ return range.min;
127
+ }
128
+
129
+ function blankLineRangeBetweenSameNameDefinitions(options = {}) {
84
130
  options = normalizeWolframOptions(options);
85
- return nonNegativeIntegerOption(
131
+ return blankLineRangeOption(
86
132
  options.wolframNewlinesBetweenSameNameDefinitions,
87
- DEFAULT_BLANK_LINES_BETWEEN_SAME_NAME_DEFINITIONS,
133
+ exactBlankLineRange(DEFAULT_BLANK_LINES_BETWEEN_SAME_NAME_DEFINITIONS),
88
134
  );
89
135
  }
90
136
 
@@ -187,6 +233,48 @@ function blankLinesBetweenDeclarationKinds(
187
233
  return null;
188
234
  }
189
235
 
236
+ function blankLineRangeBetweenDeclarationKinds(
237
+ prevKind,
238
+ nextKind,
239
+ options,
240
+ { requireSpecificOption = false } = {},
241
+ ) {
242
+ if (prevKind === "set" && nextKind === "set") {
243
+ if (
244
+ requireSpecificOption &&
245
+ options.wolframNewlinesBetweenSetDefinitions == null
246
+ ) {
247
+ return null;
248
+ }
249
+ return blankLineRangeBetweenSetDefinitions(options);
250
+ }
251
+
252
+ if (prevKind === "setDelayed" && nextKind === "setDelayed") {
253
+ if (
254
+ requireSpecificOption &&
255
+ options.wolframNewlinesBetweenSetDelayedDefinitions == null
256
+ ) {
257
+ return null;
258
+ }
259
+ return blankLineRangeBetweenSetDelayedDefinitions(options);
260
+ }
261
+
262
+ if (
263
+ (prevKind === "set" && nextKind === "setDelayed") ||
264
+ (prevKind === "setDelayed" && nextKind === "set")
265
+ ) {
266
+ if (
267
+ requireSpecificOption &&
268
+ options.wolframNewlinesBetweenSetAndSetDelayedDefinitions == null
269
+ ) {
270
+ return null;
271
+ }
272
+ return blankLineRangeBetweenSetAndSetDelayedDefinitions(options);
273
+ }
274
+
275
+ return null;
276
+ }
277
+
190
278
  function blankLinesBetweenDeclarationNodes(
191
279
  prevNode,
192
280
  nextNode,
@@ -201,6 +289,20 @@ function blankLinesBetweenDeclarationNodes(
201
289
  );
202
290
  }
203
291
 
292
+ function blankLineRangeBetweenDeclarationNodes(
293
+ prevNode,
294
+ nextNode,
295
+ options,
296
+ requireSpecificOption = false,
297
+ ) {
298
+ return blankLineRangeBetweenDeclarationKinds(
299
+ declarationSpacingKind(prevNode),
300
+ declarationSpacingKind(nextNode),
301
+ options,
302
+ { requireSpecificOption },
303
+ );
304
+ }
305
+
204
306
  function firstSemanticChild(node) {
205
307
  return semanticChildren(node)[0] ?? null;
206
308
  }
@@ -396,14 +498,17 @@ export function blankLinesForCodeGap(
396
498
  if (topLevel && mode === "none") return 0;
397
499
 
398
500
  if (hasSharedDefinitionSubject(prevNode, nextNode)) {
399
- return blankLinesBetweenSameNameDefinitions(options);
501
+ return blankLinesFromRange(
502
+ observedBlankLines,
503
+ blankLineRangeBetweenSameNameDefinitions(options),
504
+ );
400
505
  }
401
506
 
402
507
  if (isDeclarationNode(prevNode) && isDeclarationNode(nextNode)) {
403
- return (
404
- blankLinesBetweenDeclarationNodes(prevNode, nextNode, options) ??
405
- blankLinesBetweenDefinitions(options)
406
- );
508
+ const range =
509
+ blankLineRangeBetweenDeclarationNodes(prevNode, nextNode, options) ??
510
+ blankLineRangeBetweenDefinitions(options);
511
+ return blankLinesFromRange(observedBlankLines, range);
407
512
  }
408
513
 
409
514
  const maxBlankLines = maxBlankLinesBetweenCode(options);