Scoop API

scoop:scoop.readline:0.1.0

src/completion/fuzzy.scoop

Selected production source · src/completion/fuzzy.scoop

1package scoop.readline
3import scoop.charset.*
4import scoop.collections.sortedBy
5import scoop.terminal.event.*
6import scoop.text.*
8internal struct FuzzyMatch(val candidate: Candidate, val gaps: Long)
9internal class FuzzyCompletion(val result: Completions) {
10 var query: String = ""
11 var selected: Long = 0L
12 var matches: Array<Candidate> = []
15internal 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 = 0L
22 var gaps = 0L
23 var matched = true
24 for (character in query) {
25 val found = text.indexOf(character, at)
26 if (found < 0L) { matched = false; break }
27 gaps += found - at
28 at = found + 1L
29 }
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 = 0L
35 showFuzzy()
38private 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 0L
42 val limit = index + 5L
43 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 += 1L
47 }
48 messageValue = Some(message.toString())
51internal 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 = None
56 messageValue = None
57 return EditResult.Continue
58 }
59 if (control == Some('c')) {
60 fuzzy = None
61 messageValue = None
62 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 - 1L
68 fuzzy = None
69 messageValue = None
70 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.BackTab
75 state.selected = (state.selected + (if (previous) state.matches.size - 1L else 1L)) % state.matches.size
76 }
77 showFuzzy()
78 return EditResult.Continue
79 }
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 = true
88 return EditResult.Continue
89 }
90 state.query += character!!.toString()
91 }
92 updateFuzzy()
93 return EditResult.Continue