Repository navigation
Fix Vim clicks highlighting a cell and entering visual mode - #176
Conversation
A click in a Vim buffer called SetSelectionStart, which selected the cell and switched to visual mode, so focusing the window with the mouse left a one-cell highlight that survived scrolling and forced an Esc to get back to normal mode. The SDK reports held-button moves, including pointer jitter inside the pressed cell, through SetSelectionEnd, so a plain click was indistinguishable from the start of a drag. Track the left-button gesture in the mouse delegate: a click now only repositions the caret, and visual mode starts once the drag leaves the pressed cell. Double-click and triple-click keep their word and line selections and now enter the matching visual modes, and a drag that begins in insert mode still anchors the selection on the pressed cell. Signed-off-by: Santhi Prakash <[email protected]>
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
…isual Signed-off-by: Santhi Prakash <[email protected]>
The SelectLine delegate override enters visual line mode on a triple-click; pin the gesture so the mode transition cannot silently regress. Signed-off-by: Santhi Prakash <[email protected]>
| func (d *mouseDelegate) SelectWordAt(pos term.Coordinates) { | ||
| d.drag = dragPressed | ||
| d.pressedCell = pos | ||
| d.Delegate.SelectWordAt(pos) | ||
| if _, ok := d.vi.cursor.SelectionMode(); ok { | ||
| d.vi.setVisualMode() | ||
| d.vi.anchor = d.vi.cursorAtScroll() | ||
| d.vi.markMatchingBrace() | ||
| } | ||
| } |
There was a problem hiding this comment.
Double-click selects one character too many. Scroll.WordAt returns an end that points one past the word, and the base SelectWordAt leaves the cursor there. setVisualMode() then switches to vi's visual mode, which includes the character under the cursor.
What I saw:
- Double-clicking
bravoinalpha bravo charlieselects"bravo "(trailing space included), and the cursor ends up on the space. Vim selectsbravo, soy/dafter a double-click picks up the extra space. - For a word at the end of a line (
charlie) the selection is correct, but only because the cursor sits past the end of the line (X=19) and gets clamped. Mid-line and end-of-line words behave differently.
Suggested fix: after d.Delegate.SelectWordAt(pos), when a selection exists, step the cursor back one cell so it lands on the word's last character, then set anchor.
There was a problem hiding this comment.
Fixed — after SelectWordAt the cursor steps back one cell onto the word's last character before setVisualMode() and anchor are set. Double-clicking bravo now selects exactly bravo and lands the cursor at X=10, and the same path also fixes the end-of-line case (charlie ends with the cursor on the last character at X=18 rather than one past EOL at X=19).
| func (d *mouseDelegate) SelectLine(y int) { | ||
| d.Delegate.SelectLine(y) | ||
| if _, ok := d.vi.cursor.SelectionMode(); ok { | ||
| d.vi.setVisualLineMode() | ||
| d.vi.anchor = d.vi.cursorAtScroll() | ||
| d.vi.markMatchingBrace() | ||
| } | ||
| } |
There was a problem hiding this comment.
Minor: unlike SelectWordAt, this doesn't hold the click in place. By the time the triple-click arrives, OnAction has reset drag to dragIdle, so any small mouse movement goes straight to Delegate.SetSelectionEnd and moves the cursor within the line. The selection stays correct because line mode covers the whole line, but the cursor and anchor drift. For consistency, set d.drag = dragPressed and d.pressedCell here too (you'll need the full pos; y alone isn't enough).
There was a problem hiding this comment.
Done — OnAction now captures the press cell on LeftClick (the only place the full position arrives), and SelectLine re-arms dragPressed on it, so a held move inside the pressed cell no longer reaches Delegate.SetSelectionEnd.
| if _, ok := d.vi.cursor.SelectionMode(); ok { | ||
| d.vi.cursor.Unselect() | ||
| } |
There was a problem hiding this comment.
Nit: on a first click, the SDK always calls ClearSelection() right before SetSelectionStart() (see mouse.handleLeftClickSelect), so the selection is already gone here. Unless you want to guard against other callers, this check can be dropped.
There was a problem hiding this comment.
Dropped — handleLeftClickSelect is the only caller and it always runs ClearSelection() first, so there is no other path to guard.
| if d.drag == dragPressed { | ||
| if pos == d.pressedCell { | ||
| return | ||
| } | ||
| d.drag = dragSelecting | ||
| } |
There was a problem hiding this comment.
Note, no change needed: pressedCell is in window coordinates. This works because the SDK only auto-scrolls during a drag once the pointer is on a different row from the press (which has already ended dragPressed), and wheel events reset drag through OnAction. A short comment saying so would help the next reader.
There was a problem hiding this comment.
Added that explanation as a comment on the pressedCell field.
| assert.Equal(t, visualMode, v.handler.mode()) | ||
| sel, ok := v.Selection() | ||
| require.True(t, ok) | ||
| assert.Equal(t, "bravo ", sel) |
There was a problem hiding this comment.
This assertion locks in the extra-character bug flagged on SelectWordAt; after the fix it should expect "bravo". Also worth adding:
- a double-click on the last word of a line (
charlie), where the current code happens to be correct; - a check of
CursorAtScroll()after a double-click (it should be on the word's last character, X=10); - a double-click followed by
y, checking the yanked text.
There was a problem hiding this comment.
Now expects bravo and asserts CursorAtScroll() at X=10. Also added a last-word-of-line case (charlie, cursor X=18) and a double-click + y test asserting the yanked text is exactly bravo.
| assert.Equal(t, visualMode, v.handler.mode()) | ||
| sel, ok := v.Selection() | ||
| require.True(t, ok) | ||
| assert.Equal(t, "bravo ", sel) |
There was a problem hiding this comment.
Same here: this should expect "bravo" once SelectWordAt is fixed.
There was a problem hiding this comment.
Updated to bravo.
| t.Run("triple-click selects the line in visual line mode", func(t *testing.T) { | ||
| v := newVi() | ||
| v.Handle(mouseEventAt(term.MouseLeft, 7, 0)) | ||
| v.Handle(mouseEventAt(term.MouseRelease, 7, 0)) | ||
| v.Handle(mouseEventAt(term.MouseLeft, 7, 0)) | ||
| v.Handle(mouseEventAt(term.MouseRelease, 7, 0)) | ||
| v.Handle(mouseEventAt(term.MouseLeft, 7, 0)) | ||
| v.Handle(mouseEventAt(term.MouseRelease, 7, 0)) | ||
|
|
||
| assert.Equal(t, visualLineMode, v.handler.mode()) | ||
| sel, ok := v.Selection() | ||
| require.True(t, ok) | ||
| // Line selections carry a trailing newline. | ||
| assert.Equal(t, "alpha bravo charlie\n", sel) | ||
| }) |
There was a problem hiding this comment.
Consider a variant with small mouse movement after the third click (a held MouseLeft at a neighboring cell before release) that asserts the cursor position. That's the case that would catch the SelectLine drift noted in mouse.go.
There was a problem hiding this comment.
Added both halves of that gesture: a held MouseLeft inside the pressed cell now keeps the cursor at the start of the line (this is the variant that catches the drift — before the fix it moved the cursor to the pressed cell), and a held move to a neighboring cell follows the pointer to X=8 with the line selection intact.
Scroll.WordAt ends one cell past the word, and vi's inclusive visual selection then pulled in the trailing cell, so a double-click on a word selected the space after it. Land the cursor on the word's last character instead. A triple-click also left the gesture idle, so a held move inside the pressed cell drifted the cursor within the line selection; re-arm it on the press cell captured by OnAction. Signed-off-by: Santhi Prakash <[email protected]>
|
Pushed
Verified TDD-style: the updated and new assertions fail on |
…isual Signed-off-by: Santhi Prakash <[email protected]>
|
CI note: the |
| if _, ok := d.vi.cursor.SelectionMode(); ok { | ||
| // Scroll.WordAt's end is one cell past the word and visual mode | ||
| // includes the cell under the cursor, so land on the last | ||
| // character of the word. | ||
| d.vi.cursor.MoveLeft() |
There was a problem hiding this comment.
Regression: a double-click that lands on a non-word now trims an existing selection by one character.
"A selection exists" doesn't guarantee the base SelectWordAt just selected a word. When the click lands on whitespace or punctuation, the base returns early and leaves any current selection alone. The SDK counts any second click within 500ms as a double-click, wherever it lands, so a quick drag-select followed by a click gets here with the drag's selection still active, and MoveLeft() shrinks it.
Repro on alpha bravo charlie: press at X=0, drag to X=3, release, then click X=5 (the space) within 500ms. The selection goes from "alph" to "alp" and the cursor moves from X=3 to X=2. origin/main and the previous PR head both keep "alph".
Suggested fix: only step back when a word was actually selected, e.g. return early unless WordAt(d.vi.cursor.ScrollCoordinates(pos)) gives a non-empty word. Please add a test for drag, release, then a quick click on a space.
There was a problem hiding this comment.
Fixed — SelectWordAt now bails early unless Scroll.WordAt finds a word under the click, so a leftover selection is never stepped into. Your repro (drag X=0→X=3, then a quick click on the space at X=5) now keeps "alph" with the cursor at X=3; added a drag → release → click-on-a-space test covering exactly that.
| // stays armed so jitter inside the pressed cell keeps the word. | ||
| func (d *mouseDelegate) SelectWordAt(pos term.Coordinates) { | ||
| d.drag = dragPressed | ||
| d.pressedCell = pos |
There was a problem hiding this comment.
Nit: now that OnAction records pressedCell on every mouse.LeftClick, this assignment (and the one in SetSelectionStart, L73) is redundant. The SDK always calls OnAction with the same pos before it calls SetSelectionStart/SelectWordAt/SelectLine for a fresh press.
There was a problem hiding this comment.
Dropped both assignments — OnAction records pressedCell on every mouse.LeftClick before the SDK dispatches to SetSelectionStart/SelectWordAt/SelectLine, so the duplicates are gone.
The SDK does not clear the selection before SelectWordAt, so a second click landing on whitespace or punctuation arrived with an earlier gesture's selection still live; the base delegate then returned early on the non-word and the cursor step-back shrank that selection by a cell. Bail out unless Scroll.WordAt finds a word under the click. OnAction already records the press cell for every left click before the SDK dispatches to SetSelectionStart, SelectWordAt, or SelectLine, so the duplicate assignments there are dropped. Signed-off-by: Santhi Prakash <[email protected]>
Fixes #175.
The SDK reports held-button moves, including pointer jitter inside the
pressed cell, through
SetSelectionEnd. The Vim mouse delegate selectedthe cell and switched to visual mode in
SetSelectionStart, before anydrag could be told apart from a click, so focusing the window with the
mouse left a one-cell highlight that survived scrolling and forced an
Esc to get back to normal mode — the same shape #126 fixed on the
terminal side.
The delegate now tracks the left-button gesture: a click only
repositions the caret, and visual mode starts once the drag leaves the
pressed cell (vim's
mouse=abehavior). Double-click and triple-clickkeep their word and line selections and now enter the matching visual
modes, a drag that begins in insert mode still anchors on the pressed
cell, and jitter inside the pressed cell after a double-click no longer
collapses the word selection.
testing
go test -race -count=1 ./internal/text/...— ok. NewTestHandleMouseWindowCoordinatessubtests cover a focus click stayingin normal mode with no selection, jitter inside the pressed cell in
normal and insert modes, click-then-scroll leaving no highlight, a
click during visual exiting visual and repositioning, a drag that
returns to the pressed cell, and double-click word selection; they fail
on the previous delegate and pass with this change.
make testshowsonly pre-existing environment failures in this sandbox (glfw DISPLAY,
a flaky
internal/iderace that passes standalone, and a gemini clienttest that also fails on main).
Authorship