diff --git a/core/diff/streamDiff.ts b/core/diff/streamDiff.ts index a7f22ef0d..912fc20db 100644 --- a/core/diff/streamDiff.ts +++ b/core/diff/streamDiff.ts @@ -17,11 +17,10 @@ export async function* streamDiff( let seenIndentationMistake = false; let newLineResult = await newLines.next(); - const postponedLines: string[] = []; while (oldLines.length > 0 && !newLineResult.done) { const [matchIndex, isPerfectMatch, newLine] = matchLine( newLineResult.value, - mutatedOldLines, + oldLines, seenIndentationMistake ); if (!seenIndentationMistake && newLineResult.value !== newLine) { @@ -30,25 +29,20 @@ export async function* streamDiff( if (matchIndex < 0) { // Insert new line - postponedLines.push(newLine); + yield { type: "new", line: newLine }; } else { // Insert all deleted lines before match for (let i = 0; i < matchIndex; i++) { - yield { type: "old", line: mutatedOldLines.shift()! }; - } - if (postponedLines.length > 0) { - for (let i = 0; i <= postponedLines.length; i++) { - yield { type: "new", line: postponedLines.shift()! }; - } + yield { type: "old", line: oldLines.shift()! }; } if (isPerfectMatch) { // Same - yield { type: "same", line: mutatedOldLines.shift()! }; + yield { type: "same", line: oldLines.shift()! }; } else { // Delete old line and insert the new - yield { type: "old", line: mutatedOldLines.shift()! }; - postponedLines.push(newLine); + yield { type: "old", line: oldLines.shift()! }; + yield { type: "new", line: newLine }; } } @@ -56,22 +50,16 @@ export async function* streamDiff( } // Once at the edge, only one choice - if (newLineResult.done === true && mutatedOldLines.length > 0) { + if (newLineResult.done === true && oldLines.length > 0) { for (let oldLine of oldLines) { yield { type: "old", line: oldLine }; } } - if (!newLineResult.done && mutatedOldLines.length === 0) { + if (!newLineResult.done && oldLines.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 f5df65307..9a41b18b8 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]]; }