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

View file

@ -9,24 +9,24 @@ import helium314.keyboard.latin.settings.SpacingAndPunctuations
import java.math.BigInteger import java.math.BigInteger
import java.util.Locale 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() val s = text.toString()
var offset = 0 var offset = 0
while (offset < s.length) { while (offset < s.length) {
val cp = s.codePointAt(offset) val cp = s.codePointAt(offset)
val charCount = Character.charCount(cp) val charCount = Character.charCount(cp)
loop(cp, charCount) if (loop(cp, charCount)) return
offset += charCount 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() val s = text.toString()
var offset = s.length var offset = s.length
while (offset > 0) { while (offset > 0) {
val cp = s.codePointBefore(offset) val cp = s.codePointBefore(offset)
val charCount = Character.charCount(cp) val charCount = Character.charCount(cp)
loop(cp, charCount) if (loop(cp, charCount)) return
offset -= charCount offset -= charCount
} }
} }
@ -40,7 +40,7 @@ fun nonWordCodePointAndNoSpaceBeforeCursor(text: CharSequence, spacingAndPunctua
if (!nonWordCodePoint && !spacingAndPunctuations.isWordCodePoint(cp) && cp != '"'.code) { if (!nonWordCodePoint && !spacingAndPunctuations.isWordCodePoint(cp) && cp != '"'.code) {
nonWordCodePoint = true 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 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, _ -> loopOverCodePointsBackwards(text) { cp, _ ->
if (Character.isWhitespace(cp)) return false if (Character.isWhitespace(cp)) return false
else if (Character.isLetter(cp)) return true else if (Character.isLetter(cp)) return true
false // continue
} }
return false return false
} }