nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Bug] An emoji in the prompt desyncs the cursor: it vanishes on the first Left, then edits the wrong character

Open addyCooks opened this issue 2 months ago • 2 comments

Description

TextInput's cursor render loop iterates the value with for...of, which yields Unicode code points but every cursor offset the edit path computes (cursorOffsetRef.current, nextCursorOffset--, value.length) is counted in UTF-16 code units. Any astral character (every emoji) is 1 code point but 2 code units, so the two counting systems desync by one position per emoji in the value.

Parent: #907

Steps to Reproduce

  1. Paste 😀abc into the composer (a bracketed paste of an emoji followed by plain text works well, since typing an emoji directly depends on IME support).
  2. Press Left once.
  3. Press Left two more times, observing the caret after each press.

Expected Behavior

The caret moves one visible character left per Left press, staying visible and landing on the correct character the whole time.

Actual Behavior

  • After the first Left, the caret disappears entirely, no character on screen is highlighted.
  • After the second Left, the caret reappears, but on the wrong character.
  • After the third Left, the caret sits one glyph to the right of where an edit would actually apply, a Backspace there deletes a different character than the one visually under the caret, and pressing further can split the emoji's surrogate pair, leaving a lone unpaired surrogate in the value (renders as a replacement-character glyph, sent verbatim to the model).

Reproduced live, frame by frame: 😀abc → Left → caret gone → Left → Left → caret visibly offset.

Recording

Image

Root Cause

source/components/text-input.tsx:126-138:

for (const char of value) {
	if (i >= cursorOffset - cursorActualWidth && i <= cursorOffset) {
		renderedValue +=
			char === '\n' ? chalk.inverse(' ') + '\n' : chalk.inverse(char);
	} else {
		renderedValue += char;
	}
	i++;
}

i here counts code points (one increment per for...of iteration), while cursorOffset is a UTF-16 code-unit index computed elsewhere in the same file (e.g. nextCursorOffset--, slices against originalValueRef.current). The two never agree once the value contains a character outside the Basic Multilingual Plane.

Suggested Fix

Measure and index consistently in one unit everywhere, either convert cursorOffset to a code-point index before this render loop (e.g. via Array.from(value) for both the render and the edit paths), or switch the render loop to walk UTF-16 units directly instead of for...of.

Additional Context

  • [x] I have searched existing issues to ensure this is not a duplicate
  • [x] I can reproduce this issue consistently
  • [x] This issue occurs on the latest main

addyCooks avatar Sep 23 '26 11:09 addyCooks

Hi @akramcodez, could you assign this to me ?

addyCooks avatar Sep 23 '26 11:09 addyCooks

Picking this up - a fix will follow shortly.

awhite0030 avatar Sep 23 '26 11:09 awhite0030