Visualize Your Ideas With Code
feat: new Code node (#946)
Jacob26e55a37c416
diff --git a/package-lock.json b/package-lock.jsonindex 1a081a06..34cc6b86 100644--- a/package-lock.json+++ b/package-lock.json@@ -2278,13 +2278,13 @@}},"node_modules/@codemirror/language": {- "version": "6.6.0",- "resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.6.0.tgz",- "integrity": "sha512-cwUd6lzt3MfNYOobdjf14ZkLbJcnv4WtndYaoBkbor/vF+rCNguMPK0IRtvZJG4dsWiaWPcK8x1VijhvSxnstg==",+ "version": "6.10.1",+ "resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.10.1.tgz",+ "integrity": "sha512-5GrXzrhq6k+gL5fjkAwt90nYDmjlzTIJV8THnxNFtNKWotMIlzzN+CpqxqwXOECnUdOndmSeWntVrVcv5axWRQ==","dependencies": {"@codemirror/state": "^6.0.0",- "@codemirror/view": "^6.0.0",- "@lezer/common": "^1.0.0",+ "@codemirror/view": "^6.23.0",+ "@lezer/common": "^1.1.0","@lezer/highlight": "^1.0.0","@lezer/lr": "^1.0.0","style-mod": "^4.0.0"@@ -2311,17 +2311,17 @@}},"node_modules/@codemirror/state": {- "version": "6.2.0",- "resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.2.0.tgz",- "integrity": "sha512-69QXtcrsc3RYtOtd+GsvczJ319udtBf1PTrr2KbLWM/e2CXUPnh0Nz9AUo8WfhSQ7GeL8dPVNUmhQVgpmuaNGA=="+ "version": "6.4.0",+ "resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.4.0.tgz",+ "integrity": "sha512-hm8XshYj5Fo30Bb922QX9hXB/bxOAVH+qaqHBzw5TKa72vOeslyGwd4X8M0c1dJ9JqxlaMceOQ8RsL9tC7gU0A=="},"node_modules/@codemirror/view": {- "version": "6.9.2",- "resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.9.2.tgz",- "integrity": "sha512-ci0r/v6aKOSlzOs7/STMTYP3jX/+YMq2dAfAJcLIB6uom4ThtrUlzeuS7GTRGNqJJ+qAJR1vGWfXgu4CO/0myQ==",+ "version": "6.23.1",+ "resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.23.1.tgz",+ "integrity": "sha512-J2Xnn5lFYT1ZN/5ewEoMBCmLlL71lZ3mBdb7cUEuHhX2ESoSrNEucpsDXpX22EuTGm9LOgC9v4Z0wx+Ez8QmGA==","dependencies": {- "@codemirror/state": "^6.1.4",- "style-mod": "^4.0.0",+ "@codemirror/state": "^6.4.0",+ "style-mod": "^4.1.0","w3c-keyname": "^2.2.4"}},@@ -4766,14 +4766,14 @@}},"node_modules/@lezer/common": {- "version": "1.0.2",- "resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.0.2.tgz",- "integrity": "sha512-SVgiGtMnMnW3ActR8SXgsDhw7a0w0ChHSYAyAUxxrOiJ1OqYWEKk/xJd84tTSPo1mo6DXLObAJALNnd0Hrv7Ng=="+ "version": "1.2.1",+ "resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.2.1.tgz",+ "integrity": "sha512-yemX0ZD2xS/73llMZIK6KplkjIjf2EvAHcinDi/TfJ9hS25G0388+ClHt6/3but0oOxinTcQHJLDXh6w1crzFQ=="},"node_modules/@lezer/highlight": {- "version": "1.1.3",- "resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.1.3.tgz",- "integrity": "sha512-3vLKLPThO4td43lYRBygmMY18JN3CPh9w+XS2j8WC30vR4yZeFG4z1iFe4jXE43NtGqe//zHW5q8ENLlHvz9gw==",+ "version": "1.2.0",+ "resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.2.0.tgz",+ "integrity": "sha512-WrS5Mw51sGrpqjlh3d4/fOwpEV2Hd3YOkp9DBt4k8XZQcoTHZFB7sx030A6OcahF4J1nDQAa3jXlTVVYH50IFA==","dependencies": {"@lezer/common": "^1.0.0"}@@ -23180,9 +23180,9 @@"dev": true},"node_modules/style-mod": {- "version": "4.0.2",- "resolved": "https://registry.npmjs.org/style-mod/-/style-mod-4.0.2.tgz",- "integrity": "sha512-C4myMmRTO8iaC5Gg+N1ftK2WT4eXUTMAa+HEFPPrfVeO/NtqLTtAmV1HbqnuGtLwCek44Ra76fdGUkSqjiMPcQ=="+ "version": "4.1.0",+ "resolved": "https://registry.npmjs.org/style-mod/-/style-mod-4.1.0.tgz",+ "integrity": "sha512-Ca5ib8HrFn+f+0n4N4ScTIA9iTOQ7MaGS1ylHcoVqW9J7w2w8PzN6g9gKmTYgGEBH8e120+RCmhpje6jC5uGWA=="},"node_modules/style-to-object": {"version": "0.3.0",@@ -25681,6 +25681,9 @@"version": "3.14.2","license": "MIT","dependencies": {+ "@codemirror/language": "^6.10.1",+ "@lezer/common": "^1.2.1",+ "@lezer/highlight": "^1.2.0","@motion-canvas/core": "^3.14.1","code-fns": "^0.8.2","mathjax-full": "^3.2.2",@@ -27534,13 +27537,13 @@}},"@codemirror/language": {- "version": "6.6.0",- "resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.6.0.tgz",- "integrity": "sha512-cwUd6lzt3MfNYOobdjf14ZkLbJcnv4WtndYaoBkbor/vF+rCNguMPK0IRtvZJG4dsWiaWPcK8x1VijhvSxnstg==",+ "version": "6.10.1",+ "resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.10.1.tgz",+ "integrity": "sha512-5GrXzrhq6k+gL5fjkAwt90nYDmjlzTIJV8THnxNFtNKWotMIlzzN+CpqxqwXOECnUdOndmSeWntVrVcv5axWRQ==","requires": {"@codemirror/state": "^6.0.0",- "@codemirror/view": "^6.0.0",- "@lezer/common": "^1.0.0",+ "@codemirror/view": "^6.23.0",+ "@lezer/common": "^1.1.0","@lezer/highlight": "^1.0.0","@lezer/lr": "^1.0.0","style-mod": "^4.0.0"@@ -27567,17 +27570,17 @@}},"@codemirror/state": {- "version": "6.2.0",- "resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.2.0.tgz",- "integrity": "sha512-69QXtcrsc3RYtOtd+GsvczJ319udtBf1PTrr2KbLWM/e2CXUPnh0Nz9AUo8WfhSQ7GeL8dPVNUmhQVgpmuaNGA=="+ "version": "6.4.0",+ "resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.4.0.tgz",+ "integrity": "sha512-hm8XshYj5Fo30Bb922QX9hXB/bxOAVH+qaqHBzw5TKa72vOeslyGwd4X8M0c1dJ9JqxlaMceOQ8RsL9tC7gU0A=="},"@codemirror/view": {- "version": "6.9.2",- "resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.9.2.tgz",- "integrity": "sha512-ci0r/v6aKOSlzOs7/STMTYP3jX/+YMq2dAfAJcLIB6uom4ThtrUlzeuS7GTRGNqJJ+qAJR1vGWfXgu4CO/0myQ==",+ "version": "6.23.1",+ "resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.23.1.tgz",+ "integrity": "sha512-J2Xnn5lFYT1ZN/5ewEoMBCmLlL71lZ3mBdb7cUEuHhX2ESoSrNEucpsDXpX22EuTGm9LOgC9v4Z0wx+Ez8QmGA==","requires": {- "@codemirror/state": "^6.1.4",- "style-mod": "^4.0.0",+ "@codemirror/state": "^6.4.0",+ "style-mod": "^4.1.0","w3c-keyname": "^2.2.4"}},@@ -29267,14 +29270,14 @@}},"@lezer/common": {- "version": "1.0.2",- "resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.0.2.tgz",- "integrity": "sha512-SVgiGtMnMnW3ActR8SXgsDhw7a0w0ChHSYAyAUxxrOiJ1OqYWEKk/xJd84tTSPo1mo6DXLObAJALNnd0Hrv7Ng=="+ "version": "1.2.1",+ "resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.2.1.tgz",+ "integrity": "sha512-yemX0ZD2xS/73llMZIK6KplkjIjf2EvAHcinDi/TfJ9hS25G0388+ClHt6/3but0oOxinTcQHJLDXh6w1crzFQ=="},"@lezer/highlight": {- "version": "1.1.3",- "resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.1.3.tgz",- "integrity": "sha512-3vLKLPThO4td43lYRBygmMY18JN3CPh9w+XS2j8WC30vR4yZeFG4z1iFe4jXE43NtGqe//zHW5q8ENLlHvz9gw==",+ "version": "1.2.0",+ "resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.2.0.tgz",+ "integrity": "sha512-WrS5Mw51sGrpqjlh3d4/fOwpEV2Hd3YOkp9DBt4k8XZQcoTHZFB7sx030A6OcahF4J1nDQAa3jXlTVVYH50IFA==","requires": {"@lezer/common": "^1.0.0"}@@ -29482,6 +29485,9 @@"@motion-canvas/2d": {"version": "file:packages/2d","requires": {+ "@codemirror/language": "^6.10.1",+ "@lezer/common": "^1.2.1",+ "@lezer/highlight": "^1.2.0","@motion-canvas/core": "^3.14.1","@motion-canvas/internal": "0.0.0","@motion-canvas/ui": "^3.14.2",@@ -42174,9 +42180,9 @@"dev": true},"style-mod": {- "version": "4.0.2",- "resolved": "https://registry.npmjs.org/style-mod/-/style-mod-4.0.2.tgz",- "integrity": "sha512-C4myMmRTO8iaC5Gg+N1ftK2WT4eXUTMAa+HEFPPrfVeO/NtqLTtAmV1HbqnuGtLwCek44Ra76fdGUkSqjiMPcQ=="+ "version": "4.1.0",+ "resolved": "https://registry.npmjs.org/style-mod/-/style-mod-4.1.0.tgz",+ "integrity": "sha512-Ca5ib8HrFn+f+0n4N4ScTIA9iTOQ7MaGS1ylHcoVqW9J7w2w8PzN6g9gKmTYgGEBH8e120+RCmhpje6jC5uGWA=="},"style-to-object": {"version": "0.3.0",diff --git a/packages/2d/package.json b/packages/2d/package.jsonindex bf363dbb..70ce0478 100644--- a/packages/2d/package.json+++ b/packages/2d/package.json@@ -37,6 +37,9 @@"vitest": "^0.34.6"},"dependencies": {+ "@codemirror/language": "^6.10.1",+ "@lezer/common": "^1.2.1",+ "@lezer/highlight": "^1.2.0","@motion-canvas/core": "^3.14.1","code-fns": "^0.8.2","mathjax-full": "^3.2.2",diff --git a/packages/2d/src/lib/code/CodeCursor.ts b/packages/2d/src/lib/code/CodeCursor.tsnew file mode 100644index 00000000..63c68263--- /dev/null+++ b/packages/2d/src/lib/code/CodeCursor.ts@@ -0,0 +1,421 @@+import {+ clampRemap,+ Color,+ map,+ SerializedVector2,+ unwrap,+ Vector2,+} from '@motion-canvas/core';+import {Code, DrawHooks} from '../components';+import {CodeFragment, parseCodeFragment} from './CodeFragment';+import {CodeHighlighter} from './CodeHighlighter';+import {CodeMetrics} from './CodeMetrics';+import {CodePoint, CodeRange} from './CodeRange';+import {CodeScope, isCodeScope} from './CodeScope';+import {isPointInCodeSelection} from './CodeSelection';++/**+ * A stateful class for recursively traversing a code scope.+ *+ * @internal+ */+export class CodeCursor {+ public cursor = new Vector2();+ public beforeCursor = new Vector2();+ public afterCursor = new Vector2();+ public beforeIndex = 0;+ public afterIndex = 0;+ private context = {} as CanvasRenderingContext2D;+ private monoWidth = 0;+ private maxWidth = 0;+ private lineHeight = 0;+ private fallbackFill = new Color('white');+ private caches: {before: unknown; after: unknown} | null = null;+ private highlighter: CodeHighlighter | null = null;+ private selection: CodeRange[] = [];+ private selectionProgress: number | null = null;+ private globalProgress: number[] = [];+ private drawHooks = {} as DrawHooks;++ public constructor(private readonly node: Code) {}++ /**+ * Prepare the cursor for the next traversal.+ *+ * @param context - The context used to measure and draw the code.+ */+ public setupMeasure(context: CanvasRenderingContext2D) {+ this.context = context;+ this.monoWidth = context.measureText('X').width;+ this.lineHeight = parseFloat(this.node.styles.lineHeight);+ this.cursor = new Vector2();+ this.beforeCursor = new Vector2();+ this.afterCursor = new Vector2();+ this.beforeIndex = 0;+ this.afterIndex = 0;+ this.maxWidth = 0;+ }++ public setupDraw(context: CanvasRenderingContext2D) {+ this.setupMeasure(context);+ const fill = this.node.fill();+ this.fallbackFill =+ fill instanceof Color ? (fill as Color) : new Color('white');+ this.caches = this.node.highlighterCache();+ this.highlighter = this.node.highlighter();+ this.selection = this.node.selection();+ this.selectionProgress = this.node.selectionProgress();+ this.drawHooks = this.node.drawHooks();+ this.globalProgress = [];+ }++ /**+ * Measure the desired size of the code scope.+ *+ * @remarks+ * The result can be retrieved with {@link getSize}.+ *+ * @param scope - The code scope to measure.+ */+ public measureSize(scope: CodeScope) {+ const progress = unwrap(scope.progress);+ for (const wrapped of scope.fragments) {+ const possibleFragment = unwrap(wrapped);+ if (isCodeScope(possibleFragment)) {+ this.measureSize(possibleFragment);+ continue;+ }+ if (Array.isArray(possibleFragment)) {+ this.measureSize({+ progress: scope.progress,+ fragments: possibleFragment,+ });+ continue;+ }++ const fragment = parseCodeFragment(+ possibleFragment,+ this.context,+ this.monoWidth,+ );++ const beforeMaxWidth = this.calculateMaxWidth(fragment.before);+ const afterMaxWidth = this.calculateMaxWidth(fragment.after);++ const maxWidth = map(beforeMaxWidth, afterMaxWidth, progress);+ if (maxWidth > this.maxWidth) {+ this.maxWidth = maxWidth;+ }++ const beforeEnd = this.calculateWidth(fragment.before);+ const afterEnd = this.calculateWidth(fragment.after);+ this.cursor.x = map(beforeEnd, afterEnd, progress);++ if (this.cursor.y === 0) {+ this.cursor.y = 1;+ }++ this.cursor.y += map(+ fragment.before.newRows,+ fragment.after.newRows,+ progress,+ );+ }+ }++ /**+ * Get the size measured by the cursor.+ */+ public getSize() {+ return {+ x: this.maxWidth * this.monoWidth,+ y: this.cursor.y * this.lineHeight,+ };+ }++ /**+ * Draw the given code scope.+ *+ * @param scope - The code scope to draw.+ */+ public drawScope(scope: CodeScope) {+ const progress = unwrap(scope.progress);+ for (const wrappedFragment of scope.fragments) {+ const possibleFragment = unwrap(wrappedFragment);+ if (isCodeScope(possibleFragment)) {+ this.drawScope(possibleFragment);+ continue;+ }+ if (Array.isArray(possibleFragment)) {+ this.drawScope({+ progress: scope.progress,+ fragments: possibleFragment,+ });+ continue;+ }++ const fragment = parseCodeFragment(+ possibleFragment,+ this.context,+ this.monoWidth,+ );+ const timingOffset = 0.8;+ let alpha = 1;+ let offsetY = 0;+ if (fragment.before.content !== fragment.after.content) {+ const mirrored = Math.abs(progress - 0.5) * 2;+ alpha = clampRemap(1, 1 - timingOffset, 1, 0, mirrored);++ const scale = progress < 0.5 ? 4 : -4;+ offsetY = map(+ Math.abs(fragment.after.newRows - fragment.before.newRows) / scale,+ 0,+ mirrored,+ );+ }++ this.drawToken(fragment, scope, this.cursor.addY(offsetY), alpha);++ this.beforeCursor.x = this.calculateWidth(+ fragment.before,+ this.beforeCursor.x,+ );+ this.afterCursor.x = this.calculateWidth(+ fragment.after,+ this.afterCursor.x,+ );+ this.beforeCursor.y += fragment.before.newRows;+ this.afterCursor.y += fragment.after.newRows;++ this.beforeIndex += fragment.before.content.length;+ this.afterIndex += fragment.after.content.length;++ this.cursor.y += map(+ fragment.before.newRows,+ fragment.after.newRows,+ progress,+ );++ const beforeEnd = this.calculateWidth(fragment.before);+ const afterEnd = this.calculateWidth(fragment.after);+ this.cursor.x = map(beforeEnd, afterEnd, progress);+ }+ }++ private drawToken(+ fragment: CodeFragment,+ scope: CodeScope,+ offset: SerializedVector2,+ alpha: number,+ ) {+ const progress = unwrap(scope.progress);+ const currentProgress = this.currentProgress();+ if (progress > 0) {+ this.globalProgress.push(progress);+ }++ const code = progress < 0.5 ? fragment.before : fragment.after;++ this.context.save();+ this.context.globalAlpha *= alpha;+ let hasOffset = true;+ let width = 0;+ let stringLength = 0;+ let y = 0;+ for (let i = 0; i < code.content.length; i++) {+ let color = this.fallbackFill.serialize();+ let char = code.content.charAt(i);+ const selection: {before: number | null; after: number | null} = {+ before: null,+ after: null,+ };++ if (char === '\n') {+ y++;+ hasOffset = false;+ width = 0;+ stringLength = 0;+ selection.before = null;+ selection.after = null;+ continue;+ }++ this.context.save();++ const beforeHighlight =+ this.caches &&+ this.highlighter?.highlight(this.beforeIndex + i, this.caches.before);+ const afterHighlight =+ this.caches &&+ this.highlighter?.highlight(this.afterIndex + i, this.caches.after);++ const highlight = progress < 0.5 ? beforeHighlight : afterHighlight;+ if (highlight) {+ // Handle edge cases where the highlight style changes despite the+ // content being the same. The code doesn't fade in and out so the color+ // has to be interpolated to avoid jarring changes.+ if (+ fragment.before.content === fragment.after.content &&+ beforeHighlight?.color !== afterHighlight?.color+ ) {+ highlight.color = Color.lerp(+ beforeHighlight?.color ?? this.fallbackFill,+ afterHighlight?.color ?? this.fallbackFill,+ progress,+ ).serialize();+ }++ if (highlight.color) {+ color = highlight.color;+ }++ let skipAhead = 0;+ do {+ if (+ this.processSelection(+ selection,+ skipAhead,+ hasOffset,+ stringLength,+ y,+ )+ ) {+ break;+ }++ skipAhead++;+ } while (skipAhead < highlight.skipAhead);++ if (skipAhead > 1) {+ char = code.content.slice(i, i + skipAhead);+ }++ i += char.length - 1;+ } else {+ this.processSelection(selection, 0, hasOffset, stringLength, y);+ let skipAhead = 1;+ while (+ i < code.content.length - 1 &&+ code.content.charAt(i + 1) !== '\n'+ ) {+ if (+ this.processSelection(+ selection,+ skipAhead,+ hasOffset,+ stringLength,+ y,+ )+ ) {+ break;+ }++ skipAhead++;+ char += code.content.charAt(++i);+ }+ }++ let time: number;+ const selectionAfter = selection.after ?? 0;+ const selectionBefore = selection.before ?? 0;+ if (fragment.before.content === '') {+ time = selectionAfter;+ } else if (fragment.after.content === '') {+ time = selectionBefore;+ } else {+ time = map(+ selectionBefore,+ selectionAfter,+ this.selectionProgress ?? currentProgress,+ );+ }++ this.drawHooks.token(+ this.context,+ char,+ new Vector2(+ (hasOffset ? offset.x + width : width) * this.monoWidth,+ (offset.y + y) * this.lineHeight,+ ),+ color,+ time,+ );+ this.context.restore();++ stringLength += char.length;+ width += Math.round(+ this.context.measureText(char).width / this.monoWidth,+ );+ }+ this.context.restore();+ }++ private calculateWidth(metrics: CodeMetrics, x = this.cursor.x): number {+ return metrics.newRows === 0 ? x + metrics.lastWidth : metrics.lastWidth;+ }++ private calculateMaxWidth(metrics: CodeMetrics, x = this.cursor.x): number {+ return Math.max(this.maxWidth, metrics.maxWidth, x + metrics.firstWidth);+ }++ private currentProgress() {+ if (this.globalProgress.length === 0) {+ return 0;+ }++ let sum = 0;+ for (const progress of this.globalProgress) {+ sum += progress;+ }++ return sum / this.globalProgress.length;+ }++ private processSelection(+ selection: {before: number | null; after: number | null},+ skipAhead: number,+ hasOffset: boolean,+ stringLength: number,+ y: number,+ ): boolean {+ let shouldBreak = false;+ let currentSelected = this.isSelected(+ (hasOffset ? this.beforeCursor.x + stringLength : stringLength) ++ skipAhead,+ this.beforeCursor.y + y,+ );+ if (selection.before !== null && selection.before !== currentSelected) {+ shouldBreak = true;+ } else {+ selection.before = currentSelected;+ }++ currentSelected = this.isSelected(+ (hasOffset ? this.afterCursor.x + stringLength : stringLength) ++ skipAhead,+ this.afterCursor.y + y,+ true,+ );+ if (selection.after !== null && selection.after !== currentSelected) {+ shouldBreak = true;+ } else {+ selection.after = currentSelected;+ }++ return shouldBreak;+ }++ private isSelected(x: number, y: number, isAfter?: boolean): number {+ const point: CodePoint = [y, x];+ const maxSelection = isPointInCodeSelection(point, this.selection) ? 1 : 0;+ if (this.node.oldSelection === null || this.selectionProgress === null) {+ return maxSelection;+ }++ if (isAfter) {+ return maxSelection;+ }++ return isPointInCodeSelection(point, this.node.oldSelection) ? 1 : 0;+ }+}diff --git a/packages/2d/src/lib/code/CodeDiffer.ts b/packages/2d/src/lib/code/CodeDiffer.tsnew file mode 100644index 00000000..23a150c7--- /dev/null+++ b/packages/2d/src/lib/code/CodeDiffer.ts@@ -0,0 +1,77 @@+import {patienceDiff} from './diff';+import {CodeScope, CodeTag, resolveScope} from './CodeScope';+import {CodeTokenizer} from './CodeTokenizer';++/**+ * A function that compares two code snippets and returns a list of+ * {@link CodeTag}s describing a transition between them.+ */+export type CodeDiffer = (+ /**+ * The original code scope.+ */+ from: CodeScope,+ /**+ * The new code scope.+ */+ to: CodeScope,+ /**+ * The inherited tokenizer to use.+ */+ tokenize: CodeTokenizer,+) => CodeTag[];++/**+ * Default diffing function utilizing {@link code.patienceDiff}.+ *+ * @param from - The original code scope.+ * @param to - The new code scope.+ * @param tokenize - The inherited tokenizer to use.+ */+export function defaultDiffer(+ from: CodeScope,+ to: CodeScope,+ tokenize: CodeTokenizer,+) {+ const fromString = resolveScope(from, false);+ const toString = resolveScope(to, true);++ const diff = patienceDiff(tokenize(fromString), tokenize(toString));++ const fragments: CodeTag[] = [];+ let before = '';+ let after = '';+ let lastAdded = false;+ const flush = () => {+ if (before !== '' || after !== '') {+ fragments.push({+ before,+ after,+ });+ before = '';+ after = '';+ }+ };++ for (const line of diff.lines) {+ if (line.aIndex === -1) {+ if (after !== '' && !lastAdded) {+ flush();+ }+ lastAdded = true;+ after += line.line;+ } else if (line.bIndex === -1) {+ if (before !== '' && lastAdded) {+ flush();+ }+ lastAdded = false;+ before += line.line;+ } else {+ flush();+ fragments.push(line.line);+ }+ }+ flush();++ return fragments;+}diff --git a/packages/2d/src/lib/code/CodeFragment.ts b/packages/2d/src/lib/code/CodeFragment.tsnew file mode 100644index 00000000..8771a8bb--- /dev/null+++ b/packages/2d/src/lib/code/CodeFragment.ts@@ -0,0 +1,100 @@+import {+ CodeMetrics,+ isCodeMetrics,+ measureString,+} from './CodeMetrics';++export interface CodeFragment {+ before: CodeMetrics;+ after: CodeMetrics;+}+export interface RawCodeFragment {+ before: string;+ after: string;+}++export type PossibleCodeFragment =+ | CodeFragment+ | CodeMetrics+ | RawCodeFragment+ | string;++export function metricsToFragment(value: CodeMetrics): CodeFragment {+ return {+ before: value,+ after: value,+ };+}++export function parseCodeFragment(+ value: PossibleCodeFragment,+ context: CanvasRenderingContext2D,+ monoWidth: number,+): CodeFragment {+ let fragment: CodeFragment;+ if (typeof value === 'string') {+ fragment = metricsToFragment(measureString(context, monoWidth, value));+ } else if (isCodeMetrics(value)) {+ fragment = metricsToFragment(value);+ } else {+ fragment = {+ before:+ typeof value.before === 'string'+ ? measureString(context, monoWidth, value.before)+ : value.before,+ after:+ typeof value.after === 'string'+ ? measureString(context, monoWidth, value.after)+ : value.after,+ };+ }++ return fragment;+}++/**+ * Create a code fragment that represents an insertion of code.+ *+ * @remarks+ * Can be used in conjunction with {@link code.CodeSignalHelpers.edit}.+ *+ * @param code - The code to insert.+ */+export function insert(code: string): RawCodeFragment {+ return {+ before: '',+ after: code,+ };+}++/**+ * Create a code fragment that represents a change from one piece of code to+ * another.+ *+ * @remarks+ * Can be used in conjunction with {@link code.CodeSignalHelpers.edit}.+ *+ * @param before - The code to change from.+ * @param after - The code to change to.+ */+export function replace(before: string, after: string): RawCodeFragment {+ return {+ before,+ after,+ };+}++/**+ * Create a code fragment that represents a removal of code.+ *+ * @remarks+ * Can be used in conjunction with {@link code.CodeSignalHelpers.edit}.+ *+ * @param code - The code to remove.+ */+export function remove(code: string): RawCodeFragment {+ return {+ before: code,+ after: '',+ };+}diff --git a/packages/2d/src/lib/code/CodeHighlighter.ts b/packages/2d/src/lib/code/CodeHighlighter.tsnew file mode 100644index 00000000..5ec1fbda--- /dev/null+++ b/packages/2d/src/lib/code/CodeHighlighter.ts@@ -0,0 +1,75 @@+/**+ * Describes the result of a highlight operation.+ */+export interface HighlightResult {+ /**+ * The color of the code at the given index.+ */+ color: string | null;++ /**+ * The number of characters to skip ahead.+ *+ * @remarks+ * This should be used to skip to the end of the currently highlighted token.+ * The returned style will be used for the skipped characters, and they will+ * be drawn as one continuous string keeping emojis and ligatures intact.+ *+ * The returned value is the number of characters to skip ahead, not the+ * index of the end of the token.+ */+ skipAhead: number;+}++/**+ * Describes custom highlighters used by the Code node.+ *+ * @typeParam T - The type of the cache used by the highlighter.+ */+export interface CodeHighlighter<T = unknown> {+ /**+ * Initializes the highlighter.+ *+ * @remarks+ * This method is called when collecting async resources for the node.+ * It can be called multiple times so caching the initialization is+ * recommended.+ *+ * If initialization is asynchronous, a promise should be registered using+ * {@link DependencyContext.collectPromise} and the value of `false` should+ * be returned. The hook will be called again when the promise resolves.+ * This process can be repeated until the value of `true` is returned which+ * will mark the highlighter as ready.+ */+ initialize(): boolean;++ /**+ * Prepares the code for highlighting.+ *+ * @remarks+ * This method is called each time the code changes. It can be used to do+ * any preprocessing of the code before highlighting. The result of this+ * method is cached and passed to {@link highlight} when the code is+ * highlighted.+ *+ * @param code - The code to prepare.+ * @param dialect - The language in which the code is written.+ */+ prepare(code: string, dialect: string): T;++ /**+ * Highlights the code at the given index.+ *+ * @param index - The index of the code to highlight.+ * @param cache - The result of {@link prepare}.+ */+ highlight(index: number, cache: T): HighlightResult;++ /**+ * Tokenize the code.+ *+ * @param code - The code to tokenize.+ * @param dialect - The language in which the code is written.+ */+ tokenize(code: string, dialect: string): string[];+}diff --git a/packages/2d/src/lib/code/CodeMetrics.ts b/packages/2d/src/lib/code/CodeMetrics.tsnew file mode 100644index 00000000..bb92e03a--- /dev/null+++ b/packages/2d/src/lib/code/CodeMetrics.ts@@ -0,0 +1,47 @@+export interface CodeMetrics {+ content: string;+ newRows: number;+ endColumn: number;+ firstWidth: number;+ maxWidth: number;+ lastWidth: number;+}++export function measureString(+ context: CanvasRenderingContext2D,+ monoWidth: number,+ value: string,+): CodeMetrics {+ const lines = value.split('\n');+ const lastLine = lines[lines.length - 1];+ const firstWidth = Math.round(+ context.measureText(lines[0]).width / monoWidth,+ );+ let lastWidth = firstWidth;+ let maxWidth = firstWidth;++ for (let i = 1; i < lines.length - 1; i++) {+ const line = lines[i];+ const width = Math.round(context.measureText(line).width / monoWidth);+ if (width > maxWidth) {+ maxWidth = width;+ }+ }++ if (lines.length > 0) {+ lastWidth = Math.round(context.measureText(lastLine).width / monoWidth);+ }++ return {+ content: value,+ newRows: lines.length - 1,+ endColumn: lastLine.length,+ firstWidth,+ maxWidth,+ lastWidth,+ };+}++export function isCodeMetrics(value: any): value is CodeMetrics {+ return value?.content !== undefined;+}diff --git a/packages/2d/src/lib/code/CodeRange.test.ts b/packages/2d/src/lib/code/CodeRange.test.tsnew file mode 100644index 00000000..6baf7258--- /dev/null+++ b/packages/2d/src/lib/code/CodeRange.test.ts@@ -0,0 +1,74 @@+import {describe, expect, test} from 'vitest';+import {+ consolidateCodeRanges,+ inverseCodeRange,+ isPointInCodeRange,+ pointToPoint,+} from './CodeRange';++describe('CodeRange', () => {+ test('should detect ranges', () => {+ expect(+ isPointInCodeRange([50, 50], pointToPoint(0, 0, 100, 100)),+ 'Should work within both ranges',+ ).toBeTruthy();+ expect(+ isPointInCodeRange([50, 1000], pointToPoint(0, 0, 100, 100)),+ 'Should accept any column when within lines',+ ).toBeTruthy();+ expect(+ isPointInCodeRange([50, 50], pointToPoint(0, 0, Infinity, Infinity)),+ 'Should work with infinity',+ ).toBeTruthy();+ expect(+ isPointInCodeRange([100, 100], pointToPoint(0, 0, 100, 100)),+ 'Should reject points on the outer edge',+ ).toBeFalsy();+ });++ test('should consolidate ranges that are completely contained', () => {+ expect(+ consolidateCodeRanges([+ pointToPoint(0, 0, 100, 100),+ pointToPoint(20, 0, 50, 0),+ ]),+ ).toEqual([pointToPoint(0, 0, 100, 100)]);+ expect(+ consolidateCodeRanges([+ pointToPoint(20, 0, 50, 0),+ pointToPoint(0, 0, 100, 100),+ ]),+ ).toEqual([pointToPoint(0, 0, 100, 100)]);+ });++ test('should combine ranges that overlap', () => {+ expect(+ consolidateCodeRanges([+ pointToPoint(0, 5, 0, 10),+ pointToPoint(0, 7, 0, 13),+ ]),+ ).toEqual([pointToPoint(0, 5, 0, 13)]);+ expect(+ consolidateCodeRanges([+ pointToPoint(0, 7, 2, 13),+ pointToPoint(0, 5, 2, 10),+ ]),+ ).toEqual([pointToPoint(0, 5, 2, 13)]);+ });++ test('should not combine ranges that do not overlap', () => {+ expect(+ consolidateCodeRanges([+ pointToPoint(0, 5, 0, 10),+ pointToPoint(0, 7, 0, 13),+ pointToPoint(1, 5, 1, 10),+ ]),+ ).toEqual([pointToPoint(0, 5, 0, 13), pointToPoint(1, 5, 1, 10)]);+ });++ test('should invert empty ranges', () => {+ expect(inverseCodeRange([])).toEqual([+ pointToPoint(0, 0, Infinity, Infinity),+ ]);+ });+});diff --git a/packages/2d/src/lib/code/CodeRange.ts b/packages/2d/src/lib/code/CodeRange.tsnew file mode 100644index 00000000..b907cf44--- /dev/null+++ b/packages/2d/src/lib/code/CodeRange.ts@@ -0,0 +1,165 @@+export type CodePoint = [number, number];++function isCodePoint(value: unknown): value is CodePoint {+ return (+ Array.isArray(value) &&+ value.length === 2 &&+ typeof value[0] === 'number' &&+ typeof value[1] === 'number'+ );+}++export type CodeRange = [CodePoint, CodePoint];++export function isCodeRange(value: unknown): value is CodeRange {+ return (+ Array.isArray(value) &&+ value.length === 2 &&+ isCodePoint(value[0]) &&+ isCodePoint(value[1])+ );+}++/**+ * Create a code range that spans the given lines.+ *+ * @param from - The line from which the range starts.+ * @param to - The line at which the range ends. If omitted, the range will+ * cover only one line.+ */+export function lines(from: number, to?: number): CodeRange {+ return [+ [from, 0],+ [to ?? from, Infinity],+ ];+}++/**+ * Create a code range that highlights the given word.+ *+ * @param line - The line at which the word appears.+ * @param from - The column at which the word starts.+ * @param length - The length of the word. If omitted, the range will cover the+ * rest of the line.+ */+export function word(line: number, from: number, length?: number): CodeRange {+ return [+ [line, from],+ [line, from + (length ?? Infinity)],+ ];+}++/**+ * Create a custom selection range.+ *+ * @param startLine - The line at which the selection starts.+ * @param startColumn - The column at which the selection starts.+ * @param endLine - The line at which the selection ends.+ * @param endColumn - The column at which the selection ends.+ */+export function pointToPoint(+ startLine: number,+ startColumn: number,+ endLine: number,+ endColumn: number,+): CodeRange {+ return [+ [startLine, startColumn],+ [endLine, endColumn],+ ];+}++export function isPointInCodeRange(point: CodePoint, range: CodeRange) {+ const [y, x] = point;+ const [[startLine, startColumn], [endLine, endColumn]] = range;+ return (+ ((y === startLine && x >= startColumn) || y > startLine) &&+ ((y === endLine && x < endColumn) || y < endLine)+ );+}++export function consolidateCodeRanges(ranges: CodeRange[]): CodeRange[] {+ // Sort by start position+ ranges.sort((a, b) => {+ const lines = b[0][0] - a[0][0];+ // Break ties on start column+ if (lines === 0) {+ return b[0][1] - a[0][1];+ }+ return lines;+ });++ const staged: CodeRange[] = [...ranges];+ const results = [];+ while (staged.length > 0) {+ let current = staged.pop();+ if (!current) {+ continue;+ }+ const [[initStartLine, initStartColumn], [initEndLine, initEndColumn]] =+ current;++ for (const targetRange of staged) {+ const [+ [targetStartLine, targetStartColumn],+ [targetEndLine, targetEndColumn],+ ] = targetRange;+ if (+ isPointInCodeRange(targetRange[0], current) ||+ isPointInCodeRange(targetRange[1], current)+ ) {+ staged.pop();++ let startColumn;+ if (initStartLine === targetStartLine) {+ startColumn = Math.min(initStartColumn, targetStartColumn);+ } else if (initStartLine < targetStartLine) {+ startColumn = initStartColumn;+ } else {+ startColumn = targetStartColumn;+ }++ let endColumn;+ if (initEndLine === targetEndLine) {+ endColumn = Math.max(initEndColumn, targetEndColumn);+ } else if (initEndLine > targetEndLine) {+ endColumn = initEndColumn;+ } else {+ endColumn = targetEndColumn;+ }+ // Update the current to the consolidated one and get rid of the+ // remaining instance of the unconsolidated target+ current = [+ [Math.min(initStartLine, targetStartLine), startColumn],+ [Math.max(initEndLine, targetEndLine), endColumn],+ ];+ }+ }+ results.push(current);+ }+ return results;+}++export function inverseCodeRange(ranges: CodeRange[]): CodeRange[] {+ if (ranges.length === 0) {+ return [+ [+ [0, 0],+ [Infinity, Infinity],+ ],+ ];+ }+ const firstRange = ranges[0];+ const result: CodeRange[] = [];+ for (let first = 0; first < ranges.length - 1; first++) {+ const range1 = ranges[first];+ const range2 = ranges[first + 1];+ result.push([range1[1], range2[0]]);+ }+ const lastRange = ranges.slice(-1)[0];+ return [+ [[0, 0], firstRange[0]],+ ...result,+ [lastRange[1], [Infinity, Infinity]],+ ];+}diff --git a/packages/2d/src/lib/code/CodeScope.ts b/packages/2d/src/lib/code/CodeScope.tsnew file mode 100644index 00000000..1005ac7a--- /dev/null+++ b/packages/2d/src/lib/code/CodeScope.ts@@ -0,0 +1,100 @@+import {SignalValue, unwrap} from '@motion-canvas/core';+import {PossibleCodeFragment} from './CodeFragment';+import {isCodeMetrics} from './CodeMetrics';++export interface CodeScope {+ progress: SignalValue<number>;+ fragments: CodeTag[];+}++export type PossibleCodeScope = CodeScope | CodeTag[] | string;++export type CodeTag = SignalValue<PossibleCodeFragment | CodeScope | CodeTag[]>;++export function CODE(+ strings: TemplateStringsArray,+ ...tags: CodeTag[]+): CodeTag[] {+ const result: CodeTag[] = [];+ for (let i = 0; i < strings.length; i++) {+ result.push(strings[i]);+ const tag = tags[i];+ if (tag !== undefined) {+ if (Array.isArray(tag)) {+ result.push(...tag);+ } else {+ result.push(tag);+ }+ }+ }++ return result;+}++export function isCodeScope(value: any): value is CodeScope {+ return value?.fragments !== undefined;+}++export function parseCodeScope(value: PossibleCodeScope): CodeScope {+ if (typeof value === 'string') {+ return {+ progress: 0,+ fragments: [value],+ };+ }++ if (Array.isArray(value)) {+ return {+ progress: 0,+ fragments: value,+ };+ }++ return value;+}++type IsAfterPredicate = ((scope: CodeScope) => boolean) | boolean;++export function resolveScope(+ scope: CodeScope,+ isAfter: IsAfterPredicate,+): string {+ let code = '';+ const after = typeof isAfter === 'boolean' ? isAfter : isAfter(scope);+ for (const wrapped of scope.fragments) {+ code += resolveCodeTag(wrapped, after, isAfter);+ }++ return code;+}++export function resolveCodeTag(+ wrapped: CodeTag,+ after: boolean,+ isAfter: IsAfterPredicate = after,+) {+ const fragment = unwrap(wrapped);+ if (typeof fragment === 'string') {+ return fragment;+ } else if (isCodeScope(fragment)) {+ return resolveScope(fragment, isAfter);+ } else if (isCodeMetrics(fragment)) {+ return fragment.content;+ } else if (Array.isArray(fragment)) {+ return resolveScope(+ {+ progress: 0,+ fragments: fragment,+ },+ isAfter,+ );+ } else {+ return after+ ? typeof fragment.after === 'string'+ ? fragment.after+ : fragment.after.content+ : typeof fragment.before === 'string'+ ? fragment.before+ : fragment.before.content;+ }+}diff --git a/packages/2d/src/lib/code/CodeSelection.ts b/packages/2d/src/lib/code/CodeSelection.tsnew file mode 100644index 00000000..0258d2d1--- /dev/null+++ b/packages/2d/src/lib/code/CodeSelection.ts@@ -0,0 +1,28 @@+import {+ CodePoint,+ CodeRange,+ isCodeRange,+ isPointInCodeRange,+} from './CodeRange';++export type CodeSelection = CodeRange[];+export type PossibleCodeSelection = CodeRange | CodeRange[];++export function parseCodeSelection(+ value: PossibleCodeSelection,+): CodeSelection {+ return isCodeRange(value) ? [value] : value;+}++export function isPointInCodeSelection(+ point: CodePoint,+ selection: CodeSelection,+) {+ for (const range of selection) {+ if (isPointInCodeRange(point, range)) {+ return true;+ }+ }++ return false;+}diff --git a/packages/2d/src/lib/code/CodeSignal.ts b/packages/2d/src/lib/code/CodeSignal.tsnew file mode 100644index 00000000..d3ed9eaa--- /dev/null+++ b/packages/2d/src/lib/code/CodeSignal.ts@@ -0,0 +1,338 @@+import {+ createSignal,+ deepLerp,+ DependencyContext,+ Signal,+ SignalContext,+ SignalValue,+ ThreadGenerator,+ TimingFunction,+ unwrap,+} from '@motion-canvas/core';+import {Code} from '../components';+import {addInitializer, getPropertyMetaOrCreate} from '../decorators';+import {defaultDiffer} from './CodeDiffer';+import {insert, replace} from './CodeFragment';+import {CodeHighlighter} from './CodeHighlighter';+import {CodePoint, CodeRange} from './CodeRange';+import {+ CODE,+ CodeScope,+ CodeTag,+ parseCodeScope,+ PossibleCodeScope,+ resolveCodeTag,+} from './CodeScope';+import {defaultTokenize} from './CodeTokenizer';+import {extractRange} from './extractRange';++interface CodeModifier<TOwner> {+ (code: string): TOwner;+ (code: string, duration: number): ThreadGenerator;+ (duration?: number): TagGenerator;+}++interface CodeInsert<TOwner> {+ (point: CodePoint, code: string): TOwner;+ (point: CodePoint, code: string, duration: number): ThreadGenerator;+}++interface CodeRemove<TOwner> {+ (range: CodeRange): TOwner;+ (range: CodeRange, duration: number): ThreadGenerator;+}++interface CodeReplace<TOwner> {+ (range: CodeRange, code: string): TOwner;+ (range: CodeRange, code: string, duration: number): ThreadGenerator;+}++type TagGenerator = (+ strings: TemplateStringsArray,+ ...tags: CodeTag[]+) => ThreadGenerator;++export interface CodeSignalHelpers<TOwner> {+ edit(duration?: number): TagGenerator;+ append: CodeModifier<TOwner>;+ prepend: CodeModifier<TOwner>;+ insert: CodeInsert<TOwner>;+ remove: CodeRemove<TOwner>;+ replace: CodeReplace<TOwner>;+}++export type CodeSignal<TOwner> = Signal<+ PossibleCodeScope,+ CodeScope,+ TOwner,+ CodeSignalContext<TOwner>+> &+ CodeSignalHelpers<TOwner>;++export class CodeSignalContext<TOwner>+ extends SignalContext<PossibleCodeScope, CodeScope, TOwner>+ implements CodeSignalHelpers<TOwner>+{+ private readonly progress = createSignal(0);++ public constructor(+ initial: PossibleCodeScope,+ owner: TOwner,+ private readonly highlighter?: SignalValue<CodeHighlighter>,+ private readonly dialect?: SignalValue<string>,+ ) {+ super(initial, deepLerp, owner);+ if (owner instanceof Code) {+ this.highlighter ??= owner.highlighter;+ this.dialect ??= owner.dialect;+ }+ Object.defineProperty(this.invokable, 'edit', {+ value: this.edit.bind(this),+ });+ Object.defineProperty(this.invokable, 'append', {+ value: this.append.bind(this),+ });+ Object.defineProperty(this.invokable, 'prepend', {+ value: this.prepend.bind(this),+ });+ Object.defineProperty(this.invokable, 'insert', {+ value: this.insert.bind(this),+ });+ Object.defineProperty(this.invokable, 'remove', {+ value: this.remove.bind(this),+ });+ Object.defineProperty(this.invokable, 'replace', {+ value: this.replace.bind(this),+ });+ }++ public override *tweener(+ value: SignalValue<PossibleCodeScope>,+ duration: number,+ timingFunction: TimingFunction,+ ): ThreadGenerator {+ let tokenize = defaultTokenize;+ const highlighter = unwrap(this.highlighter);+ const dialect = unwrap(this.dialect);+ if (highlighter && dialect) {+ yield (async () => {+ do {+ await DependencyContext.consumePromises();+ highlighter.initialize();+ } while (DependencyContext.hasPromises());+ })();+ tokenize = (input: string) => highlighter.tokenize(input, dialect);+ }++ this.progress(0);+ this.set({+ progress: this.progress,+ fragments: defaultDiffer(this.get(), this.parse(unwrap(value)), tokenize),+ });+ yield* this.progress(1, duration, timingFunction);+ this.set(value);+ }++ public edit(duration: number = 0.6): TagGenerator {+ return (strings, ...tags) =>+ this.editTween(CODE(strings, ...tags), duration);+ }++ public append(code: string): TOwner;+ public append(code: string, duration: number): ThreadGenerator;+ public append(duration?: number): TagGenerator;+ public append(+ first: string | number = 0.6,+ duration?: number,+ ): TOwner | ThreadGenerator | TagGenerator {+ if (typeof first === 'string') {+ if (duration === undefined) {+ const current = this.get();+ return this.set({+ progress: 0,+ fragments: [...current.fragments, first],+ });+ } else {+ return this.appendTween(first, duration);+ }+ }++ const savedDuration = first;+ return (strings, ...tags) =>+ this.append(resolveCodeTag(CODE(strings, ...tags), true), savedDuration);+ }++ public prepend(code: string): TOwner;+ public prepend(code: string, duration: number): ThreadGenerator;+ public prepend(duration?: number): TagGenerator;+ public prepend(+ first: string | number = 0.6,+ duration?: number,+ ): TOwner | ThreadGenerator | TagGenerator {+ if (typeof first === 'string') {+ if (duration === undefined) {+ const current = this.get();+ return this.set({+ progress: 0,+ fragments: [first, ...current.fragments],+ });+ } else {+ return this.prependTween(first, duration);+ }+ }++ const savedDuration = first;+ return (strings, ...tags) =>+ this.prepend(resolveCodeTag(CODE(strings, ...tags), true), savedDuration);+ }++ public insert(point: CodePoint, code: string): TOwner;+ public insert(+ point: CodePoint,+ code: string,+ duration: number,+ ): ThreadGenerator;+ public insert(+ point: CodePoint,+ code: string,+ duration?: number,+ ): TOwner | ThreadGenerator {+ return this.replace([point, point], code, duration!);+ }++ public remove(range: CodeRange): TOwner;+ public remove(range: CodeRange, duration: number): ThreadGenerator;+ public remove(range: CodeRange, duration?: number): TOwner | ThreadGenerator {+ return this.replace(range, '', duration!);+ }++ public replace(range: CodeRange, code: string): TOwner;+ public replace(+ range: CodeRange,+ code: string,+ duration: number,+ ): ThreadGenerator;+ public replace(+ range: CodeRange,+ code: string,+ duration?: number,+ ): TOwner | ThreadGenerator {+ if (duration === undefined) {+ const current = this.get();+ const [fragments, index] = extractRange(range, current.fragments);+ fragments[index] = code;+ return this.set({+ progress: current.progress,+ fragments,+ });+ } else {+ return this.replaceTween(range, code, duration);+ }+ }++ private *replaceTween(range: CodeRange, code: string, duration: number) {+ let current = this.get();+ const [fragments, index] = extractRange(range, current.fragments);+ const progress = createSignal(0);+ const scope = {+ progress,+ fragments: [replace(fragments[index] as string, code)],+ };+ fragments[index] = scope;+ this.set({+ progress: current.progress,+ fragments,+ });++ yield* progress(1, duration);++ current = this.get();+ this.set({+ progress: current.progress,+ fragments: current.fragments.map(fragment =>+ fragment === scope ? code : fragment,+ ),+ });+ progress.context.dispose();+ }++ private *editTween(value: CodeTag[], duration: number) {+ this.progress(0);+ this.set({+ progress: this.progress,+ fragments: value,+ });+ yield* this.progress(1, duration);+ const current = this.get();+ this.set({+ progress: 0,+ fragments: current.fragments.map(fragment =>+ value.includes(fragment) ? resolveCodeTag(fragment, true) : fragment,+ ),+ });+ }++ private *appendTween(value: string, duration: number) {+ let current = this.get();+ const progress = createSignal(0);+ const scope = {+ progress,+ fragments: [insert(value)],+ };+ this.set({+ progress: current.progress,+ fragments: [...current.fragments, scope],+ });+ yield* progress(1, duration);+ current = this.get();+ this.set({+ progress: current.progress,+ fragments: current.fragments.map(fragment =>+ fragment === scope ? value : fragment,+ ),+ });+ progress.context.dispose();+ }++ private *prependTween(value: string, duration: number) {+ let current = this.get();+ const progress = createSignal(0);+ const scope = {+ progress,+ fragments: [insert(value)],+ };+ this.set({+ progress: current.progress,+ fragments: [scope, ...current.fragments],+ });+ yield* progress(1, duration);+ current = this.get();+ this.set({+ progress: current.progress,+ fragments: current.fragments.map(fragment =>+ fragment === scope ? value : fragment,+ ),+ });+ progress.context.dispose();+ }++ public override parse(value: PossibleCodeScope): CodeScope {+ return parseCodeScope(value);+ }++ public override toSignal(): CodeSignal<TOwner> {+ return this.invokable;+ }+}++export function codeSignal(): PropertyDecorator {+ return (target: any, key) => {+ const meta = getPropertyMetaOrCreate<PossibleCodeScope>(target, key);+ addInitializer(target, (instance: any) => {+ instance[key] = new CodeSignalContext(+ meta.default ?? [],+ instance,+ ).toSignal();+ });+ };+}diff --git a/packages/2d/src/lib/code/CodeTokenizer.ts b/packages/2d/src/lib/code/CodeTokenizer.tsnew file mode 100644index 00000000..7f4fbf5c--- /dev/null+++ b/packages/2d/src/lib/code/CodeTokenizer.ts@@ -0,0 +1,54 @@+export type CodeTokenizer = (input: string) => string[];++/**+ * Default tokenizer function used by ownerless code signals.+ *+ * @param input - The code to tokenize.+ */+export function defaultTokenize(input: string): string[] {+ const tokens: string[] = [];+ let currentToken = '';+ let whitespace = false;++ for (const char of input) {+ switch (char) {+ case ' ':+ case '\t':+ case '\n':+ if (!whitespace && currentToken !== '') {+ tokens.push(currentToken);+ currentToken = '';+ }+ whitespace = true;+ currentToken += char;+ break;+ case '(':+ case ')':+ case '{':+ case '}':+ case '[':+ case ']':+ if (currentToken !== '') {+ tokens.push(currentToken);+ currentToken = '';+ }+ whitespace = false;+ tokens.push(char);+ break;+ default:+ if (whitespace && currentToken !== '') {+ tokens.push(currentToken);+ currentToken = '';+ }+ whitespace = false;+ currentToken += char;+ break;+ }+ }++ if (currentToken !== '') {+ tokens.push(currentToken);+ }++ return tokens;+}diff --git a/packages/2d/src/lib/code/DefaultHighlightStyle.ts b/packages/2d/src/lib/code/DefaultHighlightStyle.tsnew file mode 100644index 00000000..219a9344--- /dev/null+++ b/packages/2d/src/lib/code/DefaultHighlightStyle.ts@@ -0,0 +1,98 @@+import {HighlightStyle} from '@codemirror/language';+import {tags as t} from '@lezer/highlight';++export const DefaultHighlightStyle = HighlightStyle.define([+ {tag: t.keyword, color: '#5e81ac'},+ {+ tag: [t.name, t.deleted, t.character, t.propertyName, t.macroName],+ color: '#88c0d0',+ },+ {tag: [t.variableName], color: '#8fbcbb'},+ {tag: [t.function(t.variableName)], color: '#8fbcbb'},+ {tag: [t.labelName], color: '#81a1c1'},+ {+ tag: [t.color, t.constant(t.name), t.standard(t.name)],+ color: '#5e81ac',+ },+ {tag: [t.definition(t.name), t.separator], color: '#a3be8c'},+ {tag: [t.brace], color: '#8fbcbb'},+ {+ tag: [t.annotation],+ color: '#d30102',+ },+ {+ tag: [t.number, t.changed, t.annotation, t.modifier, t.self, t.namespace],+ color: '#b48ead',+ },+ {+ tag: [t.typeName, t.className],+ color: '#ECEFF4',+ },+ {+ tag: [t.operator, t.operatorKeyword],+ color: '#a3be8c',+ },+ {+ tag: [t.tagName],+ color: '#b48ead',+ },+ {+ tag: [t.squareBracket],+ color: '#ECEFF4',+ },+ {+ tag: [t.angleBracket],+ color: '#ECEFF4',+ },+ {+ tag: [t.attributeName],+ color: '#eceff4',+ },+ {+ tag: [t.regexp],+ color: '#5e81ac',+ },+ {+ tag: [t.quote],+ color: '#b48ead',+ },+ {tag: [t.string], color: '#a3be8c'},+ {+ tag: t.link,+ color: '#a3be8c',+ textDecoration: 'underline',+ textUnderlinePosition: 'under',+ },+ {+ tag: [t.url, t.escape, t.special(t.string)],+ color: '#8fbcbb',+ },+ {tag: [t.meta], color: '#88c0d0'},+ {tag: [t.monospace], color: '#d8dee9', fontStyle: 'italic'},+ {tag: [t.comment], color: '#4c566a', fontStyle: 'italic'},+ {tag: t.strong, fontWeight: 'bold', color: '#5e81ac'},+ {tag: t.emphasis, fontStyle: 'italic', color: '#5e81ac'},+ {tag: t.strikethrough, textDecoration: 'line-through'},+ {tag: t.heading, fontWeight: 'bold', color: '#5e81ac'},+ {tag: t.special(t.heading1), fontWeight: 'bold', color: '#5e81ac'},+ {tag: t.heading1, fontWeight: 'bold', color: '#5e81ac'},+ {+ tag: [t.heading2, t.heading3, t.heading4],+ fontWeight: 'bold',+ color: '#5e81ac',+ },+ {+ tag: [t.heading5, t.heading6],+ color: '#5e81ac',+ },+ {tag: [t.atom, t.bool, t.special(t.variableName)], color: '#d08770'},+ {+ tag: [t.processingInstruction, t.inserted],+ color: '#8fbcbb',+ },+ {+ tag: [t.contentSeparator],+ color: '#ebcb8b',+ },+ {tag: t.invalid, color: '#434c5e', borderBottom: `1px dotted #d30102`},+]);diff --git a/packages/2d/src/lib/code/LezerHighlighter.ts b/packages/2d/src/lib/code/LezerHighlighter.tsnew file mode 100644index 00000000..0faf5a6b--- /dev/null+++ b/packages/2d/src/lib/code/LezerHighlighter.ts@@ -0,0 +1,139 @@+import {HighlightStyle} from '@codemirror/language';+import {Parser, SyntaxNode, Tree} from '@lezer/common';+import {highlightTree} from '@lezer/highlight';+import {useLogger} from '@motion-canvas/core';+import {CodeHighlighter, HighlightResult} from './CodeHighlighter';+import {defaultTokenize} from './CodeTokenizer';++interface LezerCache {+ tree: Tree;+ code: string;+ colorLookup: Map<number, string>;+}++export class LezerHighlighter implements CodeHighlighter<LezerCache | null> {+ private static readonly parserMap = new Map<string, Parser>();+ private static classRegex = /\.(\S+).*color:([^;]+)/;+ private readonly classLookup = new Map<string, string>();++ public static registerParser(parser: Parser, dialect = ''): void {+ this.parserMap.set(dialect, parser);+ }++ public constructor(private readonly style: HighlightStyle) {+ for (const rule of this.style.module?.getRules().split('\n') ?? []) {+ const match = rule.match(LezerHighlighter.classRegex);+ if (!match) {+ continue;+ }++ const className = match[1];+ const color = match[2].trim();+ this.classLookup.set(className, color);+ }+ }++ public initialize(): boolean {+ return true;+ }++ public prepare(code: string, dialect: string): LezerCache | null {+ const parser = LezerHighlighter.parserMap.get(dialect);+ if (!parser) {+ if (dialect !== '') {+ useLogger().warn(`No parser found for dialect: ${dialect}`);+ }+ return null;+ }++ const colorLookup = new Map<number, string>();+ const tree = parser.parse(code);+ highlightTree(tree, this.style, (from, to, classes) => {+ const color = this.classLookup.get(classes);+ if (!color) {+ return;+ }++ const cursor = tree.cursorAt(from, 1);+ do {+ const id = this.getNodeId(cursor.node);+ colorLookup.set(id, color);+ } while (cursor.next() && cursor.to <= to);+ });++ return {+ tree,+ code,+ colorLookup,+ };+ }++ public highlight(index: number, cache: LezerCache | null): HighlightResult {+ if (!cache) {+ return {+ color: null,+ skipAhead: 0,+ };+ }++ const node = cache.tree.resolveInner(index, 1);+ const id = this.getNodeId(node);+ const color = cache.colorLookup.get(id);+ if (color) {+ return {+ color,+ skipAhead: node.to - index,+ };+ }++ let skipAhead = 0;+ if (!node.firstChild) {+ skipAhead = node.to - index;+ }++ return {+ color: null,+ skipAhead,+ };+ }++ public tokenize(code: string, dialect: string): string[] {+ const parser = LezerHighlighter.parserMap.get(dialect);+ if (!parser) {+ if (dialect !== '') {+ useLogger().warn(`No parser found for dialect: ${dialect}`);+ }+ return defaultTokenize(code);+ }++ const tree = parser.parse(code);+ const cursor = tree.cursor();+ const tokens: string[] = [];+ let current = 0;++ do {+ if (!cursor.node.firstChild) {+ if (cursor.from > current) {+ tokens.push(code.slice(current, cursor.from));+ }+ if (cursor.from < cursor.to) {+ tokens.push(code.slice(cursor.from, cursor.to));+ }+ current = cursor.to;+ }+ } while (cursor.next());++ return tokens;+ }++ private getNodeId(node: SyntaxNode): number {+ if (!node.parent) {+ return -1;+ }++ // NOTE: They don't want us to know about this property.+ // We need a way to persistently identify nodes and this seems to work.+ // Perhaps it could break if the tree is edited? But we don't do that. Yet.+ return (node as any).index;+ }+}diff --git a/packages/2d/src/lib/code/diff.test.ts b/packages/2d/src/lib/code/diff.test.tsnew file mode 100644index 00000000..8fb46b92--- /dev/null+++ b/packages/2d/src/lib/code/diff.test.ts@@ -0,0 +1,311 @@+import {expect, test} from 'vitest';+import {patienceDiff} from './diff';++test('should find simple strings', () => {+ const result = patienceDiff(+ ['abc', 'def', 'ghi'],+ ['abc', 'def', 'ghi', 'jkl'],+ );++ // printDiff(result);+ /*+ abc+ def+ ghi+ + jkl+ */++ expect(result).toMatchObject({+ lines: [+ // We don't care too much about the first three line results+ {},+ {},+ {},+ {+ line: 'jkl',+ // We care that we know that this line was not there.+ aIndex: -1,+ bIndex: 3,+ },+ ],+ });+});++test('should diff code effectively', () => {+ const result = patienceDiff(+ `function test() {+ console.log('hello world');+}`.split('\n'),+ `function test() {+ console.log('hello world');+}++function other() {+ return 5;+}`.split('\n'),+ );++ // printDiff(result);+ /*+ function test() {+ console.log('hello world');+ }+ ++ + function other() {+ + return 5;+ + }+ */++ expect(result).toMatchObject({+ lines: [+ {+ line: 'function test() {',+ },+ {+ line: " console.log('hello world');",+ },+ {+ line: '}',+ },+ {+ line: '',+ },+ {+ line: 'function other() {',+ // Make sure this line was added+ aIndex: -1,+ },+ {+ line: ' return 5;',+ aIndex: -1,+ },+ {+ line: '}',+ aIndex: -1,+ },+ ],+ });+});++test('should diff code movement effectively', () => {+ const result = patienceDiff(+ `function test() {+ console.log('hello world');+}++function other() {+ return 5;+}`.split('\n'),+ `function other() {+ return 5;+}++function test() {+ console.log('hello world');+}`.split('\n'),+ );++ // printDiff(result);+ /*+ - function test() {+ - console.log('hello world');+ - }+ -+ function other() {+ return 5;+ }+ ++ + function test() {+ + console.log('hello world');+ + }+ */++ expect(result).toMatchObject({+ lines: [+ {+ line: 'function test() {',+ // Make sure this line was removed+ bIndex: -1,+ },+ {+ line: " console.log('hello world');",+ bIndex: -1,+ },+ {+ line: '}',+ bIndex: -1,+ },+ {+ line: '',+ bIndex: -1,+ },+ {+ line: 'function other() {',+ },+ {+ line: ' return 5;',+ },+ {+ line: '}',+ },+ {+ line: '',+ aIndex: -1,+ },+ {+ line: 'function test() {',+ // Make sure this line was added+ aIndex: -1,+ },+ {+ line: " console.log('hello world');",+ aIndex: -1,+ },+ {+ line: '}',+ aIndex: -1,+ },+ ],+ });+});++test('should diff complex code movement effectively', () => {+ const result = patienceDiff(+ `void Chunk_copy(Chunk *src, size_t src_start, Chunk *dst, size_t dst_start, size_t n)+{+ if (!Chunk_bounds_check(src, src_start, n)) return;+ if (!Chunk_bounds_check(dst, dst_start, n)) return;++ memcpy(dst->data + dst_start, src->data + src_start, n);+}++int Chunk_bounds_check(Chunk *chunk, size_t start, size_t n)+{+ if (chunk == NULL) return 0;++ return start <= chunk->length && n <= chunk->length - start;+}`.split('\n'),+ `int Chunk_bounds_check(Chunk *chunk, size_t start, size_t n)+{+ if (chunk == NULL) return 0;++ return start <= chunk->length && n <= chunk->length - start;+}++void Chunk_copy(Chunk *src, size_t src_start, Chunk *dst, size_t dst_start, size_t n)+{+ if (!Chunk_bounds_check(src, src_start, n)) return;+ if (!Chunk_bounds_check(dst, dst_start, n)) return;++ memcpy(dst->data + dst_start, src->data + src_start, n);+}`.split('\n'),+ );++ // printDiff(result);+ /*+ + int Chunk_bounds_check(Chunk *chunk, size_t start, size_t n)+ + {+ + if (chunk == NULL) return 0;+ ++ + return start <= chunk->length && n <= chunk->length - start;+ + }+ ++ void Chunk_copy(Chunk *src, size_t src_start, Chunk *dst, size_t dst_start, size_t n)+ {+ if (!Chunk_bounds_check(src, src_start, n)) return;+ if (!Chunk_bounds_check(dst, dst_start, n)) return;++ memcpy(dst->data + dst_start, src->data + src_start, n);+ }+ -+ - int Chunk_bounds_check(Chunk *chunk, size_t start, size_t n)+ - {+ - if (chunk == NULL) return 0;+ -+ - return start <= chunk->length && n <= chunk->length - start;+ - }+ */++ expect(result).toMatchObject({+ lines: [+ {+ line: 'int Chunk_bounds_check(Chunk *chunk, size_t start, size_t n)',+ // Make sure this is the line added+ aIndex: -1,+ },+ {+ line: '{',+ aIndex: -1,+ },+ {+ line: ' if (chunk == NULL) return 0;',+ aIndex: -1,+ },+ {+ line: '',+ aIndex: -1,+ },+ {+ line: ' return start <= chunk->length && n <= chunk->length - start;',+ aIndex: -1,+ },+ {+ line: '}',+ aIndex: -1,+ },+ {+ line: '',+ },+ {+ line: 'void Chunk_copy(Chunk *src, size_t src_start, Chunk *dst, size_t dst_start, size_t n)',+ },+ {+ line: '{',+ },+ {+ line: ' if (!Chunk_bounds_check(src, src_start, n)) return;',+ },+ {+ line: ' if (!Chunk_bounds_check(dst, dst_start, n)) return;',+ },+ {+ line: '',+ },+ {+ line: ' memcpy(dst->data + dst_start, src->data + src_start, n);',+ },+ {+ line: '}',+ },+ {+ line: '',+ bIndex: -1,+ },+ {+ line: 'int Chunk_bounds_check(Chunk *chunk, size_t start, size_t n)',+ // Make sure this is the line removed+ bIndex: -1,+ },+ {+ line: '{',+ bIndex: -1,+ },+ {+ line: ' if (chunk == NULL) return 0;',+ bIndex: -1,+ },+ {+ line: '',+ bIndex: -1,+ },+ {+ line: ' return start <= chunk->length && n <= chunk->length - start;',+ bIndex: -1,+ },+ {+ line: '}',+ bIndex: -1,+ },+ ],+ });+});diff --git a/packages/2d/src/lib/code/diff.ts b/packages/2d/src/lib/code/diff.tsnew file mode 100644index 00000000..d05d8ebc--- /dev/null+++ b/packages/2d/src/lib/code/diff.ts@@ -0,0 +1,319 @@+type Subsequence = {+ aIndex: number;+ bIndex: number;+ prev?: Subsequence | undefined;+};++/**+ * Performs a patience diff on two arrays of strings, returning an object+ * containing the lines that were deleted, inserted, and potentially moved+ * lines. The plus parameter can result in a significant performance hit due+ * to additional Longest Common Substring searches.+ *+ * @param aLines - The original array of strings+ * @param bLines - The new array of strings+ * @param plus - Whether to return the moved lines+ *+ * Adapted from Jonathan "jonTrent" Trent's patience-diff algorithm.+ * Types and tests added by Hunter "hhenrichsen" Henrichsen.+ *+ * {@link https://github.com/jonTrent/PatienceDiff}+ */+export function patienceDiff(+ aLines: string[],+ bLines: string[],+): {+ lines: {+ line: string;+ aIndex: number;+ bIndex: number;+ }[];+ lineCountDeleted: number;+ lineCountInserted: number;+} {+ /**+ * Finds all unique values in lines[start...end], inclusive. This+ * function is used in preparation for determining the longest common+ * subsequence.+ *+ * @param lines - The array to search+ * @param start - The starting index (inclusive)+ * @param end - The ending index (inclusive)+ * @returns A map of the unique lines to their index+ */+ function findUnique(lines: string[], start: number, end: number) {+ const lineMap = new Map<string, {count: number; index: number}>();+ for (let i = start; i <= end; i++) {+ const line = lines[i];+ const data = lineMap.get(line);+ if (data) {+ data.count++;+ data.index = i;+ } else {+ lineMap.set(line, {count: 1, index: i});+ }+ }++ const newMap = new Map<string, number>();+ for (const [key, value] of lineMap) {+ if (value.count === 1) {+ newMap.set(key, value.index);+ }+ }++ return newMap;+ }++ /**+ * Finds all the unique common entries between aArray[aStart...aEnd] and+ * bArray[bStart...bEnd], inclusive. This function uses findUnique to pare+ * down the aArray and bArray ranges first, before then walking the+ * comparison between the two arrays.+ *+ *+ * @param aArray - The original array+ * @param aStart - The start of the original array to search+ * @param aEnd - The end of the original array to search, inclusive+ * @param bArray - The new array+ * @param bStart - the start of the new array to search+ * @param bEnd - The end of the new array to search, inclusive+ * @returns a Map, with the key as the common line between aArray and+ * bArray, with the value as an object containing the array indices of the+ * matching uniqe lines.+ */+ function uniqueCommon(+ aArray: string[],+ aStart: number,+ aEnd: number,+ bArray: string[],+ bStart: number,+ bEnd: number,+ ): Map<string, Subsequence> {+ const aUnique = findUnique(aArray, aStart, aEnd);+ const bUnique = findUnique(bArray, bStart, bEnd);++ return [...aUnique.entries()].reduce<Map<string, Subsequence>>(+ (paired, [key, value]) => {+ const bIndex = bUnique.get(key);+ if (bIndex !== undefined) {+ paired.set(key, {+ aIndex: value,+ bIndex,+ });+ }+ return paired;+ },+ new Map(),+ );+ }++ /**+ * Takes a map from the unique common lines between two arrays and determines+ * the longest common subsequence.+ *+ * @see uniqueCommon+ *+ * @param abMap - The map of unique common lines between two arrays.+ * @returns An array of objects containing the indices of the longest common+ * subsequence.+ */+ function longestCommonSubsequence(+ abMap: Map<string, Subsequence>,+ ): Subsequence[] {+ const jagged: [Subsequence][] = [];++ abMap.forEach(value => {+ let i = 0;+ while (jagged[i] && jagged[i].at(-1)!.bIndex < value.bIndex) {+ i++;+ }++ if (i > 0) {+ value.prev = jagged[i - 1].at(-1);+ }++ if (!jagged[i]) {+ jagged[i] = [value];+ } else {+ jagged[i].push(value);+ }+ });++ // Pull out the longest common subsequence+ let lcs: Subsequence[] = [];++ if (jagged.length > 0) {+ lcs = [jagged.at(-1)!.at(-1)!];+ let cursor = lcs.at(-1);+ while (cursor?.prev) {+ cursor = cursor.prev;+ lcs.push(cursor);+ }+ }++ return lcs.reverse();+ }++ /**+ * Keeps track of the aLines that have been deleted, are shared between aLines+ * and bLines, and bLines that have been inserted.+ */+ const result: {+ line: string;+ aIndex: number;+ bIndex: number;+ moved: boolean;+ }[] = [];+ let deleted = 0;+ let inserted = 0;++ function addToResult(aIndex: number, bIndex: number) {+ if (bIndex < 0) {+ deleted++;+ } else if (aIndex < 0) {+ inserted++;+ }+ result.push({+ line: 0 <= aIndex ? aLines[aIndex] : bLines[bIndex],+ aIndex,+ bIndex,+ moved: false,+ });+ }++ function addSubMatch(+ aStart: number,+ aEnd: number,+ bStart: number,+ bEnd: number,+ ) {+ // Match any lines at the beginning of aLines and bLines.+ while (+ aStart <= aEnd &&+ bStart <= bEnd &&+ aLines[aStart] === bLines[bStart]+ ) {+ addToResult(aStart++, bStart++);+ }++ // Match any lines at the end of aLines and bLines, but don't place them+ // in the "result" array just yet, as the lines between these matches at+ // the beginning and the end need to be analyzed first.+ const aEndTemp = aEnd;+ while (aStart <= aEnd && bStart <= bEnd && aLines[aEnd] === bLines[bEnd]) {+ aEnd--;+ bEnd--;+ }++ // Now, check to determine with the remaining lines in the subsequence+ // whether there are any unique common lines between aLines and bLines.+ //+ // If not, add the subsequence to the result (all aLines having been+ // deleted, and all bLines having been inserted).+ //+ // If there are unique common lines between aLines and bLines, then let's+ // recursively perform the patience diff on the subsequence.+ const uniqueCommonMap = uniqueCommon(+ aLines,+ aStart,+ aEnd,+ bLines,+ bStart,+ bEnd,+ );++ if (uniqueCommonMap.size === 0) {+ while (aStart <= aEnd) {+ addToResult(aStart++, -1);+ }+ while (bStart <= bEnd) {+ addToResult(-1, bStart++);+ }+ } else {+ recurseLCS(aStart, aEnd, bStart, bEnd, uniqueCommonMap);+ }++ // Finally, let's add the matches at the end to the result.+ while (aEnd < aEndTemp) {+ addToResult(++aEnd, ++bEnd);+ }+ }++ /**+ * Finds the longest common subsequence between the arrays+ * aLines[aStart...aEnd] and bLines[bStart...bEnd], inclusive. Then for each+ * subsequence, recursively performs another LCS search (via addSubMatch),+ * until there are none found, at which point the subsequence is dumped to+ * the result.+ *+ * @param aStart - The start of the original array to search+ * @param aEnd - The end of the original array to search, inclusive+ * @param bStart - The start of the new array to search+ * @param bEnd - The end of the new array to search, inclusive+ * @param uniqueCommonMap - A map of the unique common lines between+ * aLines[aStart...aEnd] and bLines[bStart...bEnd], inclusive.+ */+ function recurseLCS(+ aStart: number,+ aEnd: number,+ bStart: number,+ bEnd: number,+ uniqueCommonMap: Map<string, Subsequence> = uniqueCommon(+ aLines,+ aStart,+ aEnd,+ bLines,+ bStart,+ bEnd,+ ),+ ) {+ const lcs = longestCommonSubsequence(uniqueCommonMap);++ if (lcs.length === 0) {+ addSubMatch(aStart, aEnd, bStart, bEnd);+ } else {+ if (aStart < lcs[0].aIndex || bStart < lcs[0].bIndex) {+ addSubMatch(aStart, lcs[0].aIndex - 1, bStart, lcs[0].bIndex - 1);+ }++ let i;+ for (i = 0; i < lcs.length - 1; i++) {+ addSubMatch(+ lcs[i].aIndex,+ lcs[i + 1].aIndex - 1,+ lcs[i].bIndex,+ lcs[i + 1].bIndex - 1,+ );+ }++ if (lcs[i].aIndex <= aEnd || lcs[i].bIndex <= bEnd) {+ addSubMatch(lcs[i].aIndex, aEnd, lcs[i].bIndex, bEnd);+ }+ }+ }++ recurseLCS(0, aLines.length - 1, 0, bLines.length - 1);++ return {+ lines: result,+ lineCountDeleted: deleted,+ lineCountInserted: inserted,+ };+}++/**+ * Utility function for debugging patienceDiff.+ *+ * @internal+ */+export function printDiff(diff: ReturnType<typeof patienceDiff>) {+ diff.lines.forEach(line => {+ if (line.bIndex < 0) {+ console.log(`- ${line.line}`);+ } else if (line.aIndex < 0) {+ console.log(`+ ${line.line}`);+ } else {+ console.log(` ${line.line}`);+ }+ });+}diff --git a/packages/2d/src/lib/code/extractRange.ts b/packages/2d/src/lib/code/extractRange.tsnew file mode 100644index 00000000..fb56d5e2--- /dev/null+++ b/packages/2d/src/lib/code/extractRange.ts@@ -0,0 +1,120 @@+import {CodeRange} from './CodeRange';+import {CodeTag, resolveCodeTag} from './CodeScope';++/**+ * Transform the fragments to isolate the given range into its own fragment.+ *+ * @remarks+ * This function will try to preserve the original fragments, resolving them+ * only if they overlap with the range.+ *+ * @param range - The range to extract.+ * @param fragments - The fragments to transform.+ *+ * @returns A tuple containing the transformed fragments and the index of the+ * isolated fragment within.+ */+export function extractRange(+ range: CodeRange,+ fragments: CodeTag[],+): [CodeTag[], number] {+ const [from, to] = range;+ let [fromRow, fromColumn] = from;+ let [toRow, toColumn] = to;+ if (fromRow > toRow || (fromRow === toRow && fromColumn > toColumn)) {+ [fromRow, fromColumn] = to;+ [toRow, toColumn] = from;+ }++ let currentRow = 0;+ let currentColumn = 0;+ const newFragments: CodeTag[] = [];+ let index = -1;+ let found = false;+ let extracted = '';++ for (const fragment of fragments) {+ if (found) {+ newFragments.push(fragment);+ continue;+ }++ const resolved = resolveCodeTag(fragment, false);+ const lines = resolved.split('\n');+ const newRows = lines.length - 1;+ const lastColumn = lines[newRows].length;+ const nextColumn = newRows > 0 ? lastColumn : currentColumn + lastColumn;++ if (+ fromRow > currentRow + newRows ||+ (fromRow === currentRow + newRows && fromColumn > nextColumn)+ ) {+ currentRow += newRows;+ currentColumn = nextColumn;+ newFragments.push(fragment);+ continue;+ }++ for (let i = 0; i < resolved.length; i++) {+ const char = resolved.charAt(i);+ if (fromRow === currentRow && fromColumn >= currentColumn) {+ if (fromColumn === currentColumn) {+ index = newFragments.length + 1;+ newFragments.push(resolved.slice(0, i), '');+ }++ if (char === '\n') {+ index = newFragments.length + 1;+ newFragments.push(+ resolved.slice(0, i) + ' '.repeat(fromColumn - currentColumn),+ '',+ );+ }+ }++ if (index !== -1 && toRow === currentRow && toColumn >= currentColumn) {+ if (toColumn === currentColumn) {+ newFragments.push(resolved.slice(i));+ found = true;+ break;+ }++ if (char === '\n') {+ newFragments.push(resolved.slice(i));+ found = true;+ break;+ }+ }++ if (index !== -1) {+ extracted += char;+ }++ if (char === '\n') {+ currentRow++;+ currentColumn = 0;+ } else {+ currentColumn++;+ }+ }++ if (index === -1) {+ newFragments.push(fragment);+ }+ }++ if (index === -1) {+ index = newFragments.length + 1;+ const missingRows = fromRow - currentRow;+ const missingColumns =+ missingRows > 0 ? fromColumn : fromColumn - currentColumn;+ newFragments.push(+ '\n'.repeat(missingRows) + ' '.repeat(missingColumns),+ '',+ );+ }++ newFragments[index] = extracted;++ return [newFragments, index];+}diff --git a/packages/2d/src/lib/code/index.ts b/packages/2d/src/lib/code/index.tsnew file mode 100644index 00000000..462fdffa--- /dev/null+++ b/packages/2d/src/lib/code/index.ts@@ -0,0 +1,13 @@+export * from './CodeCursor';+export * from './CodeDiffer';+export * from './CodeFragment';+export * from './CodeHighlighter';+export * from './CodeRange';+export * from './CodeScope';+export * from './CodeSelection';+export * from './CodeSignal';+export * from './CodeTokenizer';+export * from './DefaultHighlightStyle';+export * from './LezerHighlighter';+export * from './diff';+export * from './extractRange';diff --git a/packages/2d/src/lib/components/Code.ts b/packages/2d/src/lib/components/Code.tsnew file mode 100644index 00000000..3b8efaa6--- /dev/null+++ b/packages/2d/src/lib/components/Code.ts@@ -0,0 +1,380 @@+import {+ createSignal,+ ExperimentalError,+ map,+ SerializedVector2,+ Signal,+ SignalValue,+ SimpleSignal,+ ThreadGenerator,+ TimingFunction,+ unwrap,+ Vector2,+} from '@motion-canvas/core';+import {+ CodeCursor,+ CodeHighlighter,+ CodeRange,+ CodeSelection,+ CodeSignal,+ codeSignal,+ CodeSignalContext,+ DefaultHighlightStyle,+ LezerHighlighter,+ lines,+ parseCodeSelection,+ PossibleCodeScope,+ PossibleCodeSelection,+ resolveScope,+} from '../code';+import {computed, initial, nodeName, parser, signal} from '../decorators';+import {DesiredLength} from '../partials';+import {useScene2D} from '../scenes';+import {Shape, ShapeProps} from './Shape';++export interface DrawTokenHook {+ (+ ctx: CanvasRenderingContext2D,+ text: string,+ position: Vector2,+ color: string,+ selection: number,+ ): void;+}++/**+ * Describes custom drawing logic used by the Code node.+ */+export interface DrawHooks {+ /**+ * Custom drawing logic for individual code tokens.+ *+ * @example+ * ```ts+ * token(ctx, text, position, color, selection) {+ * const blur = map(3, 0, selection);+ * const alpha = map(0.5, 1, selection);+ * ctx.globalAlpha *= alpha;+ * ctx.filter = `blur(${blur}px)`;+ * ctx.fillStyle = color;+ * ctx.fillText(text, position.x, position.y);+ * }+ * ```+ */+ token: DrawTokenHook;+}++export interface CodeProps extends ShapeProps {+ /**+ * {@inheritDoc Code.highlighter}+ */+ highlighter?: SignalValue<CodeHighlighter | null>;+ /**+ * {@inheritDoc Code.dialect}+ */+ dialect?: SignalValue<string>;+ /**+ * {@inheritDoc Code.code}+ */+ code?: SignalValue<PossibleCodeScope>;+ /**+ * {@inheritDoc Code.selection}+ */+ selection?: SignalValue<PossibleCodeSelection>;+ /**+ * {@inheritDoc Code.drawHooks}+ */+ drawHooks?: SignalValue<DrawHooks>;+ children?: never;+}++/**+ * A node for displaying and animating code.+ *+ * @experimental+ *+ * @preview+ * ```tsx editor+ * import {parser} from '@lezer/javascript';+ * import {Code, LezerHighlighter, makeScene2D} from '@motion-canvas/2d';+ * import {createRef} from '@motion-canvas/core';+ *+ * export default makeScene2D(function* (view) {+ * LezerHighlighter.registerParser(parser);+ * const code = createRef<Code>();+ *+ * view.add(+ * <Code+ * ref={code}+ * offset={-1}+ * position={view.size().scale(-0.5).add(60)}+ * fontFamily={'JetBrains Mono, monospace'}+ * fontSize={36}+ * code={`\+ * function hello() {+ * console.log('Hello');+ * }`}+ * />,+ * );+ *+ * yield* code()+ * .code(+ * `\+ * function hello() {+ * console.warn('Hello World');+ * }`,+ * 1,+ * )+ * .wait(0.5)+ * .back(1)+ * .wait(0.5);+ * });+ * ```+ */+@nodeName('CodeBlock')+export class Code extends Shape {+ /**+ * Create a standalone code signal.+ *+ * @param initial - The initial code.+ * @param highlighter - Custom highlighter to use.+ * @param dialect - Custom dialect to use.+ */+ public static createSignal(+ initial: PossibleCodeScope,+ highlighter?: SignalValue<CodeHighlighter>,+ dialect?: SignalValue<string>,+ ): CodeSignal<void> {+ return new CodeSignalContext<void>(+ initial,+ undefined,+ highlighter,+ dialect,+ ).toSignal();+ }++ public static readonly defaultHighlighter = new LezerHighlighter(+ DefaultHighlightStyle,+ );++ /**+ * The dialect to use for highlighting the code.+ *+ * @remarks+ * This value will be passed to the {@link code.CodeHighlighter}+ * defined by the {@link highlighter} property. Different highlighters may use+ * it differently.+ *+ * The default {@link code.LezerHighlighter} uses it to select+ * the language parser to use. The parser for the given dialect can be+ * registered as follows:+ * ```tsx+ * // Import the lezer parser:+ * import {parser} from '@lezer/javascript';+ *+ * // Register it in the highlighter:+ * LezerHighlighter.registerParser(parser, 'js');+ *+ * // Use the dialect in a code node:+ * <Code dialect="js" code="const a = 7;" />+ * ```+ * When no dialect is provided, the highlighter will use the default+ * parser:+ * ```tsx+ * // Register the default parser by omitting the dialect:+ * LezerHighlighter.registerParser(parser);+ *+ * // Code nodes with no dialect will now use the default parser:+ * <Code code="const a = 7;" />+ * ```+ */+ @initial('')+ @signal()+ public declare readonly dialect: SimpleSignal<string, this>;++ /**+ * The code highlighter to use for this code node.+ *+ * @remarks+ * Defaults to a shared {@link code.LezerHighlighter}.+ */+ @initial(() => Code.defaultHighlighter)+ @signal()+ public declare readonly highlighter: SimpleSignal<CodeHighlighter, this>;++ /**+ * The code to display.+ */+ @codeSignal()+ public declare readonly code: CodeSignal<this>;++ /**+ * Custom drawing logic for the code.+ *+ * @remarks+ * Check out {@link DrawHooks} for available render hooks.+ *+ * @example+ * Make the unselected code blurry and transparent:+ * ```tsx+ * <Code+ * drawHooks={{+ * token(ctx, text, position, color, selection) {+ * const blur = map(3, 0, selection);+ * const alpha = map(0.5, 1, selection);+ * ctx.globalAlpha *= alpha;+ * ctx.filter = `blur(${blur}px)`;+ * ctx.fillStyle = color;+ * ctx.fillText(text, position.x, position.y);+ * },+ * }}+ * // ...+ * />+ * ```+ */+ @initial<DrawHooks>({+ token(ctx, text, position, color, selection) {+ ctx.fillStyle = color;+ ctx.globalAlpha *= map(0.2, 1, selection);+ ctx.fillText(text, position.x, position.y);+ },+ })+ @signal()+ public declare readonly drawHooks: SimpleSignal<DrawHooks, this>;++ /**+ * The currently selected code range.+ *+ * @remarks+ * Either a single {@link code.CodeRange} or an array of them+ * describing which parts of the code should be visually emphasized.+ *+ * You can use {@link code.word} and+ * {@link code.lines} to quickly create ranges.+ *+ * @example+ * The following will select the word "console" in the code.+ * Both lines and columns are 0-based. So it will select a 7-character-long+ * (`7`) word in the second line (`1`) starting at the third character (`2`).+ * ```tsx+ * <Code+ * selection={word(1, 2, 7)}+ * code={`\+ * function hello() => {+ * console.log('Hello');+ * }`}+ * // ...+ * />+ * ```+ */+ @initial(lines(0, Infinity))+ @parser(parseCodeSelection)+ @signal()+ public declare readonly selection: Signal<+ PossibleCodeSelection,+ CodeSelection,+ this+ >;+ public oldSelection: CodeSelection | null = null;+ public selectionProgress = createSignal<number | null>(null);+ protected *tweenSelection(+ value: CodeRange[],+ duration: number,+ timingFunction: TimingFunction,+ ): ThreadGenerator {+ this.oldSelection = this.selection();+ this.selection(value);+ this.selectionProgress(0);+ yield* this.selectionProgress(1, duration, timingFunction);+ this.selectionProgress(null);+ this.oldSelection = null;+ }++ /**+ * Get the currently displayed code as a string.+ */+ @computed()+ public parsed(): string {+ return resolveScope(this.code(), scope => unwrap(scope.progress) > 0.5);+ }++ @computed()+ public highlighterCache() {+ const highlighter = this.highlighter();+ if (!highlighter || !highlighter.initialize()) return null;+ const code = this.code();+ const before = resolveScope(code, false);+ const after = resolveScope(code, true);+ const dialect = this.dialect();++ return {+ before: highlighter.prepare(before, dialect),+ after: highlighter.prepare(after, dialect),+ };+ }++ private cursorCache: CodeCursor | undefined;+ private get cursor() {+ this.cursorCache ??= new CodeCursor(this);+ return this.cursorCache;+ }++ public constructor(props: CodeProps) {+ super({+ fontFamily: 'monospace',+ ...props,+ });+ if (!useScene2D().experimentalFeatures) {+ throw new ExperimentalError('The Code node is an experimental feature');+ }+ }++ /**+ * Create a child code signal.+ *+ * @param initial - The initial code.+ */+ public createSignal(initial: PossibleCodeScope): CodeSignal<this> {+ return new CodeSignalContext<this>(+ initial,+ this,+ this.highlighter,+ this.dialect,+ ).toSignal();+ }++ protected override desiredSize(): SerializedVector2<DesiredLength> {+ this.requestFontUpdate();+ const context = this.cacheCanvas();+ const code = this.code();++ context.save();+ this.applyStyle(context);+ context.font = this.styles.font;+ this.cursor.setupMeasure(context);+ this.cursor.measureSize(code);+ const size = this.cursor.getSize();+ context.restore();++ return size;+ }++ protected override draw(context: CanvasRenderingContext2D): void {+ this.requestFontUpdate();+ this.applyStyle(context);+ const code = this.code();+ const size = this.computedSize();++ context.translate(-size.width / 2, -size.height / 2);+ context.font = this.styles.font;+ context.textBaseline = 'top';++ this.cursor.setupDraw(context);+ this.cursor.drawScope(code);+ }++ protected override collectAsyncResources(): void {+ super.collectAsyncResources();+ this.highlighter()?.initialize();+ }+}diff --git a/packages/2d/src/lib/components/Spline.ts b/packages/2d/src/lib/components/Spline.tsindex 900e9cf1..218ee377 100644--- a/packages/2d/src/lib/components/Spline.ts+++ b/packages/2d/src/lib/components/Spline.ts@@ -88,7 +88,7 @@ export interface SplineProps extends CurveProps {*/export class Spline extends Curve {/**- * Determine the smoothness of the spline when using auto-calculated handles.+ * The smoothness of the spline when using auto-calculated handles.** @remarks* This property is only applied to knots that don't use explicit handles.@@ -99,6 +99,13 @@ export class Spline extends Curve {@signal()public declare readonly smoothness: SimpleSignal<number>;+ /**+ * The knots of the spline as an array of knots with auto-calculated handles.+ *+ * @remarks+ * You can control the smoothness of the resulting curve+ * via the {@link smoothness} property.+ */@initial(null)@signal()public declare readonly points: SimpleSignal<diff --git a/packages/2d/src/lib/components/index.ts b/packages/2d/src/lib/components/index.tsindex 4303fff6..99f4390f 100644--- a/packages/2d/src/lib/components/index.ts+++ b/packages/2d/src/lib/components/index.ts@@ -1,5 +1,6 @@export * from './Bezier';export * from './Circle';+export * from './Code';export * from './CubicBezier';export * from './Curve';export * from './Grid';diff --git a/packages/2d/src/lib/index.ts b/packages/2d/src/lib/index.tsindex 15c12ab7..0cffd3c0 100644--- a/packages/2d/src/lib/index.ts+++ b/packages/2d/src/lib/index.ts@@ -1,5 +1,6 @@export * from './components';export * from './curves';+export * from './code';export * from './partials';export * from './decorators';export * from './jsx-runtime';diff --git a/packages/core/src/meta/ObjectMetaField.ts b/packages/core/src/meta/ObjectMetaField.tsindex c483487e..d1efedca 100644--- a/packages/core/src/meta/ObjectMetaField.ts+++ b/packages/core/src/meta/ObjectMetaField.ts@@ -18,9 +18,6 @@ type CallableKeys<T> = {[K in keyof T]: T[K] extends () => void ? K : never;}[keyof T];-/**- * Represents an object with nested meta-fields.- */class ObjectMetaFieldInternal<T extends Record<string, MetaField<any>>,> extends MetaField<ValueOf<T>> {@@ -108,13 +105,13 @@ class ObjectMetaFieldInternal<}/**- * {@inheritDoc ObjectMetaFieldInternal}+ * Represents an object with nested meta-fields.*/export type ObjectMetaField<T extends Record<string, MetaField<any>>> =ObjectMetaFieldInternal<T> & T;/**- * {@inheritDoc ObjectMetaFieldInternal}+ * Represents an object with nested meta-fields.*/export const ObjectMetaField = ObjectMetaFieldInternal as {new <T extends Record<string, MetaField<any>>>(diff --git a/packages/docs/bundle.js b/packages/docs/bundle.jsindex c3114d53..318b246b 100644--- a/packages/docs/bundle.js+++ b/packages/docs/bundle.js@@ -26,6 +26,7 @@ module.exports = () => ({'@motion-canvas/core': `${core}`,'@motion-canvas/2d': `${two}`,'@motion-canvas/2d/jsx-runtime': `${two}`,+ '@lezer/javascript': 'https://esm.sh/@lezer/javascript',},}),},diff --git a/packages/docs/package.json b/packages/docs/package.jsonindex 5286d1df..c7ddf7c3 100644--- a/packages/docs/package.json+++ b/packages/docs/package.json@@ -11,6 +11,7 @@"clear": "docusaurus clear","serve": "docusaurus serve","blog": "node ./release-blog-generator.js",+ "typedoc": "node ./typedoc-standalone.js","write-translations": "docusaurus write-translations","write-heading-ids": "docusaurus write-heading-ids","typecheck": "tsc"diff --git a/packages/docs/src/components/Api/Preview/TypeLiteralPreview.tsx b/packages/docs/src/components/Api/Preview/TypeLiteralPreview.tsxindex b61e8679..ab59232a 100644--- a/packages/docs/src/components/Api/Preview/TypeLiteralPreview.tsx+++ b/packages/docs/src/components/Api/Preview/TypeLiteralPreview.tsx@@ -14,7 +14,13 @@ export default function TypeLiteralPreview({}) {const find = useApiFinder();if (reflection.signatures) {- return <SignaturePreview reflection={reflection.signatures[0]} />;+ let signature = reflection.signatures[0];+ if (!signature?.kind) {+ signature = find(signature);+ }+ if (signature) {+ return <SignaturePreview reflection={signature} />;+ }}if (reflection.children) {return (diff --git a/packages/docs/typedoc-standalone.js b/packages/docs/typedoc-standalone.jsnew file mode 100644index 00000000..e9468784--- /dev/null+++ b/packages/docs/typedoc-standalone.js@@ -0,0 +1,7 @@+const factory = require('./typedoc.js');++(async () => {+ process.env.NODE_ENV = 'production';+ const plugin = factory();+ await plugin.loadContent();+})();diff --git a/packages/docs/typedoc.js b/packages/docs/typedoc.jsindex 1d0e3a7d..5661a1ef 100644--- a/packages/docs/typedoc.js+++ b/packages/docs/typedoc.js@@ -59,6 +59,7 @@ module.exports = () => ({entryPoints: ['../2d/src/lib/components','../2d/src/lib/curves',+ '../2d/src/lib/code','../2d/src/lib/decorators','../2d/src/lib/partials','../2d/src/lib/scenes',@@ -405,8 +406,11 @@ function partsToMarkdown(parts) {if (part.kind === 'inline-tag' && part.tag === '@link') {if (part.target instanceof DeclarationReflection) {return `[\`${part.text}\`](${part.target.href})`;- } else {+ } else if (part.target) {return `[\`${part.text}\`](${part.target})`;+ } else {+ console.error(part);+ throw new Error('Invalid link: ' + part.text);}}return part.text;