Skip to content

Commit

Permalink
better word delete
Browse files Browse the repository at this point in the history
  • Loading branch information
pxeemo committed Feb 14, 2024
1 parent dbb7a5a commit ddd56cc
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions app/src/main/java/com/dessalines/thumbkey/utils/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -794,23 +794,19 @@ fun isUriOrEmailOrPasswordField(ime: IMEService): Boolean {
fun deleteWordBeforeCursor(ime: IMEService) {
val wordsBeforeCursor = ime.currentInputConnection.getTextBeforeCursor(9999, 0)

val trailingSpacesLength = wordsBeforeCursor?.length?.minus(wordsBeforeCursor.trimEnd().length) ?: 0
val trimmed = wordsBeforeCursor?.trim()
val lastWordLength = trimmed?.split("\\s".toRegex())?.lastOrNull()?.length ?: 1
val minDelete = lastWordLength + trailingSpacesLength
val pattern = Regex("(\\w+\\W?|\\W+)\\s*$")
val lastWordLength = wordsBeforeCursor?.let { pattern.find(it)?.value?.length } ?: 0

ime.currentInputConnection.deleteSurroundingText(minDelete, 0)
ime.currentInputConnection.deleteSurroundingText(lastWordLength, 0)
}

fun deleteWordAfterCursor(ime: IMEService) {
val wordsAfterCursor = ime.currentInputConnection.getTextAfterCursor(9999, 0)

val trailingSpacesLength = wordsAfterCursor?.length?.minus(wordsAfterCursor.trimStart().length) ?: 0
val trimmed = wordsAfterCursor?.trim()
val nextWordLength = trimmed?.split("\\s".toRegex())?.firstOrNull()?.length ?: 1
val minDelete = nextWordLength + trailingSpacesLength
val pattern = Regex("^\\s?(\\w+\\W?|\\W+)")
val nextWordLength = wordsAfterCursor?.let { pattern.find(it)?.value?.length } ?: 0

ime.currentInputConnection.deleteSurroundingText(0, minDelete)
ime.currentInputConnection.deleteSurroundingText(0, nextWordLength)
}

fun buildTapActions(keyItem: KeyItemC): List<KeyAction> {
Expand Down

0 comments on commit ddd56cc

Please sign in to comment.