scoop:scoop.readline:0.1.0
src/editing/buffer.scoop
Selected production source · src/editing/buffer.scoop
1
package scoop.readline3
import scoop.charset.graphemeBoundaries4
import scoop.charset.isUnicodeAlphanumeric5
import scoop.text.*7
internal struct BufferState(val text: String, val cursor: Long)9
/** A Unicode line buffer whose cursor always lies on an extended-grapheme boundary. */10
public class LineBuffer public constructor(initial: String = "", cursor: Long = initial.charLength) {11
private var value: String = initial12
private var scalars: Array<Char> = initial.toCharArray()13
private var boundaries: Array<Long> = graphemeBoundaries(initial)14
private var position: Long = 0L15
init {16
if (cursor < 0L || cursor > scalars.size) throw IndexOutOfBoundsException()17
position = boundaryPosition(boundaries, cursor, false)18
}20
public val text: String get() = value21
public val cursor: Long get() = position22
public val length: Long get() = scalars.size24
/** Moves to a scalar position, rounding an interior cluster position toward its start. */25
public fun moveTo(cursor: Long) {26
if (cursor < 0L || cursor > length) throw IndexOutOfBoundsException()27
position = snap(cursor, false)28
}30
/** Replaces an interval aligned to grapheme boundaries, leaving the cursor after inserted text. */31
public fun replace(start: Long, end: Long, text: String) {32
if (start < 0L || end < start || end > length || snap(start, false) != start || snap(end, false) != end) {33
throw IndexOutOfBoundsException()34
}35
value = value.slice(0L, start) + text + value.slice(end, length)36
scalars = value.toCharArray()37
boundaries = graphemeBoundaries(value)38
position = snap(start + text.charLength, true)39
}41
/** Inserts text as a single edit, preserving whole grapheme clusters. */42
public fun insert(text: String) { replace(position, position, text) }44
internal fun state(): BufferState = BufferState(value, position)45
internal fun restore(state: BufferState) {46
value = state.text47
scalars = value.toCharArray()48
boundaries = graphemeBoundaries(value)49
moveTo(state.cursor)50
}51
internal fun charAt(index: Long): Char = scalars[index]53
internal fun snap(offset: Long, forward: Boolean): Long = boundaryPosition(boundaries, offset, forward)54
internal fun previous(offset: Long): Long = if (offset == 0L) 0L else snap(offset - 1L, false)55
internal fun next(offset: Long): Long = if (offset == length) length else snap(offset + 1L, true)56
internal fun lineStart(offset: Long): Long {57
var index = offset58
while (index > 0L && scalars[index - 1L] != '\n') index -= 1L59
return index60
}61
internal fun lineEnd(offset: Long): Long {62
var index = offset63
while (index < length && scalars[index] != '\n') index += 1L64
return index65
}67
internal fun wordClass(offset: Long, word: Word): Int {68
if (scalars[offset].toString().isBlank()) return 069
if (word == Word.Big || isUnicodeAlphanumeric(scalars[offset]) || (word == Word.Vi && scalars[offset] == '_')) return 170
return if (word == Word.Emacs) 0 else 271
}73
internal fun backwardWord(offset: Long, word: Word): Long {74
var index = offset75
while (index > 0L && wordClass(previous(index), word) == 0) index = previous(index)76
if (index == 0L) return 0L77
val kind = wordClass(previous(index), word)78
while (index > 0L && wordClass(previous(index), word) == kind) index = previous(index)79
return index80
}81
internal fun forwardWord(offset: Long, word: Word, end: WordEnd): Long {82
var index = offset83
if (end == WordEnd.Start) {84
if (index < length) {85
val kind = wordClass(index, word)86
while (index < length && wordClass(index, word) == kind) index = next(index)87
}88
while (index < length && wordClass(index, word) == 0) index = next(index)89
} else {90
if (end == WordEnd.BeforeEnd && index < length) index = next(index)91
while (index < length && wordClass(index, word) == 0) index = next(index)92
if (index < length) {93
val kind = wordClass(index, word)94
while (index < length && wordClass(index, word) == kind) index = next(index)95
}96
if (end == WordEnd.BeforeEnd) index = previous(index)97
}98
return index99
}101
/** Computes a bounded motion without changing the buffer. */102
public fun destination(movement: Movement): Long = bufferDestination(this, movement)104
internal fun range(movement: Movement): (Long, Long) {105
when (movement) {106
case WholeBuffer -> return (0L, length)107
case Find(val character, val count, val forward, val till) -> {108
val found = findDestination(this, character, count, forward, till)109
if (found == None) return (position, position)110
return if (forward) (position, next(found!!)) else (found!!, position)111
}112
case WholeLine(val count) -> {113
requireRepeat(count)114
var end = position115
var left = count116
while (left > 0L) {117
end = lineEnd(end)118
left -= 1L119
if (left == 0L || end == length) break120
end = next(end)121
}122
if (count == 0L) return (position, position)123
return (lineStart(position), if (end < length) next(end) else end)124
}125
else -> { return destinationRange(movement) }126
}127
}129
private fun destinationRange(movement: Movement): (Long, Long) {130
var target = destination(movement)131
val inclusive = when (movement) {132
case ForwardWord(_, _, val end) -> end == WordEnd.BeforeEnd && target < length133
case Find(_, _, val forward, val till) -> forward && !till && target != position134
else -> false135
}136
if (inclusive) target = next(target)137
return if (target < position) (target, position) else (position, target)138
}139
}141
private fun boundaryPosition(boundaries: Array<Long>, offset: Long, forward: Boolean): Long {142
var low = 0L143
var high = boundaries.size144
while (low < high) {145
val middle = low + (high - low) / 2L146
if (boundaries[middle] < offset) low = middle + 1L else high = middle147
}148
if (low < boundaries.size && boundaries[low] == offset) return offset149
return if (forward) boundaries[low] else boundaries[low - 1L]150
}