[CMI] Fix rendering metainfos at the end of file
This commit is contained in:
+23
-7
@@ -30,12 +30,31 @@ object CodeMetaInfoRenderer {
|
||||
builder.append(originalText)
|
||||
return
|
||||
}
|
||||
val sortedMetaInfos = getSortedCodeMetaInfos(codeMetaInfos)
|
||||
val sortedMetaInfos = getSortedCodeMetaInfos(codeMetaInfos).groupBy { it.start }
|
||||
val opened = Stack<CodeMetaInfo>()
|
||||
|
||||
for ((i, c) in originalText.withIndex()) {
|
||||
checkOpenedAndCloseStringIfNeeded(opened, i, builder)
|
||||
val matchedCodeMetaInfos = sortedMetaInfos.filter { it.start == i }
|
||||
processMetaInfosStartedAtOffset(i, sortedMetaInfos, opened, builder)
|
||||
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()) {
|
||||
openStartTag(builder)
|
||||
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
|
||||
checkOpenedAndCloseStringIfNeeded(opened, i, builder)
|
||||
builder.append(c)
|
||||
}
|
||||
checkOpenedAndCloseStringIfNeeded(opened, originalText.length, builder)
|
||||
checkOpenedAndCloseStringIfNeeded(opened, offset, builder)
|
||||
}
|
||||
|
||||
private val metaInfoComparator = (compareBy<CodeMetaInfo> { it.start } then compareByDescending { it.end }) then compareBy { it.tag }
|
||||
|
||||
Reference in New Issue
Block a user