Scoop API

scoop:scoop.readline:0.1.0

src/editing/indent.scoop

Selected production source · src/editing/indent.scoop

1package scoop.readline
3import scoop.text.*
5internal 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 = start
11 var first = true
12 var destination = start
13 while (position < end || first) {
14 val lineEnd = buffer.lineEnd(position)
15 var contentStart = position
16 if (remove) {
17 while (contentStart < lineEnd && contentStart - position < config.indentSize && buffer.charAt(contentStart) == ' ') contentStart += 1L
18 } else replacement.append(" ".repeat(config.indentSize))
19 if (first) destination = start + if (remove) 0L else config.indentSize
20 replacement.append(line.slice(contentStart, lineEnd))
21 if (lineEnd < end) replacement.append('\n')
22 position = lineEnd + 1L
23 first = false
24 }
25 buffer.replace(start, end, replacement.toString())
26 buffer.moveTo(destination)