-
Notifications
You must be signed in to change notification settings - Fork 90
/
DefaultKeyCommand.kt
65 lines (52 loc) · 2.22 KB
/
DefaultKeyCommand.kt
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.johnlindquist.acejump.keycommands
import com.johnlindquist.acejump.AceFinder
import com.johnlindquist.acejump.AceJumper
import com.johnlindquist.acejump.getLowerCaseStringFromChar
import com.johnlindquist.acejump.ui.SearchBox
import java.awt.event.KeyEvent
import java.util.*
import javax.swing.event.ChangeListener
class DefaultKeyCommand(val searchBox: SearchBox, val aceFinder: AceFinder, val aceJumper: AceJumper, val textAndOffsetHash: HashMap<String, Int>) : AceKeyCommand() {
override fun execute(keyEvent: KeyEvent) {
val keyChar: Char = keyEvent.keyChar
//fixes the delete bug
if (keyChar == '\b') return
//Find or jump
if (searchBox.isSearchEnabled) {
//Find
aceFinder.addResultsReadyListener(ChangeListener { p0 ->
eventDispatcher?.multicaster?.stateChanged(p0)
// eventDispatcher?.getMulticaster()?.stateChanged(ChangeEvent(toString()))
})
aceFinder.findText(searchBox.text!!, false)
searchBox.disableSearch()
} else {
//Jump to offset!
var char = getLowerCaseStringFromChar(keyChar)
if (char == " ") return
if (aceFinder.firstChar != "") {
char = aceFinder.firstChar + char
aceFinder.firstChar = ""
}
val offset = textAndOffsetHash[char]
if (offset != null) {
searchBox.popupContainer?.cancel()
if (keyEvent.isShiftDown && !keyEvent.isMetaDown) {
aceJumper.setSelectionFromCaretToOffset(offset)
aceJumper.moveCaret(offset)
} else {
aceJumper.moveCaret(offset)
}
if (aceFinder.isTargetMode) {
aceJumper.selectWordAtCaret()
}
} else if (textAndOffsetHash.size > 25 && couldPossiblyMatch(char)) {
aceFinder.firstChar = char
}
}
}
// we don't want to collect chars which would lead us to nowhere
private fun couldPossiblyMatch(char: String): Boolean {
return textAndOffsetHash.keys.any { it.startsWith(char) }
}
}