scoop:scoop.readline:0.1.0
src/completion/edit.scoop
Selected production source · src/completion/edit.scoop
1
package scoop.readline3
internal fun EditingSession.complete(backward: Boolean) {4
var active = completion5
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
return15
}16
active = Some(CompletionState(buffer.state(), result))17
completion = active18
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.candidates23
else bellValue = true24
return25
}26
}27
val state = active!!28
if (config.completionType == CompletionType.List) {29
displayedCandidates = state.result.candidates30
return31
}32
val size = state.result.candidates.size33
state.index += if (backward) -1L else 1L34
if (state.index < -1L) state.index = size - 1L35
if (state.index >= size) state.index = -1L36
buffer.restore(state.original)37
if (state.index >= 0L) buffer.replace(state.result.start, cursor, state.result.candidates[state.index].replacement)38
}40
internal fun commonCompletionPrefix(candidates: Array<Candidate>): String {41
if (candidates.size == 0L) return ""42
val first = candidates[0L].replacement43
var length = first.charLength44
var index = 1L45
while (index < candidates.size) {46
val other = candidates[index].replacement47
if (other.charLength < length) length = other.charLength48
var matched = 0L49
while (matched < length && first.getCharAt(matched) == other.getCharAt(matched)) matched += 1L50
length = matched51
index += 1L52
}53
val buffer = LineBuffer(first)54
return first.slice(0L, buffer.snap(length, false))55
}