Scoop API

scoop:scoop.readline:0.1.0

src/editing/motion.scoop

Selected production source · src/editing/motion.scoop

1package scoop.readline
3import scoop.text.*
5internal fun requireRepeat(count: Long) {
6 if (count < 0L) throw IllegalArgumentException(Some("negative repeat count"))
7}
9internal fun bufferDestination(buffer: LineBuffer, movement: Movement): Long {
10 when (movement) {
11 case Find(val character, val count, val forward, val till) -> return findDestination(buffer, character, count, forward, till) ?: buffer.cursor
12 else -> return ordinaryDestination(buffer, movement)
13 }
16internal fun findDestination(buffer: LineBuffer, character: Char, count: Long, forward: Boolean, till: Boolean): Long? {
17 requireRepeat(count)
18 if (count == 0L) return None
19 var index = buffer.cursor
20 var remaining = count
21 val start = buffer.lineStart(index)
22 val end = buffer.lineEnd(index)
23 while (remaining > 0L) {
24 if ((!forward && index == start) || (forward && index >= end)) return None
25 index = if (forward) buffer.next(index) else buffer.previous(index)
26 while (index >= start && index < end && buffer.charAt(index) != character) {
27 if (!forward && index == start) return None
28 index = if (forward) buffer.next(index) else buffer.previous(index)
29 }
30 if (index < start || index >= end) return None
31 remaining -= 1L
32 }
33 return Some(if (!till) index else if (forward) buffer.previous(index) else buffer.next(index))
36private fun ordinaryDestination(buffer: LineBuffer, movement: Movement): Long {
37 var position = buffer.cursor
38 var count = when (movement) {
39 case BackwardChar(val n) -> n
40 case ForwardChar(val n) -> n
41 case BackwardWord(val n, _) -> n
42 case ForwardWord(val n, _, _) -> n
43 case LineUp(val n) -> n
44 case LineDown(val n) -> n
45 case Find(_, val n, _, _) -> n
46 else -> 1L
47 }
48 requireRepeat(count)
49 val column = position - buffer.lineStart(position)
50 while (count > 0L) {
51 val before = position
52 position = when (movement) {
53 case BackwardChar(_) -> buffer.previous(position)
54 case ForwardChar(_) -> buffer.next(position)
55 case BeginningOfLine -> buffer.lineStart(position)
56 case WholeLine(_) -> buffer.lineStart(position)
57 case EndOfLine -> buffer.lineEnd(position)
58 case BeginningOfBuffer -> 0L
59 case WholeBuffer -> 0L
60 case EndOfBuffer -> buffer.length
61 case FirstNonBlank -> {
62 var index = buffer.lineStart(position)
63 while (index < buffer.lineEnd(position) && buffer.charAt(index).toString().isBlank()) index = buffer.next(index)
64 index
65 }
66 case BackwardWord(_, val word) -> buffer.backwardWord(position, word)
67 case ForwardWord(_, val word, val end) -> buffer.forwardWord(position, word, end)
68 case LineUp(_) -> {
69 val start = buffer.lineStart(position)
70 if (start == 0L) position else {
71 val end = start - 1L
72 val previous = buffer.lineStart(end)
73 buffer.snap(if (column < end - previous) previous + column else end, false)
74 }
75 }
76 case LineDown(_) -> {
77 val end = buffer.lineEnd(position)
78 if (end == buffer.length) position else {
79 val start = end + 1L
80 val nextEnd = buffer.lineEnd(start)
81 buffer.snap(if (column < nextEnd - start) start + column else nextEnd, false)
82 }
83 }
84 case Find(val character, val n, val forward, val till) -> findDestination(buffer, character, n, forward, till) ?: position
85 }
86 count -= 1L
87 if (position == before) break
88 }
89 return position