[CMI] Extract core of CodeMetaInfo to :compiler:tests-common

This commit is contained in:
Dmitriy Novozhilov
2020-11-03 12:55:44 +03:00
parent 4ad9f48642
commit 25c011ca40
15 changed files with 364 additions and 291 deletions
@@ -3,11 +3,9 @@
* 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.idea.codeMetaInfo
package org.jetbrains.kotlin.codeMetaInfo
import com.intellij.util.containers.Stack
import org.jetbrains.kotlin.idea.codeMetaInfo.models.ParsedCodeMetaInfo
import org.junit.Assert
import org.jetbrains.kotlin.codeMetaInfo.model.ParsedCodeMetaInfo
object CodeMetaInfoParser {
private val openingRegex = "(<!([^>]+?)!>)".toRegex()
@@ -18,8 +16,8 @@ object CodeMetaInfoParser {
fun getCodeMetaInfoFromText(renderedText: String): List<ParsedCodeMetaInfo> {
var text = renderedText
val openingMatchResults = Stack<MatchResult>()
val closingMatchResults = Stack<MatchResult>()
val openingMatchResults = ArrayDeque<MatchResult>()
val closingMatchResults = ArrayDeque<MatchResult>()
val result = mutableListOf<ParsedCodeMetaInfo>()
while (true) {
@@ -35,19 +33,21 @@ object CodeMetaInfoParser {
closingStartOffset = closing.range.first
text = if (openingStartOffset < closingStartOffset) {
openingMatchResults.push(opening)
text.removeRange(openingStartOffset, opening!!.range.last + 1)
requireNotNull(opening)
openingMatchResults.addLast(opening)
text.removeRange(openingStartOffset, opening.range.last + 1)
} else {
closingMatchResults.push(closing)
text.removeRange(closingStartOffset, closing!!.range.last + 1)
requireNotNull(closing)
closingMatchResults.addLast(closing)
text.removeRange(closingStartOffset, closing.range.last + 1)
}
}
if (openingMatchResults.size != closingMatchResults.size) {
Assert.fail("Opening and closing tags counts are not equals")
error("Opening and closing tags counts are not equals")
}
while (!openingMatchResults.isEmpty()) {
val openingMatchResult = openingMatchResults.pop()
val closingMatchResult = closingMatchResults.pop()
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()
@@ -62,4 +62,4 @@ object CodeMetaInfoParser {
}
return result
}
}
}
@@ -3,11 +3,11 @@
* 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.idea.codeMetaInfo
package org.jetbrains.kotlin.codeMetaInfo
import com.intellij.util.containers.Stack
import org.jetbrains.kotlin.checkers.utils.CheckerTestUtil
import org.jetbrains.kotlin.idea.codeMetaInfo.models.CodeMetaInfo
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
import java.io.File
object CodeMetaInfoRenderer {
@@ -77,4 +77,4 @@ fun clearFileFromDiagnosticMarkup(file: File) {
file.writeText(cleanText)
}
fun clearTextFromDiagnosticMarkup(text: String): String = CheckerTestUtil.rangeStartOrEndPattern.matcher(text).replaceAll("")
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
}
@@ -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)
}
@@ -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
}
}
@@ -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(";")}}"
}
}
@@ -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
}
}
@@ -34,14 +34,18 @@ import org.jetbrains.kotlin.checkers.diagnostics.SyntaxErrorDiagnostic
import org.jetbrains.kotlin.checkers.diagnostics.factories.DebugInfoDiagnosticFactory0
import org.jetbrains.kotlin.checkers.utils.CheckerTestUtil
import org.jetbrains.kotlin.checkers.utils.DiagnosticsRenderingConfiguration
import org.jetbrains.kotlin.codeMetaInfo.CodeMetaInfoParser
import org.jetbrains.kotlin.codeMetaInfo.CodeMetaInfoRenderer
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
import org.jetbrains.kotlin.codeMetaInfo.model.DiagnosticCodeMetaInfo
import org.jetbrains.kotlin.codeMetaInfo.renderConfigurations.AbstractCodeMetaInfoRenderConfiguration
import org.jetbrains.kotlin.codeMetaInfo.renderConfigurations.DiagnosticCodeMetaInfoRenderConfiguration
import org.jetbrains.kotlin.daemon.common.OSKind
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
import org.jetbrains.kotlin.diagnostics.AbstractDiagnostic
import org.jetbrains.kotlin.diagnostics.Severity
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.codeMetaInfo.models.*
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.AbstractCodeMetaInfoRenderConfiguration
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.DiagnosticCodeMetaInfoRenderConfiguration
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.HighlightingRenderConfiguration
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.LineMarkerRenderConfiguration
import org.jetbrains.kotlin.idea.multiplatform.setupMppProjectFromTextFile
@@ -193,17 +197,14 @@ class CodeMetaInfoTestCase(
assert(
diagnostics.any { diagnosticCodeMetaInfo ->
diagnosticCodeMetaInfo.start == highlightingCodeMetaInfo.start &&
when (diagnosticCodeMetaInfo.diagnostic) {
when (val diagnostic = diagnosticCodeMetaInfo.diagnostic) {
is SyntaxErrorDiagnostic -> {
val diagnostic: SyntaxErrorDiagnostic = diagnosticCodeMetaInfo.diagnostic
(highlightingCodeMetaInfo as HighlightingCodeMetaInfo).highlightingInfo.description in (diagnostic.psiElement as PsiErrorElementImpl).errorDescription
}
is AbstractDiagnostic<*> -> {
val diagnostic: AbstractDiagnostic<*> = diagnosticCodeMetaInfo.diagnostic
diagnostic.factory.toString() in (highlightingCodeMetaInfo as HighlightingCodeMetaInfo).highlightingInfo.description
}
is DebugInfoDiagnostic -> {
val diagnostic: DebugInfoDiagnostic = diagnosticCodeMetaInfo.diagnostic
diagnostic.factory == DebugInfoDiagnosticFactory0.MISSING_UNRESOLVED &&
"[DEBUG] Reference is not resolved to anything, but is not marked unresolved" in (highlightingCodeMetaInfo as HighlightingCodeMetaInfo).highlightingInfo.description
}
@@ -279,4 +280,4 @@ abstract class AbstractCodeMetaInfoTest : AbstractMultiModuleTest() {
}
return testSourcePath.toFile()
}
}
}
@@ -1,125 +0,0 @@
/*
* 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.idea.codeMetaInfo.models
import com.intellij.codeInsight.daemon.LineMarkerInfo
import com.intellij.codeInsight.daemon.impl.HighlightInfo
import org.jetbrains.kotlin.checkers.diagnostics.ActualDiagnostic
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.AbstractCodeMetaInfoRenderConfiguration
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.DiagnosticCodeMetaInfoRenderConfiguration
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.HighlightingRenderConfiguration
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.LineMarkerRenderConfiguration
import org.jetbrains.kotlin.idea.editor.fixers.end
import org.jetbrains.kotlin.idea.editor.fixers.start
interface CodeMetaInfo {
val start: Int
val end: Int
val renderConfiguration: AbstractCodeMetaInfoRenderConfiguration
val platforms: MutableList<String>
fun asString(): String
fun getTag(): String
}
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)
}
class LineMarkerCodeMetaInfo(
override val renderConfiguration: LineMarkerRenderConfiguration,
val lineMarker: LineMarkerInfo<*>
) : CodeMetaInfo {
override val start: Int
get() = lineMarker.startOffset
override val end: Int
get() = lineMarker.endOffset
override val platforms: MutableList<String> = mutableListOf()
override fun asString(): String = renderConfiguration.asString(this)
override fun getTag(): String = renderConfiguration.getTag()
}
class HighlightingCodeMetaInfo(
override val renderConfiguration: HighlightingRenderConfiguration,
val highlightingInfo: HighlightInfo
) : CodeMetaInfo {
override val start: Int
get() = highlightingInfo.startOffset
override val end: Int
get() = highlightingInfo.endOffset
override val platforms: MutableList<String> = mutableListOf()
override fun asString(): String = renderConfiguration.asString(this)
override fun getTag(): String = renderConfiguration.getTag()
}
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
}
}
fun createCodeMetaInfo(obj: Any, renderConfiguration: AbstractCodeMetaInfoRenderConfiguration): List<CodeMetaInfo> {
fun errorMessage() = "Unexpected render configuration for object $obj"
return when (obj) {
is Diagnostic -> {
require(renderConfiguration is DiagnosticCodeMetaInfoRenderConfiguration, ::errorMessage)
obj.textRanges.map { DiagnosticCodeMetaInfo(it.start, it.end, renderConfiguration, obj) }
}
is ActualDiagnostic -> {
require(renderConfiguration is DiagnosticCodeMetaInfoRenderConfiguration, ::errorMessage)
obj.diagnostic.textRanges.map { DiagnosticCodeMetaInfo(it.start, it.end, renderConfiguration, obj.diagnostic) }
}
is HighlightInfo -> {
require(renderConfiguration is HighlightingRenderConfiguration, ::errorMessage)
listOf(HighlightingCodeMetaInfo(renderConfiguration, obj))
}
is LineMarkerInfo<*> -> {
require(renderConfiguration is LineMarkerRenderConfiguration, ::errorMessage)
listOf(LineMarkerCodeMetaInfo(renderConfiguration, obj))
}
else -> throw IllegalArgumentException("Unknown type for creating CodeMetaInfo object $obj")
}
}
fun getCodeMetaInfo(
objects: List<Any>,
renderConfiguration: AbstractCodeMetaInfoRenderConfiguration
): List<CodeMetaInfo> {
return objects.flatMap { createCodeMetaInfo(it, renderConfiguration) }
}
@@ -0,0 +1,49 @@
/*
* 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.idea.codeMetaInfo.models
import com.intellij.codeInsight.daemon.LineMarkerInfo
import com.intellij.codeInsight.daemon.impl.HighlightInfo
import org.jetbrains.kotlin.checkers.diagnostics.ActualDiagnostic
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
import org.jetbrains.kotlin.codeMetaInfo.model.DiagnosticCodeMetaInfo
import org.jetbrains.kotlin.codeMetaInfo.renderConfigurations.AbstractCodeMetaInfoRenderConfiguration
import org.jetbrains.kotlin.codeMetaInfo.renderConfigurations.DiagnosticCodeMetaInfoRenderConfiguration
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.HighlightingRenderConfiguration
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.LineMarkerRenderConfiguration
import org.jetbrains.kotlin.idea.editor.fixers.end
import org.jetbrains.kotlin.idea.editor.fixers.start
fun createCodeMetaInfo(obj: Any, renderConfiguration: AbstractCodeMetaInfoRenderConfiguration): List<CodeMetaInfo> {
fun errorMessage() = "Unexpected render configuration for object $obj"
return when (obj) {
is Diagnostic -> {
require(renderConfiguration is DiagnosticCodeMetaInfoRenderConfiguration, ::errorMessage)
obj.textRanges.map { DiagnosticCodeMetaInfo(it.start, it.end, renderConfiguration, obj) }
}
is ActualDiagnostic -> {
require(renderConfiguration is DiagnosticCodeMetaInfoRenderConfiguration, ::errorMessage)
obj.diagnostic.textRanges.map { DiagnosticCodeMetaInfo(it.start, it.end, renderConfiguration, obj.diagnostic) }
}
is HighlightInfo -> {
require(renderConfiguration is HighlightingRenderConfiguration, ::errorMessage)
listOf(HighlightingCodeMetaInfo(renderConfiguration, obj))
}
is LineMarkerInfo<*> -> {
require(renderConfiguration is LineMarkerRenderConfiguration, ::errorMessage)
listOf(LineMarkerCodeMetaInfo(renderConfiguration, obj))
}
else -> throw IllegalArgumentException("Unknown type for creating CodeMetaInfo object $obj")
}
}
fun getCodeMetaInfo(
objects: List<Any>,
renderConfiguration: AbstractCodeMetaInfoRenderConfiguration
): List<CodeMetaInfo> {
return objects.flatMap { createCodeMetaInfo(it, renderConfiguration) }
}
@@ -0,0 +1,25 @@
/*
* 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.idea.codeMetaInfo.models
import com.intellij.codeInsight.daemon.impl.HighlightInfo
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.HighlightingRenderConfiguration
class HighlightingCodeMetaInfo(
override val renderConfiguration: HighlightingRenderConfiguration,
val highlightingInfo: HighlightInfo
) : CodeMetaInfo {
override val start: Int
get() = highlightingInfo.startOffset
override val end: Int
get() = highlightingInfo.endOffset
override val platforms: MutableList<String> = mutableListOf()
override fun asString(): String = renderConfiguration.asString(this)
override fun getTag(): String = renderConfiguration.getTag()
}
@@ -0,0 +1,25 @@
/*
* 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.idea.codeMetaInfo.models
import com.intellij.codeInsight.daemon.LineMarkerInfo
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
import org.jetbrains.kotlin.idea.codeMetaInfo.renderConfigurations.LineMarkerRenderConfiguration
class LineMarkerCodeMetaInfo(
override val renderConfiguration: LineMarkerRenderConfiguration,
val lineMarker: LineMarkerInfo<*>
) : CodeMetaInfo {
override val start: Int
get() = lineMarker.startOffset
override val end: Int
get() = lineMarker.endOffset
override val platforms: MutableList<String> = mutableListOf()
override fun asString(): String = renderConfiguration.asString(this)
override fun getTag(): String = renderConfiguration.getTag()
}
@@ -1,142 +0,0 @@
/*
* 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.idea.codeMetaInfo.renderConfigurations
import com.intellij.openapi.util.text.StringUtil
import com.intellij.util.containers.ContainerUtil
import org.jetbrains.kotlin.checkers.diagnostics.factories.DebugInfoDiagnosticFactory1
import org.jetbrains.kotlin.diagnostics.Diagnostic
import org.jetbrains.kotlin.diagnostics.rendering.*
import org.jetbrains.kotlin.idea.codeMetaInfo.models.DiagnosticCodeMetaInfo
import org.jetbrains.kotlin.idea.codeMetaInfo.models.HighlightingCodeMetaInfo
import org.jetbrains.kotlin.idea.codeMetaInfo.models.CodeMetaInfo
import org.jetbrains.kotlin.idea.codeMetaInfo.models.LineMarkerCodeMetaInfo
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(";")}}"
}
}
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!!
}
}
open class LineMarkerRenderConfiguration(var renderDescription: Boolean = true) : AbstractCodeMetaInfoRenderConfiguration() {
override fun asString(codeMetaInfo: CodeMetaInfo): String {
if (codeMetaInfo !is LineMarkerCodeMetaInfo) return ""
return getTag() + getParamsString(codeMetaInfo)
}
fun getTag() = "LINE_MARKER"
private fun getParamsString(lineMarkerCodeMetaInfo: LineMarkerCodeMetaInfo): String {
if (!renderParams) return ""
val params = mutableListOf<String>()
if (renderDescription)
params.add("descr='${sanitizeLineMarkerTooltip(lineMarkerCodeMetaInfo.lineMarker.lineMarkerTooltip)}'")
params.add(getAdditionalParams(lineMarkerCodeMetaInfo))
val paramsString = params.filter { it.isNotEmpty() }.joinToString("; ")
return if (paramsString.isEmpty()) "" else "(\"$paramsString\")"
}
}
open class HighlightingRenderConfiguration(
val renderDescription: Boolean = true,
val renderTextAttributesKey: Boolean = true,
val renderSeverity: Boolean = true
) : AbstractCodeMetaInfoRenderConfiguration() {
override fun asString(codeMetaInfo: CodeMetaInfo): String {
if (codeMetaInfo !is HighlightingCodeMetaInfo) return ""
return getTag() + getParamsString(codeMetaInfo)
}
fun getTag() = "HIGHLIGHTING"
private fun getParamsString(highlightingCodeMetaInfo: HighlightingCodeMetaInfo): String {
if (!renderParams) return ""
val params = mutableListOf<String>()
if (renderSeverity)
params.add("severity='${highlightingCodeMetaInfo.highlightingInfo.severity}'")
if (renderDescription)
params.add("descr='${sanitizeLineBreaks(highlightingCodeMetaInfo.highlightingInfo.description)}'")
if (renderTextAttributesKey)
params.add("textAttributesKey='${highlightingCodeMetaInfo.highlightingInfo.forcedTextAttributesKey}'")
params.add(getAdditionalParams(highlightingCodeMetaInfo))
val paramsString = params.filter { it.isNotEmpty() }.joinToString("; ")
return if (paramsString.isEmpty()) "" else "(\"$paramsString\")"
}
}
@@ -0,0 +1,41 @@
/*
* 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.idea.codeMetaInfo.renderConfigurations
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
import org.jetbrains.kotlin.codeMetaInfo.renderConfigurations.AbstractCodeMetaInfoRenderConfiguration
import org.jetbrains.kotlin.idea.codeMetaInfo.models.HighlightingCodeMetaInfo
open class HighlightingRenderConfiguration(
val renderDescription: Boolean = true,
val renderTextAttributesKey: Boolean = true,
val renderSeverity: Boolean = true
) : AbstractCodeMetaInfoRenderConfiguration() {
override fun asString(codeMetaInfo: CodeMetaInfo): String {
if (codeMetaInfo !is HighlightingCodeMetaInfo) return ""
return getTag() + getParamsString(codeMetaInfo)
}
fun getTag() = "HIGHLIGHTING"
private fun getParamsString(highlightingCodeMetaInfo: HighlightingCodeMetaInfo): String {
if (!renderParams) return ""
val params = mutableListOf<String>()
if (renderSeverity)
params.add("severity='${highlightingCodeMetaInfo.highlightingInfo.severity}'")
if (renderDescription)
params.add("descr='${sanitizeLineBreaks(highlightingCodeMetaInfo.highlightingInfo.description)}'")
if (renderTextAttributesKey)
params.add("textAttributesKey='${highlightingCodeMetaInfo.highlightingInfo.forcedTextAttributesKey}'")
params.add(getAdditionalParams(highlightingCodeMetaInfo))
val paramsString = params.filter { it.isNotEmpty() }.joinToString("; ")
return if (paramsString.isEmpty()) "" else "(\"$paramsString\")"
}
}
@@ -0,0 +1,32 @@
/*
* 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.idea.codeMetaInfo.renderConfigurations
import org.jetbrains.kotlin.codeMetaInfo.model.CodeMetaInfo
import org.jetbrains.kotlin.codeMetaInfo.renderConfigurations.AbstractCodeMetaInfoRenderConfiguration
import org.jetbrains.kotlin.idea.codeMetaInfo.models.LineMarkerCodeMetaInfo
open class LineMarkerRenderConfiguration(var renderDescription: Boolean = true) : AbstractCodeMetaInfoRenderConfiguration() {
override fun asString(codeMetaInfo: CodeMetaInfo): String {
if (codeMetaInfo !is LineMarkerCodeMetaInfo) return ""
return getTag() + getParamsString(codeMetaInfo)
}
fun getTag() = "LINE_MARKER"
private fun getParamsString(lineMarkerCodeMetaInfo: LineMarkerCodeMetaInfo): String {
if (!renderParams) return ""
val params = mutableListOf<String>()
if (renderDescription)
params.add("descr='${sanitizeLineMarkerTooltip(lineMarkerCodeMetaInfo.lineMarker.lineMarkerTooltip)}'")
params.add(getAdditionalParams(lineMarkerCodeMetaInfo))
val paramsString = params.filter { it.isNotEmpty() }.joinToString("; ")
return if (paramsString.isEmpty()) "" else "(\"$paramsString\")"
}
}