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")