Skip to content

Commit 583cb1a

Browse files
committed
fix: optimize empty file handling and enhance tool selection documentation
1 parent 1a41e2f commit 583cb1a

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

src/core/prompts/sections/rules.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ function getEditingInstructions(diffStrategy?: DiffStrategy, experiments?: Recor
3333
"- The append_to_file tool adds content to the end of files, such as appending new log entries or adding new data records. This tool will always add the content at the end of the file.",
3434
"- The prepend_to_file tool adds content to the beginning of files, such as adding license headers, import statements, or configuration headers. This tool will always add the content at the beginning of the file, before any existing content.",
3535
)
36+
37+
// Enhanced tool selection guidance
38+
instructions.push(
39+
"- When deciding which tool to use for modifying existing files:",
40+
" * Use write_to_file for complete file rewrites or when creating new files from scratch",
41+
" * Use prepend_to_file when adding content that logically belongs at the top of a file (license headers, import/include statements, file-level documentation)",
42+
" * Use append_to_file when adding content that logically belongs at the end (new entries, log records, additional data points)",
43+
" * Use apply_diff or insert_content for targeted changes within the middle of a file",
44+
" * Use search_and_replace for pattern-based text replacements across the file",
45+
)
3646
}
3747

3848
// Additional details for experimental features

src/core/tools/prependToFileTool.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,10 @@ export async function prependToFileTool(
8282

8383
// If file exists, prepend newContent to existing content
8484
if (fileExists && cline.diffViewProvider.originalContent) {
85-
newContent = newContent + "\n" + cline.diffViewProvider.originalContent
85+
// Optimize for empty files - don't add newline if the file is empty
86+
newContent = cline.diffViewProvider.originalContent.length === 0
87+
? newContent
88+
: newContent + "\n" + cline.diffViewProvider.originalContent
8689
}
8790

8891
// Editor is open, stream content in
@@ -119,7 +122,10 @@ export async function prependToFileTool(
119122

120123
// If file exists, prepend newContent to existing content
121124
if (fileExists && cline.diffViewProvider.originalContent) {
122-
newContent = newContent + "\n" + cline.diffViewProvider.originalContent
125+
// Optimize for empty files - don't add newline if the file is empty
126+
newContent = cline.diffViewProvider.originalContent.length === 0
127+
? newContent
128+
: newContent + "\n" + cline.diffViewProvider.originalContent
123129
}
124130

125131
await cline.diffViewProvider.update(

0 commit comments

Comments
 (0)