[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.codeMetaInfo.model.CodeMetaInfo
|
||||
import java.io.File
|
||||
import java.lang.StringBuilder
|
||||
|
||||
object CodeMetaInfoRenderer {
|
||||
fun renderTagsToText(
|
||||
codeMetaInfos: List<CodeMetaInfo>,
|
||||
originalText: String
|
||||
): StringBuffer {
|
||||
val result = StringBuffer()
|
||||
): StringBuilder {
|
||||
return StringBuilder().apply {
|
||||
renderTagsToText(this, codeMetaInfos, originalText)
|
||||
}
|
||||
}
|
||||
|
||||
fun renderTagsToText(
|
||||
builder: StringBuilder,
|
||||
codeMetaInfos: List<CodeMetaInfo>,
|
||||
originalText: String
|
||||
) {
|
||||
if (codeMetaInfos.isEmpty()) {
|
||||
result.append(originalText)
|
||||
return result
|
||||
builder.append(originalText)
|
||||
return
|
||||
}
|
||||
val sortedMetaInfos = getSortedCodeMetaInfos(codeMetaInfos)
|
||||
val opened = Stack<CodeMetaInfo>()
|
||||
|
||||
for ((i, c) in originalText.withIndex()) {
|
||||
checkOpenedAndCloseStringIfNeeded(opened, i, result)
|
||||
checkOpenedAndCloseStringIfNeeded(opened, i, builder)
|
||||
val matchedCodeMetaInfos = sortedMetaInfos.filter { it.start == i }
|
||||
if (matchedCodeMetaInfos.isNotEmpty()) {
|
||||
openStartTag(result)
|
||||
openStartTag(builder)
|
||||
val iterator = matchedCodeMetaInfos.listIterator()
|
||||
var current: CodeMetaInfo? = iterator.next()
|
||||
|
||||
while (current != null) {
|
||||
val next: CodeMetaInfo? = if (iterator.hasNext()) iterator.next() else null
|
||||
opened.push(current)
|
||||
result.append(current.asString())
|
||||
builder.append(current.asString())
|
||||
when {
|
||||
next == null ->
|
||||
closeStartTag(result)
|
||||
closeStartTag(builder)
|
||||
next.end == current.end ->
|
||||
result.append(", ")
|
||||
builder.append(", ")
|
||||
else ->
|
||||
closeStartAndOpenNewTag(result)
|
||||
closeStartAndOpenNewTag(builder)
|
||||
}
|
||||
current = next
|
||||
}
|
||||
}
|
||||
result.append(c)
|
||||
builder.append(c)
|
||||
}
|
||||
checkOpenedAndCloseStringIfNeeded(opened, originalText.length, result)
|
||||
return result
|
||||
checkOpenedAndCloseStringIfNeeded(opened, originalText.length, builder)
|
||||
}
|
||||
|
||||
private fun getSortedCodeMetaInfos(metaInfos: Collection<CodeMetaInfo>): List<CodeMetaInfo> {
|
||||
return metaInfos.sortedWith(compareBy<CodeMetaInfo> { it.start }.then(compareByDescending { it.end }))
|
||||
}
|
||||
|
||||
private fun closeString(result: StringBuffer) = result.append("<!>")
|
||||
private fun openStartTag(result: StringBuffer) = result.append("<!")
|
||||
private fun closeStartTag(result: StringBuffer) = result.append("!>")
|
||||
private fun closeStartAndOpenNewTag(result: StringBuffer) = result.append("!><!")
|
||||
private fun closeString(result: StringBuilder) = result.append("<!>")
|
||||
private fun openStartTag(result: StringBuilder) = result.append("<!")
|
||||
private fun closeStartTag(result: StringBuilder) = 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
|
||||
while (!opened.isEmpty() && end == opened.peek().end) {
|
||||
if (prev == null || prev.start != opened.peek().start)
|
||||
|
||||
Reference in New Issue
Block a user