-
Notifications
You must be signed in to change notification settings - Fork 485
Developer playground: Add lambda views before and after optimisation,… #8636
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
fhammerschmidt
wants to merge
2
commits into
master
Choose a base branch
from
playground-lambda-diff
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,241 @@ | ||
| import assert from "node:assert/strict"; | ||
| import test from "node:test"; | ||
| import { | ||
| compare, | ||
| maxCharacters, | ||
| maxLines, | ||
| sections, | ||
| } from "../src/LambdaDiff.res.mjs"; | ||
| import * as Inline from "../src/LambdaInlineDiff.res.mjs"; | ||
|
|
||
| function changes(before, after) { | ||
| const result = compare(before, after); | ||
| assert.equal(result.TAG, "Changes"); | ||
| const lines = result._0; | ||
| for (const [side, text] of [ | ||
| ["before", before], | ||
| ["after", after], | ||
| ]) { | ||
| const original = lines.filter(line => line[side] !== undefined); | ||
| assert.equal(original.map(line => line.text).join("\n"), text); | ||
| assert.deepEqual( | ||
| original.map(line => line[side]), | ||
| original.map((_, index) => index + 1), | ||
| ); | ||
| } | ||
| return lines; | ||
| } | ||
|
|
||
| test("identical dumps, including empty dumps, need no diff", () => { | ||
| assert.equal(compare("", ""), "Identical"); | ||
| assert.equal(compare("(makeblock 42)\n", "(makeblock 42)\n"), "Identical"); | ||
| }); | ||
|
|
||
| test("replacement retains before and after line numbers", () => { | ||
| assert.deepEqual( | ||
| changes("(let\n unused/1\n result)\n", "(let\n result)\n"), | ||
| [ | ||
| { kind: "Same", text: "(let", before: 1, after: 1 }, | ||
| { kind: "Removed", text: " unused/1", before: 2, after: undefined }, | ||
| { kind: "Same", text: " result)", before: 3, after: 2 }, | ||
| { kind: "Same", text: "", before: 4, after: 3 }, | ||
| ], | ||
| ); | ||
| assert.deepEqual( | ||
| changes("before", "after").map(line => line.kind), | ||
| ["Removed", "Added"], | ||
| ); | ||
| }); | ||
|
|
||
| test("insertions, deletions, empty inputs and final newlines are lossless", () => { | ||
| for (const [before, after] of [ | ||
| ["", "(block 42)"], | ||
| ["(block 42)", ""], | ||
| ["a\nb", "a\ninsert\nb"], | ||
| ["a\nb", "insert\na\nb\nend"], | ||
| ["a\n", "a"], | ||
| ["a", "a\n"], | ||
| ["\n\n", "\n"], | ||
| ]) | ||
| changes(before, after); | ||
| }); | ||
|
|
||
| test("repeated Lambda lines align deterministically without dropping text", () => { | ||
| const before = "(let\n x/1\n (let\n x/1\n ))"; | ||
| const after = "(let\n (let\n x/1\n ))"; | ||
| assert.equal( | ||
| changes(before, after).filter(line => line.kind !== "Same").length, | ||
| 1, | ||
| ); | ||
| assert.deepEqual(compare(before, after), compare(before, after)); | ||
| changes("x/1", "x/2"); // Identifier changes must not be normalized away. | ||
| }); | ||
|
|
||
| test("exhaustive small dumps reconstruct both inputs", () => { | ||
| const dumps = [""]; | ||
| for (let length = 1; length <= 4; length++) { | ||
| for (let bits = 0; bits < 2 ** length; bits++) { | ||
| dumps.push( | ||
| Array.from({ length }, (_, i) => | ||
| (bits >> i) & 1 ? "(x)" : "(y)", | ||
| ).join("\n"), | ||
| ); | ||
| } | ||
| } | ||
| for (const before of dumps) { | ||
| for (const after of dumps) { | ||
| if (before !== after) changes(before, after); | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| test("bounds comparison work and output size", () => { | ||
| assert.equal(compare("a\n".repeat(1100), "b\n".repeat(1100)), "TooLarge"); | ||
| assert.equal(compare("a".repeat(maxCharacters), "b"), "TooLarge"); | ||
| assert.equal(compare("\n".repeat(maxLines), ""), "TooLarge"); | ||
| }); | ||
|
|
||
| test("trims common edges before applying the comparison budget", () => { | ||
| const edge = "(unchanged)\n".repeat(2000); | ||
| const lines = changes(`${edge}before\n${edge}`, `${edge}after\n${edge}`); | ||
| assert.equal(lines.filter(line => line.kind !== "Same").length, 2); | ||
| }); | ||
|
|
||
| test("collapsed regions preserve every line and keep context around changes", () => { | ||
| const context = Array.from({ length: 20 }, (_, i) => `line ${i}`).join("\n"); | ||
| const lines = changes( | ||
| `${context}\nold\n${context}\nold\n${context}`, | ||
| `${context}\nnew\n${context}\nnew\n${context}`, | ||
| ); | ||
| const grouped = sections(lines); | ||
| assert.deepEqual( | ||
| grouped.flatMap(section => section._0), | ||
| lines, | ||
| ); | ||
| const hidden = grouped.filter(section => section.TAG === "Collapsed"); | ||
| assert.deepEqual( | ||
| hidden.map(section => section._0.length), | ||
| [17, 14, 17], | ||
| ); | ||
| for (const section of hidden) { | ||
| assert.ok(section._0.every(line => line.kind === "Same")); | ||
| } | ||
| assert.ok( | ||
| sections(changes("a\nb\nc", "a\nB\nc")).every( | ||
| section => section.TAG === "Visible", | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| function inlineChanges(before, after) { | ||
| const lines = changes(before, after); | ||
| const highlights = Inline.highlight(lines); | ||
| for (const line of lines) { | ||
| const parts = Inline.forLine(highlights, line); | ||
| if (parts) assert.equal(parts.map(part => part.text).join(""), line.text); | ||
| if (line.kind === "Same") assert.equal(parts, undefined); | ||
| } | ||
| return highlights; | ||
| } | ||
|
|
||
| const changedText = parts => | ||
| parts.filter(part => part.changed).map(part => part.text); | ||
|
|
||
| test("inline diff highlights changed Lambda tokens, not the shared expression", () => { | ||
| const result = inlineChanges(" (+ value/1 10)", " (+ value/1 20)"); | ||
| assert.deepEqual(changedText(result.before[1]), ["10"]); | ||
| assert.deepEqual(changedText(result.after[1]), ["20"]); | ||
| const renamed = inlineChanges("(apply value/123 42)", "(apply value/124 42)"); | ||
| assert.deepEqual(changedText(renamed.before[1]), ["value/123"]); | ||
| assert.deepEqual(changedText(renamed.after[1]), ["value/124"]); | ||
| }); | ||
|
|
||
| test("inline diff preserves whitespace, Unicode, and escaped quoted constants", () => { | ||
| const result = inlineChanges( | ||
| ' (apply print/1 "a \\"quoted\\" 🙂")', | ||
| '\t(apply print/1 "b \\"quoted\\" 🙂")', | ||
| ); | ||
| assert.deepEqual(changedText(result.before[1]), ['"a \\"quoted\\" 🙂"']); | ||
| assert.deepEqual(changedText(result.after[1]), ['"b \\"quoted\\" 🙂"']); | ||
| const whitespace = inlineChanges( | ||
| " (apply café/1 42)", | ||
| "\t(apply café/1 42)", | ||
| ); | ||
| assert.deepEqual(changedText(whitespace.before[1]), []); | ||
| assert.deepEqual(changedText(whitespace.after[1]), []); | ||
| }); | ||
|
|
||
| test("inline diff handles token insertion, deletion and repeated tokens", () => { | ||
| const result = inlineChanges("(apply f/1 x/2 x/2)", "(apply f/1 x/2)"); | ||
| assert.deepEqual(changedText(result.before[1]), ["x/2"]); | ||
| assert.deepEqual(changedText(result.after[1]), []); | ||
| const inserted = inlineChanges("(apply f/1 x/2)", "(apply f/1 x/2 42)"); | ||
| assert.deepEqual(changedText(inserted.before[1]), []); | ||
| assert.deepEqual(changedText(inserted.after[1]), ["42"]); | ||
| }); | ||
|
|
||
| test("inline pairing skips unmatched lines in an uneven changed block", () => { | ||
| const result = inlineChanges( | ||
| "(unused z/9)\n (+ x/1 10)\n (- y/2 30)", | ||
| " (+ x/1 20)\n (- y/2 40)", | ||
| ); | ||
| assert.equal(result.before[1], undefined); | ||
| assert.deepEqual(changedText(result.before[2]), ["10"]); | ||
| assert.deepEqual(changedText(result.after[1]), ["20"]); | ||
| assert.deepEqual(changedText(result.before[3]), ["30"]); | ||
| assert.deepEqual(changedText(result.after[2]), ["40"]); | ||
| }); | ||
|
|
||
| test("inline pairing prefers a better later match over a weaker first match", () => { | ||
| const result = inlineChanges( | ||
| "(apply other/1 10)\n(apply f/2 20)", | ||
| "(apply f/2 30)", | ||
| ); | ||
| assert.equal(result.before[1], undefined); | ||
| assert.deepEqual(changedText(result.before[2]), ["20"]); | ||
| assert.deepEqual(changedText(result.after[1]), ["30"]); | ||
| }); | ||
|
|
||
| test("unrelated expressions and one-sided changes retain only line highlighting", () => { | ||
| for (const [before, after] of [ | ||
| [" (foo/1 10)", " (bar/2 20)"], | ||
| ["((", "))"], | ||
| ["(a)\n(b)", "(a)"], | ||
| ["(a)", "(a)\n(b)"], | ||
| ]) { | ||
| assert.deepEqual(inlineChanges(before, after), { before: {}, after: {} }); | ||
| } | ||
| }); | ||
|
|
||
| test("inline highlighting falls back for oversized blocks, lines and token counts", () => { | ||
| const largeBlock = Array.from( | ||
| { length: Inline.maxBlockLines }, | ||
| (_, i) => `(apply f/${i} 10)`, | ||
| ).join("\n"); | ||
| assert.deepEqual( | ||
| inlineChanges(largeBlock, largeBlock.replaceAll(" 10)", " 20)")), | ||
| { before: {}, after: {} }, | ||
| ); | ||
| for (const body of [ | ||
| "x".repeat(Inline.maxLineCharacters), | ||
| "x ".repeat(Inline.maxTokens), | ||
| ]) { | ||
| assert.deepEqual( | ||
| inlineChanges(`(apply ${body} 10)`, `(apply ${body} 20)`), | ||
| { before: {}, after: {} }, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| test("inline token work is budgeted across blocks and resets for each dump", () => { | ||
| const body = "x ".repeat(110); | ||
| const before = Array.from( | ||
| { length: 30 }, | ||
| (_, i) => `(apply ${body} 10)\nseparator/${i}`, | ||
| ).join("\n"); | ||
| const after = before.replaceAll(" 10)", " 20)"); | ||
| const result = inlineChanges(before, after); | ||
| assert.ok(Object.keys(result.before).length > 0); | ||
| assert.ok(Object.keys(result.before).length < 30); | ||
| assert.deepEqual(inlineChanges(before, after), result); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems I cannot extract optimized lambda for the playground without this addition