[CMI] Replace StringBuffer with StringBuilder in CodeMetaInfoRenderer
This commit is contained in:
+27
-18
@@ -9,59 +9,68 @@ import com.intellij.util.containers.Stack
|
|||||||
import org.jetbrains.kotlin.checkers.utils.CheckerTestUtil
|
import org.jetbrains.kotlin.checkers.utils.CheckerTestUtil
|
||||||
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
|
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
import java.lang.StringBuilder
|
||||||
|
|
||||||
object CodeMetaInfoRenderer {
|
object CodeMetaInfoRenderer {
|
||||||
fun renderTagsToText(
|
fun renderTagsToText(
|
||||||
codeMetaInfos: List<CodeMetaInfo>,
|
codeMetaInfos: List<CodeMetaInfo>,
|
||||||
originalText: String
|
originalText: String
|
||||||
): StringBuffer {
|
): StringBuilder {
|
||||||
val result = StringBuffer()
|
return StringBuilder().apply {
|
||||||
|
renderTagsToText(this, codeMetaInfos, originalText)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun renderTagsToText(
|
||||||
|
builder: StringBuilder,
|
||||||
|
codeMetaInfos: List<CodeMetaInfo>,
|
||||||
|
originalText: String
|
||||||
|
) {
|
||||||
if (codeMetaInfos.isEmpty()) {
|
if (codeMetaInfos.isEmpty()) {
|
||||||
result.append(originalText)
|
builder.append(originalText)
|
||||||
return result
|
return
|
||||||
}
|
}
|
||||||
val sortedMetaInfos = getSortedCodeMetaInfos(codeMetaInfos)
|
val sortedMetaInfos = getSortedCodeMetaInfos(codeMetaInfos)
|
||||||
val opened = Stack<CodeMetaInfo>()
|
val opened = Stack<CodeMetaInfo>()
|
||||||
|
|
||||||
for ((i, c) in originalText.withIndex()) {
|
for ((i, c) in originalText.withIndex()) {
|
||||||
checkOpenedAndCloseStringIfNeeded(opened, i, result)
|
checkOpenedAndCloseStringIfNeeded(opened, i, builder)
|
||||||
val matchedCodeMetaInfos = sortedMetaInfos.filter { it.start == i }
|
val matchedCodeMetaInfos = sortedMetaInfos.filter { it.start == i }
|
||||||
if (matchedCodeMetaInfos.isNotEmpty()) {
|
if (matchedCodeMetaInfos.isNotEmpty()) {
|
||||||
openStartTag(result)
|
openStartTag(builder)
|
||||||
val iterator = matchedCodeMetaInfos.listIterator()
|
val iterator = matchedCodeMetaInfos.listIterator()
|
||||||
var current: CodeMetaInfo? = iterator.next()
|
var current: CodeMetaInfo? = iterator.next()
|
||||||
|
|
||||||
while (current != null) {
|
while (current != null) {
|
||||||
val next: CodeMetaInfo? = if (iterator.hasNext()) iterator.next() else null
|
val next: CodeMetaInfo? = if (iterator.hasNext()) iterator.next() else null
|
||||||
opened.push(current)
|
opened.push(current)
|
||||||
result.append(current.asString())
|
builder.append(current.asString())
|
||||||
when {
|
when {
|
||||||
next == null ->
|
next == null ->
|
||||||
closeStartTag(result)
|
closeStartTag(builder)
|
||||||
next.end == current.end ->
|
next.end == current.end ->
|
||||||
result.append(", ")
|
builder.append(", ")
|
||||||
else ->
|
else ->
|
||||||
closeStartAndOpenNewTag(result)
|
closeStartAndOpenNewTag(builder)
|
||||||
}
|
}
|
||||||
current = next
|
current = next
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.append(c)
|
builder.append(c)
|
||||||
}
|
}
|
||||||
checkOpenedAndCloseStringIfNeeded(opened, originalText.length, result)
|
checkOpenedAndCloseStringIfNeeded(opened, originalText.length, builder)
|
||||||
return result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getSortedCodeMetaInfos(metaInfos: Collection<CodeMetaInfo>): List<CodeMetaInfo> {
|
private fun getSortedCodeMetaInfos(metaInfos: Collection<CodeMetaInfo>): List<CodeMetaInfo> {
|
||||||
return metaInfos.sortedWith(compareBy<CodeMetaInfo> { it.start }.then(compareByDescending { it.end }))
|
return metaInfos.sortedWith(compareBy<CodeMetaInfo> { it.start }.then(compareByDescending { it.end }))
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun closeString(result: StringBuffer) = result.append("<!>")
|
private fun closeString(result: StringBuilder) = result.append("<!>")
|
||||||
private fun openStartTag(result: StringBuffer) = result.append("<!")
|
private fun openStartTag(result: StringBuilder) = result.append("<!")
|
||||||
private fun closeStartTag(result: StringBuffer) = result.append("!>")
|
private fun closeStartTag(result: StringBuilder) = result.append("!>")
|
||||||
private fun closeStartAndOpenNewTag(result: StringBuffer) = result.append("!><!")
|
private fun closeStartAndOpenNewTag(result: StringBuilder) = result.append("!><!")
|
||||||
|
|
||||||
private fun checkOpenedAndCloseStringIfNeeded(opened: Stack<CodeMetaInfo>, end: Int, result: StringBuffer) {
|
private fun checkOpenedAndCloseStringIfNeeded(opened: Stack<CodeMetaInfo>, end: Int, result: StringBuilder) {
|
||||||
var prev: CodeMetaInfo? = null
|
var prev: CodeMetaInfo? = null
|
||||||
while (!opened.isEmpty() && end == opened.peek().end) {
|
while (!opened.isEmpty() && end == opened.peek().end) {
|
||||||
if (prev == null || prev.start != opened.peek().start)
|
if (prev == null || prev.start != opened.peek().start)
|
||||||
|
|||||||
Reference in New Issue
Block a user