Skip to content

Commit

Permalink
Refactoring: add exception handling
Browse files Browse the repository at this point in the history
  • Loading branch information
izhangzhihao committed Aug 8, 2020
1 parent e683968 commit 090cd57
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 090cd57

Please sign in to comment.