Update insertContentTool to handle new files without generating diffs

This commit is contained in:
Ruakij 2025-06-08 11:09:12 +02:00 committed by Daniel Riccio
parent 8805f7bc01
commit c96bc404e5
No known key found for this signature in database
GPG key ID: FFD5FD825F8E8209
2 changed files with 16 additions and 27 deletions

View file

@ -48,28 +48,6 @@ jest.mock("../../ignore/RooIgnoreController", () => ({
},
}))
// Mock insertGroups from diff/insert-groups
jest.mock("../../diff/insert-groups", () => ({
insertGroups: jest.fn().mockImplementation((lines, groups) => {
let newLines = [...lines]
for (const group of groups) {
const { index, elements } = group
if (index === -1 || index >= newLines.length) {
// Append to end
newLines.push(...elements)
} else if (index < 0) {
// Insert at beginning (index -1 for line 0, but insertGroups expects 0 for beginning)
// This mock simplifies, assuming index -1 is always append.
// For line 1, index is 0.
newLines.splice(0, 0, ...elements)
} else {
newLines.splice(index, 0, ...elements)
}
}
return newLines
}),
}))
describe("insertContentTool", () => {
const testFilePath = "test/file.txt"
const absoluteFilePath = "/test/file.txt"
@ -77,7 +55,6 @@ describe("insertContentTool", () => {
const mockedFileExistsAtPath = fileExistsAtPath as jest.MockedFunction<typeof fileExistsAtPath>
const mockedFsReadFile = fs.readFile as jest.MockedFunction<typeof fs.readFile>
const mockedPathResolve = path.resolve as jest.MockedFunction<typeof path.resolve>
const mockedInsertGroups = require("../../diff/insert-groups").insertGroups as jest.MockedFunction<any>
let mockCline: any
let mockAskApproval: jest.Mock

View file

@ -116,11 +116,22 @@ export async function insertContentTool(
await delay(200)
}
const diff = formatResponse.createPrettyPatch(relPath, fileContent, updatedContent)
// For consistency with writeToFileTool, handle new files differently
let diff: string | undefined
let approvalContent: string | undefined
if (fileExists && !diff) {
pushToolResult(`No changes needed for '${relPath}'`)
return
if (fileExists) {
// For existing files, generate diff and check for changes
diff = formatResponse.createPrettyPatch(relPath, fileContent, updatedContent)
if (!diff) {
pushToolResult(`No changes needed for '${relPath}'`)
return
}
approvalContent = undefined
} else {
// For new files, skip diff generation and provide full content
diff = undefined
approvalContent = updatedContent
}
await cline.diffViewProvider.update(updatedContent, true)
@ -128,6 +139,7 @@ export async function insertContentTool(
const completeMessage = JSON.stringify({
...sharedMessageProps,
diff,
content: approvalContent,
lineNumber: lineNumber,
isProtected: isWriteProtected,
} satisfies ClineSayTool)