Scoop API

scoop:scoop.readline:0.1.0

src/completion/edit.scoop

Selected production source · src/completion/edit.scoop

1package scoop.readline
3internal fun EditingSession.complete(backward: Boolean) {
4 var active = completion
5 if ((when (active) { case Some(_) -> false; case None -> true })) {
6 val result = completer.complete(line, cursor, context)
7 if (result.start < 0L || result.start > cursor || buffer.snap(result.start, false) != result.start) {
8 throw IllegalArgumentException(Some("completion start is not a valid grapheme boundary"))
9 }
10 if (result.candidates.size == 0L) { bellValue = true; return }
11 if (config.completionType == CompletionType.Fuzzy) {
12 fuzzy = Some(FuzzyCompletion(result))
13 updateFuzzy()
14 return
15 }
16 active = Some(CompletionState(buffer.state(), result))
17 completion = active
18 if (config.completionType == CompletionType.List) {
19 val prefix = commonCompletionPrefix(result.candidates)
20 buffer.replace(result.start, cursor, prefix)
21 if (result.candidates.size == 1L) { completion = None; return }
22 if (config.completionShowAllIfAmbiguous) displayedCandidates = result.candidates
23 else bellValue = true
24 return
25 }
26 }
27 val state = active!!
28 if (config.completionType == CompletionType.List) {
29 displayedCandidates = state.result.candidates
30 return
31 }
32 val size = state.result.candidates.size
33 state.index += if (backward) -1L else 1L
34 if (state.index < -1L) state.index = size - 1L
35 if (state.index >= size) state.index = -1L
36 buffer.restore(state.original)
37 if (state.index >= 0L) buffer.replace(state.result.start, cursor, state.result.candidates[state.index].replacement)
40internal fun commonCompletionPrefix(candidates: Array<Candidate>): String {
41 if (candidates.size == 0L) return ""
42 val first = candidates[0L].replacement
43 var length = first.charLength
44 var index = 1L
45 while (index < candidates.size) {
46 val other = candidates[index].replacement
47 if (other.charLength < length) length = other.charLength
48 var matched = 0L
49 while (matched < length && first.getCharAt(matched) == other.getCharAt(matched)) matched += 1L
50 length = matched
51 index += 1L
52 }
53 val buffer = LineBuffer(first)
54 return first.slice(0L, buffer.snap(length, false))