scoop:scoop.readline:0.1.0
src/editing/indent.scoop
Selected production source · src/editing/indent.scoop
1
package scoop.readline3
import scoop.text.*5
internal fun EditingSession.indentLines(movement: Movement, remove: Boolean) {6
val (from, to) = buffer.range(movement)7
val start = buffer.lineStart(from)8
val end = if (to > start && buffer.charAt(to - 1L) == '\n') to else buffer.lineEnd(to)9
val replacement = StringBuilder()10
var position = start11
var first = true12
var destination = start13
while (position < end || first) {14
val lineEnd = buffer.lineEnd(position)15
var contentStart = position16
if (remove) {17
while (contentStart < lineEnd && contentStart - position < config.indentSize && buffer.charAt(contentStart) == ' ') contentStart += 1L18
} else replacement.append(" ".repeat(config.indentSize))19
if (first) destination = start + if (remove) 0L else config.indentSize20
replacement.append(line.slice(contentStart, lineEnd))21
if (lineEnd < end) replacement.append('\n')22
position = lineEnd + 1L23
first = false24
}25
buffer.replace(start, end, replacement.toString())26
buffer.moveTo(destination)27
}