continue-custom/core/diff/streamDiff.ts
Nate Sesti 98affeaa81
🔥 Improved Ctrl/Cmd+I (#1023)
* ⚡️ improved diff streaming algo

* 🎨 better messaging/formatting for further cmd/ctrl+I instructions

* ⚡️ more reliably filter out unwanted explanations

* 🚸 better follow up edits

* 💄 accept/reject diffs block-by-block

* ✨ cmd/ctrl+z to reject diff

* 🚚 rename variables

* 💄 allow switching files when inline diff still visible

* 🚸 don't show quick pick if not ctx providers exist

* 🚧 (sort of) allow switching editors while streaming diff

* 💄 show model being used for cmd/ctrl+I

* 💄 don't add  undo stops when generating diff

* 🐛 fix shortcuts for accept/reject diff blocks

* ✨ improved GPT edit prompt, taking prefix/suffix into account

* ✨ improved prompting for empty selection ctrl/cmd+I

* ⚡️ immediately refresh codelens

* 🐛 use first model if default undefined

* ⚡️ refresh codelens after diff cleared

* 💄 update keyboard shortcuts
2024-03-26 15:55:32 -07:00

67 lines
2 KiB
TypeScript

import { DiffLine } from "..";
import { LineStream, matchLine } from "./util";
/**
* https://blog.jcoglan.com/2017/02/12/the-myers-diff-algorithm-part-1/
* Invariants:
* - new + same = newLines.length
* - old + same = oldLines.length
* ^ (above two guarantee that all lines get represented)
* - Lines are always output in order, at least among old and new separately
*/
export async function* streamDiff(
oldLines: string[],
newLines: LineStream,
): AsyncGenerator<DiffLine> {
const mutatedOldLines = [...oldLines]; // be careful
// If one indentation mistake is made, others are likely. So we are more permissive about matching
let seenIndentationMistake = false;
let newLineResult = await newLines.next();
while (oldLines.length > 0 && !newLineResult.done) {
const [matchIndex, isPerfectMatch, newLine] = matchLine(
newLineResult.value,
oldLines,
seenIndentationMistake,
);
if (!seenIndentationMistake && newLineResult.value !== newLine) {
seenIndentationMistake = true;
}
if (matchIndex < 0) {
// Insert new line
yield { type: "new", line: newLine };
} else {
// Insert all deleted lines before match
for (let i = 0; i < matchIndex; i++) {
yield { type: "old", line: oldLines.shift()! };
}
if (isPerfectMatch) {
// Same
yield { type: "same", line: oldLines.shift()! };
} else {
// Delete old line and insert the new
yield { type: "old", line: oldLines.shift()! };
yield { type: "new", line: newLine };
}
}
newLineResult = await newLines.next();
}
// Once at the edge, only one choice
if (newLineResult.done === true && oldLines.length > 0) {
for (let oldLine of oldLines) {
yield { type: "old", line: oldLine };
}
}
if (!newLineResult.done && oldLines.length === 0) {
yield { type: "new", line: newLineResult.value };
for await (const newLine of newLines) {
yield { type: "new", line: newLine };
}
}
}