fix(app): keep increment and decrement lines in Edit diffs

A changed line inside a hunk can start with the same marker as a file header. Use the hunk ranges to preserve those lines in tool output.

Co-authored-by: Ouyang Xingyuan <galaxyroaming@qq.com>
This commit is contained in:
paseo-bot[bot]
2026-11-30 06:46:36 +00:00
co-authored by Ouyang Xingyuan
parent c45689d1ba
commit 8cc28cd78f
2 changed files with 34 additions and 14 deletions
@@ -62,6 +62,20 @@ describe("tool-call-parsers", () => {
expect(parsed.find((entry) => entry.type === "add")?.content).toBe("+new");
});
it("keeps changed lines that start with two diff markers", () => {
const parsed = parseUnifiedDiff(
["--- a/counter.ts", "+++ b/counter.ts", "@@ -1 +1 @@", "---counter;", "+++counter;"].join(
"\n",
),
);
expect(parsed).toEqual([
{ type: "header", content: "@@ -1 +1 @@" },
{ type: "remove", content: "---counter;" },
{ type: "add", content: "+++counter;" },
]);
});
it("keeps file headers in multi-file diffs", () => {
const parsed = parseUnifiedDiff(
[
+20 -14
View File
@@ -162,47 +162,53 @@ export function parseUnifiedDiff(diffText?: string): DiffLine[] {
const fileHeaders = lines.filter((line) => line.startsWith("diff --git "));
const hasMultipleFiles = fileHeaders.length > 1;
const diff: DiffLine[] = [];
let oldLinesRemaining = 0;
let newLinesRemaining = 0;
for (const line of lines) {
if (!line.length) {
diff.push({ type: "context", content: line });
if (line.startsWith("diff --git ")) {
oldLinesRemaining = 0;
newLinesRemaining = 0;
if (hasMultipleFiles) diff.push({ type: "header", content: line });
continue;
}
const isFileHeader = hasMultipleFiles && line.startsWith("diff --git ");
if (line.startsWith("@@") || isFileHeader) {
if (line.startsWith("@@")) {
const range = /^@@ -\d+(?:,(\d+))? \+\d+(?:,(\d+))? @@/.exec(line);
oldLinesRemaining = Number(range?.[1] ?? 1);
newLinesRemaining = Number(range?.[2] ?? 1);
diff.push({ type: "header", content: line });
continue;
}
const inHunk = oldLinesRemaining > 0 || newLinesRemaining > 0;
if (line.startsWith("+")) {
if (!line.startsWith("+++")) {
if (inHunk || !line.startsWith("+++")) {
diff.push({ type: "add", content: line });
newLinesRemaining--;
}
continue;
}
if (line.startsWith("-")) {
if (!line.startsWith("---")) {
if (inHunk || !line.startsWith("---")) {
diff.push({ type: "remove", content: line });
oldLinesRemaining--;
}
continue;
}
if (
line.startsWith("diff --git") ||
line.startsWith("index ") ||
line.startsWith("---") ||
line.startsWith("+++")
) {
continue;
}
if (line.startsWith("index ")) continue;
if (line.startsWith("\\ No newline")) {
diff.push({ type: "header", content: line });
continue;
}
if (line.startsWith(" ")) {
oldLinesRemaining--;
newLinesRemaining--;
}
diff.push({ type: "context", content: line });
}