🐛 test and fix streamDiff
This commit is contained in:
parent
ce4d3ba8f1
commit
d7c2c53263
9 changed files with 120 additions and 45 deletions
|
|
@ -8,22 +8,24 @@ export async function* streamDiff(
|
|||
oldLines: string[],
|
||||
newLines: AsyncGenerator<string>
|
||||
): AsyncGenerator<DiffLine> {
|
||||
oldLines = [...oldLines]; // be careful
|
||||
|
||||
let newLineResult = await newLines.next();
|
||||
while (oldLines.length > 0 && !newLineResult.done) {
|
||||
const newLine = newLineResult.value;
|
||||
const [matchIndex, isPerfectMatch] = matchLine(newLine, oldLines);
|
||||
|
||||
if (matchIndex < 0) {
|
||||
yield { type: "new", newLine };
|
||||
yield { type: "new", line: newLine };
|
||||
} else {
|
||||
for (let i = 0; i < matchIndex; i++) {
|
||||
yield { type: "old" };
|
||||
yield { type: "old", line: oldLines.shift()! };
|
||||
}
|
||||
if (isPerfectMatch) {
|
||||
yield { type: "same" };
|
||||
yield { type: "same", line: oldLines.shift()! };
|
||||
} else {
|
||||
yield { type: "old" };
|
||||
yield { type: "new", newLine };
|
||||
yield { type: "old", line: oldLines.shift()! };
|
||||
yield { type: "new", line: newLine };
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -32,12 +34,13 @@ export async function* streamDiff(
|
|||
|
||||
// Once at the edge, only one choice
|
||||
if (newLineResult.done === true && oldLines.length > 0) {
|
||||
for (let _ of oldLines) {
|
||||
yield { type: "old" };
|
||||
for (let oldLine of oldLines) {
|
||||
yield { type: "old", line: oldLine };
|
||||
}
|
||||
} else if (!newLineResult.done && oldLines.length === 0) {
|
||||
yield { type: "new", line: newLineResult.value };
|
||||
for await (const newLine of newLines) {
|
||||
yield { type: "new", newLine };
|
||||
yield { type: "new", line: newLine };
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ function linesMatchPerfectly(lineA: string, lineB: string): boolean {
|
|||
}
|
||||
|
||||
function linesMatch(lineA: string, lineB: string): boolean {
|
||||
return distance(lineA, lineB) / Math.max(lineA.length, lineB.length) < 0.25;
|
||||
const d = distance(lineA, lineB);
|
||||
return d / Math.max(lineA.length, lineB.length) < 0.5;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
12
core/index.d.ts
vendored
12
core/index.d.ts
vendored
|
|
@ -229,17 +229,11 @@ export type CustomLLM = RequireAtLeastOne<
|
|||
|
||||
// IDE
|
||||
|
||||
interface DiffLineNew {
|
||||
type: "new";
|
||||
newLine: string;
|
||||
export interface DiffLine {
|
||||
type: "new" | "old" | "same";
|
||||
line: string;
|
||||
}
|
||||
|
||||
interface DiffLineOldOrSame {
|
||||
type: "old" | "same";
|
||||
}
|
||||
|
||||
export type DiffLine = DiffLineNew | DiffLineOldOrSame;
|
||||
|
||||
export interface IDE {
|
||||
getSerializedConfig(): Promise<SerializedContinueConfig>;
|
||||
getConfigJsUrl(): Promise<string | undefined>;
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@
|
|||
"description": "The Continue Core contains functionality that can be shared across web, VS Code, or Node.js server. It is written in TypeScript and contains much of the functionality that was previously inside of the legacy Continue Python Server.",
|
||||
"main": "index.node",
|
||||
"scripts": {
|
||||
"test": "jest",
|
||||
"test": "jest ./test/diff.test.ts",
|
||||
"build": "cargo-cp-artifact -nc index.node -- cargo build --message-format=json-render-diagnostics",
|
||||
"build-debug": "npm run build --",
|
||||
"build-release": "npm run build -- --release",
|
||||
|
|
|
|||
75
core/test/diff.test.ts
Normal file
75
core/test/diff.test.ts
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
import { streamDiff } from "../diff/diffLines";
|
||||
|
||||
const oldLines = [
|
||||
"function mergeSortAlgorithm() {",
|
||||
" // TODO: implement",
|
||||
"}",
|
||||
];
|
||||
|
||||
const newLines = [
|
||||
"function mergeSortAlgorithm(arr: number[]): number[] {",
|
||||
" if (arr.length <= 1) {",
|
||||
" return arr;",
|
||||
" }",
|
||||
"",
|
||||
" const middle = Math.floor(arr.length / 2);",
|
||||
" const left = arr.slice(0, middle);",
|
||||
" const right = arr.slice(middle);",
|
||||
"",
|
||||
" return merge(mergeSortAlgorithm(left), mergeSortAlgorithm(right));",
|
||||
"}",
|
||||
"",
|
||||
"function merge(left: number[], right: number[]): number[] {",
|
||||
" let resultArray = [], leftIndex = 0, rightIndex = 0;",
|
||||
"",
|
||||
" while (leftIndex < left.length && rightIndex < right.length) {",
|
||||
" if (left[leftIndex] < right[rightIndex]) {",
|
||||
" resultArray.push(left[leftIndex]);",
|
||||
" leftIndex++;",
|
||||
" } else {",
|
||||
" resultArray.push(right[rightIndex]);",
|
||||
" rightIndex++;",
|
||||
" }",
|
||||
" }",
|
||||
"",
|
||||
" return resultArray",
|
||||
" .concat(left.slice(leftIndex))",
|
||||
" .concat(right.slice(rightIndex));",
|
||||
"}",
|
||||
];
|
||||
|
||||
async function* generateLines(lines: string[]): AsyncGenerator<string> {
|
||||
for (const line of lines) {
|
||||
yield line;
|
||||
}
|
||||
}
|
||||
|
||||
describe("streamDiff", () => {
|
||||
test("outputs valid diff", async () => {
|
||||
const diff = [];
|
||||
for await (const diffLine of streamDiff(
|
||||
oldLines,
|
||||
generateLines(newLines)
|
||||
)) {
|
||||
diff.push(diffLine);
|
||||
}
|
||||
|
||||
console.log(
|
||||
diff
|
||||
.map((dl) => {
|
||||
return (
|
||||
(dl.type === "old" ? "- " : dl.type === "new" ? "+ " : " ") +
|
||||
dl.line
|
||||
);
|
||||
})
|
||||
.join("\n")
|
||||
);
|
||||
|
||||
const numSame = diff.filter((dl) => dl.type === "same").length;
|
||||
const numOld = diff.filter((dl) => dl.type === "old").length;
|
||||
const numNew = diff.filter((dl) => dl.type === "new").length;
|
||||
|
||||
expect(oldLines.length).toEqual(numOld + numSame);
|
||||
expect(newLines.length).toEqual(numNew + numSame);
|
||||
});
|
||||
});
|
||||
|
|
@ -1,11 +1,11 @@
|
|||
import * as dotenv from "dotenv";
|
||||
import { ContinueSDK } from "../..";
|
||||
import EditSlashCommand, { getPromptParts } from "../../commands/slash/edit";
|
||||
import { contextItemToRangeInFileWithContents } from "../../commands/util";
|
||||
import FileSystemIde from "../../ide/filesystem";
|
||||
import { dedentAndGetCommonWhitespace } from "../../util";
|
||||
import FreeTrial from "../llms/FreeTrial";
|
||||
import Ollama from "../llms/Ollama";
|
||||
import { ContinueSDK } from "..";
|
||||
import EditSlashCommand, { getPromptParts } from "../commands/slash/edit";
|
||||
import { contextItemToRangeInFileWithContents } from "../commands/util";
|
||||
import FileSystemIde from "../ide/filesystem";
|
||||
import FreeTrial from "../llm/llms/FreeTrial";
|
||||
import Ollama from "../llm/llms/Ollama";
|
||||
import { dedentAndGetCommonWhitespace } from "../util";
|
||||
|
||||
jest.setTimeout(100_000);
|
||||
|
||||
|
|
@ -1,7 +1,7 @@
|
|||
import * as dotenv from "dotenv";
|
||||
|
||||
import { BaseLLM } from "..";
|
||||
import { CompletionOptions } from "../..";
|
||||
import { CompletionOptions } from "..";
|
||||
import { BaseLLM } from "../llm";
|
||||
|
||||
jest.setTimeout(100_000);
|
||||
|
||||
|
|
@ -6,21 +6,25 @@ import { llmFromTitle } from "../loadConfig";
|
|||
const redDecorationType = vscode.window.createTextEditorDecorationType({
|
||||
isWholeLine: true,
|
||||
backgroundColor: "rgba(255, 0, 0, 0.2)",
|
||||
color: "--vscode-editorForeground",
|
||||
color: "white",
|
||||
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
|
||||
});
|
||||
|
||||
const greenDecorationType = vscode.window.createTextEditorDecorationType({
|
||||
isWholeLine: true,
|
||||
backgroundColor: "rgba(0, 255, 0, 0.2)",
|
||||
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
|
||||
});
|
||||
|
||||
const indexDecorationType = vscode.window.createTextEditorDecorationType({
|
||||
isWholeLine: true,
|
||||
backgroundColor: "rgba(255, 255, 255, 0.2)",
|
||||
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
|
||||
});
|
||||
const belowIndexDecorationType = vscode.window.createTextEditorDecorationType({
|
||||
isWholeLine: true,
|
||||
backgroundColor: "rgba(255, 255, 255, 0.1)",
|
||||
rangeBehavior: vscode.DecorationRangeBehavior.ClosedClosed,
|
||||
});
|
||||
|
||||
class DecorationTypeRangeManager {
|
||||
|
|
@ -45,7 +49,9 @@ class DecorationTypeRangeManager {
|
|||
lastRange.end.translate(1)
|
||||
);
|
||||
} else {
|
||||
this.ranges.push(new vscode.Range(index, 0, index + 1, 0));
|
||||
this.ranges.push(
|
||||
new vscode.Range(index, 0, index, Number.MAX_SAFE_INTEGER)
|
||||
);
|
||||
}
|
||||
|
||||
this.editor.setDecorations(this.decorationType, this.ranges);
|
||||
|
|
@ -98,19 +104,15 @@ export class VerticalPerLineDiffHandler {
|
|||
async handleDiffLine(diffLine: DiffLine) {
|
||||
switch (diffLine.type) {
|
||||
case "same":
|
||||
this.incrementCurrentLineIndex();
|
||||
break;
|
||||
case "old":
|
||||
this.setLineAtIndexRed(this.currentLineIndex);
|
||||
this.incrementCurrentLineIndex();
|
||||
break;
|
||||
case "new":
|
||||
await this.insertLineAboveIndex(
|
||||
this.currentLineIndex,
|
||||
diffLine.newLine
|
||||
);
|
||||
await this.insertLineAboveIndex(this.currentLineIndex, diffLine.line);
|
||||
break;
|
||||
}
|
||||
this.incrementCurrentLineIndex();
|
||||
}
|
||||
|
||||
private incrementCurrentLineIndex() {
|
||||
|
|
@ -136,15 +138,14 @@ export class VerticalPerLineDiffHandler {
|
|||
private updateIndexLineDecorations() {
|
||||
// Highlight the line at the currentLineIndex
|
||||
// And lightly highlight all lines between that and endLine
|
||||
const start = new vscode.Position(this.currentLineIndex, 0);
|
||||
this.editor.setDecorations(indexDecorationType, [
|
||||
new vscode.Range(start, start.translate(1)),
|
||||
]);
|
||||
|
||||
const end = new vscode.Position(this.endLine, 0);
|
||||
this.editor.setDecorations(belowIndexDecorationType, [
|
||||
new vscode.Range(start.translate(1), end),
|
||||
]);
|
||||
// const start = new vscode.Position(this.currentLineIndex, 0);
|
||||
// this.editor.setDecorations(indexDecorationType, [
|
||||
// new vscode.Range(start, start.translate(1)),
|
||||
// ]);
|
||||
// const end = new vscode.Position(this.endLine, 0);
|
||||
// this.editor.setDecorations(belowIndexDecorationType, [
|
||||
// new vscode.Range(start.translate(1), end),
|
||||
// ]);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -216,6 +217,7 @@ export async function streamEdit(input: string) {
|
|||
llm,
|
||||
input
|
||||
)) {
|
||||
console.log(diffLine.type + " " + diffLine.line);
|
||||
await diffHandler.handleDiffLine(diffLine);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue