fix: correct cursor positioning with CJK (wide) characters#13
Open
WayneKent wants to merge 1 commit into
Open
Conversation
Use display-width offsets instead of character indices when interacting with input.cursorOffset, which returns display-width values. CJK chars occupy 2 display columns but count as 1 JS character. - Add charDisplayWidth/displayToChar/charToDisplay/displayWidth helpers - Fix hostPosition/hostOffset/visualWrapOffsets in map.ts - Fix clampNormalCursor, enterNormal, hostFromVimOffset, hostRange in vimee.ts - Fix preparePassThroughKey, isNativeCompletionToken in index.tsx
Author
|
Hi, just a friendly bump — appreciate your time when you get a chance to review. Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
After typing CJK characters and pressing ESC to enter normal mode,
pressing h/l to move the cursor causes it to jump to the middle of
the text (more noticeable with 4-5+ CJK chars). l sometimes cannot
reach the end of line.
Root Cause
input.cursorOffset returns display-width offset (CJK=2, ASCII=1),
but the code clamped it against text.length (JS char count).
Example: 5 CJK chars "你你你你你"
text.length = 5, displayWidth = 10
Cursor at end: cursorOffset = 10 (or 9)
clamp(..., 0, text.length) truncates 9/10 to 5
hostPosition maps offset 5 to the 3rd char -> jump to middle
Fix
Added width helpers (charDisplayWidth/charToDisplay/displayToChar/
displayWidth) in map.ts. All cursorOffset boundaries now use
displayWidth instead of text.length. Convert to char index only
when string manipulation is needed.
Files changed