From c558df5b4a175c5f24622bfcda55b9a0b9d0600e Mon Sep 17 00:00:00 2001 From: Dmitriy Novozhilov Date: Tue, 1 Dec 2020 12:59:12 +0300 Subject: [PATCH] [CMI] Fix rendering metainfos at the end of file --- .../codeMetaInfo/CodeMetaInfoRenderer.kt | 64 ++++++++++++------- 1 file changed, 40 insertions(+), 24 deletions(-) diff --git a/compiler/tests-common/tests/org/jetbrains/kotlin/codeMetaInfo/CodeMetaInfoRenderer.kt b/compiler/tests-common/tests/org/jetbrains/kotlin/codeMetaInfo/CodeMetaInfoRenderer.kt index 63624ec8ad6..f93b3851edf 100644 --- a/compiler/tests-common/tests/org/jetbrains/kotlin/codeMetaInfo/CodeMetaInfoRenderer.kt +++ b/compiler/tests-common/tests/org/jetbrains/kotlin/codeMetaInfo/CodeMetaInfoRenderer.kt @@ -30,37 +30,53 @@ object CodeMetaInfoRenderer { builder.append(originalText) return } - val sortedMetaInfos = getSortedCodeMetaInfos(codeMetaInfos) + val sortedMetaInfos = getSortedCodeMetaInfos(codeMetaInfos).groupBy { it.start } val opened = Stack() for ((i, c) in originalText.withIndex()) { - checkOpenedAndCloseStringIfNeeded(opened, i, builder) - val matchedCodeMetaInfos = sortedMetaInfos.filter { it.start == i } - if (matchedCodeMetaInfos.isNotEmpty()) { - openStartTag(builder) - val iterator = matchedCodeMetaInfos.listIterator() - var current: CodeMetaInfo? = iterator.next() + 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>, + opened: Stack, + builder: StringBuilder + ) { + checkOpenedAndCloseStringIfNeeded(opened, offset, builder) + val matchedCodeMetaInfos = sortedMetaInfos[offset] ?: emptyList() + if (matchedCodeMetaInfos.isNotEmpty()) { + 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) - builder.append(current.asString()) - when { - next == null -> - closeStartTag(builder) - next.end == current.end -> - builder.append(", ") - else -> - closeStartAndOpenNewTag(builder) - } - current = next + while (current != null) { + val next: CodeMetaInfo? = if (iterator.hasNext()) iterator.next() else null + opened.push(current) + builder.append(current.asString()) + when { + next == null -> + closeStartTag(builder) + next.end == current.end -> + builder.append(", ") + else -> + closeStartAndOpenNewTag(builder) } + current = next } - // 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) + // Here we need to handle meta infos which has start == end and close them immediately + checkOpenedAndCloseStringIfNeeded(opened, offset, builder) } private val metaInfoComparator = (compareBy { it.start } then compareByDescending { it.end }) then compareBy { it.tag } -- GitLab