From 1368bc6e9bce38567c641d2277a7a9769d11ebca Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 30 Nov 2026 15:40:49 +0530 Subject: [PATCH] fix(ai): don't overwrite a selection made while an append request runs With nothing selected at the start, text selected up to the end of the field during the request was replaced by the appended result. The selection is now collapsed to the end of the text before appending. --- .../helium314/keyboard/latin/inputlogic/InputLogic.kt | 3 ++- .../test/java/helium314/keyboard/latin/InputLogicTest.kt | 8 ++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt b/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt index 70f5ff641..16050e671 100644 --- a/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt +++ b/app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt @@ -3484,7 +3484,8 @@ class InputLogic( Log.e(TAG, "Failed to get text after cursor: $e") 0 } - if (after > 0) mConnection.setSelection(end + after, end + after) + // also collapse a selection made during the request, the result would replace it + if (after > 0 || mConnection.expectedSelectionStart != end) mConnection.setSelection(end + after, end + after) } private fun tryJumpToNextPlaceholder(): Boolean { diff --git a/app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt b/app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt index b92bb5c8d..8e87217ab 100644 --- a/app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt @@ -2733,6 +2733,14 @@ class InputLogicTest { assertEquals("first line\n\nAnswer.", text) } + @Test fun `custom AI key result does not overwrite text selected during the request when appending`() { + reset() + setText("first line") + setCursorPosition(6, 10) // "line", up to the end of the text + latinIME.mInputLogic.insertCustomAiResult("Answer.", shouldAppend = true, hasSelection = false) + assertEquals("first line\n\nAnswer.", text) + } + @Test fun `custom AI key result replaces the selection`() { reset() setText("one teh two")