-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Fix #3653: insertContentTool to handle appending content when origina… #4380
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
Fix #3653: insertContentTool to handle appending content when origina… #4380
Conversation
…en original file already has newline at end
5bc4d82 to
d8b6267
Compare
daniel-lxs
left a comment
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.
Hey @Ruakij, thank you for working on this fix!
I left a couple of questions regarding the behavior of the fix on files that intentionally include an empty line at the end.
Let me know what you think!
| // If the original file content ends with a newline, split("\n") will result in a trailing empty string. | ||
| // When appending, we want to avoid adding an extra blank line. | ||
| if (lineNumber === 0 && lines.length > 0 && lines[lines.length - 1] === "") { | ||
| lines.pop() // Remove the trailing empty string |
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.
Will this fix affect files that intentionally end with a new line?
Usually typescript files always have a trailing \n at the end, while a small detail, it is important to keep to prevent unintended changes to the files.
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.
I have tested a few things just now and found some inconsistencies:
insertGroups
- lways adds a trailing \n when original is empty-str (an empty new file)
- wont add a trailing \n when original has \n
cline.diffViewProvider.update:
- Always adds a trailing \n in the initial diff-view, double newlines later get removed
- Has preservation of trailing newline if the original file has one
- Triggers 2-3 diffs? This function is a bit weird to me.
createPrettyPatch or rather diff.createPatch:
- always adds a trailing \n if oldStr didnt have one
- But its only used for chat-box
This leads to insertContentTool itself not adding the trailing \n because it wasnt technically part of the content, but those get introduced by either insertGroups or ultimately cline.diffViewProvider.update.
Its also currently not possible to add leading or trailing \n in the tool-call as these dont survive the tool-call parsing.
So i am not sure how to proceed with this. The tests correctly cover the insertContentTool, but the mocks are not accurate i suppose and its not actually possible to check for trailing newline or what actually got inserted.
For separation-of-concerns, the insertContentTool shouldnt concern itself with that problematic. The mockCline is a simplified stand-in, so to me thats also perfectly fine.
Not 100% sure what to do with insertGroups though.
| // Expected: no extra blank line | ||
| const expectedContent = "Existing Line 1\nExisting Line 2\nAppended Line 1\nAppended Line 2" | ||
| expect(mockCline.diffViewProvider.update).toHaveBeenCalledWith(expectedContent, true) | ||
| }) |
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.
It might be a good idea to test if an intentional empty line is preserved.
Consider adding test cases for:
// File with intentional empty line at end
const originalContent = "Line 1\nLine 2\n\n"; // Should preserve the empty line
// File with multiple intentional empty lines
const originalContent = "Line 1\nLine 2\n\n\n"; // Should preserve both empty linesThese cases would help catch the issue where the current fix incorrectly removes legitimate empty lines.
|
Hey @Ruakij I think I worked on this and solved it, can you take a look to the latest version to see if the issue is still happening? |
|
This seems to be solved by #3550 |
Related GitHub Issue
Closes: #3653
Description
This PR addresses a bug where sequential
insert_contentoperations would add an extra blank line between the inserted contents, specifically when appending to a file that already ends with a newline.The action by Roo should be correct, but some editors add a trailing newline. And while the insert-operation after the trailing newline is technically correct, this causes unexpected insertions like in the issue.
The fix introduces special handling within
insertContentTool.tsto detect if an append operation is being performed on a file that already has a trailing newline. In such cases, the existing trailing empty string (resulting fromsplit('\n')) is removed before inserting.This prevents the
join('\n')operation from introducing an additional blank line.Test Procedure
Unit tests have been added to cover this bug fix and ensure proper handling of various newline scenarios.
To verify the fix:
Type of Change
srcor test files.Pre-Submission Checklist
npm run lint).console.log) has been removed.npm test).mainbranch.npm run changesetif this PR includes user-facing changes or dependency updates.Screenshots / Videos
Documentation Updates
Additional Notes
Get in Touch
Discord:
ruakijImportant
Fixes bug in
insertContentTool.tsto prevent extra blank lines when appending to files ending with a newline, with tests added ininsertContentTool.test.ts.insertContentTool.tswhere appending content to a file ending with a newline added an extra blank line.linesarray if appending to a file with a trailing newline.insertContentTool.test.tsto verify appending behavior with and without trailing newlines.This description was created by
for 5bc4d82e9b976915ae3b6af1a3fac02705ec41de. You can customize this summary. It will automatically update as commits are pushed.