prettier-plugin-wolfram 0.7.4 → 0.7.5

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.4",
3
+ "version": "0.7.5",
4
4
  "description": "Prettier plugin for Wolfram Language using tree-sitter",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -9,9 +9,12 @@ const GROUP_CLOSE_LEAF = { "}": "Token`CloseCurly", ")": "Token`CloseParen", "]"
9
9
 
10
10
  // preprocessedSource is the version passed to tree-sitter (may have ⁢ for InvisibleTimes);
11
11
  // source is the original — used only for the unformattable fallback.
12
- export function adapt(tree, source, preprocessedSource) {
12
+ // map (optional) translates preprocessed char offsets back to original offsets;
13
+ // it is attached to lineIndex so nodeSource can record exact original positions.
14
+ export function adapt(tree, source, preprocessedSource, map) {
13
15
  const ps = preprocessedSource ?? source;
14
16
  const lineIndex = makeLineIndex(ps);
17
+ lineIndex.map = map;
15
18
  const ctx = { source: ps, lineIndex };
16
19
  const root = tree.rootNode;
17
20
  if (subtreeHasError(root)) {
@@ -20,10 +20,24 @@ async function getLanguage() {
20
20
 
21
21
  // Replace space-based implicit multiplication (a b) with U+2062 (InvisibleTimes)
22
22
  // so the grammar can parse it. Skip content inside strings and nested comments.
23
- export function preprocessInvisibleTimes(src) {
23
+ //
24
+ // Returns { text, map } where `text` is the preprocessed source and `map` is an
25
+ // index translation table: map[i] is the original-source character offset that
26
+ // 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.
31
+ export function preprocess(src) {
24
32
  let result = "";
25
- let i = 0;
33
+ const map = [];
26
34
  const n = src.length;
35
+ // Copy src[start..end) verbatim, recording the source offset of each char.
36
+ const copyVerbatim = (start, end) => {
37
+ for (let k = start; k < end; k++) map.push(k);
38
+ result += src.slice(start, end);
39
+ };
40
+ let i = 0;
27
41
  while (i < n) {
28
42
  // Skip quoted string
29
43
  if (src[i] === '"') {
@@ -33,7 +47,7 @@ export function preprocessInvisibleTimes(src) {
33
47
  i++;
34
48
  }
35
49
  if (i < n) i++;
36
- result += src.slice(start, i);
50
+ copyVerbatim(start, i);
37
51
  continue;
38
52
  }
39
53
  // Skip nested WL comment (* ... *)
@@ -46,7 +60,7 @@ export function preprocessInvisibleTimes(src) {
46
60
  else if (src[i] === "*" && src[i + 1] === ")") { depth--; i += 2; }
47
61
  else i++;
48
62
  }
49
- result += src.slice(start, i);
63
+ copyVerbatim(start, i);
50
64
  continue;
51
65
  }
52
66
  // Two or more spaces between word chars on same line → InvisibleTimes
@@ -59,14 +73,22 @@ export function preprocessInvisibleTimes(src) {
59
73
  while (j < n && src[j] === " ") j++;
60
74
  if (j < n && /\w/.test(src[j])) {
61
75
  result += "⁢"; // InvisibleTimes, spaces stripped (they're extras)
76
+ map.push(i); // the single char maps to the start of the run
62
77
  i = j;
63
78
  continue;
64
79
  }
65
80
  }
66
81
  }
82
+ map.push(i);
67
83
  result += src[i++];
68
84
  }
69
- return result;
85
+ map.push(n); // sentinel for end offsets (node endIndex === text.length)
86
+ return { text: result, map };
87
+ }
88
+
89
+ // Backward-compatible wrapper returning only the preprocessed text.
90
+ export function preprocessInvisibleTimes(src) {
91
+ return preprocess(src).text;
70
92
  }
71
93
 
72
94
  export class WolframParser {
@@ -74,8 +96,8 @@ export class WolframParser {
74
96
  const lang = await getLanguage();
75
97
  const parser = new Parser();
76
98
  parser.setLanguage(lang);
77
- const preprocessed = preprocessInvisibleTimes(sourceText);
99
+ const { text: preprocessed, map } = preprocess(sourceText);
78
100
  const tree = parser.parse(preprocessed);
79
- return adapt(tree, sourceText, preprocessed);
101
+ return adapt(tree, sourceText, preprocessed, map);
80
102
  }
81
103
  }
@@ -55,8 +55,26 @@ export function offsetToLineCol(lineIndex, charOffset) {
55
55
  }
56
56
 
57
57
  export function nodeSource(tsNode, lineIndex) {
58
- return [
58
+ const source = [
59
59
  offsetToLineCol(lineIndex, tsNode.startIndex),
60
60
  offsetToLineCol(lineIndex, tsNode.endIndex),
61
61
  ];
62
+ // When a preprocessing offset map is available, record the exact original
63
+ // character offsets (non-enumerably, so node.source stays a [[l,c],[l,c]]
64
+ // pair for lint rules). These bypass the lossy WL-byte/visual-column line/col
65
+ // round-trip in addOffsets, which otherwise mismaps offsets whenever the
66
+ // preprocessed text differs from the original (collapsed spaces, tabs, or
67
+ // non-ASCII characters earlier on the line).
68
+ const map = lineIndex?.map;
69
+ if (map) {
70
+ const charStart = map[tsNode.startIndex];
71
+ const charEnd = map[tsNode.endIndex];
72
+ if (typeof charStart === "number" && typeof charEnd === "number") {
73
+ Object.defineProperties(source, {
74
+ charStart: { value: charStart, enumerable: false },
75
+ charEnd: { value: charEnd, enumerable: false },
76
+ });
77
+ }
78
+ }
79
+ return source;
62
80
  }
@@ -57,6 +57,14 @@ export function lineColToOffset(table, line, col) {
57
57
 
58
58
  function sourceToOffsets(source, table) {
59
59
  if (!Array.isArray(source) || source.length !== 2) return null;
60
+ // Exact original char offsets recorded by the parser (see nodeSource) are
61
+ // authoritative when present — they avoid the lossy line/col conversion below.
62
+ if (
63
+ typeof source.charStart === "number" &&
64
+ typeof source.charEnd === "number"
65
+ ) {
66
+ return { locStart: source.charStart, locEnd: source.charEnd };
67
+ }
60
68
  const [start, end] = source;
61
69
  if (!Array.isArray(start) || !Array.isArray(end)) return null;
62
70
  const [startLine, startCol] = start;