Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions app/src/main/java/helium314/keyboard/latin/inputlogic/InputLogic.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class InputLogic(
private var mLastExpandedCursorPosition = -1
private var mLastExpandedCursorOffset = -1
private var mJustRevertedExpandedShortcut: String? = null
private var mRevertedShortcutEnd = -1 // cursor position right after the reverted shortcut
private var mJustRevertedACommit = false
/** a swiped word removed with backspace, for learning from the word typed in its place, see [recordRetypedSwipeCorrection] */
private var mDeletedSwipe: DeletedSwipe? = null
Expand Down Expand Up @@ -151,6 +152,7 @@ class InputLogic(
StatsUtils.onWordCommitUserTyped(mWordComposer.getTypedWord(), mWordComposer.isBatchMode())
}
mWordComposer.restartCombining(combiningSpec)
mJustRevertedExpandedShortcut = null
resetComposingState(true)
mDeleteCount = 0
mSpaceState = SpaceState.NONE
Expand Down Expand Up @@ -498,6 +500,9 @@ class InputLogic(
mLastShortcutText = null
mLastExpandedCursorPosition = -1
mLastExpandedCursorOffset = -1
}
if (mConnection.expectedSelectionEnd < mRevertedShortcutEnd) {
// part of the reverted shortcut was deleted, so typing it again may expand it
mJustRevertedExpandedShortcut = null
}
mLastKeyTime = inputTransaction.timestamp
Expand Down Expand Up @@ -1342,7 +1347,7 @@ class InputLogic(
val textStr = textBefore.toString()
val result = TextExpanderUtils.getExpandedWordForTyped(typedWord, textStr, mLatinIME)
if (result != null && (result.imageUri == null || mLatinIME.acceptsAnyImage())) {
if (mJustRevertedExpandedShortcut != null && result.matchedString.equals(mJustRevertedExpandedShortcut, ignoreCase = true)) {
if (isRevertedShortcut(result.matchedString)) {
// Skip re-expanding
} else {
if (result.imageUri != null) {
Expand Down Expand Up @@ -1399,7 +1404,7 @@ class InputLogic(
if (textBefore != null) {
val result = TextExpanderUtils.getExpandedWordForTyped(null, textBefore.toString(), mLatinIME)
if (result != null && (result.imageUri == null || mLatinIME.acceptsAnyImage())) {
if (mJustRevertedExpandedShortcut != null && result.matchedString.equals(mJustRevertedExpandedShortcut, ignoreCase = true)) {
if (isRevertedShortcut(result.matchedString)) {
// Skip re-expanding
} else {
mConnection.deleteTextBeforeCursor(result.matchedString.length)
Expand Down Expand Up @@ -1531,9 +1536,7 @@ class InputLogic(
if (textBefore != null) {
val result = TextExpanderUtils.getExpandedWordForTyped(null, textBefore.toString(), mLatinIME)
if (result != null && (result.imageUri == null || mLatinIME.acceptsAnyImage())) {
if (mJustRevertedExpandedShortcut == null
|| !result.matchedString.equals(mJustRevertedExpandedShortcut, ignoreCase = true)
) {
if (!isRevertedShortcut(result.matchedString)) {
mConnection.deleteTextBeforeCursor(result.matchedString.length)
if (result.imageUri != null) {
dispatchExpandedImage(result.imageUri)
Expand Down Expand Up @@ -1812,8 +1815,14 @@ class InputLogic(
&& textAfter != null && textAfter.toString() == expectedAfter
) {
mJustRevertedExpandedShortcut = mLastShortcutText
// deleting the space after an expansion resumes composing its last word, end that first
if (mWordComposer.isComposingWord()) {
mConnection.finishComposingText()
resetComposingState(true)
}
mConnection.deleteSurroundingText(beforeLen, afterLen)
mConnection.commitText(mLastShortcutText ?: "", 1)
mRevertedShortcutEnd = mConnection.expectedSelectionEnd
mLastExpandedText = null
mLastShortcutText = null
mLastExpandedCursorPosition = -1
Expand Down Expand Up @@ -1880,7 +1889,7 @@ class InputLogic(
if (textBefore != null) {
val result = TextExpanderUtils.getExpandedWordForTyped(typedWord, textBefore.toString(), mLatinIME)
if (result != null && result.imageUri == null) {
if (mJustRevertedExpandedShortcut == null || !result.matchedString.equals(mJustRevertedExpandedShortcut, ignoreCase = true)) {
if (!isRevertedShortcut(result.matchedString)) {
if (result.prefixLength > 0) {
mConnection.commitText("", 1)
mConnection.deleteTextBeforeCursor(result.prefixLength)
Expand Down Expand Up @@ -1936,9 +1945,8 @@ class InputLogic(
updateInlineEmojiSearch()
inputTransaction.setRequiresUpdateSuggestions()
} else {
if (mJustRevertedExpandedShortcut != null) {
if (mJustRevertedExpandedShortcut != null && mConnection.expectedSelectionEnd == mRevertedShortcutEnd) {
mLastComposedWord = LastComposedWord.NOT_A_COMPOSED_WORD
mJustRevertedExpandedShortcut = null
}
if (mLastComposedWord.canRevertCommit()
&& inputTransaction.settingsValues.mBackspaceRevertsAutocorrect
Expand Down Expand Up @@ -3024,9 +3032,7 @@ class InputLogic(
val plainWord = UnicodeStyleEngine.toPlainText(chosenWord)
val result = TextExpanderUtils.getExpandedWordForTyped(plainWord, textStr, mLatinIME)
if (result != null && (result.imageUri == null || (separatorString.isNotEmpty() && mLatinIME.acceptsAnyImage()))) {
if (mJustRevertedExpandedShortcut != null
&& result.matchedString.equals(mJustRevertedExpandedShortcut, ignoreCase = true)
) {
if (isRevertedShortcut(result.matchedString)) {
// Skip re-expanding
} else {
mConnection.commitText(getTextWithSuggestionSpan(mLatinIME, chosenWord, mSuggestedWords, getDictionaryFacilitatorLocale()), 1)
Expand Down Expand Up @@ -3519,6 +3525,10 @@ class InputLogic(
mSpaceState = SpaceState.NONE
}

/** @return whether [matched] is the shortcut the user just got back with backspace, still at the cursor */
private fun isRevertedShortcut(matched: String) = mConnection.expectedSelectionEnd == mRevertedShortcutEnd
&& matched.equals(mJustRevertedExpandedShortcut, ignoreCase = true)

private fun commitExpandedText(shortcut: String, expanded: String): Boolean {
val cursorOffset = expanded.indexOf("%cursor%")
if (cursorOffset != -1) {
Expand Down
91 changes: 91 additions & 0 deletions app/src/test/java/helium314/keyboard/latin/InputLogicTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,97 @@ class InputLogicTest {
assertEquals("", composingText)
}

private fun setUpRevertableShortcut(immediate: Boolean) {
reset()
latinIME.prefs().edit().apply {
putBoolean(helium314.keyboard.latin.utils.TextExpanderUtils.PREF_ENABLED, true)
putBoolean(helium314.keyboard.latin.utils.TextExpanderUtils.PREF_IMMEDIATE, immediate)
putBoolean(helium314.keyboard.latin.utils.TextExpanderUtils.PREF_BACKSPACE_REVERTS, true)
}.commit()
val shortcuts = mapOf("brb" to helium314.keyboard.latin.utils.TextExpanderUtils.ShortcutEntry("be right back", ""))
helium314.keyboard.latin.utils.TextExpanderUtils.saveShortcuts(latinIME, shortcuts)
}

private fun revertExpansion() {
typeNoAssert("brb")
assertEquals("be right back", text)
functionalKeyPress(KeyCode.DELETE)
assertEquals("brb", text)
}

@Test fun `space does not expand a shortcut reverted by backspace`() {
setUpRevertableShortcut(immediate = true)
revertExpansion()
typeNoAssert(" ")
assertEquals("brb ", text)
checkConnectionConsistency()
}

@Test fun `punctuation does not expand a shortcut reverted by backspace`() {
setUpRevertableShortcut(immediate = true)
revertExpansion()
typeNoAssert(".")
assertEquals("brb.", text)
}

@Test fun `functional key keeps a reverted shortcut unexpanded`() {
setUpRevertableShortcut(immediate = true)
revertExpansion()
functionalKeyPress(KeyCode.SHIFT)
typeNoAssert(" ")
assertEquals("brb ", text)
}

@Test fun `shortcut expands again when typed again after a revert`() {
setUpRevertableShortcut(immediate = true)
revertExpansion()
typeNoAssert(" brb")
assertEquals("brb be right back", text)
}

@Test fun `deleting the space after a reverted shortcut keeps it unexpanded`() {
setUpRevertableShortcut(immediate = true)
revertExpansion()
typeNoAssert(" ")
functionalKeyPress(KeyCode.DELETE)
assertEquals("brb", text)
typeNoAssert(" ")
assertEquals("brb ", text)
}

@Test fun `retyping part of a reverted shortcut expands it again`() {
setUpRevertableShortcut(immediate = true)
revertExpansion()
functionalKeyPress(KeyCode.DELETE)
assertEquals("br", text)
typeNoAssert("b")
assertEquals("be right back", text)
}

@Test fun `reverted number shortcut expands again when typed again`() {
setUpRevertableShortcut(immediate = true)
helium314.keyboard.latin.utils.TextExpanderUtils.saveShortcuts(latinIME,
mapOf("123" to helium314.keyboard.latin.utils.TextExpanderUtils.ShortcutEntry("numbers", "")))
typeNoAssert("123")
assertEquals("numbers", text)
functionalKeyPress(KeyCode.DELETE)
typeNoAssert(" ")
assertEquals("123 ", text)
typeNoAssert("123")
assertEquals("123 numbers", text)
}

@Test fun `space does not repeat a reverted expansion made on space`() {
setUpRevertableShortcut(immediate = false)
typeNoAssert("brb ")
assertEquals("be right back ", text)
functionalKeyPress(KeyCode.DELETE) // the space
functionalKeyPress(KeyCode.DELETE) // the expansion
assertEquals("brb", text)
typeNoAssert(" ")
assertEquals("brb ", text)
}

@Test fun `immediate text expansion uses full segmented thai word`() {
reset()
latinIME.switchToSubtype(SubtypeSettings.getResourceSubtypesForLocale("th".constructLocale()).first())
Expand Down
Loading