Skip to content

fix(textarea): stop wordLeft from spinning on an empty buffer#974

Open
c-tonneslan wants to merge 1 commit into
charmbracelet:masterfrom
c-tonneslan:fix/textarea-wordLeft-infinite-loop
Open

fix(textarea): stop wordLeft from spinning on an empty buffer#974
c-tonneslan wants to merge 1 commit into
charmbracelet:masterfrom
c-tonneslan:fix/textarea-wordLeft-infinite-loop

Conversation

@c-tonneslan
Copy link
Copy Markdown

Filed against bubbletea but lives here: charmbracelet/bubbletea#1652.

`textarea.wordLeft` (the `alt+left` / `alt+b` binding) has an unconditional `for` loop whose only exit is landing on a non-space rune. When the textarea is empty (or all trailing whitespace) `characterLeft` is a no-op at `(0, 0)` and the break condition `col < len(value[row])` is `0 < 0`, so the loop spins forever and freezes the Bubble Tea event loop. `doWordRight` already has the same kind of guard for the end-of-text case.

Added an explicit beginning-of-text check at the top of the loop. If we're already at `(0, 0)`, return early.

Test plan

```
go test ./textarea/...
```

Added `TestWordLeftOnEmptyDoesNotHang` which dispatches `alt+left` on an empty textarea inside a goroutine and fails if the call doesn't return within 2 seconds — without the fix the test hangs until the test framework times out.

textarea.wordLeft has an unconditional for-loop whose only exit is
landing on a non-space rune. When the textarea is empty (or all
trailing whitespace) characterLeft is a no-op at (0,0) and the break
condition col < len(value[row]) is 0 < 0, so the loop spins forever
and freezes the Bubble Tea event loop. doWordRight already guards the
same way for end-of-text.

Add a beginning-of-text check at the top of the loop so the function
returns instead of looping. Filed against bubbletea (charmbracelet/
bubbletea#1652) but the code lives here.

Regression test triggers WordBackward on an empty textarea inside a
goroutine and fails if it doesn't return within 2 seconds.

Signed-off-by: Charlie Tonneslan <cst0520@gmail.com>
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.

1 participant