Repository navigation
Conversation
Line() and Column() locate the cursor in the value's grid of rows and
runes. That is the wrong coordinate space for a caller that wants to
treat Value() as a flat string — slicing around the cursor, scanning
backwards for a token boundary, splicing a completion in. Converting
grid coordinates to a byte index means re-deriving the join Value()
already performed, and every caller writes the same loop.
Add the pair:
func (m Model) ByteOffset() int
func (m *Model) SetCursorByteOffset(off int)
Two positions are reachable as offsets but not as cursor positions:
the interior of a multi-byte rune, and the "\n" Value() inserts between
rows (it belongs to no row). SetCursorByteOffset snaps both forward to
the next real position, so round-tripping an offset from ByteOffset is
always exact.
Getting the mid-rune case wrong is quiet and surprising: slicing the
row at the requested byte yields a partial UTF-8 sequence, which
decodes to U+FFFD and counts as a whole rune, so asking for byte 2 of
"世界" lands the cursor at byte 6 — past the rune the caller was
pointing into, not before it. Counting rune starts up to the offset
instead lands it at byte 3. A test pins this.
SetCursorByteOffset repositions the viewport like every other cursor
mover, so restoring a saved offset after an edit cannot leave the
cursor scrolled out of sight.
No new dependencies.
The godoc said an offset landing on the "\n" Value inserts between rows was a position the cursor cannot occupy, and that it snaps forward like a mid-rune offset does. It does not, because there is nothing to snap past. Offsets are insertion points: the offset of the separator byte is the end of the row before it, and the offset after it is the start of the row that follows. Both are ordinary cursor positions, and the test covering them already asserts exactly that — it was just named for the snap it never makes. Only the mid-rune case snaps. Say so, and rename the test to match.
joestump-agent
left a comment
There was a problem hiding this comment.
Verified by checking out the branch and running the full gate: go test ./..., go vet ./textarea/..., gofmt -l — all green. No changes outside the two new files, as claimed.
The two behavioral edges are both correct and both pinned by tests:
- Mid-rune snap-forward: counting rune starts up to the offset (rather than slicing at it) lands the cursor at the start of the rune the offset points into, which is the useful direction — byte 1 of
"世界"→ column 1 / byte 3, not column 0.TestSetCursorByteOffsetMidRuneSnapsForwardcovers both interior bytes. - Row separators: offsets on either side of a joining
"\n"map to distinct ordinary positions (end of row N, start of row N+1) —TestSetCursorByteOffsetAtRowSeparatorcovers both. Round-trip exactness is exhaustively checked over a value mixing CJK, an emoji with skin-tone modifier, and an empty row.
One small nit, non-blocking:
- In
SetCursorByteOffset, theif off-seen >= len(s) { col = len([]rune(s)) }branch after the rune-counting loop is dead code: whenoff-seen >= len(s)the loop's break condition never fires, socolalready equals the row's rune count. It can simply be deleted.
Also worth a maintainer's eye: ByteOffset() returns the accumulated off unchanged if m.row is somehow past the end of m.value (the loop falls through). I couldn't construct that state through the public API, but if the maintainers know of one, a defensive clamp would make the "always a valid UTF-8 boundary into Value()" doc promise unconditional.
Well-scoped, well-tested pair of methods — exactly the coordinate-space conversion every caller of Value() currently re-implements.
Feature request: #1044 — opened as an issue rather than a Discussion; happy to move it if the team prefers the Ideas board.
Line()andColumn()locate the cursor in the value's grid of rows and runes. That is the wrong coordinate space for a caller that wants to treatValue()as a flat string — slicing around the cursor, scanning backwards for a token boundary, splicing a completion in. Converting grid coordinates to a byte index means re-deriving the joinValue()already performed, and every caller writes the same loop.Add the pair:
One offset is not a position the cursor can occupy: one landing inside a multi-byte rune.
SetCursorByteOffsetsnaps it forward to the next one that is, so round-tripping an offset fromByteOffsetis always exact. A negative offset clamps to the start, a past-the-end offset to the end.The
"\n"Value()inserts between rows belongs to no row, but it costs no position either: the offset of that byte is the end of the row before it, and the offset after it is the start of the row that follows. Both are ordinary cursor positions. A test covers each side.Getting the mid-rune case wrong is quiet and surprising: slicing the row at the requested byte yields a partial UTF-8 sequence, which decodes to U+FFFD and counts as a whole rune, so asking for byte 2 of
"世界"lands the cursor at byte 6 — past the rune the caller was pointing into, not before it. Counting rune starts up to the offset instead lands it at byte 3. A test pins this.SetCursorByteOffsetrepositions the viewport like every other cursor mover, so restoring a saved offset after an edit cannot leave the cursor scrolled out of sight.No new dependencies, and no change to existing behaviour — two new methods in a new file.
CONTRIBUTING.md.🤖 Posted on behalf of
@joestumpbyclaude-opus-5using Claude Code.