From def1c965cd1616306218cbe022e27068ea06b798 Mon Sep 17 00:00:00 2001 From: LeanBitLab Date: Mon, 30 Nov 2026 11:55:06 +0530 Subject: [PATCH] fix(expander): follow the number of suggestions setting for expansion matches (#998) The Text Expander offered at most 3 matches even with 4 to 6 suggestions in the strip. It still offers at least 3, so a third one stays reachable in more suggestions. --- app/src/main/java/helium314/keyboard/latin/Suggest.kt | 4 +++- .../helium314/keyboard/latin/utils/TextExpanderUtils.kt | 8 ++++++-- .../helium314/keyboard/latin/utils/TextExpanderTabTest.kt | 8 ++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/helium314/keyboard/latin/Suggest.kt b/app/src/main/java/helium314/keyboard/latin/Suggest.kt index 1affc65ad..28fdb4db6 100644 --- a/app/src/main/java/helium314/keyboard/latin/Suggest.kt +++ b/app/src/main/java/helium314/keyboard/latin/Suggest.kt @@ -175,7 +175,9 @@ class Suggest(private val mDictionaryFacilitator: DictionaryFacilitator) { val ime = LatinIME.getInstance() if (ime != null && TextExpanderUtils.isEnabled(ime) && TextExpanderUtils.isSuggestionsEnabled(ime) && typedWordString.isNotEmpty()) { val textBefore = ime.inputLogic.connection.getTextBeforeCursor(50, 0)?.toString() ?: "" - val matches = TextExpanderUtils.getMatchingSuggestions(typedWordString, textBefore, ime) + val matches = TextExpanderUtils.getMatchingSuggestions( + typedWordString, textBefore, ime, Settings.getValues().mSuggestionsCountInStrip + ) // Keep INDEX_OF_AUTO_CORRECTION (index 1) intact when auto-correct is active. var insertPos = if (hasAutoCorrection) { minOf(2, suggestionsContainer.size) diff --git a/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt b/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt index 58973c530..30b64bc63 100644 --- a/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt +++ b/app/src/main/java/helium314/keyboard/latin/utils/TextExpanderUtils.kt @@ -680,10 +680,13 @@ object TextExpanderUtils { return null } + private const val MIN_SUGGESTION_MATCHES = 3 + fun getMatchingSuggestions( typedWord: String, textBeforeCursor: String, - context: Context + context: Context, + suggestionsInStrip: Int = MIN_SUGGESTION_MATCHES ): List { if (!isEnabled(context) || !isSuggestionsEnabled(context) || typedWord.isEmpty()) { return emptyList() @@ -791,7 +794,8 @@ object TextExpanderUtils { .thenByDescending { it.specificity } .thenBy { it.shortcutLength } ) - return matches.take(3).map { it.match } + // with fewer slots in the strip, the remaining matches are still reachable in more suggestions + return matches.take(maxOf(suggestionsInStrip, MIN_SUGGESTION_MATCHES)).map { it.match } } private fun formatPreview(template: String): String { diff --git a/app/src/test/java/helium314/keyboard/latin/utils/TextExpanderTabTest.kt b/app/src/test/java/helium314/keyboard/latin/utils/TextExpanderTabTest.kt index f2113687c..a766fa0ad 100644 --- a/app/src/test/java/helium314/keyboard/latin/utils/TextExpanderTabTest.kt +++ b/app/src/test/java/helium314/keyboard/latin/utils/TextExpanderTabTest.kt @@ -189,6 +189,14 @@ class TextExpanderTabTest { assertEquals(1, matchesHashPartial.single().prefixLength) // '#' needs to be deleted assertEquals("Task list:", matchesHashPartial.single().previewLabel) + // the match limit follows the number of suggestions setting, but is at least 3 + TextExpanderUtils.saveShortcuts(context, (1..7).associate { + "add$it" to TextExpanderUtils.ShortcutEntry(template = "Address $it", prefix = "") + }.toMutableMap()) + listOf(2 to 3, 3 to 3, 5 to 5, 6 to 6).forEach { (inStrip, expected) -> + assertEquals(expected, TextExpanderUtils.getMatchingSuggestions("add", "add", context, inStrip).size) + } + // a blank expansion falls back to the shortcut instead of an empty chip TextExpanderUtils.saveShortcuts(context, mutableMapOf( "nl" to TextExpanderUtils.ShortcutEntry(template = "\n", prefix = "")