From 9ba2e76705f162cfb97d3591bfef2e89c75f1834 Mon Sep 17 00:00:00 2001 From: Ihor Pavlenko Date: Mon, 26 Feb 2024 05:49:14 +0100 Subject: [PATCH] Fix streamDiff tests (#796) * fix: make streamDiff tests to pass * add missing lline --- core/diff/streamDiff.ts | 30 +++++++++++++++++++++--------- core/diff/util.ts | 4 ++-- core/test/diff.test.ts | 31 ++++++++++++++++++++++++++++++- 3 files changed, 53 insertions(+), 12 deletions(-) diff --git a/core/diff/streamDiff.ts b/core/diff/streamDiff.ts index 410a16835..a7f22ef0d 100644 --- a/core/diff/streamDiff.ts +++ b/core/diff/streamDiff.ts @@ -13,14 +13,15 @@ export async function* streamDiff( oldLines: string[], newLines: LineStream ): AsyncGenerator { - oldLines = [...oldLines]; // be careful + const mutatedOldLines = [...oldLines]; // be careful let seenIndentationMistake = false; let newLineResult = await newLines.next(); + const postponedLines: string[] = []; while (oldLines.length > 0 && !newLineResult.done) { const [matchIndex, isPerfectMatch, newLine] = matchLine( newLineResult.value, - oldLines, + mutatedOldLines, seenIndentationMistake ); if (!seenIndentationMistake && newLineResult.value !== newLine) { @@ -29,20 +30,25 @@ export async function* streamDiff( if (matchIndex < 0) { // Insert new line - yield { type: "new", line: newLine }; + postponedLines.push(newLine); } else { // Insert all deleted lines before match for (let i = 0; i < matchIndex; i++) { - yield { type: "old", line: oldLines.shift()! }; + yield { type: "old", line: mutatedOldLines.shift()! }; + } + if (postponedLines.length > 0) { + for (let i = 0; i <= postponedLines.length; i++) { + yield { type: "new", line: postponedLines.shift()! }; + } } if (isPerfectMatch) { // Same - yield { type: "same", line: oldLines.shift()! }; + yield { type: "same", line: mutatedOldLines.shift()! }; } else { // Delete old line and insert the new - yield { type: "old", line: oldLines.shift()! }; - yield { type: "new", line: newLine }; + yield { type: "old", line: mutatedOldLines.shift()! }; + postponedLines.push(newLine); } } @@ -50,16 +56,22 @@ export async function* streamDiff( } // Once at the edge, only one choice - if (newLineResult.done === true && oldLines.length > 0) { + if (newLineResult.done === true && mutatedOldLines.length > 0) { for (let oldLine of oldLines) { yield { type: "old", line: oldLine }; } } - if (!newLineResult.done && oldLines.length === 0) { + if (!newLineResult.done && mutatedOldLines.length === 0) { yield { type: "new", line: newLineResult.value }; for await (const newLine of newLines) { yield { type: "new", line: newLine }; } } + + if (postponedLines.length > 0) { + for await (const newLine of postponedLines) { + yield { type: "new", line: newLine }; + } + } } diff --git a/core/diff/util.ts b/core/diff/util.ts index 9a41b18b8..f5df65307 100644 --- a/core/diff/util.ts +++ b/core/diff/util.ts @@ -41,8 +41,8 @@ export function matchLine( } else if (linesMatch(newLine, oldLines[i])) { // This is a way to fix indentation, but only for sufficiently long lines to avoid matching whitespace or short lines if ( - newLine.trimStart() === oldLines[i].trimStart() && - (permissiveAboutIndentation || newLine.trim().length > 8) + newLine.trimStart() === oldLines[i].trimStart() // && + // (permissiveAboutIndentation) || newLine.trim().length > 8) ) { return [i, true, oldLines[i]]; } diff --git a/core/test/diff.test.ts b/core/test/diff.test.ts index 92a5cfc2f..ccbbfa4b6 100644 --- a/core/test/diff.test.ts +++ b/core/test/diff.test.ts @@ -1,6 +1,18 @@ import { streamDiff } from "../diff/streamDiff"; const oldCode = [ + `A +B`, + `A +B +C`, + `A +B +C +A +B +B +A`, `function mergeSortAlgorithm() { // TODO: implement }`, @@ -10,6 +22,19 @@ const oldCode = [ ]; const newCode = [ + `C +D`, + `D +E +C +F +C`, + `C +B +A +B +A +C`, `function mergeSortAlgorithm(arr: number[]): number[] { if (arr.length <= 1) { return arr; @@ -113,7 +138,11 @@ describe("streamDiff", () => { // Check that there are no red lines immediately following green (they should always be above) for (let i = 1; i < diff.length; i++) { if (diff[i].type === "old" && diff[i - 1].type === "new") { - throw new Error("Found red immediately after green line"); + throw new Error( + `Found red '${diff[i].line}' immediately after green line '${ + diff[i - 1].line + }` + ); } } });