-
Notifications
You must be signed in to change notification settings - Fork 801
Maintenance: DeleteToCaretAction - Add edge case handling and test coverage #1369
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
base: master
Are you sure you want to change the base?
Conversation
Add early return when caret is at position 0 to match the pattern used by similar command line delete actions (DeletePreviousWordAction, DeletePreviousCharAction). This makes the intent clearer and avoids unnecessary calls to deleteText with length 0. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
Add test case for when caret is at the beginning of the command line to ensure the action correctly handles this edge case without side effects. This matches test coverage patterns in similar actions like DeletePreviousWordActionTest and DeletePreviousCharActionTest. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]>
| override fun execute(commandLine: VimCommandLine): Boolean { | ||
| commandLine.deleteText(0, commandLine.caret.offset) | ||
| val caretOffset = commandLine.caret.offset | ||
| if (caretOffset == 0) return 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.
Question about necessity of this check:
Looking at the deleteText API documentation in VimCommandLine.kt:84, it accepts offset and length parameters. When caretOffset == 0, calling deleteText(0, 0) should be a no-op (deleting zero-length text).
While I understand this change follows the pattern in DeletePreviousCharAction and DeletePreviousWordAction, those actions have different semantics:
DeletePreviousCharActionandDeletePreviousWordActiondelete backwards from the caretDeleteToCaretActiondeletes forwards from position 0 to the caret
For backward deletion, the guard makes sense because:
- There's computation involved (finding previous grapheme/word boundary)
- They need to close the command line when text is empty (special behavior)
For DeleteToCaretAction, the guard is essentially an optimization to avoid a no-op call. While this is fine, it's worth confirming:
- Does
deleteText(0, 0)have any side effects that should be avoided? - Does the test suite verify this is actually the correct Vim behavior?
Suggestion: Consider adding a comment explaining why this early return is needed, especially if there are side effects in deleteText beyond the obvious text deletion.
| @Test | ||
| fun `test delete at beginning does nothing`() { |
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.
Test coverage observation:
Good addition! However, this test validates that the implementation doesn't crash or cause issues, but it doesn't strictly verify that "nothing happens" since:
- The text is already empty after typing
: - Pressing
<C-U>on an empty command line still results in empty text
Consider adding a more explicit test case:
@Test
fun `test delete at beginning with text does nothing to caret position`() {
typeText(":text")
// Move caret to beginning
typeText("<Home>")
// Should not delete anything since caret is at position 0
typeText("<C-U>")
assertExText("text")
}This would verify that when the caret is at position 0 with text present, the text remains unchanged (which is the actual edge case being guarded against).
Code Review SummaryThanks for this contribution! This is a well-structured maintenance PR that improves edge case handling and test coverage. Here's my detailed review: ✅ Strengths
🔍 Areas for Consideration1. Necessity of the Guard ClauseI've left an inline comment questioning whether this guard is strictly necessary. The key difference between this action and the referenced similar actions is:
When caretOffset == 0, calling deleteText(0, 0) should be a no-op. The guard is an optimization, which is fine, but it would help to:
2. Test Case Could Be More ExplicitThe new test validates the code doesn't crash, but doesn't strictly test the edge case scenario. See my inline suggestion for a test that would verify the behavior when the caret is at position 0 with text present in the command line. 📊 Overall AssessmentCode Quality: ✅ High - Clean, follows conventions RecommendationThis is a solid maintenance improvement. I'd suggest:
The change is safe to merge as-is if the team is satisfied with the current approach, but the suggestions above would make it even more robust. Review completed by Claude Code |
Summary
DeleteToCaretActionfor when caret is at position 0Area Inspected
vim-engine/src/main/kotlin/com/maddyhome/idea/vim/action/ex/DeleteToCaretAction.ktIssues Found
caretOffset == 0before callingdeleteText, unlike similar actions (DeletePreviousWordAction,DeletePreviousCharAction)Changes Made
caretOffset == 0to match the pattern used by similar command line delete actionstest delete at beginning does nothingto verify correct behavior at edge casecaretOffsetto a local variable for improved readabilityWhy These Changes Improve the Code
References
Similar implementations that follow this pattern:
DeletePreviousWordAction.kt:21-if (caretOffset == 0) return trueDeletePreviousCharAction.kt:26-if (caretOffset == 0) return true🤖 Generated with Claude Code