diff --git a/src/main/kotlin/com/github/izhangzhihao/rainbow/fart/RainbowFartLookupHandler.kt b/src/main/kotlin/com/github/izhangzhihao/rainbow/fart/RainbowFartLookupHandler.kt index eb931f9..852eaa8 100644 --- a/src/main/kotlin/com/github/izhangzhihao/rainbow/fart/RainbowFartLookupHandler.kt +++ b/src/main/kotlin/com/github/izhangzhihao/rainbow/fart/RainbowFartLookupHandler.kt @@ -9,14 +9,17 @@ import java.beans.PropertyChangeListener class RainbowFartLookupHandler : LookupListener { override fun itemSelected(event: LookupEvent) { - val currentItem: LookupElement = event.lookup.currentItem ?: return - val lookupString: String = currentItem.lookupString - if (BuildInContributes.buildInContributes.containsKey(lookupString)) { - GlobalScope.launch((Dispatchers.Default)) { - releaseFart(BuildInContributes.buildInContributes.getOrDefault(lookupString, emptyList())) + try { + val currentItem: LookupElement = event.lookup.currentItem ?: return + val lookupString: String = currentItem.lookupString + if (BuildInContributes.buildInContributes.containsKey(lookupString)) { + GlobalScope.launch((Dispatchers.Default)) { + releaseFart(BuildInContributes.buildInContributes.getOrDefault(lookupString, emptyList())) + } } + } finally { + super.itemSelected(event) } - super.itemSelected(event) } } diff --git a/src/main/kotlin/com/github/izhangzhihao/rainbow/fart/RainbowFartTypedHandler.kt b/src/main/kotlin/com/github/izhangzhihao/rainbow/fart/RainbowFartTypedHandler.kt index e11d714..d20866c 100644 --- a/src/main/kotlin/com/github/izhangzhihao/rainbow/fart/RainbowFartTypedHandler.kt +++ b/src/main/kotlin/com/github/izhangzhihao/rainbow/fart/RainbowFartTypedHandler.kt @@ -21,23 +21,26 @@ class RainbowFartTypedHandler(originalHandler: TypedActionHandler) : TypedAction this.myOriginalHandler?.execute(editor, charTyped, dataContext) return } - candidates.add(charTyped) - val str = candidates.joinToString("") - - BuildInContributes.buildInContributesSeq - .firstOrNull { (keyword, _) -> - str.contains(keyword, true) - }?.let { (_, voices) -> - GlobalScope.launch(Dispatchers.Default) { - releaseFart(voices) + try { + candidates.add(charTyped) + val str = candidates.joinToString("") + BuildInContributes.buildInContributesSeq + .firstOrNull { (keyword, _) -> + str.contains(keyword, true) + }?.let { (_, voices) -> + GlobalScope.launch(Dispatchers.Default) { + releaseFart(voices) + } + candidates.clear() } - candidates.clear() - } - if (candidates.size > 20) { - candidates = candidates.subList(10, candidates.size - 1) + if (candidates.size > 20) { + candidates = candidates.subList(10, candidates.size - 1) + } + } finally { + // Ensure original handler is called no matter what errors are thrown, to prevent typing from being lost. + this.myOriginalHandler?.execute(editor, charTyped, dataContext) } - this.myOriginalHandler?.execute(editor, charTyped, dataContext) } object FartTypedHandler {