scoop:scoop.readline:0.1.0
src/completion/fuzzy.scoop
Selected production source · src/completion/fuzzy.scoop
1
package scoop.readline3
import scoop.charset.*4
import scoop.collections.sortedBy5
import scoop.terminal.event.*6
import scoop.text.*8
internal struct FuzzyMatch(val candidate: Candidate, val gaps: Long)9
internal class FuzzyCompletion(val result: Completions) {10
var query: String = ""11
var selected: Long = 0L12
var matches: Array<Candidate> = []13
}15
internal fun EditingSession.updateFuzzy() {16
val state = fuzzy!!17
val query = unicodeCasefold(state.query)18
val matches = ArrayList<FuzzyMatch>()19
for (candidate in state.result.candidates) {20
val text = unicodeCasefold(candidate.display)21
var at = 0L22
var gaps = 0L23
var matched = true24
for (character in query) {25
val found = text.indexOf(character, at)26
if (found < 0L) { matched = false; break }27
gaps += found - at28
at = found + 1L29
}30
if (matched) matches.add(FuzzyMatch(candidate, gaps))31
}32
val ordered = matches.sortedBy { it.gaps }33
state.matches = Array<Candidate>(ordered.size) { index -> ordered[index].candidate }34
state.selected = 0L35
showFuzzy()36
}38
private fun EditingSession.showFuzzy() {39
val state = fuzzy!!40
val message = StringBuilder().append("fuzzy: ").append(state.query)41
var index = if (state.selected >= 5L) state.selected - 4L else 0L42
val limit = index + 5L43
while (index < state.matches.size && index < limit) {44
message.append(if (index == state.selected) "\n> " else "\n ")45
message.append(state.matches[index].display)46
index += 1L47
}48
messageValue = Some(message.toString())49
}51
internal fun EditingSession.handleFuzzyKey(key: KeyEvent): EditResult {52
val state = fuzzy!!53
val control = controlCharacter(key)54
if (key.code == KeyCode.Esc || control == Some('g')) {55
fuzzy = None56
messageValue = None57
return EditResult.Continue58
}59
if (control == Some('c')) {60
fuzzy = None61
messageValue = None62
return execute(Command.Interrupt)63
}64
if (key.code == KeyCode.Enter) {65
if (state.matches.size == 0L) { bellValue = true; return EditResult.Continue }66
val candidate = state.matches[state.selected]67
val count = graphemeBoundaries(line.slice(state.result.start, cursor)).size - 1L68
fuzzy = None69
messageValue = None70
return execute(Command.Replace(Movement.BackwardChar(count), candidate.replacement))71
}72
if (key.code == KeyCode.Up || key.code == KeyCode.BackTab || key.code == KeyCode.Down || key.code == KeyCode.Tab) {73
if (state.matches.size > 0L) {74
val previous = key.code == KeyCode.Up || key.code == KeyCode.BackTab75
state.selected = (state.selected + (if (previous) state.matches.size - 1L else 1L)) % state.matches.size76
}77
showFuzzy()78
return EditResult.Continue79
}80
if (key.code == KeyCode.Backspace || control == Some('h')) {81
val query = LineBuffer(state.query)82
state.query = state.query.slice(0L, query.previous(query.length))83
} else if (control == Some('u')) state.query = ""84
else {85
val character = keyCharacter(key)86
if (character == None || key.modifiers.has(KeyModifier.Control) || key.modifiers.has(KeyModifier.Alt)) {87
bellValue = true88
return EditResult.Continue89
}90
state.query += character!!.toString()91
}92
updateFuzzy()93
return EditResult.Continue94
}