Skip to content

Commit c96bc40

Browse files
Ruakijdaniel-lxs
authored andcommitted
Update insertContentTool to handle new files without generating diffs
1 parent 8805f7b commit c96bc40

File tree

2 files changed

+17
-28
lines changed

2 files changed

+17
-28
lines changed

src/core/tools/__tests__/insertContentTool.test.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,13 @@ jest.mock("../../ignore/RooIgnoreController", () => ({
4848
},
4949
}))
5050

51-
// Mock insertGroups from diff/insert-groups
52-
jest.mock("../../diff/insert-groups", () => ({
53-
insertGroups: jest.fn().mockImplementation((lines, groups) => {
54-
let newLines = [...lines]
55-
for (const group of groups) {
56-
const { index, elements } = group
57-
if (index === -1 || index >= newLines.length) {
58-
// Append to end
59-
newLines.push(...elements)
60-
} else if (index < 0) {
61-
// Insert at beginning (index -1 for line 0, but insertGroups expects 0 for beginning)
62-
// This mock simplifies, assuming index -1 is always append.
63-
// For line 1, index is 0.
64-
newLines.splice(0, 0, ...elements)
65-
} else {
66-
newLines.splice(index, 0, ...elements)
67-
}
68-
}
69-
return newLines
70-
}),
71-
}))
72-
7351
describe("insertContentTool", () => {
7452
const testFilePath = "test/file.txt"
7553
const absoluteFilePath = "/test/file.txt"
7654

7755
const mockedFileExistsAtPath = fileExistsAtPath as jest.MockedFunction<typeof fileExistsAtPath>
7856
const mockedFsReadFile = fs.readFile as jest.MockedFunction<typeof fs.readFile>
7957
const mockedPathResolve = path.resolve as jest.MockedFunction<typeof path.resolve>
80-
const mockedInsertGroups = require("../../diff/insert-groups").insertGroups as jest.MockedFunction<any>
8158

8259
let mockCline: any
8360
let mockAskApproval: jest.Mock

src/core/tools/insertContentTool.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,18 +116,30 @@ export async function insertContentTool(
116116
await delay(200)
117117
}
118118

119-
const diff = formatResponse.createPrettyPatch(relPath, fileContent, updatedContent)
120-
121-
if (fileExists && !diff) {
122-
pushToolResult(`No changes needed for '${relPath}'`)
123-
return
119+
// For consistency with writeToFileTool, handle new files differently
120+
let diff: string | undefined
121+
let approvalContent: string | undefined
122+
123+
if (fileExists) {
124+
// For existing files, generate diff and check for changes
125+
diff = formatResponse.createPrettyPatch(relPath, fileContent, updatedContent)
126+
if (!diff) {
127+
pushToolResult(`No changes needed for '${relPath}'`)
128+
return
129+
}
130+
approvalContent = undefined
131+
} else {
132+
// For new files, skip diff generation and provide full content
133+
diff = undefined
134+
approvalContent = updatedContent
124135
}
125136

126137
await cline.diffViewProvider.update(updatedContent, true)
127138

128139
const completeMessage = JSON.stringify({
129140
...sharedMessageProps,
130141
diff,
142+
content: approvalContent,
131143
lineNumber: lineNumber,
132144
isProtected: isWriteProtected,
133145
} satisfies ClineSayTool)

0 commit comments

Comments
 (0)