revive boolean looper

This commit is contained in:
devycarol 2025-03-11 17:39:49 -06:00
parent 3776a0829e
commit 3059beaff0
2 changed files with 10 additions and 9 deletions

View file

@ -120,13 +120,13 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
val text = connection.getSelectedText(0) ?: return steps
loopOverCodePoints(text) { cp, charCount ->
actualSteps += charCount
if (actualSteps >= steps) return actualSteps
actualSteps >= steps
}
} else {
val text = connection.getTextBeforeCursor(-steps * 4, 0) ?: return steps
loopOverCodePointsBackwards(text) { cp, charCount ->
actualSteps -= charCount
if (actualSteps <= steps) return actualSteps
actualSteps <= steps
}
}
return actualSteps
@ -223,7 +223,7 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
loopOverCodePoints(text) { cp, charCount ->
if (StringUtils.mightBeEmoji(cp)) return 0
actualSteps += charCount
if (actualSteps >= steps) return min(actualSteps, text.length)
actualSteps >= steps
}
return min(actualSteps, text.length)
}
@ -234,7 +234,7 @@ class KeyboardActionListenerImpl(private val latinIME: LatinIME, private val inp
loopOverCodePointsBackwards(text) { cp, charCount ->
if (StringUtils.mightBeEmoji(cp)) return 0
actualSteps -= charCount
if (actualSteps <= steps) return -min(-actualSteps, text.length)
actualSteps <= steps
}
return -min(-actualSteps, text.length)
}

View file

@ -9,24 +9,24 @@ import helium314.keyboard.latin.settings.SpacingAndPunctuations
import java.math.BigInteger
import java.util.Locale
inline fun loopOverCodePoints(text: CharSequence, loop: (cp: Int, charCount: Int) -> Unit) {
inline fun loopOverCodePoints(text: CharSequence, loop: (cp: Int, charCount: Int) -> Boolean) {
val s = text.toString()
var offset = 0
while (offset < s.length) {
val cp = s.codePointAt(offset)
val charCount = Character.charCount(cp)
loop(cp, charCount)
if (loop(cp, charCount)) return
offset += charCount
}
}
inline fun loopOverCodePointsBackwards(text: CharSequence, loop: (cp: Int, charCount: Int) -> Unit) {
inline fun loopOverCodePointsBackwards(text: CharSequence, loop: (cp: Int, charCount: Int) -> Boolean) {
val s = text.toString()
var offset = s.length
while (offset > 0) {
val cp = s.codePointBefore(offset)
val charCount = Character.charCount(cp)
loop(cp, charCount)
if (loop(cp, charCount)) return
offset -= charCount
}
}
@ -40,7 +40,7 @@ fun nonWordCodePointAndNoSpaceBeforeCursor(text: CharSequence, spacingAndPunctua
if (!nonWordCodePoint && !spacingAndPunctuations.isWordCodePoint(cp) && cp != '"'.code) {
nonWordCodePoint = true
}
if (space && nonWordCodePoint) return false // stop if both are found
space && nonWordCodePoint // stop if both are found
}
return nonWordCodePoint // return true if a non-word codepoint and no space was found
}
@ -49,6 +49,7 @@ fun hasLetterBeforeLastSpaceBeforeCursor(text: CharSequence): Boolean {
loopOverCodePointsBackwards(text) { cp, _ ->
if (Character.isWhitespace(cp)) return false
else if (Character.isLetter(cp)) return true
false // continue
}
return false
}