continue-custom/core/diff/streamDiff.ts
Nate Sesti 53b61288ec
🎨 Refactor core (#1281)
* 🎨 refactor out react context

* 🎨 refactor local storage

* 🎨 refactor IdeMessenger

* 🔥 removing unused files

* 🚚 move scripts

* ✅ setup VSCode integration testing

* 🎨 fix test-related stuff

* 🚧 testing

* 🎨 tweak terminal cmd+L

* 🧑‍💻 install biome in core

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 run biome check in binary

* 🧑‍💻 global biome config

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 fix a few biome warnings

* 🚧 WIP on protocols

* 🚧 progress, organizing, about to remove webviewCore protocol

* 🚧 now running

* 🚧 move indexing code to core

* 🚧 WIP on JetBrains indexing implementation

* 🎨 finish listDir

* 🚧 gui fixes

* 🏷️ update IMessenger.on return type

* 🐛 fix jetbrains types + IDE detection

* 🧑‍💻 set up debugging for binary with TcpMessenger

* 👷 fix prepackage.js

* 🧑‍💻 turn off debug mode for intellij

* 🐛 merge fixes

* 🐛 fixes after refactor

* 🐛 merge fixes

* 💄 increase font size in JB

* 🔀 merge changes

* 🩹 small fixes

* 🐛 fix jetbrains build

* 🩹 more jetbrains fixes

* 🎨 jetbrains improvements

* 🐛 fix lancedb prob in jetbrains build

* 👷 update jetbrains build process

* ✨ intellij problems context provider

* 👷 add script utils file

* 💚 fix jetbrains build regression

* 🎨 dynamic import transformers.js

* 🩹 small jetbrains updates

* ✨ folder context provider in jetbrains

* 🎨 many more jetbrains improvements

* 🔀 merge fixes

* ⚡️ small improvements

* 🎨 tell users transformers.js not supported

* 🎨 trial updates

* 📝 update jetbrains readmej

* ⚡️ only use smaller context for local autocomplete models

* ⚡️ improve bracket filter

* ✨ global .continue/.prompts folder

* 💄 improved model setup process

* 🥅 display VSCode req errs
2024-05-24 19:38:13 -07:00

67 lines
2 KiB
TypeScript

import { DiffLine } from "../index.js";
import { LineStream, matchLine } from "./util.js";
/**
* 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 (const 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 };
}
}
}