Repository navigation
Fix Vim cw to change through word end - #173
Conversation
In Vim `cw` is a `ce` in disguise: the change stops at the end of the current word and keeps the whitespace after it, while `dw` consumes that whitespace. Routing `cw` through the same start-word motion as `dw` deleted the separator users intended to keep. Select the word-end range for `cw`/`cW` instead, counting a word end already under the cursor as the first end so `cw` at a word end still changes just that cell. On a blank the plain `w` motion applies, matching Vim. Signed-off-by: Santhi Prakash <[email protected]>
|
Note on CI: the |
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
| if bigWord { | ||
| return false | ||
| } | ||
| return !isWordRune(cur) || !isWordRune(next.Ch) |
There was a problem hiding this comment.
atWordEnd uses different word rules from MoveRightEndWord.
This uses Vim's word classes (letters, digits, _), but Cursor.MoveRightEndWord (internal/text/cursor.go:1038) breaks words on allSpecialCharacters (cursor.go:971). There, _ is a separator, while $, ~ and non-ASCII punctuation are not. When the two disagree, count gets decremented wrongly and the change overshoots:
| buffer | keys | this PR | Vim |
|---|---|---|---|
a$b x |
c2w |
x |
b x |
ab$ x |
c2w |
`` (whole line) | x |
Suggest exposing an "at word end" check from text.Cursor that uses the same character table as e, rather than keeping a second set of rules in vi.
| return !isWordRune(cur) || !isWordRune(next.Ch) | ||
| } | ||
|
|
||
| func isWordRune(r rune) bool { |
There was a problem hiding this comment.
isWordRune / isWordBlank duplicate isWordObjectRune / isWordObjectBlank in internal/text/cursor.go:1094-1100. If the check moves into text.Cursor (see the comment on atWordEnd), these can go away.
| for i := 0; i < count; i++ { | ||
| prev := vi.cursor.CursorAtScroll() | ||
| if !move() && vi.cursor.CursorAtScroll() == prev { | ||
| break | ||
| } | ||
| } |
There was a problem hiding this comment.
Regression: counted cw across an empty line leaves part of the next word.
Routing cw through MoveRightEndWord picks up an existing bug in how e crosses empty lines (d2e is wrong on main too), and the result is worse than before:
| buffer | keys | main | this PR | Vim |
|---|---|---|---|---|
one\n\ntwo |
wc2w |
\ntwo |
wo |
|
one\n\ntwo |
c2w |
\ntwo |
wo |
`` |
Either fix how e crosses empty lines first, or fall back to the previous w path when the motion would cross a line.
| // selectChangeWord selects the range a `cw`/`cW` delete acts on and reports | ||
| // whether the event is such a motion. In Vim `cw` is a `ce` in disguise: the | ||
| // change stops at the end of the word instead of consuming the whitespace | ||
| // after it, and a word end already under the cursor counts as the first end | ||
| // reached, so a single `cw` there covers just the cell under it where `ce` | ||
| // would reach the following word's end. On a blank the plain `w` motion | ||
| // applies. |
There was a problem hiding this comment.
nit: this comment mostly explains how the function works. Docstring comments should state the contract: what range gets selected, the return value, and the edge cases (i.e. word end under the cursor, blank under the cursor), not narrate the code. The code should narrate itself.
| {name: "d2w from line end over empty line joins lines", content: "one\n\ntwo", seq: "$d2w", wantContent: "on\ntwo", wantScroll: coord(1, 0)}, | ||
| {name: "c2w over two empty lines keeps one line", content: "\n\nthree", seq: "c2wX", wantContent: "X\nthree", wantMode: insertMode, wantScroll: coord(1, 0)}, | ||
| {name: "c2w from indented word over empty line keeps indent", content: " one\n\ntwo", seq: "wc2wX", wantContent: " X\ntwo", wantMode: insertMode, wantScroll: coord(3, 0)}, | ||
| {name: "c2w from indented word over empty line keeps indent", content: " one\n\ntwo", seq: "wc2wX", wantContent: " Xwo", wantMode: insertMode, wantScroll: coord(3, 0)}, |
There was a problem hiding this comment.
This expectation was changed to match the bug in selectChangeWord (see the comment on the move loop). " Xwo" matches neither the old behaviour (" X\ntwo") nor Vim (" X"), so the test now locks in the bug. It should expect Vim's result and fail until the empty-line case is fixed.
| } | ||
| } | ||
|
|
||
| // cw changes to the end of the word like ce, keeping the whitespace after |
There was a problem hiding this comment.
Please significantly expand the set of test cases here (i.e. where the two word rules disagree, plus a counted cw across an empty line, wide chars, nulls, oob, etc.):
a$b x+c2w→b xfoo_bar x+cw→x(currently_bar x, becauseetreats_as a separator; this already happens on main)one\n\ntwo+c2w→ ``
The first and last would have caught the two bugs above.
Review feedback showed the previous change kept a second word-rule set in the vi handler that disagreed with the e-motion table: counted cw overshot on punctuation and underscores (a$b x + c2w, foo_bar x + cw), and crossing an empty line left part of the next word behind. The word-end check and motion now live on text.Cursor as AtWordObjectEnd and MoveRightWordObjectEnd, sharing the wordClass table used by the aw/iw text objects. The empty-line expectation is corrected to Vim's result and the matrix now covers punctuation runs, underscores, wide runes, null cells, empty-line crossing, and out-of-bounds cursors. Signed-off-by: Santhi Prakash <[email protected]>
|
Thanks for the detailed review — all six points addressed in Root cause. The word-end check lived in Rework. The check and the motion moved into Empty-line crossing. Fixed as a property of the new motion rather than by touching Tests. Verification.
CI is running on the new head — DCO, module integrity, and gofmt+license are already green; golangci-lint and the platform test legs are still in flight. |
| return !ok || c.wordClass(nextCell.Ch, bigWord) != class | ||
| } | ||
|
|
||
| func IsWordObjectBlank(r rune) bool { |
There was a problem hiding this comment.
golangci-lint fails here: exported function IsWordObjectBlank should have comment or be unexported (revive). vi uses it, so it needs a doc comment that states the contract (what counts as blank, including the \x00 empty cell).
| } | ||
|
|
||
| func isWordObjectRune(r rune) bool { | ||
| func IsWordObjectRune(r rune) bool { |
There was a problem hiding this comment.
Same revive error. IsWordObjectRune is only used inside cursor.go (by wordClass), so it can go back to being unexported.
The cw rework exported both helpers; IsWordObjectRune is only used inside cursor.go by wordClass so it returns to unexported, while IsWordObjectBlank is used by the vi package and gains a doc comment stating what counts as blank: space, tab, or the \x00 empty cell. Signed-off-by: Santhi Prakash <[email protected]>
|
Both fixed in
Verified with the same command CI runs: |
Fixes #152.
In Vim,
cwis acein disguise: the change stops at the end of the currentword and keeps the whitespace after it, while
dwconsumes that whitespace.Rune routed
cwthrough the same start-word motion asdw, so changing a wordin the middle of a line also ate the separator before the next word.
handleDeletenow selects the word-end range forcw/cWbefore fallingthrough to the generic motion path. A word end already under the cursor counts
as the first end, so
cwat a word end changes just that cell instead ofreaching into the following word, and on a blank the plain
wmotion applies,matching Vim.
dw,yw, text objects, and the other operators are unchanged.Verified against Vim 9.1 for start/middle/end of word, single-letter words,
counts (
2cw,c2w,2c2w),cW, and blank/empty-line cases. A few existingcount tests encoded the
dw-style behavior and are updated to the Vimexpectations (e.g.
2cwonone two three fournow yieldsthree four,keeping the separator).
go test -race ./internal/text/...passes.Authorship