[CMI] Extract core of CodeMetaInfo to :compiler:tests-common
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codeMetaInfo
|
||||
|
||||
import org.jetbrains.kotlin.codeMetaInfo.model.ParsedCodeMetaInfo
|
||||
|
||||
object CodeMetaInfoParser {
|
||||
private val openingRegex = "(<!([^>]+?)!>)".toRegex()
|
||||
private val closingRegex = "(<!>)".toRegex()
|
||||
|
||||
private val descriptionRegex = "\\(\".*?\"\\)".toRegex()
|
||||
private val platformRegex = "\\{(.+)}".toRegex()
|
||||
|
||||
fun getCodeMetaInfoFromText(renderedText: String): List<ParsedCodeMetaInfo> {
|
||||
var text = renderedText
|
||||
val openingMatchResults = ArrayDeque<MatchResult>()
|
||||
val closingMatchResults = ArrayDeque<MatchResult>()
|
||||
val result = mutableListOf<ParsedCodeMetaInfo>()
|
||||
|
||||
while (true) {
|
||||
var openingStartOffset = Int.MAX_VALUE
|
||||
var closingStartOffset = Int.MAX_VALUE
|
||||
val opening = openingRegex.find(text)
|
||||
val closing = closingRegex.find(text)
|
||||
if (opening == null && closing == null) break
|
||||
|
||||
if (opening != null)
|
||||
openingStartOffset = opening.range.first
|
||||
if (closing != null)
|
||||
closingStartOffset = closing.range.first
|
||||
|
||||
text = if (openingStartOffset < closingStartOffset) {
|
||||
requireNotNull(opening)
|
||||
openingMatchResults.addLast(opening)
|
||||
text.removeRange(openingStartOffset, opening.range.last + 1)
|
||||
} else {
|
||||
requireNotNull(closing)
|
||||
closingMatchResults.addLast(closing)
|
||||
text.removeRange(closingStartOffset, closing.range.last + 1)
|
||||
}
|
||||
}
|
||||
if (openingMatchResults.size != closingMatchResults.size) {
|
||||
error("Opening and closing tags counts are not equals")
|
||||
}
|
||||
while (!openingMatchResults.isEmpty()) {
|
||||
val openingMatchResult = openingMatchResults.removeLast()
|
||||
val closingMatchResult = closingMatchResults.removeLast()
|
||||
val metaInfoWithoutParams = openingMatchResult.groups[2]!!.value.replace(descriptionRegex, "")
|
||||
metaInfoWithoutParams.split(",").forEach {
|
||||
val tag = platformRegex.replace(it, "").trim()
|
||||
val platforms =
|
||||
if (platformRegex.containsMatchIn(it)) platformRegex.find(it)!!.destructured.component1().split(";") else listOf()
|
||||
result.add(
|
||||
ParsedCodeMetaInfo(
|
||||
openingMatchResult.range.first, closingMatchResult.range.first, platforms.toMutableList(), tag,
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codeMetaInfo
|
||||
|
||||
import com.intellij.util.containers.Stack
|
||||
import org.jetbrains.kotlin.checkers.utils.CheckerTestUtil
|
||||
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
|
||||
import java.io.File
|
||||
|
||||
object CodeMetaInfoRenderer {
|
||||
fun renderTagsToText(
|
||||
codeMetaInfos: List<CodeMetaInfo>,
|
||||
originalText: String
|
||||
): StringBuffer {
|
||||
val result = StringBuffer()
|
||||
if (codeMetaInfos.isEmpty()) {
|
||||
result.append(originalText)
|
||||
return result
|
||||
}
|
||||
val sortedMetaInfos = getSortedCodeMetaInfos(codeMetaInfos)
|
||||
val opened = Stack<CodeMetaInfo>()
|
||||
|
||||
for ((i, c) in originalText.withIndex()) {
|
||||
checkOpenedAndCloseStringIfNeeded(opened, i, result)
|
||||
val matchedCodeMetaInfos = sortedMetaInfos.filter { it.start == i }
|
||||
if (matchedCodeMetaInfos.isNotEmpty()) {
|
||||
openStartTag(result)
|
||||
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())
|
||||
when {
|
||||
next == null ->
|
||||
closeStartTag(result)
|
||||
next.end == current.end ->
|
||||
result.append(", ")
|
||||
else ->
|
||||
closeStartAndOpenNewTag(result)
|
||||
}
|
||||
current = next
|
||||
}
|
||||
}
|
||||
result.append(c)
|
||||
}
|
||||
checkOpenedAndCloseStringIfNeeded(opened, originalText.length, result)
|
||||
return result
|
||||
}
|
||||
|
||||
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 checkOpenedAndCloseStringIfNeeded(opened: Stack<CodeMetaInfo>, end: Int, result: StringBuffer) {
|
||||
var prev: CodeMetaInfo? = null
|
||||
while (!opened.isEmpty() && end == opened.peek().end) {
|
||||
if (prev == null || prev.start != opened.peek().start)
|
||||
closeString(result)
|
||||
prev = opened.pop()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun clearFileFromDiagnosticMarkup(file: File) {
|
||||
val text = file.readText()
|
||||
val cleanText = clearTextFromDiagnosticMarkup(text)
|
||||
file.writeText(cleanText)
|
||||
}
|
||||
|
||||
fun clearTextFromDiagnosticMarkup(text: String): String = CheckerTestUtil.rangeStartOrEndPattern.matcher(text).replaceAll("")
|
||||
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codeMetaInfo.model
|
||||
|
||||
import org.jetbrains.kotlin.codeMetaInfo.renderConfigurations.AbstractCodeMetaInfoRenderConfiguration
|
||||
|
||||
interface CodeMetaInfo {
|
||||
val start: Int
|
||||
val end: Int
|
||||
val renderConfiguration: AbstractCodeMetaInfoRenderConfiguration
|
||||
val platforms: MutableList<String>
|
||||
|
||||
fun asString(): String
|
||||
fun getTag(): String
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codeMetaInfo.model
|
||||
|
||||
import org.jetbrains.kotlin.codeMetaInfo.renderConfigurations.DiagnosticCodeMetaInfoRenderConfiguration
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
|
||||
class DiagnosticCodeMetaInfo(
|
||||
override val start: Int,
|
||||
override val end: Int,
|
||||
override val renderConfiguration: DiagnosticCodeMetaInfoRenderConfiguration,
|
||||
val diagnostic: Diagnostic
|
||||
) : CodeMetaInfo {
|
||||
override val platforms: MutableList<String> = mutableListOf()
|
||||
|
||||
override fun asString(): String = renderConfiguration.asString(this)
|
||||
|
||||
override fun getTag(): String = renderConfiguration.getTag(this)
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codeMetaInfo.model
|
||||
|
||||
import org.jetbrains.kotlin.codeMetaInfo.renderConfigurations.AbstractCodeMetaInfoRenderConfiguration
|
||||
|
||||
class ParsedCodeMetaInfo(
|
||||
override val start: Int,
|
||||
override val end: Int,
|
||||
override val platforms: MutableList<String>,
|
||||
private val tag: String
|
||||
) : CodeMetaInfo {
|
||||
override val renderConfiguration = object : AbstractCodeMetaInfoRenderConfiguration(false) {}
|
||||
|
||||
override fun asString(): String = renderConfiguration.asString(this)
|
||||
|
||||
override fun getTag(): String = tag
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (other == null || other !is CodeMetaInfo) return false
|
||||
return this.tag == other.getTag() && this.start == other.start && this.end == other.end
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = start
|
||||
result = 31 * result + end
|
||||
result = 31 * result + tag.hashCode()
|
||||
return result
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codeMetaInfo.renderConfigurations
|
||||
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
|
||||
|
||||
abstract class AbstractCodeMetaInfoRenderConfiguration(var renderParams: Boolean = true) {
|
||||
private val clickOrPressRegex = "Click or press (.*)to navigate".toRegex() // We have different hotkeys on different platforms
|
||||
open fun asString(codeMetaInfo: CodeMetaInfo) = codeMetaInfo.getTag() + getPlatformsString(codeMetaInfo)
|
||||
|
||||
open fun getAdditionalParams(codeMetaInfo: CodeMetaInfo) = ""
|
||||
|
||||
protected fun sanitizeLineMarkerTooltip(originalText: String?): String {
|
||||
if (originalText == null) return "null"
|
||||
val noHtmlTags = StringUtil.removeHtmlTags(originalText)
|
||||
.replace(" ", "")
|
||||
.replace(clickOrPressRegex, "")
|
||||
.trim()
|
||||
return sanitizeLineBreaks(noHtmlTags)
|
||||
}
|
||||
|
||||
protected fun sanitizeLineBreaks(originalText: String): String {
|
||||
var sanitizedText = originalText
|
||||
sanitizedText = StringUtil.replace(sanitizedText, "\r\n", " ")
|
||||
sanitizedText = StringUtil.replace(sanitizedText, "\n", " ")
|
||||
sanitizedText = StringUtil.replace(sanitizedText, "\r", " ")
|
||||
return sanitizedText
|
||||
}
|
||||
|
||||
protected fun getPlatformsString(codeMetaInfo: CodeMetaInfo): String {
|
||||
if (codeMetaInfo.platforms.isEmpty()) return ""
|
||||
return "{${codeMetaInfo.platforms.joinToString(";")}}"
|
||||
}
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
/*
|
||||
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.codeMetaInfo.renderConfigurations
|
||||
|
||||
import com.intellij.util.containers.ContainerUtil
|
||||
import org.jetbrains.kotlin.checkers.diagnostics.factories.DebugInfoDiagnosticFactory1
|
||||
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
|
||||
import org.jetbrains.kotlin.codeMetaInfo.model.DiagnosticCodeMetaInfo
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.diagnostics.rendering.*
|
||||
|
||||
open class DiagnosticCodeMetaInfoRenderConfiguration(
|
||||
val withNewInference: Boolean = true,
|
||||
val renderSeverity: Boolean = false
|
||||
) : AbstractCodeMetaInfoRenderConfiguration() {
|
||||
private val crossPlatformLineBreak = """\r?\n""".toRegex()
|
||||
|
||||
override fun asString(codeMetaInfo: CodeMetaInfo): String {
|
||||
if (codeMetaInfo !is DiagnosticCodeMetaInfo) return ""
|
||||
return (getTag(codeMetaInfo)
|
||||
+ getPlatformsString(codeMetaInfo)
|
||||
+ getParamsString(codeMetaInfo))
|
||||
.replace(crossPlatformLineBreak, "")
|
||||
}
|
||||
|
||||
private fun getParamsString(codeMetaInfo: DiagnosticCodeMetaInfo): String {
|
||||
if (!renderParams) return ""
|
||||
val params = mutableListOf<String>()
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
val renderer = when (codeMetaInfo.diagnostic.factory) {
|
||||
is DebugInfoDiagnosticFactory1 -> DiagnosticWithParameters1Renderer(
|
||||
"{0}",
|
||||
Renderers.TO_STRING
|
||||
) as DiagnosticRenderer<Diagnostic>
|
||||
else -> DefaultErrorMessages.getRendererForDiagnostic(codeMetaInfo.diagnostic)
|
||||
}
|
||||
if (renderer is AbstractDiagnosticWithParametersRenderer) {
|
||||
val renderParameters = renderer.renderParameters(codeMetaInfo.diagnostic)
|
||||
params.addAll(ContainerUtil.map(renderParameters) { it.toString() })
|
||||
}
|
||||
if (renderSeverity)
|
||||
params.add("severity='${codeMetaInfo.diagnostic.severity}'")
|
||||
|
||||
params.add(getAdditionalParams(codeMetaInfo))
|
||||
|
||||
return "(\"${params.filter { it.isNotEmpty() }.joinToString("; ")}\")"
|
||||
}
|
||||
|
||||
fun getTag(codeMetaInfo: DiagnosticCodeMetaInfo): String {
|
||||
return codeMetaInfo.diagnostic.factory.name
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user