Merge pull request #10873 from keymanapp/fix/android/repeated-char-backspace

fix(android): fixes context-change detection for repeated-char cases
This commit is contained in:
Joshua Horton 2024-03-06 08:20:10 +07:00 committed by GitHub
commit ccbeecf7d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 3 deletions

View file

@ -297,12 +297,13 @@ public class KMKeyboardJSHandler {
expectedChars = "";
}
ic.deleteSurroundingText(dn + numPairs, 0);
CharSequence newContext = getCharacterSequence(ic, originalBufferLength - 2*dn);
// Shorten the retrieved context by exactly as many characters as were just deleted.
CharSequence newContext = getCharacterSequence(ic, originalBufferLength - dn - numPairs);
CharSequence charsToRestore = CharSequenceUtil.restoreChars(expectedChars, newContext);
if (charsToRestore.length() > 0) {
// Restore expectedChars that Chromium deleted.
// Use newCusorPosition 1 so cursor will be after the inserted string
// Use newCursorPosition 1 so cursor will be after the inserted string
ic.commitText(charsToRestore, 1);
}
}

View file

@ -48,7 +48,10 @@ public final class CharSequenceUtil {
if (expectedChars.length() != currentContext.length()) {
String expectedCharsString = expectedChars.toString();
String currentContextString = currentContext.toString();
int index = expectedCharsString.indexOf(currentContextString);
int index = expectedCharsString.lastIndexOf(currentContextString);
if (currentContextString.length() == 0) {
index = 0;
}
if (index > -1) {
// subSequence indices are start(inclusive) to end(exclusive)
charsToRestore = expectedChars.subSequence(index + currentContextString.length(), expectedChars.length());

View file

@ -151,5 +151,12 @@ public class CharSequenceUtilTest {
Assert.assertEquals("p" + COMPOSING_CIRCUMFLEX_ACCENT + "p" + COMPOSING_CIRCUMFLEX_ACCENT, charsToRestore);
}
@Test
public void test_repeated_char_backspace() {
CharSequence currentContext = "----------------";
CharSequence expectedChars = "-----------------";
CharSequence charsToRestore = CharSequenceUtil.restoreChars(expectedChars, currentContext);
Assert.assertEquals("", charsToRestore);
}
//endregion
}