[CMI] Fix rendering metainfos at the end of file

This commit is contained in:
Dmitriy Novozhilov
2020-12-01 12:59:12 +03:00
parent ceb44ddccd
commit c558df5b4a
@@ -30,12 +30,31 @@ object CodeMetaInfoRenderer {
builder.append(originalText) builder.append(originalText)
return return
} }
val sortedMetaInfos = getSortedCodeMetaInfos(codeMetaInfos) val sortedMetaInfos = getSortedCodeMetaInfos(codeMetaInfos).groupBy { it.start }
val opened = Stack<CodeMetaInfo>() val opened = Stack<CodeMetaInfo>()
for ((i, c) in originalText.withIndex()) { for ((i, c) in originalText.withIndex()) {
checkOpenedAndCloseStringIfNeeded(opened, i, builder) processMetaInfosStartedAtOffset(i, sortedMetaInfos, opened, builder)
val matchedCodeMetaInfos = sortedMetaInfos.filter { it.start == i } builder.append(c)
}
val lastSymbolIsNewLine = builder.last() == '\n'
if (lastSymbolIsNewLine) {
builder.deleteCharAt(builder.length - 1)
}
processMetaInfosStartedAtOffset(originalText.length, sortedMetaInfos, opened, builder)
if (lastSymbolIsNewLine) {
builder.appendLine()
}
}
private fun processMetaInfosStartedAtOffset(
offset: Int,
sortedMetaInfos: Map<Int, List<CodeMetaInfo>>,
opened: Stack<CodeMetaInfo>,
builder: StringBuilder
) {
checkOpenedAndCloseStringIfNeeded(opened, offset, builder)
val matchedCodeMetaInfos = sortedMetaInfos[offset] ?: emptyList()
if (matchedCodeMetaInfos.isNotEmpty()) { if (matchedCodeMetaInfos.isNotEmpty()) {
openStartTag(builder) openStartTag(builder)
val iterator = matchedCodeMetaInfos.listIterator() val iterator = matchedCodeMetaInfos.listIterator()
@@ -57,10 +76,7 @@ object CodeMetaInfoRenderer {
} }
} }
// Here we need to handle meta infos which has start == end and close them immediately // Here we need to handle meta infos which has start == end and close them immediately
checkOpenedAndCloseStringIfNeeded(opened, i, builder) checkOpenedAndCloseStringIfNeeded(opened, offset, builder)
builder.append(c)
}
checkOpenedAndCloseStringIfNeeded(opened, originalText.length, builder)
} }
private val metaInfoComparator = (compareBy<CodeMetaInfo> { it.start } then compareByDescending { it.end }) then compareBy { it.tag } private val metaInfoComparator = (compareBy<CodeMetaInfo> { it.start } then compareByDescending { it.end }) then compareBy { it.tag }