Scoop API

scoop:scoop.readline:0.1.0

src/helpers.scoop

Selected production source · src/helpers.scoop

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