From d7c2c5326380769e12b29418a2df44e9a4e6cd58 Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Sat, 30 Dec 2023 11:09:43 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20test=20and=20fix=20streamDiff?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/diff/diffLines.ts | 19 +++-- core/diff/util.ts | 3 +- core/index.d.ts | 12 +-- core/package.json | 2 +- core/test/diff.test.ts | 75 +++++++++++++++++++ core/{llm => }/test/edit.test.ts | 14 ++-- core/{llm => }/test/editme.py | 0 core/{llm => }/test/llm.test.ts | 4 +- extensions/vscode/src/diff/verticalPerLine.ts | 36 ++++----- 9 files changed, 120 insertions(+), 45 deletions(-) create mode 100644 core/test/diff.test.ts rename core/{llm => }/test/edit.test.ts (87%) rename core/{llm => }/test/editme.py (100%) rename core/{llm => }/test/llm.test.ts (97%) diff --git a/core/diff/diffLines.ts b/core/diff/diffLines.ts index 6c2b2d4f0..d7a44c672 100644 --- a/core/diff/diffLines.ts +++ b/core/diff/diffLines.ts @@ -8,22 +8,24 @@ export async function* streamDiff( oldLines: string[], newLines: AsyncGenerator ): AsyncGenerator { + 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 }; } } } diff --git a/core/diff/util.ts b/core/diff/util.ts index 0dcbf0818..1808a5b4b 100644 --- a/core/diff/util.ts +++ b/core/diff/util.ts @@ -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; } /** diff --git a/core/index.d.ts b/core/index.d.ts index 59b334961..ab499bc4c 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -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; getConfigJsUrl(): Promise; diff --git a/core/package.json b/core/package.json index d3c6e494a..17c7974f9 100644 --- a/core/package.json +++ b/core/package.json @@ -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", diff --git a/core/test/diff.test.ts b/core/test/diff.test.ts new file mode 100644 index 000000000..6ec359d2b --- /dev/null +++ b/core/test/diff.test.ts @@ -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 { + 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); + }); +}); diff --git a/core/llm/test/edit.test.ts b/core/test/edit.test.ts similarity index 87% rename from core/llm/test/edit.test.ts rename to core/test/edit.test.ts index 4abf7e018..eedfaa436 100644 --- a/core/llm/test/edit.test.ts +++ b/core/test/edit.test.ts @@ -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); diff --git a/core/llm/test/editme.py b/core/test/editme.py similarity index 100% rename from core/llm/test/editme.py rename to core/test/editme.py diff --git a/core/llm/test/llm.test.ts b/core/test/llm.test.ts similarity index 97% rename from core/llm/test/llm.test.ts rename to core/test/llm.test.ts index 6a7b220c9..7a4ee0005 100644 --- a/core/llm/test/llm.test.ts +++ b/core/test/llm.test.ts @@ -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); diff --git a/extensions/vscode/src/diff/verticalPerLine.ts b/extensions/vscode/src/diff/verticalPerLine.ts index 8e79af8fd..d24f96309 100644 --- a/extensions/vscode/src/diff/verticalPerLine.ts +++ b/extensions/vscode/src/diff/verticalPerLine.ts @@ -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); } }