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
4 changes: 3 additions & 1 deletion app/src/main/java/helium314/keyboard/latin/Suggest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SuggestionMatch> {
if (!isEnabled(context) || !isSuggestionsEnabled(context) || typedWord.isEmpty()) {
return emptyList()
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "")
Expand Down
Loading