-
Notifications
You must be signed in to change notification settings - Fork 2.6k
feat: add prepend_to_file tool for prepending content to files #2787
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
…on and test coverage
|
|
|
||
| pushToolResult( | ||
| `The user made the following updates to your content:\n\n${userEdits}\n\n` + | ||
| `The updated content, which includes both your original modifications and the user's edits, has been successfully saved to ${relPath.toPosix()}. Here is the full, updated content of the file, including line numbers:\n\n` + |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Possible issue: using relPath.toPosix() assumes that relPath is an object with a .toPosix() method, but it's defined as a string. Consider using a proper path conversion helper (e.g., path.posix.normalize(relPath)) to ensure cross-platform compatibility.
| await cline.diffViewProvider.open(relPath) | ||
| } | ||
|
|
||
| // If file exists, prepend newContent to existing content |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider refactoring: there is duplicated logic in handling partial updates versus full updates for prepending content. Extracting the common logic into a helper function would improve maintainability and reduce code duplication.
| everyLineHasLineNumbers(newContent) ? stripLineNumbers(newContent) : newContent, | ||
| true, | ||
| ) | ||
| await delay(300) // wait for diff view to update |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A hardcoded delay of 300 ms is used after updating the diff view. This may be brittle in some environments; it would be better to either extract this value as a configurable constant or wait for a specific event/acknowledgment from the diff view for updates.
|
I've been talking to @samhvw8 and we've been discussing just having one insert_content tool that can insert at the beginning, end, or any line within the file (and removing the append_to_file). I think that might be better than having a lot of special case tools, after discussing more. What do you think? |
|
I'm going to close this now that insert_content is available for everyone. Thank you for the contribution and pushing our thinking on this! |
Add prepend_to_file Tool for Prepending Content to Files
Context
Adding a new
prepend_to_filetool to complement the existingappend_to_filetool. This provides a more efficient and explicit way to add content to the beginning of files, such as license headers, import statements, and configuration blocks.Implementation
append_to_filetool with the key difference being content placementsrc/shared/tools.tssrc/core/prompts/tools/prepend-to-file.tssrc/core/Cline.tssrc/schemas/index.tsScreenshots
No UI changes - this PR adds a new tool accessible through the AI interface.
How to Test
Use the AI to add a license header to a file:
The AI should recommend using the prepend_to_file tool:
Verify the content is added to the beginning of the file
Also test with an empty file to verify only the content is added without extra newlines
Important
Introduces
prepend_to_filetool to prepend content to files, with integration, testing, and error handling updates.prepend_to_filetool to prepend content to files, handling empty files and different line endings.tools.ts,index.ts, andrules.tsto includeprepend_to_file.getPrependToFileDescription()inprepend-to-file.ts.prependToFileTool.test.tsfor testing various cases like empty files, line endings, and error handling.rooIgnoreControllerand handles missing parameters.This description was created by
for a52ec5d. You can customize this summary. It will automatically update as commits are pushed.