Analysis API: extract PrettyPrinter from DebugSymbolRenderer to utils

This commit is contained in:
Ilya Kirillov
2021-11-29 15:07:29 +01:00
parent 8cb5283a7d
commit 9cb950a2ae
3 changed files with 130 additions and 68 deletions
@@ -0,0 +1,105 @@
/*
* Copyright 2010-2021 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.analysis.utils.printer
import java.lang.Appendable
public class PrettyPrinter(private val indentSize: Int = 2) : Appendable {
private val builder = StringBuilder()
public var indent: Int = 0
override fun append(seq: CharSequence): Appendable = apply {
seq.split('\n').forEachIndexed { index, line ->
if (index > 0) {
builder.append('\n')
}
appendIndentIfNeeded()
builder.append(line)
}
}
override fun append(seq: CharSequence, start: Int, end: Int): Appendable = apply {
append(seq.subSequence(start, end))
}
override fun append(c: Char): Appendable = apply {
if (c != '\n') {
appendIndentIfNeeded()
}
builder.append(c)
}
public inline fun withIndent(block: PrettyPrinter.() -> Unit) {
indent += 1
block(this)
indent -= 1
}
public inline fun withIndentInBraces(block: PrettyPrinter.() -> Unit) {
withIndentWrapped(before = "{", after = "}", block)
}
public inline fun withIndentInSquareBrackets(block: PrettyPrinter.() -> Unit) {
withIndentWrapped(before = "[", after = "]", block)
}
public inline fun withIndentWrapped(before: String, after: String, block: PrettyPrinter.() -> Unit) {
append(before)
appendLine()
withIndent(block)
appendLine()
append(after)
}
public inline fun <T> printCollection(
collection: Iterable<T>,
separator: String = ", ",
prefix: String = "",
postfix: String = "",
renderItem: PrettyPrinter.(T) -> Unit
) {
append(prefix)
val iterator = collection.iterator()
while (iterator.hasNext()) {
renderItem(iterator.next())
if (iterator.hasNext()) {
append(separator)
}
}
append(postfix)
}
public inline fun <T> printCollectionIfNotEmpty(
collection: Iterable<T>,
separator: String = ", ",
prefix: String = "",
postfix: String = "",
renderItem: PrettyPrinter.(T) -> Unit
) {
if (!collection.iterator().hasNext()) return
printCollection(collection, separator, prefix, postfix, renderItem)
}
public fun printCharIfNotThere(char: Char) {
if (builder.lastOrNull() != char) {
append(char)
}
}
private fun appendIndentIfNeeded() {
if (builder.isEmpty() || builder[builder.lastIndex] == '\n') {
builder.append(" ".repeat(indentSize * indent))
}
}
override fun toString(): String {
return builder.toString()
}
}
public inline fun prettyPrint(body: PrettyPrinter.() -> Unit): String =
PrettyPrinter().apply(body).toString()