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