feat: use a list of diffContexts instead of a single one
This commit is contained in:
parent
c34b1d82b1
commit
72399ca5cc
4 changed files with 95 additions and 48 deletions
|
|
@ -67,56 +67,62 @@ export function currentFileContentBlock(
|
|||
}
|
||||
|
||||
export function editHistoryBlock(
|
||||
editDiffHistories: string, // unified diffs with Index headers
|
||||
editDiffHistories: string[], // array of unified diffs with Index headers
|
||||
) {
|
||||
if (!editDiffHistories.trim()) {
|
||||
if (!editDiffHistories.length) {
|
||||
return "";
|
||||
}
|
||||
|
||||
const blocks: string[] = [];
|
||||
|
||||
// Split on Index: lines to get the unified diff.
|
||||
const diffSections = editDiffHistories
|
||||
.split(/^Index: /m)
|
||||
.filter((section) => section.trim());
|
||||
for (const editDiffHistory of editDiffHistories) {
|
||||
if (!editDiffHistory.trim()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (const section of diffSections) {
|
||||
const lines = section.split("\n");
|
||||
// Split on Index: lines to get the unified diff.
|
||||
const diffSections = editDiffHistory
|
||||
.split(/^Index: /m)
|
||||
.filter((section) => section.trim());
|
||||
|
||||
// Extract filename from the first line (after "Index: " was split off).
|
||||
const filename = lines[0];
|
||||
for (const section of diffSections) {
|
||||
const lines = section.split("\n");
|
||||
|
||||
// Find the start of the actual diff content (skip ---, +++, and === lines).
|
||||
const diffLines = lines
|
||||
.filter(
|
||||
// Extract filename from the first line (after "Index: " was split off).
|
||||
const filename = lines[0];
|
||||
|
||||
// Find the start of the actual diff content (skip ---, +++, and === lines).
|
||||
const diffLines = lines
|
||||
.filter(
|
||||
(line) =>
|
||||
!line.startsWith("---") &&
|
||||
!line.startsWith("+++") &&
|
||||
!line.startsWith("===") &&
|
||||
line.trim() !== "", // remove empty lines from header section
|
||||
)
|
||||
.slice(1); // remove the filename line
|
||||
|
||||
// Only include lines that are actual diff content (@@, +, -, or context lines).
|
||||
const actualDiffContent = diffLines.filter(
|
||||
(line) =>
|
||||
!line.startsWith("---") &&
|
||||
!line.startsWith("+++") &&
|
||||
!line.startsWith("===") &&
|
||||
line.trim() !== "", // remove empty lines from header section
|
||||
)
|
||||
.slice(1); // remove the filename line
|
||||
line.startsWith("@@") ||
|
||||
line.startsWith("+") ||
|
||||
line.startsWith("-") ||
|
||||
line.startsWith(" "), // context lines
|
||||
);
|
||||
|
||||
// Only include lines that are actual diff content (@@, +, -, or context lines).
|
||||
const actualDiffContent = diffLines.filter(
|
||||
(line) =>
|
||||
line.startsWith("@@") ||
|
||||
line.startsWith("+") ||
|
||||
line.startsWith("-") ||
|
||||
line.startsWith(" "), // context lines
|
||||
);
|
||||
if (actualDiffContent.length === 0) continue;
|
||||
|
||||
if (actualDiffContent.length === 0) continue;
|
||||
const diffBlock = [
|
||||
`User edited file "${filename}"`,
|
||||
"",
|
||||
"```diff",
|
||||
actualDiffContent.join("\n"),
|
||||
"```",
|
||||
].join("\n");
|
||||
|
||||
const diffBlock = [
|
||||
`User edited file "${filename}"`,
|
||||
"",
|
||||
"```diff",
|
||||
actualDiffContent.join("\n"),
|
||||
"```",
|
||||
].join("\n");
|
||||
|
||||
blocks.push(diffBlock);
|
||||
blocks.push(diffBlock);
|
||||
}
|
||||
}
|
||||
|
||||
return blocks.join("\n");
|
||||
|
|
|
|||
|
|
@ -291,7 +291,7 @@ line 3`;
|
|||
|
||||
describe("editHistoryBlock", () => {
|
||||
test("should format empty edit history", () => {
|
||||
const result = editHistoryBlock("");
|
||||
const result = editHistoryBlock([]);
|
||||
expect(result).toBe("");
|
||||
});
|
||||
|
||||
|
|
@ -306,7 +306,7 @@ describe("editHistoryBlock", () => {
|
|||
+ console.log("new");
|
||||
}`;
|
||||
|
||||
const result = editHistoryBlock(diff);
|
||||
const result = editHistoryBlock([diff]);
|
||||
const expected = `User edited file "test.js"
|
||||
|
||||
\`\`\`diff
|
||||
|
|
@ -337,7 +337,45 @@ Index: file2.js
|
|||
-let x = "old";
|
||||
+let x = "new";`;
|
||||
|
||||
const result = editHistoryBlock(unifiedDiff);
|
||||
const result = editHistoryBlock([unifiedDiff]);
|
||||
const expected = `User edited file "file1.js"
|
||||
|
||||
\`\`\`diff
|
||||
@@ -1 +1 @@
|
||||
-const old = 1;
|
||||
+const new = 1;
|
||||
\`\`\`
|
||||
User edited file "file2.js"
|
||||
|
||||
\`\`\`diff
|
||||
@@ -1 +1 @@
|
||||
-let x = "old";
|
||||
+let x = "new";
|
||||
\`\`\``;
|
||||
|
||||
expect(result).toBe(expected);
|
||||
});
|
||||
|
||||
test("should format multiple diff history entries", () => {
|
||||
// Multiple diffs should be concatenated into a single string
|
||||
const unifiedDiffs = [
|
||||
`Index: file1.js
|
||||
===================================================================
|
||||
--- a/file1.js 2023-01-01 10:00:00.000000000 +0000
|
||||
+++ b/file1.js 2023-01-01 10:01:00.000000000 +0000
|
||||
@@ -1 +1 @@
|
||||
-const old = 1;
|
||||
+const new = 1;`,
|
||||
`Index: file2.js
|
||||
===================================================================
|
||||
--- a/file2.js 2023-01-01 10:02:00.000000000 +0000
|
||||
+++ b/file2.js 2023-01-01 10:03:00.000000000 +0000
|
||||
@@ -1 +1 @@
|
||||
-let x = "old";
|
||||
+let x = "new";`,
|
||||
];
|
||||
|
||||
const result = editHistoryBlock(unifiedDiffs);
|
||||
const expected = `User edited file "file1.js"
|
||||
|
||||
\`\`\`diff
|
||||
|
|
@ -366,7 +404,7 @@ User edited file "file2.js"
|
|||
-export const Button = () => <button>Old</button>;
|
||||
+export const Button = () => <button>New</button>;`;
|
||||
|
||||
const result = editHistoryBlock(diff);
|
||||
const result = editHistoryBlock([diff]);
|
||||
const expected = `User edited file "src/components/Button/Button.tsx"
|
||||
|
||||
\`\`\`diff
|
||||
|
|
@ -388,7 +426,7 @@ User edited file "file2.js"
|
|||
+ return "hello world";
|
||||
+}`;
|
||||
|
||||
const result = editHistoryBlock(diff);
|
||||
const result = editHistoryBlock([diff]);
|
||||
const expected = `User edited file "newfile.js"
|
||||
|
||||
\`\`\`diff
|
||||
|
|
@ -411,7 +449,7 @@ User edited file "file2.js"
|
|||
- return "goodbye";
|
||||
-}`;
|
||||
|
||||
const result = editHistoryBlock(diff);
|
||||
const result = editHistoryBlock([diff]);
|
||||
const expected = `User edited file "deletedfile.js"
|
||||
|
||||
\`\`\`diff
|
||||
|
|
@ -431,7 +469,7 @@ User edited file "file2.js"
|
|||
-old text
|
||||
+new text`;
|
||||
|
||||
const result = editHistoryBlock(diff);
|
||||
const result = editHistoryBlock([diff]);
|
||||
const expected = `User edited file "simple.txt"
|
||||
|
||||
\`\`\`diff
|
||||
|
|
|
|||
|
|
@ -50,12 +50,15 @@ export function currentFileContentBlock(
|
|||
}
|
||||
|
||||
export function editHistoryBlock(
|
||||
editDiffHistory: string, // could be a singe large unified diff
|
||||
editDiffHistory: string[], // could be a singe large unified diff
|
||||
) {
|
||||
// diffHistory is made from createDiff.
|
||||
// This uses createPatch from npm diff library, which includes an index line and a separator.
|
||||
// We get rid of these first two lines.
|
||||
return editDiffHistory.split("\n").slice(2).join("\n");
|
||||
return editDiffHistory
|
||||
.map((diff) => diff.split("\n").slice(2).join("\n"))
|
||||
.join("\n");
|
||||
// return editDiffHistory.split("\n").slice(2).join("\n");
|
||||
}
|
||||
|
||||
function mercuryNextEditTemplateBuilder(
|
||||
|
|
|
|||
|
|
@ -108,12 +108,12 @@ describe("mercuryCoderNextEdit", () => {
|
|||
it("should return the edit diff history unchanged", () => {
|
||||
const diffHistory =
|
||||
"diff --git a/file.ts b/file.ts\n==============================\n@@ -1,3 +1,4 @@";
|
||||
const result = editHistoryBlock(diffHistory);
|
||||
const result = editHistoryBlock([diffHistory]);
|
||||
expect(result).toBe("@@ -1,3 +1,4 @@");
|
||||
});
|
||||
|
||||
it("should handle empty diff history", () => {
|
||||
const result = editHistoryBlock("");
|
||||
const result = editHistoryBlock([]);
|
||||
expect(result).toBe("");
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue