Skip to content

Fix Vim clicks highlighting a cell and entering visual mode - #176

Merged
ernestrc merged 7 commits into
unstablebuild:mainfrom
santhiprakash:fix/175-vim-click-visual
Oct 5, 2026
Merged

ernestrc merged 7 commits into
unstablebuild:mainfrom
santhiprakash:fix/175-vim-click-visual

Conversation

@santhiprakash

Copy link
Copy Markdown
Contributor

Fixes #175.

The SDK reports held-button moves, including pointer jitter inside the
pressed cell, through SetSelectionEnd. The Vim mouse delegate selected
the cell and switched to visual mode in SetSelectionStart, before any
drag 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=a behavior). Double-click and triple-click
keep 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. New
TestHandleMouseWindowCoordinates subtests cover a focus click staying
in 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 test shows
only pre-existing environment failures in this sandbox (glfw DISPLAY,
a flaky internal/ide race that passes standalone, and a gemini client
test that also fails on main).


Authorship

  • Written by hand. I would like a human review.
  • Mostly or partially generated by an AI agent, which I reviewed and understand.

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-commenter

codecov-commenter commented Oct 3, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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]>
Comment thread internal/text/vi/mouse.go
Comment on lines +103 to +112
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()
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 bravo in alpha bravo charlie selects "bravo " (trailing space included), and the cursor ends up on the space. Vim selects bravo, so y/d after 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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).

Comment thread internal/text/vi/mouse.go
Comment on lines +116 to +123
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()
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread internal/text/vi/mouse.go Outdated
Comment on lines +74 to +76
if _, ok := d.vi.cursor.SelectionMode(); ok {
d.vi.cursor.Unselect()
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Dropped — handleLeftClickSelect is the only caller and it always runs ClearSelection() first, so there is no other path to guard.

Comment thread internal/text/vi/mouse.go
Comment on lines +83 to +88
if d.drag == dragPressed {
if pos == d.pressedCell {
return
}
d.drag = dragSelecting
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added that explanation as a comment on the pressedCell field.

Comment thread internal/text/vi/handler_test.go Outdated
assert.Equal(t, visualMode, v.handler.mode())
sel, ok := v.Selection()
require.True(t, ok)
assert.Equal(t, "bravo ", sel)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread internal/text/vi/handler_test.go Outdated
assert.Equal(t, visualMode, v.handler.mode())
sel, ok := v.Selection()
require.True(t, ok)
assert.Equal(t, "bravo ", sel)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Same here: this should expect "bravo" once SelectWordAt is fixed.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Updated to bravo.

Comment on lines +12557 to +12571
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)
})

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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]>
@santhiprakash

Copy link
Copy Markdown
Contributor Author

Pushed 55fe7075 addressing all seven review threads. Summary:

  • SelectWordAt now steps the cursor back onto the word's last character after the base selection lands on WordAt's exclusive end, so vi's inclusive visual selection no longer pulls in the trailing cell.
  • SelectLine re-arms dragPressed on the press cell that OnAction captures on LeftClick, so a held move inside the pressed cell no longer drifts the cursor within the line.
  • Dropped the dead SelectionMode/Unselect check in SetSelectionStart and documented why pressedCell stays valid in window coordinates.
  • Tests: the double-click assertions now expect bravo plus a CursorAtScroll() check at X=10, and there are new cases for the last word of a line (charlie, cursor X=18), double-click + y asserting the yanked text is bravo, and held-move-after-triple-click variants for same-cell jitter (cursor stays at line start) and a neighboring cell (cursor follows the pointer).

Verified TDD-style: the updated and new assertions fail on 0e82a690 (selecting bravo , cursor at X=11/X=19, yank picking up bravo , and cursor drift to the pressed cell after triple-click) and pass on this head.

$ go test ./internal/text/vi/ -run TestHandleMouseWindowCoordinates -v
--- PASS: TestHandleMouseWindowCoordinates (0.00s) — 17/17 subtests
$ go test ./internal/text/...
ok  unstable.build/rune/internal/text/vi  (all 13 text packages)
$ go build ./...   # clean; gofmt clean

@santhiprakash

Copy link
Copy Markdown
Contributor Author

CI note: the macos-15 leg on 55fe7075 failed in TestWorkspaceOpenSchemeCompletionNeverFallsBackToHistory (internal/ide) — that test was added on main after this branch's base (734494a4/52ff9f0c area), doesn't exist in this diff (vi-only changes), and passed on main's own macOS run on 2e540fe0 ~20 minutes earlier, so it looks like a fresh upstream flake. Merged latest upstream/main (096b2738) to pick up the new test and re-trigger CI.

Comment thread internal/text/vi/mouse.go
Comment on lines +114 to +118
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()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment thread internal/text/vi/mouse.go Outdated
// stays armed so jitter inside the pressed cell keeps the word.
func (d *mouseDelegate) SelectWordAt(pos term.Coordinates) {
d.drag = dragPressed
d.pressedCell = pos

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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]>
@ernestrc
ernestrc enabled auto-merge (squash) October 5, 2026 12:01
@ernestrc
ernestrc merged commit 0705adb into unstablebuild:main Oct 5, 2026
8 checks passed
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.

Clicking in Vim highlights a cell

3 participants