@motionscript/code 0.1.0-alpha.0 → 0.1.0-alpha.10

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.
Files changed (65) hide show
  1. package/CHANGELOG.md +96 -0
  2. package/NOTICE +49 -0
  3. package/README.md +148 -50
  4. package/dist/browser/THIRD_PARTY_LICENSES.txt +431 -0
  5. package/dist/browser/index.js +10 -7
  6. package/dist/browser/index.js.map +3 -3
  7. package/dist/browser/manifest.json +85 -3
  8. package/dist/code-range.d.ts +7 -1
  9. package/dist/code-range.d.ts.map +1 -1
  10. package/dist/code-range.js +9 -1
  11. package/dist/code-range.js.map +1 -1
  12. package/dist/diff.d.ts +14 -0
  13. package/dist/diff.d.ts.map +1 -1
  14. package/dist/diff.js +134 -2
  15. package/dist/diff.js.map +1 -1
  16. package/dist/engine.d.ts +2 -2
  17. package/dist/engine.d.ts.map +1 -1
  18. package/dist/engine.js +4 -4
  19. package/dist/engine.js.map +1 -1
  20. package/dist/highlight.d.ts +10 -2
  21. package/dist/highlight.d.ts.map +1 -1
  22. package/dist/highlight.js +40 -6
  23. package/dist/highlight.js.map +1 -1
  24. package/dist/layout.d.ts +1 -1
  25. package/dist/layout.d.ts.map +1 -1
  26. package/dist/layout.js.map +1 -1
  27. package/dist/measure-cache.d.ts +2 -2
  28. package/dist/measure-cache.d.ts.map +1 -1
  29. package/dist/measure-cache.js +0 -0
  30. package/dist/measure-cache.js.map +1 -1
  31. package/dist/node.d.ts +275 -281
  32. package/dist/node.d.ts.map +1 -1
  33. package/dist/node.js +634 -700
  34. package/dist/node.js.map +1 -1
  35. package/dist/nodes.d.ts +6 -9
  36. package/dist/nodes.d.ts.map +1 -1
  37. package/dist/nodes.js +7 -9
  38. package/dist/nodes.js.map +1 -1
  39. package/dist/render.d.ts +10 -3
  40. package/dist/render.d.ts.map +1 -1
  41. package/dist/render.js +60 -1
  42. package/dist/render.js.map +1 -1
  43. package/dist/style.d.ts +3 -3
  44. package/dist/style.d.ts.map +1 -1
  45. package/dist/style.js +10 -6
  46. package/dist/style.js.map +1 -1
  47. package/dist/tokens.d.ts.map +1 -1
  48. package/dist/tokens.js +5 -1
  49. package/dist/tokens.js.map +1 -1
  50. package/dist/transitions.d.ts +19 -1
  51. package/dist/transitions.d.ts.map +1 -1
  52. package/dist/transitions.js.map +1 -1
  53. package/package.json +7 -4
  54. package/src/code-range.ts +10 -1
  55. package/src/diff.ts +137 -2
  56. package/src/engine.ts +5 -5
  57. package/src/highlight.ts +54 -5
  58. package/src/layout.ts +1 -1
  59. package/src/measure-cache.ts +0 -0
  60. package/src/node.ts +767 -747
  61. package/src/nodes.ts +8 -10
  62. package/src/render.ts +71 -3
  63. package/src/style.ts +11 -7
  64. package/src/tokens.ts +5 -1
  65. package/src/transitions.ts +21 -1
@@ -1,11 +1,93 @@
1
1
  {
2
2
  "name": "@motionscript/code",
3
- "version": "0.1.0-alpha.0",
3
+ "version": "0.1.0-alpha.9",
4
4
  "exports": {
5
5
  ".": "dist/browser/index.js"
6
6
  },
7
7
  "peers": [
8
- "@motionscript/core"
8
+ "@motionscript/sdk"
9
9
  ],
10
- "assets": {}
10
+ "assets": {},
11
+ "thirdParty": [
12
+ {
13
+ "name": "@lezer/common",
14
+ "version": "1.5.2",
15
+ "license": "MIT"
16
+ },
17
+ {
18
+ "name": "@lezer/cpp",
19
+ "version": "1.1.6",
20
+ "license": "MIT"
21
+ },
22
+ {
23
+ "name": "@lezer/css",
24
+ "version": "1.3.3",
25
+ "license": "MIT"
26
+ },
27
+ {
28
+ "name": "@lezer/go",
29
+ "version": "1.0.1",
30
+ "license": "MIT"
31
+ },
32
+ {
33
+ "name": "@lezer/highlight",
34
+ "version": "1.2.3",
35
+ "license": "MIT"
36
+ },
37
+ {
38
+ "name": "@lezer/html",
39
+ "version": "1.3.13",
40
+ "license": "MIT"
41
+ },
42
+ {
43
+ "name": "@lezer/java",
44
+ "version": "1.1.3",
45
+ "license": "MIT"
46
+ },
47
+ {
48
+ "name": "@lezer/javascript",
49
+ "version": "1.5.4",
50
+ "license": "MIT"
51
+ },
52
+ {
53
+ "name": "@lezer/json",
54
+ "version": "1.0.3",
55
+ "license": "MIT"
56
+ },
57
+ {
58
+ "name": "@lezer/lr",
59
+ "version": "1.4.10",
60
+ "license": "MIT"
61
+ },
62
+ {
63
+ "name": "@lezer/markdown",
64
+ "version": "1.6.4",
65
+ "license": "MIT"
66
+ },
67
+ {
68
+ "name": "@lezer/php",
69
+ "version": "1.0.5",
70
+ "license": "MIT"
71
+ },
72
+ {
73
+ "name": "@lezer/python",
74
+ "version": "1.1.19",
75
+ "license": "MIT"
76
+ },
77
+ {
78
+ "name": "@lezer/rust",
79
+ "version": "1.0.2",
80
+ "license": "MIT"
81
+ },
82
+ {
83
+ "name": "@lezer/xml",
84
+ "version": "1.0.6",
85
+ "license": "MIT"
86
+ },
87
+ {
88
+ "name": "@lezer/yaml",
89
+ "version": "1.0.4",
90
+ "license": "MIT"
91
+ }
92
+ ]
11
93
  }
@@ -30,7 +30,7 @@ export declare function word(line: number, col: number, length?: number): CodeRa
30
30
  export declare function lines(from: number, to?: number): CodeRange;
31
31
  /**
32
32
  * Immutable, chainable, iterable builder over multiple ranges — mirrors
33
- * {@link FillChain}/`Fills` in `@motionscript/core`. Every method returns a
33
+ * {@link FillChain}/`Fills` in `@motionscript/sdk`. Every method returns a
34
34
  * *new* chain with one more range appended, so the next range-kind method is
35
35
  * called directly rather than through a separate combinator:
36
36
  *
@@ -43,6 +43,12 @@ export declare class CodeRangeChain {
43
43
  lines(from: number, to?: number): CodeRangeChain;
44
44
  word(line: number, col: number, length?: number): CodeRangeChain;
45
45
  [Symbol.iterator](): Iterator<CodeRange>;
46
+ /**
47
+ * A chain is stored as its ranges. Without this it serialises as
48
+ * `{ "list": [...] }`, which has no iterator once read back and so selects
49
+ * nothing; a plain array of ranges reads back as the selection it was.
50
+ */
51
+ toJSON(): CodeRange[];
46
52
  }
47
53
  /**
48
54
  * Entry point for building multi-range selections fluently — an empty
@@ -1 +1 @@
1
- {"version":3,"file":"code-range.d.ts","sourceRoot":"","sources":["../src/code-range.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,WAAW,SAAS;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;;GAOG;AACH,wBAAgB,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,SAAS,CAO1E;AAED;;;;;;;GAOG;AACH,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,SAAS,CAO1D;AAID;;;;;;;;GAQG;AACH,qBAAa,cAAc;aACK,IAAI,EAAE,SAAS,EAAE;gBAAjB,IAAI,GAAE,SAAS,EAAO;IAElD,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,GAAG,cAAc;IAIhD,IAAI,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,cAAc;IAI/D,CAAC,MAAM,CAAC,QAAQ,CAAC,IAAI,QAAQ,CAAC,SAAS,CAAC;CAG5C;AAED;;;;;;GAMG;AACH,eAAO,MAAM,UAAU,gBAAuB,CAAC;AAE/C;;;;GAIG;AACH,wBAAgB,kBAAkB,CAC9B,CAAC,EAAE,SAAS,EACZ,WAAW,EAAE,MAAM,EAAE,GACtB;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAkChC;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAC9B,KAAK,EAAE,MAAM,EACb,GAAG,EAAE,MAAM,EACX,WAAW,EAAE,MAAM,EAAE,GACtB,SAAS,CAoBX"}
1
+ {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AoBX"}
@@ -32,7 +32,7 @@ export function lines(from, to) {
32
32
  }
33
33
  /**
34
34
  * Immutable, chainable, iterable builder over multiple ranges — mirrors
35
- * {@link FillChain}/`Fills` in `@motionscript/core`. Every method returns a
35
+ * {@link FillChain}/`Fills` in `@motionscript/sdk`. Every method returns a
36
36
  * *new* chain with one more range appended, so the next range-kind method is
37
37
  * called directly rather than through a separate combinator:
38
38
  *
@@ -53,6 +53,14 @@ export class CodeRangeChain {
53
53
  *[Symbol.iterator]() {
54
54
  yield* this.list;
55
55
  }
56
+ /**
57
+ * A chain is stored as its ranges. Without this it serialises as
58
+ * `{ "list": [...] }`, which has no iterator once read back and so selects
59
+ * nothing; a plain array of ranges reads back as the selection it was.
60
+ */
61
+ toJSON() {
62
+ return this.list;
63
+ }
56
64
  }
57
65
  /**
58
66
  * Entry point for building multi-range selections fluently — an empty
@@ -1 +1 @@
1
- {"version":3,"file":"code-range.js","sourceRoot":"","sources":["../src/code-range.ts"],"names":[],"mappings":"AAaA;;;;;;;GAOG;AACH,MAAM,UAAU,IAAI,CAAC,IAAY,EAAE,GAAW,EAAE,MAAe;IAC3D,OAAO;QACH,SAAS,EAAE,IAAI;QACf,QAAQ,EAAE,GAAG;QACb,OAAO,EAAE,IAAI;QACb,MAAM,EAAE,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,GAAG,MAAM;KACzD,CAAC;AACN,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,KAAK,CAAC,IAAY,EAAE,EAAW;IAC3C,OAAO;QACH,SAAS,EAAE,IAAI;QACf,QAAQ,EAAE,CAAC;QACX,OAAO,EAAE,EAAE,IAAI,IAAI;QACnB,MAAM,EAAE,QAAQ;KACnB,CAAC;AACN,CAAC;AAID;;;;;;;;GAQG;AACH,MAAM,OAAO,cAAc;IACK;IAA5B,YAA4B,OAAoB,EAAE;QAAtB,SAAI,GAAJ,IAAI,CAAkB;IAAI,CAAC;IAEvD,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IAAI,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC,EAAE,CAAC;AACtE,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,kBAAkB,CAC9B,KAAa,EACb,GAAW,EACX,WAAqB;IAErB,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,IAAI,SAAS,GAAG,CAAC,EAAE,QAAQ,GAAG,CAAC,EAAE,OAAO,GAAG,CAAC,EAAE,MAAM,GAAG,CAAC,CAAC;IACzD,IAAI,UAAU,GAAG,KAAK,CAAC;IACvB,KAAK,IAAI,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,WAAW,CAAC,MAAM,EAAE,EAAE,EAAE,EAAE,CAAC;QAC7C,MAAM,SAAS,GAAG,GAAG,CAAC;QACtB,MAAM,OAAO,GAAG,GAAG,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;QACtC,IAAI,CAAC,UAAU,IAAI,KAAK,IAAI,SAAS,IAAI,KAAK,IAAI,OAAO,EAAE,CAAC;YACxD,SAAS,GAAG,EAAE,GAAG,CAAC,CAAC;YACnB,QAAQ,GAAG,CAAC,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;YACnC,UAAU,GAAG,IAAI,CAAC;QACtB,CAAC;QACD,IAAI,GAAG,IAAI,SAAS,IAAI,GAAG,IAAI,OAAO,EAAE,CAAC;YACrC,OAAO,GAAG,EAAE,GAAG,CAAC,CAAC;YACjB,MAAM,GAAG,CAAC,GAAG,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;YAC/B,MAAM;QACV,CAAC;QACD,GAAG,GAAG,OAAO,GAAG,CAAC,CAAC;IACtB,CAAC;IACD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACpD,CAAC","sourcesContent":["/**\n * A range over a code document. Lines and columns are 1-indexed.\n *\n * `endLine`/`endCol` may be `Infinity` to mean \"to the end of the line\" or\n * \"to the end of the document\".\n */\nexport interface CodeRange {\n startLine: number;\n startCol: number;\n endLine: number;\n endCol: number;\n}\n\n/**\n * Create a range that spans `length` characters starting at (line, col).\n * If `length` is omitted, the range extends to the end of the line.\n *\n * Example:\n * word(1, 3, 3) // line 1, column 3, spans 3 characters\n * word(1, 3) // line 1, column 3, to end of line\n */\nexport function word(line: number, col: number, length?: number): CodeRange {\n return {\n startLine: line,\n startCol: col,\n endLine: line,\n endCol: length === undefined ? Infinity : col + length,\n };\n}\n\n/**\n * Create a range that covers entire lines from `from` to `to` (inclusive).\n * If `to` is omitted, only line `from` is covered.\n *\n * Example:\n * lines(1, 3) // lines 1 through 3\n * lines(2) // line 2 only\n */\nexport function lines(from: number, to?: number): CodeRange {\n return {\n startLine: from,\n startCol: 1,\n endLine: to ?? from,\n endCol: Infinity,\n };\n}\n\n\n\n/**\n * Immutable, chainable, iterable builder over multiple ranges — mirrors\n * {@link FillChain}/`Fills` in `@motionscript/core`. Every method returns a\n * *new* chain with one more range appended, so the next range-kind method is\n * called directly rather than through a separate combinator:\n *\n * @example\n * CodeRanges.lines(2).word(5, 1, 3) // a 2-range selection, one expression\n */\nexport class CodeRangeChain {\n constructor(public readonly list: CodeRange[] = []) { }\n\n lines(from: number, to?: number): CodeRangeChain {\n return new CodeRangeChain([...this.list, lines(from, to)]);\n }\n\n word(line: number, col: number, length?: number): CodeRangeChain {\n return new CodeRangeChain([...this.list, word(line, col, length)]);\n }\n\n *[Symbol.iterator](): Iterator<CodeRange> {\n yield* this.list;\n }\n}\n\n/**\n * Entry point for building multi-range selections fluently — an empty\n * {@link CodeRangeChain}, so `CodeRanges.lines(2)` *is* `new\n * CodeRangeChain().lines(2)`. `word`/`lines` stay directly importable and\n * usable standalone for a single range; reach for `CodeRanges` when a call\n * needs more than one.\n */\nexport const CodeRanges = new CodeRangeChain();\n\n/**\n * Convert a CodeRange to absolute character offsets [start, end) in the joined\n * source text, given the per-line lengths of that source. Lines/columns are\n * clamped into the document so out-of-bounds inputs produce a valid range.\n */\nexport function rangeToCharOffsets(\n r: CodeRange,\n lineLengths: number[],\n): { start: number; end: number } {\n if (lineLengths.length === 0) return { start: 0, end: 0 };\n\n // Cumulative line starts in the joined string (lines joined with '\\n').\n const lineStart: number[] = new Array(lineLengths.length);\n let acc = 0;\n for (let i = 0; i < lineLengths.length; i++) {\n lineStart[i] = acc;\n acc += lineLengths[i] + 1; // +1 for the joining newline\n }\n const docEnd = acc > 0 ? acc - 1 : 0; // strip trailing newline\n\n const clampLine = (n: number) =>\n Math.max(0, Math.min(lineLengths.length - 1, n - 1)); // to 0-indexed\n\n // A range read back from JSON holds `Infinity` as `null`; both mean \"to the end\".\n const endLine = r.endLine ?? Infinity;\n const endCol = r.endCol ?? Infinity;\n\n const sLine = clampLine(r.startLine);\n const eLine = clampLine(endLine);\n const sColMax = lineLengths[sLine];\n const eColMax = lineLengths[eLine];\n\n // Columns are 1-indexed; clamp to [1, lineLength+1] so endCol can sit past\n // the last char (one-past-end).\n const sCol = Math.max(0, Math.min(sColMax, r.startCol - 1));\n const eCol = endCol === Infinity\n ? eColMax\n : Math.max(0, Math.min(eColMax, endCol - 1));\n\n const start = Math.min(docEnd, lineStart[sLine] + sCol);\n const end = Math.min(docEnd, lineStart[eLine] + eCol);\n return { start: Math.min(start, end), end: Math.max(start, end) };\n}\n\n/**\n * Convert [start, end) character offsets in joined source to a CodeRange.\n * Lines/cols in the returned range are 1-indexed.\n */\nexport function charOffsetsToRange(\n start: number,\n end: number,\n lineLengths: number[],\n): CodeRange {\n let off = 0;\n let startLine = 1, startCol = 1, endLine = 1, endCol = 1;\n let foundStart = false;\n for (let li = 0; li < lineLengths.length; li++) {\n const lineStart = off;\n const lineEnd = off + lineLengths[li];\n if (!foundStart && start >= lineStart && start <= lineEnd) {\n startLine = li + 1;\n startCol = (start - lineStart) + 1;\n foundStart = true;\n }\n if (end >= lineStart && end <= lineEnd) {\n endLine = li + 1;\n endCol = (end - lineStart) + 1;\n break;\n }\n off = lineEnd + 1;\n }\n return { startLine, startCol, endLine, endCol };\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QACtB,MAAM,OAAO,GAAG,GAAG,GAAG,WAAW,CAAC,EAAE,CAAC,CAAC;QACtC,IAAI,CAAC,UAAU,IAAI,KAAK,IAAI,SAAS,IAAI,KAAK,IAAI,OAAO,EAAE,CAAC;YACxD,SAAS,GAAG,EAAE,GAAG,CAAC,CAAC;YACnB,QAAQ,GAAG,CAAC,KAAK,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;YACnC,UAAU,GAAG,IAAI,CAAC;QACtB,CAAC;QACD,IAAI,GAAG,IAAI,SAAS,IAAI,GAAG,IAAI,OAAO,EAAE,CAAC;YACrC,OAAO,GAAG,EAAE,GAAG,CAAC,CAAC;YACjB,MAAM,GAAG,CAAC,GAAG,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;YAC/B,MAAM;QACV,CAAC;QACD,GAAG,GAAG,OAAO,GAAG,CAAC,CAAC;IACtB,CAAC;IACD,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;AACpD,CAAC","sourcesContent":["/**\n * A range over a code document. Lines and columns are 1-indexed.\n *\n * `endLine`/`endCol` may be `Infinity` to mean \"to the end of the line\" or\n * \"to the end of the document\".\n */\nexport interface CodeRange {\n startLine: number;\n startCol: number;\n endLine: number;\n endCol: number;\n}\n\n/**\n * Create a range that spans `length` characters starting at (line, col).\n * If `length` is omitted, the range extends to the end of the line.\n *\n * Example:\n * word(1, 3, 3) // line 1, column 3, spans 3 characters\n * word(1, 3) // line 1, column 3, to end of line\n */\nexport function word(line: number, col: number, length?: number): CodeRange {\n return {\n startLine: line,\n startCol: col,\n endLine: line,\n endCol: length === undefined ? Infinity : col + length,\n };\n}\n\n/**\n * Create a range that covers entire lines from `from` to `to` (inclusive).\n * If `to` is omitted, only line `from` is covered.\n *\n * Example:\n * lines(1, 3) // lines 1 through 3\n * lines(2) // line 2 only\n */\nexport function lines(from: number, to?: number): CodeRange {\n return {\n startLine: from,\n startCol: 1,\n endLine: to ?? from,\n endCol: Infinity,\n };\n}\n\n\n\n/**\n * Immutable, chainable, iterable builder over multiple ranges — mirrors\n * {@link FillChain}/`Fills` in `@motionscript/sdk`. Every method returns a\n * *new* chain with one more range appended, so the next range-kind method is\n * called directly rather than through a separate combinator:\n *\n * @example\n * CodeRanges.lines(2).word(5, 1, 3) // a 2-range selection, one expression\n */\nexport class CodeRangeChain {\n constructor(public readonly list: CodeRange[] = []) { }\n\n lines(from: number, to?: number): CodeRangeChain {\n return new CodeRangeChain([...this.list, lines(from, to)]);\n }\n\n word(line: number, col: number, length?: number): CodeRangeChain {\n return new CodeRangeChain([...this.list, word(line, col, length)]);\n }\n\n *[Symbol.iterator](): Iterator<CodeRange> {\n yield* this.list;\n }\n\n /**\n * A chain is stored as its ranges. Without this it serialises as\n * `{ \"list\": [...] }`, which has no iterator once read back and so selects\n * nothing; a plain array of ranges reads back as the selection it was.\n */\n toJSON(): CodeRange[] {\n return this.list;\n }\n}\n\n/**\n * Entry point for building multi-range selections fluently — an empty\n * {@link CodeRangeChain}, so `CodeRanges.lines(2)` *is* `new\n * CodeRangeChain().lines(2)`. `word`/`lines` stay directly importable and\n * usable standalone for a single range; reach for `CodeRanges` when a call\n * needs more than one.\n */\nexport const CodeRanges = new CodeRangeChain();\n\n/**\n * Convert a CodeRange to absolute character offsets [start, end) in the joined\n * source text, given the per-line lengths of that source. Lines/columns are\n * clamped into the document so out-of-bounds inputs produce a valid range.\n */\nexport function rangeToCharOffsets(\n r: CodeRange,\n lineLengths: number[],\n): { start: number; end: number } {\n if (lineLengths.length === 0) return { start: 0, end: 0 };\n\n // Cumulative line starts in the joined string (lines joined with '\\n').\n const lineStart: number[] = new Array(lineLengths.length);\n let acc = 0;\n for (let i = 0; i < lineLengths.length; i++) {\n lineStart[i] = acc;\n acc += lineLengths[i] + 1; // +1 for the joining newline\n }\n const docEnd = acc > 0 ? acc - 1 : 0; // strip trailing newline\n\n const clampLine = (n: number) =>\n Math.max(0, Math.min(lineLengths.length - 1, n - 1)); // to 0-indexed\n\n // A range read back from JSON holds `Infinity` as `null`; both mean \"to the end\".\n const endLine = r.endLine ?? Infinity;\n const endCol = r.endCol ?? Infinity;\n\n const sLine = clampLine(r.startLine);\n const eLine = clampLine(endLine);\n const sColMax = lineLengths[sLine];\n const eColMax = lineLengths[eLine];\n\n // Columns are 1-indexed; clamp to [1, lineLength+1] so endCol can sit past\n // the last char (one-past-end).\n const sCol = Math.max(0, Math.min(sColMax, r.startCol - 1));\n const eCol = endCol === Infinity\n ? eColMax\n : Math.max(0, Math.min(eColMax, endCol - 1));\n\n const start = Math.min(docEnd, lineStart[sLine] + sCol);\n const end = Math.min(docEnd, lineStart[eLine] + eCol);\n return { start: Math.min(start, end), end: Math.max(start, end) };\n}\n\n/**\n * Convert [start, end) character offsets in joined source to a CodeRange.\n * Lines/cols in the returned range are 1-indexed.\n */\nexport function charOffsetsToRange(\n start: number,\n end: number,\n lineLengths: number[],\n): CodeRange {\n let off = 0;\n let startLine = 1, startCol = 1, endLine = 1, endCol = 1;\n let foundStart = false;\n for (let li = 0; li < lineLengths.length; li++) {\n const lineStart = off;\n const lineEnd = off + lineLengths[li];\n if (!foundStart && start >= lineStart && start <= lineEnd) {\n startLine = li + 1;\n startCol = (start - lineStart) + 1;\n foundStart = true;\n }\n if (end >= lineStart && end <= lineEnd) {\n endLine = li + 1;\n endCol = (end - lineStart) + 1;\n break;\n }\n off = lineEnd + 1;\n }\n return { startLine, startCol, endLine, endCol };\n}\n"]}
package/dist/diff.d.ts CHANGED
@@ -31,6 +31,20 @@ export interface CodeEdit {
31
31
  /** Previous colour of a surviving token whose highlighting changed, for the cross-fade. */
32
32
  fromColorById: Map<number, string | undefined>;
33
33
  }
34
+ /**
35
+ * The most line pairs one run of changed lines is scored against each other, and
36
+ * the most across a whole edit.
37
+ *
38
+ * Scoring every pair is quadratic: with 50-character lines it took 31 ms at 200
39
+ * lines and 741 ms at 1,000, paid again for each edit command when it is built.
40
+ * So the lines are first paired where they cannot be in doubt (see
41
+ * {@link alignLines}), and only a run left between two such anchors is scored.
42
+ * A run past the first limit, or what is left of the second, is not compared: its
43
+ * lines leave and enter as whole rows. That is the budget, and an edit that stays
44
+ * inside it is diffed exactly as before.
45
+ */
46
+ export declare const LINE_ALIGN_CELLS = 40000;
47
+ export declare const EDIT_ALIGN_CELLS = 160000;
34
48
  /**
35
49
  * Carry `from`'s identities onto `to`, and report what changed.
36
50
  *
@@ -1 +1 @@
1
- {"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAW,MAAM,UAAU,CAAC;AAEhD;;;;;;;;;;GAUG;AACH;;;;;;;GAOG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,QAAQ,CAAC;AAE1E,MAAM,WAAW,QAAQ;IACrB,+EAA+E;IAC/E,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,8DAA8D;IAC9D,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACxB,6DAA6D;IAC7D,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACtB,mFAAmF;IACnF,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACxB,2FAA2F;IAC3F,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;CAClD;AA8HD;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,QAAQ,CAgD/D"}
1
+ {"version":3,"file":"diff.d.ts","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAW,MAAM,UAAU,CAAC;AAEhD;;;;;;;;;;GAUG;AACH;;;;;;;GAOG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,QAAQ,CAAC;AAE1E,MAAM,WAAW,QAAQ;IACrB,+EAA+E;IAC/E,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,8DAA8D;IAC9D,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACxB,6DAA6D;IAC7D,QAAQ,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACtB,mFAAmF;IACnF,UAAU,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IACxB,2FAA2F;IAC3F,aAAa,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,GAAG,SAAS,CAAC,CAAC;CAClD;AAYD;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,gBAAgB,QAAS,CAAC;AACvC,eAAO,MAAM,gBAAgB,SAAU,CAAC;AA0OxC;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,EAAE,EAAE,MAAM,EAAE,GAAG,QAAQ,CAkD/D"}
package/dist/diff.js CHANGED
@@ -7,6 +7,20 @@
7
7
  * the way it was made.
8
8
  */
9
9
  const LINE_MATCH_FLOOR = 0.45;
10
+ /**
11
+ * The most line pairs one run of changed lines is scored against each other, and
12
+ * the most across a whole edit.
13
+ *
14
+ * Scoring every pair is quadratic: with 50-character lines it took 31 ms at 200
15
+ * lines and 741 ms at 1,000, paid again for each edit command when it is built.
16
+ * So the lines are first paired where they cannot be in doubt (see
17
+ * {@link alignLines}), and only a run left between two such anchors is scored.
18
+ * A run past the first limit, or what is left of the second, is not compared: its
19
+ * lines leave and enter as whole rows. That is the budget, and an edit that stays
20
+ * inside it is diffed exactly as before.
21
+ */
22
+ export const LINE_ALIGN_CELLS = 40_000;
23
+ export const EDIT_ALIGN_CELLS = 160_000;
10
24
  /** Weight a token carries when judging similarity: its visible characters. */
11
25
  function tokenWeight(tok) {
12
26
  return tok.content.trim().length;
@@ -119,6 +133,122 @@ function alignTokens(from, to) {
119
133
  return tokenWeight(to[j]) || 0.25;
120
134
  });
121
135
  }
136
+ function sameTokens(a, b) {
137
+ if (a.tokens.length !== b.tokens.length)
138
+ return false;
139
+ for (let k = 0; k < a.tokens.length; k++)
140
+ if (a.tokens[k].content !== b.tokens[k].content)
141
+ return false;
142
+ return true;
143
+ }
144
+ /** A line's text, token boundaries included, for telling identical lines apart from similar ones. */
145
+ function signature(line) {
146
+ let out = "";
147
+ for (const tok of line.tokens)
148
+ out += tok.content + "\u0001";
149
+ return out;
150
+ }
151
+ /**
152
+ * Pair the lines of two listings, in order.
153
+ *
154
+ * Lines that are the same at the start and at the end are paired outright. In
155
+ * what is left, a line that occurs exactly once on each side is an anchor, and
156
+ * the longest run of anchors that keeps their order splits the rest into runs of
157
+ * changed lines, each scored against itself within the budget (see
158
+ * {@link LINE_ALIGN_CELLS}). A one-line edit to a long listing therefore never
159
+ * builds a table bigger than the lines around it.
160
+ */
161
+ function alignLines(from, to) {
162
+ const fromSig = from.map(signature);
163
+ const toSig = to.map(signature);
164
+ const pairs = [];
165
+ let head = 0;
166
+ while (head < from.length && head < to.length && fromSig[head] === toSig[head]) {
167
+ pairs.push([head, head]);
168
+ head++;
169
+ }
170
+ let tail = 0;
171
+ while (tail < from.length - head && tail < to.length - head
172
+ && fromSig[from.length - 1 - tail] === toSig[to.length - 1 - tail])
173
+ tail++;
174
+ const anchors = anchorLines(fromSig, toSig, head, from.length - tail, head, to.length - tail);
175
+ let budget = EDIT_ALIGN_CELLS;
176
+ let fi = head;
177
+ let ti = head;
178
+ for (const [af, at] of [...anchors, [from.length - tail, to.length - tail]]) {
179
+ const rows = af - fi;
180
+ const cols = at - ti;
181
+ const cells = rows * cols;
182
+ if (cells > 0 && cells <= LINE_ALIGN_CELLS && cells <= budget) {
183
+ budget -= cells;
184
+ for (const [i, j] of align(rows, cols, (a, b) => similarity(from[fi + a], to[ti + b]) - LINE_MATCH_FLOOR)) {
185
+ pairs.push([fi + i, ti + j]);
186
+ }
187
+ }
188
+ if (af < from.length - tail)
189
+ pairs.push([af, at]);
190
+ fi = af + 1;
191
+ ti = at + 1;
192
+ }
193
+ for (let k = tail; k > 0; k--)
194
+ pairs.push([from.length - k, to.length - k]);
195
+ return pairs;
196
+ }
197
+ /**
198
+ * The lines that occur once in `from[fa, fb)` and once in `to[ta, tb)`, reduced to
199
+ * the longest subsequence that is increasing on both sides (patience sorting).
200
+ * Blank lines are never anchors: they say nothing about where they belong.
201
+ */
202
+ function anchorLines(fromSig, toSig, fa, fb, ta, tb) {
203
+ const fromAt = new Map();
204
+ const seen = new Set();
205
+ for (let i = fa; i < fb; i++) {
206
+ const sig = fromSig[i];
207
+ if (seen.has(sig))
208
+ fromAt.delete(sig);
209
+ else
210
+ fromAt.set(sig, i);
211
+ seen.add(sig);
212
+ }
213
+ const toAt = new Map();
214
+ const seenTo = new Set();
215
+ for (let j = ta; j < tb; j++) {
216
+ const sig = toSig[j];
217
+ if (seenTo.has(sig))
218
+ toAt.delete(sig);
219
+ else
220
+ toAt.set(sig, j);
221
+ seenTo.add(sig);
222
+ }
223
+ // In `to` order, each candidate carries its `from` position.
224
+ const candidates = [];
225
+ for (let j = ta; j < tb; j++) {
226
+ const i = fromAt.get(toSig[j]);
227
+ if (i !== undefined && toAt.get(toSig[j]) === j && toSig[j].replaceAll("\u0001", "").trim() !== "") {
228
+ candidates.push([i, j]);
229
+ }
230
+ }
231
+ const tails = [];
232
+ const previous = new Array(candidates.length).fill(-1);
233
+ for (let c = 0; c < candidates.length; c++) {
234
+ let lo = 0;
235
+ let hi = tails.length;
236
+ while (lo < hi) {
237
+ const mid = (lo + hi) >> 1;
238
+ if (candidates[tails[mid]][0] < candidates[c][0])
239
+ lo = mid + 1;
240
+ else
241
+ hi = mid;
242
+ }
243
+ if (lo > 0)
244
+ previous[c] = tails[lo - 1];
245
+ tails[lo] = c;
246
+ }
247
+ const chain = [];
248
+ for (let c = tails.length > 0 ? tails[tails.length - 1] : -1; c >= 0; c = previous[c])
249
+ chain.push(candidates[c]);
250
+ return chain.reverse();
251
+ }
122
252
  /**
123
253
  * Carry `from`'s identities onto `to`, and report what changed.
124
254
  *
@@ -131,7 +261,7 @@ export function diffCode(from, to) {
131
261
  const addedIds = new Set();
132
262
  const newLineIds = new Set();
133
263
  const fromColorById = new Map();
134
- const linePairs = align(from.length, to.length, (i, j) => similarity(from[i], to[j]) - LINE_MATCH_FLOOR);
264
+ const linePairs = alignLines(from, to);
135
265
  const pairedTo = new Set();
136
266
  const pairedFrom = new Set();
137
267
  for (const [fi, ti] of linePairs) {
@@ -140,7 +270,9 @@ export function diffCode(from, to) {
140
270
  const fromLine = from[fi];
141
271
  const toLine = to[ti];
142
272
  toLine.id = fromLine.id;
143
- const matched = alignTokens(fromLine.tokens, toLine.tokens);
273
+ const matched = sameTokens(fromLine, toLine)
274
+ ? fromLine.tokens.map((_, k) => [k, k])
275
+ : alignTokens(fromLine.tokens, toLine.tokens);
144
276
  const keptFrom = new Set();
145
277
  const keptTo = new Set();
146
278
  for (const [a, b] of matched) {
package/dist/diff.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"diff.js","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAoCA;;;;;;;GAOG;AACH,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAE9B,8EAA8E;AAC9E,SAAS,WAAW,CAAC,GAAY;IAC7B,OAAO,GAAG,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC;AACrC,CAAC;AAED,SAAS,UAAU,CAAC,IAAY;IAC5B,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,MAAM;QAAE,GAAG,IAAI,WAAW,CAAC,GAAG,CAAC,CAAC;IACvD,OAAO,GAAG,CAAC;AACf,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,UAAU,CAAC,CAAS,EAAE,CAAS;IACpC,MAAM,EAAE,GAAG,UAAU,CAAC,CA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,EAAE,EAAE,CAAC;QACnC,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;YAAE,SAAS;QAChC,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM;YAAE,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC7D,CAAC;IACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACjC,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;YAAE,SAAS;QAC9B,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACzB,KAAK,MAAM,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM;YAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACzD,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE,aAAa,EAAE,CAAC;AAC1E,CAAC","sourcesContent":["import type { IdLine, IdToken } from \"./tokens\";\n\n/**\n * The result of diffing one listing structure against another: the *next*\n * structure, with token and line ids carried over from the previous one\n * wherever the content survived, plus the three sets a transition needs to know\n * which role each token plays.\n *\n * Identity is the whole product here. `const variable = 3` becoming\n * `const number = 3` should move `const` and `= 3` rather than dissolve and\n * rebuild them, and a token can only *move* if the frame after the edit still\n * knows it is the same token.\n */\n/**\n * How a `Code` node decides which token became which across an edit. Given\n * the old and new tokenized structures, return the carried-over structure\n * plus the added/removed/renamed-line bookkeeping a transition needs.\n * `diffCode` below is the default implementation (exported as\n * `defaultCodeDiff`) — pass a `diffStrategy` of this shape to `<Code>` to\n * replace it with your own.\n */\nexport type CodeDiffStrategy = (from: IdLine[], to: IdLine[]) => CodeEdit;\n\nexport interface CodeEdit {\n /** The new structure. Ids are rewritten in place onto the tokens handed in. */\n lines: IdLine[];\n /** Ids present only in the old structure — these fade out. */\n removedIds: Set<number>;\n /** Ids present only in the new structure — these fade in. */\n addedIds: Set<number>;\n /** Ids of {@link lines} that did not exist before — these enter as a whole row. */\n newLineIds: Set<number>;\n /** Previous colour of a surviving token whose highlighting changed, for the cross-fade. */\n fromColorById: Map<number, string | undefined>;\n}\n\n/**\n * How similar two lines must be before they are treated as *one line that was\n * edited* rather than as one line deleted and another inserted.\n *\n * Below this, a row's tokens have so little in common that carrying ids across\n * would animate glyphs sliding between unrelated words. Above it, the edit reads\n * the way it was made.\n */\nconst LINE_MATCH_FLOOR = 0.45;\n\n/** Weight a token carries when judging similarity: its visible characters. */\nfunction tokenWeight(tok: IdToken): number {\n return tok.content.trim().length;\n}\n\nfunction lineWeight(line: IdLine): number {\n let sum = 0;\n for (const tok of line.tokens) sum += tokenWeight(tok);\n return sum;\n}\n\n/**\n * Dice coefficient over the two lines' token multisets, weighted by visible\n * characters so `variable` counts for far more than a run of spaces.\n *\n * Two blank lines are identical (1); a blank line against a non-blank one shares\n * nothing (0), which keeps the blank rows a reformat introduces from anchoring\n * themselves onto real code.\n */\nfunction similarity(a: IdLine, b: IdLine): number {\n const wa = lineWeight(a);\n const wb = lineWeight(b);\n if (wa === 0 && wb === 0) return 1;\n if (wa === 0 || wb === 0) return 0;\n\n const bag = new Map<string, number>();\n for (const tok of a.tokens) {\n if (tokenWeight(tok) === 0) continue;\n bag.set(tok.content, (bag.get(tok.content) ?? 0) + 1);\n }\n let common = 0;\n for (const tok of b.tokens) {\n const w = tokenWeight(tok);\n if (w === 0) continue;\n const left = bag.get(tok.content) ?? 0;\n if (left > 0) {\n bag.set(tok.content, left - 1);\n common += w;\n }\n }\n return (2 * common) / (wa + wb);\n}\n\n/**\n * Align two sequences by maximising total `score`, skipping is free.\n *\n * A plain LCS would only ever pair *identical* elements, which is the wrong tool\n * twice over: two lines that differ by one word are the same line, and two\n * tokens that differ only in colour are the same token. Scoring the pairing and\n * letting anything that scores ≤ 0 fall out expresses that directly, and because\n * skips cost nothing the alignment stays in order.\n *\n * Returns the matched index pairs, in order.\n */\nfunction align(\n aLength: number,\n bLength: number,\n score: (i: number, j: number) => number,\n): Array<[number, number]> {\n // dp[i][j] = best score aligning a[0..i) with b[0..j).\n const width = bLength + 1;\n const dp = new Float64Array((aLength + 1) * width);\n const pairScore = new Float64Array(aLength * bLength);\n\n for (let i = 1; i <= aLength; i++) {\n for (let j = 1; j <= bLength; j++) {\n const s = score(i - 1, j - 1);\n pairScore[(i - 1) * bLength + (j - 1)] = s;\n const skipA = dp[(i - 1) * width + j];\n const skipB = dp[i * width + (j - 1)];\n let best = skipA > skipB ? skipA : skipB;\n if (s > 0) {\n const paired = dp[(i - 1) * width + (j - 1)] + s;\n if (paired > best) best = paired;\n }\n dp[i * width + j] = best;\n }\n }\n\n const pairs: Array<[number, number]> = [];\n let i = aLength;\n let j = bLength;\n while (i > 0 && j > 0) {\n const s = pairScore[(i - 1) * bLength + (j - 1)];\n if (s > 0 && dp[i * width + j] === dp[(i - 1) * width + (j - 1)] + s) {\n pairs.push([i - 1, j - 1]);\n i--; j--;\n } else if (dp[(i - 1) * width + j] >= dp[i * width + (j - 1)]) {\n i--;\n } else {\n j--;\n }\n }\n pairs.reverse();\n return pairs;\n}\n\n/**\n * Match the tokens of one line against another's.\n *\n * Scored by visible characters rather than token count, so an alignment that\n * keeps `items.reduce` together beats one that keeps four more spaces. A token\n * pairs only when its content is unchanged — a changed word is a different word,\n * and should be seen to be replaced.\n */\nfunction alignTokens(from: IdToken[], to: IdToken[]): Array<[number, number]> {\n return align(from.length, to.length, (i, j) => {\n if (from[i].content !== to[j].content) return 0;\n // Whitespace still pairs (it holds indentation together), just weakly\n // enough that it never outranks real content.\n return tokenWeight(to[j]) || 0.25;\n });\n}\n\n/**\n * Carry `from`'s identities onto `to`, and report what changed.\n *\n * `to` is freshly tokenized and owns ids nobody has seen yet, so rewriting them\n * in place is safe and is what makes the transition cheap: after this, the two\n * structures share an id for every token that survived the edit.\n */\nexport function diffCode(from: IdLine[], to: IdLine[]): CodeEdit {\n const removedIds = new Set<number>();\n const addedIds = new Set<number>();\n const newLineIds = new Set<number>();\n const fromColorById = new Map<number, string | undefined>();\n\n const linePairs = align(from.length, to.length, (i, j) => similarity(from[i], to[j]) - LINE_MATCH_FLOOR);\n\n const pairedTo = new Set<number>();\n const pairedFrom = new Set<number>();\n\n for (const [fi, ti] of linePairs) {\n pairedFrom.add(fi);\n pairedTo.add(ti);\n const fromLine = from[fi];\n const toLine = to[ti];\n toLine.id = fromLine.id;\n\n const matched = alignTokens(fromLine.tokens, toLine.tokens);\n const keptFrom = new Set<number>();\n const keptTo = new Set<number>();\n for (const [a, b] of matched) {\n keptFrom.add(a);\n keptTo.add(b);\n const oldTok = fromLine.tokens[a];\n const newTok = toLine.tokens[b];\n newTok.id = oldTok.id;\n if (oldTok.color !== newTok.color) fromColorById.set(oldTok.id, oldTok.color);\n }\n for (let a = 0; a < fromLine.tokens.length; a++) {\n if (!keptFrom.has(a)) removedIds.add(fromLine.tokens[a].id);\n }\n for (let b = 0; b < toLine.tokens.length; b++) {\n if (!keptTo.has(b)) addedIds.add(toLine.tokens[b].id);\n }\n }\n\n for (let i = 0; i < from.length; i++) {\n if (pairedFrom.has(i)) continue;\n for (const tok of from[i].tokens) removedIds.add(tok.id);\n }\n for (let j = 0; j < to.length; j++) {\n if (pairedTo.has(j)) continue;\n newLineIds.add(to[j].id);\n for (const tok of to[j].tokens) addedIds.add(tok.id);\n }\n\n return { lines: to, removedIds, addedIds, newLineIds, fromColorById };\n}\n"]}
1
+ {"version":3,"file":"diff.js","sourceRoot":"","sources":["../src/diff.ts"],"names":[],"mappings":"AAoCA;;;;;;;GAOG;AACH,MAAM,gBAAgB,GAAG,IAAI,CAAC;AAE9B;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,MAAM,CAAC;AACvC,MAAM,CAAC,MAAM,gBAAgB,GAAG,OAA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AED,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACnC,IAAI,UAAU,CAAC,GAAG,CAAC,CAAC,CAAC;YAAE,SAAS;QAChC,KAAK,MAAM,GAAG,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC,MAAM;YAAE,UAAU,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IAC7D,CAAC;IACD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,EAAE,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QACjC,IAAI,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC;YAAE,SAAS;QAC9B,UAAU,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC;QACzB,KAAK,MAAM,GAAG,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM;YAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACzD,CAAC;IAED,OAAO,EAAE,KAAK,EAAE,EAAE,EAAE,UAAU,EAAE,QAAQ,EAAE,UAAU,EAAE,aAAa,EAAE,CAAC;AAC1E,CAAC","sourcesContent":["import type { IdLine, IdToken } from \"./tokens\";\n\n/**\n * The result of diffing one listing structure against another: the *next*\n * structure, with token and line ids carried over from the previous one\n * wherever the content survived, plus the three sets a transition needs to know\n * which role each token plays.\n *\n * Identity is the whole product here. `const variable = 3` becoming\n * `const number = 3` should move `const` and `= 3` rather than dissolve and\n * rebuild them, and a token can only *move* if the frame after the edit still\n * knows it is the same token.\n */\n/**\n * How a `Code` node decides which token became which across an edit. Given\n * the old and new tokenized structures, return the carried-over structure\n * plus the added/removed/renamed-line bookkeeping a transition needs.\n * `diffCode` below is the default implementation (exported as\n * `defaultCodeDiff`) — pass a `diffStrategy` of this shape to `<Code>` to\n * replace it with your own.\n */\nexport type CodeDiffStrategy = (from: IdLine[], to: IdLine[]) => CodeEdit;\n\nexport interface CodeEdit {\n /** The new structure. Ids are rewritten in place onto the tokens handed in. */\n lines: IdLine[];\n /** Ids present only in the old structure — these fade out. */\n removedIds: Set<number>;\n /** Ids present only in the new structure — these fade in. */\n addedIds: Set<number>;\n /** Ids of {@link lines} that did not exist before — these enter as a whole row. */\n newLineIds: Set<number>;\n /** Previous colour of a surviving token whose highlighting changed, for the cross-fade. */\n fromColorById: Map<number, string | undefined>;\n}\n\n/**\n * How similar two lines must be before they are treated as *one line that was\n * edited* rather than as one line deleted and another inserted.\n *\n * Below this, a row's tokens have so little in common that carrying ids across\n * would animate glyphs sliding between unrelated words. Above it, the edit reads\n * the way it was made.\n */\nconst LINE_MATCH_FLOOR = 0.45;\n\n/**\n * The most line pairs one run of changed lines is scored against each other, and\n * the most across a whole edit.\n *\n * Scoring every pair is quadratic: with 50-character lines it took 31 ms at 200\n * lines and 741 ms at 1,000, paid again for each edit command when it is built.\n * So the lines are first paired where they cannot be in doubt (see\n * {@link alignLines}), and only a run left between two such anchors is scored.\n * A run past the first limit, or what is left of the second, is not compared: its\n * lines leave and enter as whole rows. That is the budget, and an edit that stays\n * inside it is diffed exactly as before.\n */\nexport const LINE_ALIGN_CELLS = 40_000;\nexport const EDIT_ALIGN_CELLS = 160_000;\n\n/** Weight a token carries when judging similarity: its visible characters. */\nfunction tokenWeight(tok: IdToken): number {\n return tok.content.trim().length;\n}\n\nfunction lineWeight(line: IdLine): number {\n let sum = 0;\n for (const tok of line.tokens) sum += tokenWeight(tok);\n return sum;\n}\n\n/**\n * Dice coefficient over the two lines' token multisets, weighted by visible\n * characters so `variable` counts for far more than a run of spaces.\n *\n * Two blank lines are identical (1); a blank line against a non-blank one shares\n * nothing (0), which keeps the blank rows a reformat introduces from anchoring\n * themselves onto real code.\n */\nfunction similarity(a: IdLine, b: IdLine): number {\n const wa = lineWeight(a);\n const wb = lineWeight(b);\n if (wa === 0 && wb === 0) return 1;\n if (wa === 0 || wb === 0) return 0;\n\n const bag = new Map<string, number>();\n for (const tok of a.tokens) {\n if (tokenWeight(tok) === 0) continue;\n bag.set(tok.content, (bag.get(tok.content) ?? 0) + 1);\n }\n let common = 0;\n for (const tok of b.tokens) {\n const w = tokenWeight(tok);\n if (w === 0) continue;\n const left = bag.get(tok.content) ?? 0;\n if (left > 0) {\n bag.set(tok.content, left - 1);\n common += w;\n }\n }\n return (2 * common) / (wa + wb);\n}\n\n/**\n * Align two sequences by maximising total `score`, skipping is free.\n *\n * A plain LCS would only ever pair *identical* elements, which is the wrong tool\n * twice over: two lines that differ by one word are the same line, and two\n * tokens that differ only in colour are the same token. Scoring the pairing and\n * letting anything that scores ≤ 0 fall out expresses that directly, and because\n * skips cost nothing the alignment stays in order.\n *\n * Returns the matched index pairs, in order.\n */\nfunction align(\n aLength: number,\n bLength: number,\n score: (i: number, j: number) => number,\n): Array<[number, number]> {\n // dp[i][j] = best score aligning a[0..i) with b[0..j).\n const width = bLength + 1;\n const dp = new Float64Array((aLength + 1) * width);\n const pairScore = new Float64Array(aLength * bLength);\n\n for (let i = 1; i <= aLength; i++) {\n for (let j = 1; j <= bLength; j++) {\n const s = score(i - 1, j - 1);\n pairScore[(i - 1) * bLength + (j - 1)] = s;\n const skipA = dp[(i - 1) * width + j];\n const skipB = dp[i * width + (j - 1)];\n let best = skipA > skipB ? skipA : skipB;\n if (s > 0) {\n const paired = dp[(i - 1) * width + (j - 1)] + s;\n if (paired > best) best = paired;\n }\n dp[i * width + j] = best;\n }\n }\n\n const pairs: Array<[number, number]> = [];\n let i = aLength;\n let j = bLength;\n while (i > 0 && j > 0) {\n const s = pairScore[(i - 1) * bLength + (j - 1)];\n if (s > 0 && dp[i * width + j] === dp[(i - 1) * width + (j - 1)] + s) {\n pairs.push([i - 1, j - 1]);\n i--; j--;\n } else if (dp[(i - 1) * width + j] >= dp[i * width + (j - 1)]) {\n i--;\n } else {\n j--;\n }\n }\n pairs.reverse();\n return pairs;\n}\n\n/**\n * Match the tokens of one line against another's.\n *\n * Scored by visible characters rather than token count, so an alignment that\n * keeps `items.reduce` together beats one that keeps four more spaces. A token\n * pairs only when its content is unchanged — a changed word is a different word,\n * and should be seen to be replaced.\n */\nfunction alignTokens(from: IdToken[], to: IdToken[]): Array<[number, number]> {\n return align(from.length, to.length, (i, j) => {\n if (from[i].content !== to[j].content) return 0;\n // Whitespace still pairs (it holds indentation together), just weakly\n // enough that it never outranks real content.\n return tokenWeight(to[j]) || 0.25;\n });\n}\n\nfunction sameTokens(a: IdLine, b: IdLine): boolean {\n if (a.tokens.length !== b.tokens.length) return false;\n for (let k = 0; k < a.tokens.length; k++) if (a.tokens[k].content !== b.tokens[k].content) return false;\n return true;\n}\n\n/** A line's text, token boundaries included, for telling identical lines apart from similar ones. */\nfunction signature(line: IdLine): string {\n let out = \"\";\n for (const tok of line.tokens) out += tok.content + \"\\u0001\";\n return out;\n}\n\n/**\n * Pair the lines of two listings, in order.\n *\n * Lines that are the same at the start and at the end are paired outright. In\n * what is left, a line that occurs exactly once on each side is an anchor, and\n * the longest run of anchors that keeps their order splits the rest into runs of\n * changed lines, each scored against itself within the budget (see\n * {@link LINE_ALIGN_CELLS}). A one-line edit to a long listing therefore never\n * builds a table bigger than the lines around it.\n */\nfunction alignLines(from: IdLine[], to: IdLine[]): Array<[number, number]> {\n const fromSig = from.map(signature);\n const toSig = to.map(signature);\n const pairs: Array<[number, number]> = [];\n\n let head = 0;\n while (head < from.length && head < to.length && fromSig[head] === toSig[head]) {\n pairs.push([head, head]);\n head++;\n }\n let tail = 0;\n while (\n tail < from.length - head && tail < to.length - head\n && fromSig[from.length - 1 - tail] === toSig[to.length - 1 - tail]\n ) tail++;\n\n const anchors = anchorLines(fromSig, toSig, head, from.length - tail, head, to.length - tail);\n let budget = EDIT_ALIGN_CELLS;\n let fi = head;\n let ti = head;\n for (const [af, at] of [...anchors, [from.length - tail, to.length - tail] as [number, number]]) {\n const rows = af - fi;\n const cols = at - ti;\n const cells = rows * cols;\n if (cells > 0 && cells <= LINE_ALIGN_CELLS && cells <= budget) {\n budget -= cells;\n for (const [i, j] of align(rows, cols, (a, b) => similarity(from[fi + a], to[ti + b]) - LINE_MATCH_FLOOR)) {\n pairs.push([fi + i, ti + j]);\n }\n }\n if (af < from.length - tail) pairs.push([af, at]);\n fi = af + 1;\n ti = at + 1;\n }\n for (let k = tail; k > 0; k--) pairs.push([from.length - k, to.length - k]);\n return pairs;\n}\n\n/**\n * The lines that occur once in `from[fa, fb)` and once in `to[ta, tb)`, reduced to\n * the longest subsequence that is increasing on both sides (patience sorting).\n * Blank lines are never anchors: they say nothing about where they belong.\n */\nfunction anchorLines(\n fromSig: string[],\n toSig: string[],\n fa: number,\n fb: number,\n ta: number,\n tb: number,\n): Array<[number, number]> {\n const fromAt = new Map<string, number>();\n const seen = new Set<string>();\n for (let i = fa; i < fb; i++) {\n const sig = fromSig[i];\n if (seen.has(sig)) fromAt.delete(sig);\n else fromAt.set(sig, i);\n seen.add(sig);\n }\n const toAt = new Map<string, number>();\n const seenTo = new Set<string>();\n for (let j = ta; j < tb; j++) {\n const sig = toSig[j];\n if (seenTo.has(sig)) toAt.delete(sig);\n else toAt.set(sig, j);\n seenTo.add(sig);\n }\n\n // In `to` order, each candidate carries its `from` position.\n const candidates: Array<[number, number]> = [];\n for (let j = ta; j < tb; j++) {\n const i = fromAt.get(toSig[j]);\n if (i !== undefined && toAt.get(toSig[j]) === j && toSig[j].replaceAll(\"\\u0001\", \"\").trim() !== \"\") {\n candidates.push([i, j]);\n }\n }\n\n const tails: number[] = [];\n const previous: number[] = new Array(candidates.length).fill(-1);\n for (let c = 0; c < candidates.length; c++) {\n let lo = 0;\n let hi = tails.length;\n while (lo < hi) {\n const mid = (lo + hi) >> 1;\n if (candidates[tails[mid]][0] < candidates[c][0]) lo = mid + 1;\n else hi = mid;\n }\n if (lo > 0) previous[c] = tails[lo - 1];\n tails[lo] = c;\n }\n const chain: Array<[number, number]> = [];\n for (let c = tails.length > 0 ? tails[tails.length - 1] : -1; c >= 0; c = previous[c]) chain.push(candidates[c]);\n return chain.reverse();\n}\n\n/**\n * Carry `from`'s identities onto `to`, and report what changed.\n *\n * `to` is freshly tokenized and owns ids nobody has seen yet, so rewriting them\n * in place is safe and is what makes the transition cheap: after this, the two\n * structures share an id for every token that survived the edit.\n */\nexport function diffCode(from: IdLine[], to: IdLine[]): CodeEdit {\n const removedIds = new Set<number>();\n const addedIds = new Set<number>();\n const newLineIds = new Set<number>();\n const fromColorById = new Map<number, string | undefined>();\n\n const linePairs = alignLines(from, to);\n\n const pairedTo = new Set<number>();\n const pairedFrom = new Set<number>();\n\n for (const [fi, ti] of linePairs) {\n pairedFrom.add(fi);\n pairedTo.add(ti);\n const fromLine = from[fi];\n const toLine = to[ti];\n toLine.id = fromLine.id;\n\n const matched = sameTokens(fromLine, toLine)\n ? fromLine.tokens.map((_, k): [number, number] => [k, k])\n : alignTokens(fromLine.tokens, toLine.tokens);\n const keptFrom = new Set<number>();\n const keptTo = new Set<number>();\n for (const [a, b] of matched) {\n keptFrom.add(a);\n keptTo.add(b);\n const oldTok = fromLine.tokens[a];\n const newTok = toLine.tokens[b];\n newTok.id = oldTok.id;\n if (oldTok.color !== newTok.color) fromColorById.set(oldTok.id, oldTok.color);\n }\n for (let a = 0; a < fromLine.tokens.length; a++) {\n if (!keptFrom.has(a)) removedIds.add(fromLine.tokens[a].id);\n }\n for (let b = 0; b < toLine.tokens.length; b++) {\n if (!keptTo.has(b)) addedIds.add(toLine.tokens[b].id);\n }\n }\n\n for (let i = 0; i < from.length; i++) {\n if (pairedFrom.has(i)) continue;\n for (const tok of from[i].tokens) removedIds.add(tok.id);\n }\n for (let j = 0; j < to.length; j++) {\n if (pairedTo.has(j)) continue;\n newLineIds.add(to[j].id);\n for (const tok of to[j].tokens) addedIds.add(tok.id);\n }\n\n return { lines: to, removedIds, addedIds, newLineIds, fromColorById };\n}\n"]}
package/dist/engine.d.ts CHANGED
@@ -1,10 +1,10 @@
1
1
  export * from './transitions.js';
2
2
  export * from './layout.js';
3
3
  export { drawCode } from './render.js';
4
- export type { DrawCodeState } from './render.js';
4
+ export type { CodeCanvas, DrawCodeState } from './render.js';
5
5
  export { diffCode as defaultCodeDiff } from './diff.js';
6
6
  export type { CodeDiffStrategy, CodeEdit } from './diff.js';
7
- export { loadCodeLanguage, initSyntaxHighlighter, ensureHighlighter, registerCodeTheme, resolveTheme, canHighlight } from './highlight.js';
7
+ export { loadCodeLanguage, initSyntaxHighlighter, ensureHighlighter, registerCodeTheme, resolveTheme, canHighlight, normalizeSource } from './highlight.js';
8
8
  export { tokenizeCodeToIdLines } from './tokens.js';
9
9
  export type { IdLine, IdToken } from './tokens.js';
10
10
  export { DefaultHighlightStyle, GithubDarkStyle, GithubLightStyle, VscodeDarkStyle, VscodeLightStyle, BUILTIN_THEMES, compileStyle } from './style.js';
@@ -1 +1 @@
1
- {"version":3,"file":"engine.d.ts","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAOA,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,YAAY,EAAE,aAAa,EAAE,MAAM,UAAU,CAAC;AAC9C,OAAO,EAAE,QAAQ,IAAI,eAAe,EAAE,MAAM,QAAQ,CAAC;AACrD,YAAY,EAAE,gBAAgB,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AACzD,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACxI,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AACjD,YAAY,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,UAAU,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,gBAAgB,EAAE,eAAe,EAAE,gBAAgB,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,S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B,EAAE,kBAAkB,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAC/G,YAAY,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC"}
package/dist/engine.js CHANGED
@@ -1,15 +1,15 @@
1
1
  // The Code "engine": syntax highlighting, tokenizing, layout, diffing, and
2
- // transition/phase scheduling — kept separate from node.ts so `ms add code`
3
- // can copy just the node declaration and import this behind
2
+ // transition/phase scheduling — kept separate from node.ts so a copy of the node
3
+ // can take just its declaration and import this behind
4
4
  // `@motionscript/code` instead. What gets copied is whatever this
5
5
  // package's registry.json declares, which for this component is node.ts;
6
- // packages/site/scripts/build-registry.mjs enforces that anything it
6
+ // src/tests/registry.test.ts checks that anything it
7
7
  // reaches for is either copied alongside it or re-exported from here.
8
8
  export * from './transitions.js';
9
9
  export * from './layout.js';
10
10
  export { drawCode } from './render.js';
11
11
  export { diffCode as defaultCodeDiff } from './diff.js';
12
- export { loadCodeLanguage, initSyntaxHighlighter, ensureHighlighter, registerCodeTheme, resolveTheme, canHighlight } from './highlight.js';
12
+ export { loadCodeLanguage, initSyntaxHighlighter, ensureHighlighter, registerCodeTheme, resolveTheme, canHighlight, normalizeSource } from './highlight.js';
13
13
  export { tokenizeCodeToIdLines } from './tokens.js';
14
14
  export { DefaultHighlightStyle, GithubDarkStyle, GithubLightStyle, VscodeDarkStyle, VscodeLightStyle, BUILTIN_THEMES, compileStyle } from './style.js';
15
15
  export { TokenAdvanceCache } from './measure-cache.js';
@@ -1 +1 @@
1
- {"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,4EAA4E;AAC5E,4DAA4D;AAC5D,kEAAkE;AAClE,yEAAyE;AACzE,qEAAqE;AACrE,sEAAsE;AACtE,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,OAAO,EAAE,QAAQ,IAAI,eAAe,EAAE,MAAM,QAAQ,CAAC;AAErD,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AACxI,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAEjD,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,gBAAgB,EAAE,eAAe,EAAE,gBAAgB,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEpJ,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC","sourcesContent":["// The Code \"engine\": syntax highlighting, tokenizing, layout, diffing, and\n// transition/phase scheduling — kept separate from node.ts so `ms add code`\n// can copy just the node declaration and import this behind\n// `@motionscript/code` instead. What gets copied is whatever this\n// package's registry.json declares, which for this component is node.ts;\n// packages/site/scripts/build-registry.mjs enforces that anything it\n// reaches for is either copied alongside it or re-exported from here.\nexport * from './transitions';\nexport * from './layout';\nexport { drawCode } from './render';\nexport type { DrawCodeState } from './render';\nexport { diffCode as defaultCodeDiff } from './diff';\nexport type { CodeDiffStrategy, CodeEdit } from './diff';\nexport { loadCodeLanguage, initSyntaxHighlighter, ensureHighlighter, registerCodeTheme, resolveTheme, canHighlight } from './highlight';\nexport { tokenizeCodeToIdLines } from './tokens';\nexport type { IdLine, IdToken } from './tokens';\nexport { DefaultHighlightStyle, GithubDarkStyle, GithubLightStyle, VscodeDarkStyle, VscodeLightStyle, BUILTIN_THEMES, compileStyle } from './style';\nexport type { CodeHighlightStyle, CodeStyleRule, CodeTheme, CodeThemeName } from './style';\nexport { TokenAdvanceCache } from './measure-cache';\nexport { word, lines, rangeToCharOffsets, charOffsetsToRange, CodeRanges, CodeRangeChain } from './code-range';\nexport type { CodeRange } from './code-range';\n"]}
1
+ {"version":3,"file":"engine.js","sourceRoot":"","sources":["../src/engine.ts"],"names":[],"mappings":"AAAA,2EAA2E;AAC3E,iFAAiF;AACjF,uDAAuD;AACvD,kEAAkE;AAClE,yEAAyE;AACzE,qDAAqD;AACrD,sEAAsE;AACtE,cAAc,eAAe,CAAC;AAC9B,cAAc,UAAU,CAAC;AACzB,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAEpC,OAAO,EAAE,QAAQ,IAAI,eAAe,EAAE,MAAM,QAAQ,CAAC;AAErD,OAAO,EAAE,gBAAgB,EAAE,qBAAqB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,YAAY,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACzJ,OAAO,EAAE,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAEjD,OAAO,EAAE,qBAAqB,EAAE,eAAe,EAAE,gBAAgB,EAAE,eAAe,EAAE,gBAAgB,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEpJ,OAAO,EAAE,iBAAiB,EAAE,MAAM,iBAAiB,CAAC;AACpD,OAAO,EAAE,IAAI,EAAE,KAAK,EAAE,kBAAkB,EAAE,kBAAkB,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC","sourcesContent":["// The Code \"engine\": syntax highlighting, tokenizing, layout, diffing, and\n// transition/phase scheduling — kept separate from node.ts so a copy of the node\n// can take just its declaration and import this behind\n// `@motionscript/code` instead. What gets copied is whatever this\n// package's registry.json declares, which for this component is node.ts;\n// src/tests/registry.test.ts checks that anything it\n// reaches for is either copied alongside it or re-exported from here.\nexport * from './transitions';\nexport * from './layout';\nexport { drawCode } from './render';\nexport type { CodeCanvas, DrawCodeState } from './render';\nexport { diffCode as defaultCodeDiff } from './diff';\nexport type { CodeDiffStrategy, CodeEdit } from './diff';\nexport { loadCodeLanguage, initSyntaxHighlighter, ensureHighlighter, registerCodeTheme, resolveTheme, canHighlight, normalizeSource } from './highlight';\nexport { tokenizeCodeToIdLines } from './tokens';\nexport type { IdLine, IdToken } from './tokens';\nexport { DefaultHighlightStyle, GithubDarkStyle, GithubLightStyle, VscodeDarkStyle, VscodeLightStyle, BUILTIN_THEMES, compileStyle } from './style';\nexport type { CodeHighlightStyle, CodeStyleRule, CodeTheme, CodeThemeName } from './style';\nexport { TokenAdvanceCache } from './measure-cache';\nexport { word, lines, rangeToCharOffsets, charOffsetsToRange, CodeRanges, CodeRangeChain } from './code-range';\nexport type { CodeRange } from './code-range';\n"]}
@@ -1,4 +1,10 @@
1
1
  import { CodeHighlightStyle, CodeTheme } from "./style.js";
2
+ /**
3
+ * The source as the highlighter and the commands see it: lines joined by `\n`.
4
+ * A `\r\n` (or a lone `\r`) from a file or a clipboard would otherwise survive
5
+ * as a character at the end of a line, measured and drawn as a token.
6
+ */
7
+ export declare function normalizeSource(code: string): string;
2
8
  export declare const DEFAULT_THEMES: string[];
3
9
  export declare const DEFAULT_LANGS: string[];
4
10
  /** Register a custom {@link CodeHighlightStyle} so Code nodes can reference it by name. */
@@ -6,7 +12,7 @@ export declare function registerCodeTheme(style: CodeHighlightStyle): void;
6
12
  /**
7
13
  * Resolve a theme reference (a registered name, or a CodeHighlightStyle object)
8
14
  * to a concrete style. Unknown names fall back to the Motion Canvas default so a
9
- * stray theme string never throws.
15
+ * stray theme string never throws, with one warning per name.
10
16
  */
11
17
  export declare function resolveTheme(theme: CodeTheme): CodeHighlightStyle;
12
18
  /**
@@ -14,7 +20,9 @@ export declare function resolveTheme(theme: CodeTheme): CodeHighlightStyle;
14
20
  * but kept in the signature for API parity with the former Shiki implementation).
15
21
  * Resolves once every requested language's parser is available. Unknown languages
16
22
  * resolve without loading anything — {@link canHighlight} stays false for them and
17
- * the node renders them as plain text.
23
+ * the node renders them as plain text, with one warning per language. A grammar
24
+ * whose import is rejected is reported the same way and is tried again by the
25
+ * next call.
18
26
  */
19
27
  export declare function ensureHighlighter(_themes?: string[], langs?: string[]): Promise<void>;
20
28
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"highlight.d.ts","sourceRoot":"","sources":["../src/highlight.ts"],"names":[],"mappings":"AAEA,OAAO,EAEH,kBAAkB,EAClB,SAAS,EAGZ,MAAM,SAAS,CAAC;AA0DjB,eAAO,MAAM,cAAc,UAA+B,CAAC;AAC3D,eAAO,MAAM,aAAa,UAAiD,CAAC;AAO5E,2FAA2F;AAC3F,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,kBAAkB,GAAG,IAAI,CAEjE;AAED;;;;GAIG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,GAAG,kBAAkB,CAGjE;AAOD;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAC7B,OAAO,GAAE,MAAM,EAAmB,EAClC,KAAK,GAAE,MAAM,EAAkB,GAChC,OAAO,CAAC,IAAI,CAAC,CAyBf;AAED;;;;;GAKG;AACH,wBAAsB,gBAAgB,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEvF;AAED,wBAAsB,qBAAqB,CACvC,MAAM,GAAE,MAAM,EAAmB,EACjC,KAAK,GAAE,MAAM,EAAkB,GAChC,OAAO,CAAC,IAAI,CAAC,CAEf;AAED,qEAAqE;AACrE,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,SAAS,GAAG,OAAO,CAE1E;AAqFD;;;;;;;;GAQG;AACH,wBAAgB,iBAAiB,CAC7B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,KAAK,GAAE,SAAiC,GACzC;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,EAAE,EAAE,CAyCzC"}
1
+ {"version":3,"file":"highlight.d.ts","sourceRoot":"","sources":["../src/highlight.ts"],"names":[],"mappings":"AAEA,OAAO,EAEH,kBAA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