Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions core/diff/streamDiff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { LineStream, matchLine } from "./util.js";
export async function* streamDiff(
oldLines: string[],
newLines: LineStream,
filename?: string,
): AsyncGenerator<DiffLine> {
const oldLinesCopy = [...oldLines];

Expand All @@ -27,6 +28,7 @@ export async function* streamDiff(
newLineResult.value,
oldLinesCopy,
seenIndentationMistake,
filename,
);

if (!seenIndentationMistake && newLineResult.value !== newLine) {
Expand Down
8 changes: 7 additions & 1 deletion core/diff/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export function matchLine(
newLine: string,
oldLines: string[],
permissiveAboutIndentation = false,
filename?: string,
): MatchLineResult {
// Only match empty lines if it's the next one:
if (newLine.trim() === "" && oldLines[0]?.trim() === "") {
Expand Down Expand Up @@ -77,9 +78,14 @@ export function matchLine(
}
if (linesMatch(newLineTrimmed, oldLineTrimmed, i)) {
// This is a way to fix indentation, but only for sufficiently long lines to avoid matching whitespace or short lines
// For Python files, indentation is syntax and should never be permissive
const isPythonFile = filename?.endsWith(".py");
const shouldBePermissive =
!isPythonFile &&
(permissiveAboutIndentation || newLine.trim().length > 8);
if (
newLineTrimmed.trimStart() === oldLineTrimmed.trimStart() &&
(permissiveAboutIndentation || newLine.trim().length > 8)
shouldBePermissive
) {
return {
matchIndex: i,
Expand Down
50 changes: 50 additions & 0 deletions core/edit/lazy/deterministic-python.vitest.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expect, test } from "vitest";

import { DiffLine } from "../..";
import { streamDiff } from "../../diff/streamDiff";

async function* toLineStream(
lines: string[],
): AsyncGenerator<string, void, unknown> {
for (const line of lines) {
yield line;
}
}

async function collectStreamDiff(
oldText: string,
newText: string,
filename: string,
): Promise<DiffLine[]> {
const oldLines = oldText.split("\n");
const newLines = toLineStream(newText.split("\n"));
const diffs: DiffLine[] = [];
for await (const line of streamDiff(oldLines, newLines, filename)) {
diffs.push(line);
}
return diffs;
}

describe("streamDiff python indentation awareness", () => {
test("treats indentation changes as real diffs for .py files", async () => {
const oldText = 'def hello():\n print("world")';
const newText = 'def hello():\n print("world")';

const diffs = await collectStreamDiff(oldText, newText, "example.py");

const newLines = diffs.filter((d) => d.type === "new").map((d) => d.line);
const oldLines = diffs.filter((d) => d.type === "old").map((d) => d.line);
expect(newLines).toContain(' print("world")');
expect(oldLines).toContain(' print("world")');
});

test("still treats indentation as cosmetic for non-python files", async () => {
const oldText = 'function hello() {\n console.log("world");\n}';
const newText = 'function hello() {\n console.log("world");\n}';

const diffs = await collectStreamDiff(oldText, newText, "example.js");

const allSame = diffs.every((d) => d.type === "same");
expect(allSame).toBe(true);
});
});
2 changes: 1 addition & 1 deletion core/edit/lazy/streamLazyApply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function* streamLazyApply(

// Convert output to diff
const oldLines = oldCode.split(/\r?\n/);
let diffLines = streamDiff(oldLines, lines);
let diffLines = streamDiff(oldLines, lines, filename);
diffLines = filterLeadingAndTrailingNewLineInsertion(diffLines);
for await (const diffLine of diffLines) {
yield diffLine;
Expand Down
2 changes: 1 addition & 1 deletion core/edit/streamDiffLines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export async function* streamDiffLines(
lines = filterEnglishLinesAtEnd(lines);
}

let diffLines = streamDiff(oldLines, lines);
let diffLines = streamDiff(oldLines, lines, options.fileUri);
diffLines = filterLeadingAndTrailingNewLineInsertion(diffLines);
if (highlighted.length === 0) {
const line = prefix.split("\n").slice(-1)[0];
Expand Down
Loading