Skip to content

fix(textarea): preserve newline when deleting the last character - #1072

Open
kimjooyoon wants to merge 2 commits into
charmbracelet:mainfrom
kimjooyoon:fix-delete-forward-newline
Open

kimjooyoon wants to merge 2 commits into
charmbracelet:mainfrom
kimjooyoon:fix-delete-forward-newline

Conversation

@kimjooyoon

@kimjooyoon kimjooyoon commented Oct 21, 2026 •

Copy link
Copy Markdown

Fixes #1071.

Delete on b in ab\ncd currently produces acd: 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 yields a\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:

  • The eight-case regression and go test ./textarea pass.
  • Full 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).

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 by-ae left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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. Your wide_character case 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.

  2. 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 is len(...) > 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 clamp m.col before the boundary check (textarea.go#L1281). Doing the same here would keep the three paths consistent, though >= already handles an out-of-range column.

@kimjooyoon

Copy link
Copy Markdown
Author

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
@kimjooyoon

Copy link
Copy Markdown
Author

Removed the redundant guard after the early line-end check in f70376a. Thanks for pointing this out, @by-ae. The eight boundary cases, textarea package tests, and full-project race/coverage checks pass locally on Go 1.26.9.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

textarea: Delete on the last character also removes the following newline

2 participants