aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/at/hannibal2/skyhanni/events/TabCompletionEvent.kt
blob: f3ec47b02a20154e2416285beac91ad3ee639ee7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package at.hannibal2.skyhanni.events

class TabCompletionEvent(
    val leftOfCursor: String,
    val fullText: String,
    val originalCompletions: List<String>,
) : LorenzEvent() {
    val lastWord = leftOfCursor.substringAfterLast(' ')
    val additionalSuggestions = mutableSetOf<String>()
    val suppressedSuggestions = mutableSetOf<String>()

    fun addSuggestion(suggestion: String) {
        if (suggestion.startsWith(lastWord, ignoreCase = true))
            additionalSuggestions.add(suggestion)
    }

    fun addSuggestions(suggestions: Iterable<String>) {
        suggestions.forEach(this::addSuggestion)
    }

    fun excludeAllDefault() {
        suppressedSuggestions.addAll(originalCompletions)
    }

    val command = if (leftOfCursor.startsWith("/"))
        leftOfCursor.substring(1).substringBefore(" ").lowercase()
    else ""

    fun isCommand(commandName: String): Boolean {
        return commandName.equals(command, ignoreCase = true)
    }

    fun intoSuggestionArray(): Array<String>? {
        if (additionalSuggestions.isEmpty() && suppressedSuggestions.isEmpty()) return null
        return (originalCompletions - suppressedSuggestions + additionalSuggestions).toTypedArray()
    }
}