scoop:scoop.readline:0.1.0
src/helpers.scoop
Selected production source · src/helpers.scoop
1
package scoop.readline3
import scoop.readline.history.*5
/** History view passed to extension callbacks. */6
public struct Context(val history: History, val historyIndex: Long)7
/** Completion display text and the exact text inserted into the buffer. */8
public struct Candidate(val display: String, val replacement: String)9
/** Replaces [start, cursor) with a selected candidate. Positions are scalar indices. */10
public struct Completions(val start: Long, val candidates: Array<Candidate>)12
/** Supplies completion candidates for the current input. */13
public interface Completer {14
public fun complete(line: String, cursor: Long, context: Context): Completions15
}17
/** A visible suggestion with an optional independently accepted suffix. */18
public struct Hint(val display: String, val completion: String?)19
/** Supplies suggestions without changing the input. */20
public interface Hinter {21
public fun hint(line: String, cursor: Long, context: Context): Hint?22
}24
/** ANSI styling must preserve the original visible text and width. */25
public interface Highlighter {26
public fun highlight(line: String, cursor: Long): String = line27
public fun highlightPrompt(prompt: String): String = prompt28
public fun highlightHint(hint: String): String = hint29
public fun highlightCandidate(candidate: String): String = candidate30
}32
/** Determines whether Enter accepts, rejects, or continues a multiline input. */33
public enum ValidationResult {34
Valid(val message: String? = None),35
Invalid(val message: String? = None),36
Incomplete37
}39
/** Input validation invoked on acceptance. */40
public interface Validator {41
public fun validate(input: String): ValidationResult42
}44
/** Default completion provider with no candidates. */45
public object NoCompleter : Completer {46
public override fun complete(line: String, cursor: Long, context: Context): Completions = Completions(cursor, [])47
}48
/** Default hint provider. */49
public object NoHinter : Hinter {50
public override fun hint(line: String, cursor: Long, context: Context): Hint? = None51
}52
/** Default identity highlighter. */53
public object PlainHighlighter : Highlighter54
/** Default validator accepting any input. */55
public object AcceptAll : Validator {56
public override fun validate(input: String): ValidationResult = ValidationResult.Valid()57
}59
/** Suggests the newest matching history suffix when the cursor is at the end. */60
public class HistoryHinter public constructor() : Hinter {61
public override fun hint(line: String, cursor: Long, context: Context): Hint? {62
if (line.byteLength == 0L || cursor != line.charLength || context.history.isEmpty) return None63
val start = if (context.historyIndex == context.history.size) context.historyIndex - 1L else context.historyIndex64
val found = context.history.startsWith(line, start, SearchDirection.Reverse)65
if (found == None) return None66
val entry = found!!.entry67
if (found!!.position >= entry.charLength) return None68
val suffix = entry.slice(found!!.position, entry.charLength)69
return Some(Hint(suffix, Some(suffix)))70
}71
}