scoop:scoop.readline:0.1.0
src/editing/vi_change.scoop
Selected production source · src/editing/vi_change.scoop
1
package scoop.readline3
internal class ViChange(val before: BufferState) {4
val commands: ArrayList<Command> = ArrayList<Command>()5
fun add(command: Command) {6
val normalized = when (command) {7
case Overwrite(val character) -> Command.Replace(Movement.ForwardChar(), character.toString())8
else -> command9
}10
if (commands.size > 0L) {11
val previous = commands[commands.size - 1L]12
val combined = combineInput(previous, normalized)13
when (combined) {14
case Some(val value) -> { commands[commands.size - 1L] = value; return }15
case None -> commands.add(normalized)16
}17
} else commands.add(normalized)18
}19
}21
internal fun EditingSession.recordViChange(command: Command, before: BufferState, previousMode: ViMode, result: EditResult) {22
var current = viChange23
val changed = before.text != line24
if ((when (current) { case None -> true; case Some(_) -> false }) &&25
keys.mode != ViMode.Command && (previousMode == ViMode.Command || changed)) {26
val group = ViChange(before)27
if (previousMode != ViMode.Command) group.commands.add(Command.SetViMode(previousMode))28
current = Some(group)29
viChange = current30
}31
when (current) {32
case Some(val group) -> {33
group.add(command)34
if (keys.mode == ViMode.Command || (result != EditResult.Continue && result != EditResult.Suspended)) finishViChange()35
}36
case None -> {37
undo.record(before, buffer.state(), false)38
if (changed) lastChange = Some(command)39
}40
}41
}43
internal fun EditingSession.finishViChange() {44
when (viChange) {45
case Some(val group) -> {46
viChange = None47
undo.record(group.before, buffer.state(), false)48
if (group.before.text != line) lastChange = Some(Command.Sequence(group.commands.toArray()))49
}50
case None -> return51
}52
}54
private fun combineInput(previous: Command, current: Command): Command? = when (previous) {55
case Insert(val text, 1L) -> when (current) {56
case Insert(val next, 1L) -> Some(Command.Insert(text + next))57
else -> None58
}59
case Replace(Movement.ForwardChar(val count), val text) -> when (current) {60
case Replace(Movement.ForwardChar(val nextCount), val next) -> Some(Command.Replace(Movement.ForwardChar(count + nextCount), text + next))61
else -> None62
}63
else -> None64
}