Minor reindent optimizations
This commit is contained in:
@@ -34,7 +34,7 @@ public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: St
|
||||
require(marginPrefix.isNotBlank()) { "marginPrefix should be non blank string but it is '$marginPrefix'" }
|
||||
val lines = lines()
|
||||
|
||||
return lines.reindent(length() + newIndent.length() * lines.size(), getIndentFunction(newIndent)) { line ->
|
||||
return lines.reindent(length() + newIndent.length() * lines.size(), getIndentFunction(newIndent), { line ->
|
||||
val firstNonWhitespaceIndex = line.indexOfFirst { !it.isWhitespace() }
|
||||
|
||||
when {
|
||||
@@ -42,7 +42,7 @@ public fun String.replaceIndentByMargin(newIndent: String = "", marginPrefix: St
|
||||
line.startsWith(marginPrefix, firstNonWhitespaceIndex) -> line.substring(firstNonWhitespaceIndex + marginPrefix.length())
|
||||
else -> null
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -80,7 +80,7 @@ public fun String.replaceIndent(newIndent: String = ""): String {
|
||||
.map { it.indentWidth() }
|
||||
.min() ?: 0
|
||||
|
||||
return lines.reindent(length() + newIndent.length() * lines.size(), getIndentFunction(newIndent)) { line -> line.drop(minCommonIndent) }
|
||||
return lines.reindent(length() + newIndent.length() * lines.size(), getIndentFunction(newIndent), { line -> line.drop(minCommonIndent) })
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -105,14 +105,19 @@ private fun String.indentWidth(): Int = indexOfFirst { !it.isWhitespace() }.let
|
||||
|
||||
private fun getIndentFunction(indent: String) = when {
|
||||
indent.isEmpty() -> { line: String -> line }
|
||||
else -> { line: String -> "$indent$line" }
|
||||
else -> { line: String -> indent.concat(line) }
|
||||
}
|
||||
|
||||
private inline fun List<String>.reindent(resultSizeEstimate: Int, indentAddFunction: (String) -> String, indentCutFunction: (String) -> String?) = lastIndex.let { lastLineIndex ->
|
||||
withIndex()
|
||||
.dropWhile { it.index == 0 && it.value.isBlank() }
|
||||
.takeWhile { it.index != lastLineIndex || it.value.isNotBlank() }
|
||||
.map { indentCutFunction(it.value)?.let { cutted -> indentAddFunction(cutted) } ?: it.value }
|
||||
.joinTo(StringBuilder(resultSizeEstimate), "\n")
|
||||
.toString()
|
||||
private inline fun List<String>.reindent(resultSizeEstimate: Int, indentAddFunction: (String) -> String, indentCutFunction: (String) -> String?): String {
|
||||
val lastIndex = lastIndex
|
||||
// TODO: Use mapNotNullIndexed
|
||||
return mapIndexed { index, value ->
|
||||
if ((index == 0 || index == lastIndex) && value.isBlank())
|
||||
null
|
||||
else
|
||||
indentCutFunction(value)?.let { cutted -> indentAddFunction(cutted) } ?: value
|
||||
}
|
||||
.filterNotNull()
|
||||
.joinTo(StringBuilder(resultSizeEstimate), "\n")
|
||||
.toString()
|
||||
}
|
||||
|
||||
@@ -399,7 +399,7 @@ class GenericFunction(val signature: String, val keyword: String = "fun") {
|
||||
val indent: Int = body.takeWhile { it == ' ' }.length()
|
||||
|
||||
builder.append('\n')
|
||||
StringReader(body).forEachLine {
|
||||
body.lineSequence().forEach {
|
||||
var count = indent
|
||||
val line = it.dropWhile { count-- > 0 && it == ' ' }.renderType()
|
||||
if (!line.isEmpty()) {
|
||||
|
||||
Reference in New Issue
Block a user