Repository navigation
fix(textarea): preserve newline when deleting the last character - #1072
kimjooyoon wants to merge 2 commits into
Conversation
Check for a cursor already at line end before deleting a rune, so one Delete press cannot also merge the next line. Add eight regression and boundary cases. Prepared and locally verified with Codex assistance. Fixes charmbracelet#1071
by-ae
left a comment
There was a problem hiding this comment.
I checked this against current main. With main's textarea.go, the new test fails in exactly three cases (last_character, only_character, wide_character). With this branch it passes, along with the rest of go test ./textarea/. The reorder is the right fix. The old code deleted the rune first, and then m.col >= len(...) was true for the now-shorter line, so the same keypress also joined the next line.
Two small follow-ups:
-
Overlap with #1067. That PR rewrites this same block for graphemes, and it keeps the old order: delete first, then
if m.col >= len(m.value[m.row]) { m.mergeLineBelow(...) }(#1067 diff, textarea.go around L1327). So whichever lands second needs to keep the "check line end first" order, or the bug comes back for grapheme deletes. Yourwide_charactercase covers a single wide rune, but a multi-rune grapheme case (for example"ae\u0301\nx"at col 1, expecting"a\nx"once #1067 is in) would catch that regression directly. -
The guard is redundant now. Once the line-end case breaks out early,
m.col < len(m.value[m.row])is always true, and so islen(...) > 0. The block can be just:if m.col >= len(m.value[m.row]) { m.mergeLineBelow(m.row) break } m.value[m.row] = slices.Delete(m.value[m.row], m.col, m.col+1)
The sibling cases (
DeleteAfterCursor,DeleteCharacterBackward) also clampm.colbefore the boundary check (textarea.go#L1281). Doing the same here would keep the three paths consistent, though>=already handles an out-of-range column.
|
Thanks @by-ae for checking both revisions. Agreed: whichever PR lands second must keep the line-end check before deletion. The combining-mark regression belongs with the #1067 integration; this patch keeps its current rune scope. I also agree the inner guard is redundant after the early break. I'll track that cleanup and review the sibling-path clamping separately. |
The early line-end branch already establishes the bounds needed to delete the current rune. Apply the existing review feedback without changing clamping or grapheme behavior. Assisted-by: OpenAI Codex
Fixes #1071.
Delete on
binab\ncdcurrently producesacd: removing the character makes the cursor look as though it had already been at line end, so the same keypress joins the lines. Check the line-end condition first so the keypress yieldsa\ncd; Delete at an existing line end still joins lines. The early branch also makes the following bounds guard redundant, so remove that wrapper.Regression coverage includes ASCII, a one-character line, a wide character and five boundary controls.
Validation on Go 1.26.9, macOS arm64:
go test ./textareapass.go test -race -covermode atomic -coverprofile=... ./...passes.Current CI status remains separate from these local checks. The historical native/Gooo reproduction retains its original/submitted source pins.
AI-assisted patch, regression tests and validation (Codex).