code: IME code-point deletion no longer splits surrogate pairs
- Commit
2e80798030eade487e0a3436d2a06be50fadd1ba- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-30 23:15 UTC (5 days ago)
- Parents
1f271e4
deleteSurroundingTextInCodePoints was delegated straight to deleteSurroundingText, but the two InputConnection APIs count differently: code points vs UTF-16 chars. Deleting one code point before a caret placed after an emoji removed only the low surrogate, leaving a lone high surrogate that the UTF-8 save path turns into U+FFFD — silent file corruption from ordinary IME backspace over an emoji, CJK or math character. The bridge now converts the requested code-point counts to char offsets via offsetByCodePoints around the clamped selection (clamped at the text edges) and deletes that range through the same emit path; the char-count API is untouched. Three regression tests cover the emoji before/after the caret, mixed code-point counting, and edge clamping (all failed pre-fix). Encodings, document edits/undo, dirty tracking and find/replace were audited and cleared. Verified: :code:testDevUnitTest BUILD SUCCESSFUL; 28 lint errors are pre-existing on untouched files.
Diff
code/src/main/java/com/vayunmathur/code/ui/CodeEditorInput.kt
diff --git a/code/src/main/java/com/vayunmathur/code/ui/CodeEditorInput.kt b/code/src/main/java/com/vayunmathur/code/ui/CodeEditorInput.ktindex 9516e9fc32..38dd31f7f6 100644--- a/code/src/main/java/com/vayunmathur/code/ui/CodeEditorInput.kt+++ b/code/src/main/java/com/vayunmathur/code/ui/CodeEditorInput.kt@@ -205,8 +205,36 @@ private class CodeEditorInputNode( return true } - override fun deleteSurroundingTextInCodePoints(beforeLength: Int, afterLength: Int): Boolean =- deleteSurroundingText(beforeLength, afterLength)+ override fun deleteSurroundingTextInCodePoints(beforeLength: Int, afterLength: Int): Boolean {+ // Unlike deleteSurroundingText (UTF-16 char counts), the lengths here are Unicode+ // code points: one supplementary character (e.g. an emoji) is a single code point+ // but two chars. Counting it as one char would delete only half of its surrogate+ // pair, leaving a lone surrogate that later encodes as U+FFFD on save. Convert the+ // code-point counts to char offsets around the selection, clamped at the edges.+ val v = value+ val text = v.text+ val selMin = v.selection.min.coerceIn(0, text.length)+ val selMax = v.selection.max.coerceIn(selMin, text.length)+ val start = if (beforeLength <= 0) {+ selMin+ } else {+ val available = Character.codePointCount(text, 0, selMin)+ runCatching {+ Character.offsetByCodePoints(text, selMin, -minOf(beforeLength, available))+ }.getOrDefault(selMin)+ }+ val end = if (afterLength <= 0) {+ selMax+ } else {+ val available = Character.codePointCount(text, selMax, text.length)+ runCatching {+ Character.offsetByCodePoints(text, selMax, minOf(afterLength, available))+ }.getOrDefault(selMax)+ }+ setComposing(null)+ emit(TextFieldValue(text.substring(0, start) + text.substring(end), TextRange(start)))+ return true+ } override fun setSelection(start: Int, end: Int): Boolean { val len = value.text.length
code/src/test/java/com/vayunmathur/code/ui/CodeEditorInputConnectionTest.kt
diff --git a/code/src/test/java/com/vayunmathur/code/ui/CodeEditorInputConnectionTest.kt b/code/src/test/java/com/vayunmathur/code/ui/CodeEditorInputConnectionTest.ktindex 8c3301283b..cd9ad2eeae 100644--- a/code/src/test/java/com/vayunmathur/code/ui/CodeEditorInputConnectionTest.kt+++ b/code/src/test/java/com/vayunmathur/code/ui/CodeEditorInputConnectionTest.kt@@ -138,6 +138,31 @@ class CodeEditorInputConnectionTest { assertNull(h.composingChanges.last()) } + @Test fun deleteSurroundingTextInCodePointsDeletesWholeSupplementaryCharBeforeCaret() {+ // "a" + U+1F600 (one code point, two UTF-16 chars) + "b"; caret sits after the emoji.+ val h = Harness(tf("a\uD83D\uDE00b", 3))+ assertTrue(h.connection.deleteSurroundingTextInCodePoints(1, 0))+ // The whole code point goes: no lone high surrogate may remain.+ assertEquals(tf("ab", 1), h.emitted.last())+ }++ @Test fun deleteSurroundingTextInCodePointsDeletesWholeSupplementaryCharAfterCaret() {+ val h = Harness(tf("\uD83D\uDE00b", 0))+ assertTrue(h.connection.deleteSurroundingTextInCodePoints(0, 1))+ assertEquals(tf("b", 0), h.emitted.last())+ }++ @Test fun deleteSurroundingTextInCodePointsCountsMixedCodePointsAndClamps() {+ // Caret after "a<emoji>": deleting 2 code points removes both, not 2 UTF-16 chars.+ val h = Harness(tf("a\uD83D\uDE00b", 3))+ assertTrue(h.connection.deleteSurroundingTextInCodePoints(2, 0))+ assertEquals(tf("b", 0), h.emitted.last())+ // Over-long counts clamp at the edges instead of throwing.+ val h2 = Harness(tf("a\uD83D\uDE00b", 3))+ assertTrue(h2.connection.deleteSurroundingTextInCodePoints(99, 99))+ assertEquals(tf("", 0), h2.emitted.last())+ }+ @Test fun deleteSurroundingTextClampsAtEdgesAndCodePointsVariantMatches() { val h = Harness(tf("ab", 0, 2)) assertTrue(h.connection.deleteSurroundingTextInCodePoints(10, 10))