scoop:scoop.readline:0.1.0
src/editing/motion.scoop
Selected production source · src/editing/motion.scoop
1
package scoop.readline3
import scoop.text.*5
internal fun requireRepeat(count: Long) {6
if (count < 0L) throw IllegalArgumentException(Some("negative repeat count"))7
}9
internal 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.cursor12
else -> return ordinaryDestination(buffer, movement)13
}14
}16
internal fun findDestination(buffer: LineBuffer, character: Char, count: Long, forward: Boolean, till: Boolean): Long? {17
requireRepeat(count)18
if (count == 0L) return None19
var index = buffer.cursor20
var remaining = count21
val start = buffer.lineStart(index)22
val end = buffer.lineEnd(index)23
while (remaining > 0L) {24
if ((!forward && index == start) || (forward && index >= end)) return None25
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 None28
index = if (forward) buffer.next(index) else buffer.previous(index)29
}30
if (index < start || index >= end) return None31
remaining -= 1L32
}33
return Some(if (!till) index else if (forward) buffer.previous(index) else buffer.next(index))34
}36
private fun ordinaryDestination(buffer: LineBuffer, movement: Movement): Long {37
var position = buffer.cursor38
var count = when (movement) {39
case BackwardChar(val n) -> n40
case ForwardChar(val n) -> n41
case BackwardWord(val n, _) -> n42
case ForwardWord(val n, _, _) -> n43
case LineUp(val n) -> n44
case LineDown(val n) -> n45
case Find(_, val n, _, _) -> n46
else -> 1L47
}48
requireRepeat(count)49
val column = position - buffer.lineStart(position)50
while (count > 0L) {51
val before = position52
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 -> 0L59
case WholeBuffer -> 0L60
case EndOfBuffer -> buffer.length61
case FirstNonBlank -> {62
var index = buffer.lineStart(position)63
while (index < buffer.lineEnd(position) && buffer.charAt(index).toString().isBlank()) index = buffer.next(index)64
index65
}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 - 1L72
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 + 1L80
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) ?: position85
}86
count -= 1L87
if (position == before) break88
}89
return position90
}