org.jetbrains.kotlin.diagnostics.rendering.CommonRenderers.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* Copyright 2010-2021 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.diagnostics.rendering
import com.intellij.openapi.util.text.StringUtil
import org.jetbrains.kotlin.descriptors.ClassKind
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.utils.addToStdlib.joinToWithBuffer
import java.io.PrintWriter
import java.io.StringWriter
object CommonRenderers {
@JvmField
val EMPTY = Renderer { "" }
@JvmField
val STRING = Renderer { it }
@JvmField
val NAME = Renderer { it.asString() }
@JvmField
val THROWABLE = Renderer {
val writer = StringWriter()
it.printStackTrace(PrintWriter(writer))
StringUtil.first(writer.toString(), 2048, true)
}
@JvmField
val RENDER_POSITION_VARIANCE = Renderer { variance: Variance ->
when (variance) {
Variance.INVARIANT -> "invariant"
Variance.IN_VARIANCE -> "in"
Variance.OUT_VARIANCE -> "out"
}
}
@JvmField
val CLASS_KIND = Renderer { classKind: ClassKind ->
when (classKind) {
ClassKind.CLASS -> "class"
ClassKind.INTERFACE -> "interface"
ClassKind.ENUM_CLASS -> "enum class"
ClassKind.ENUM_ENTRY -> "enum entry"
ClassKind.ANNOTATION_CLASS -> "annotation class"
ClassKind.OBJECT -> "object"
}
}
@JvmStatic
fun commaSeparated(itemRenderer: DiagnosticParameterRenderer) = ContextDependentRenderer> { collection, context ->
buildString {
val iterator = collection.iterator()
while (iterator.hasNext()) {
val next = iterator.next()
append(itemRenderer.render(next, context))
if (iterator.hasNext()) {
append(", ")
}
}
}
}
@JvmStatic
fun renderConflictingSignatureData(
signatureKind: String,
sortUsing: Comparator,
declarationRenderer: DiagnosticParameterRenderer,
renderSignature: StringBuilder.(Data) -> Unit,
declarations: (Data) -> Collection,
declarationKind: (Data) -> String = { "declarations" },
) = Renderer { data ->
val sortedDeclarations = declarations(data).sortedWith(sortUsing)
val renderingContext = RenderingContext.Impl(sortedDeclarations)
buildString {
append("The following ")
append(declarationKind(data))
append(" have the same ")
append(signatureKind)
append(" signature (")
renderSignature(data)
appendLine("):")
sortedDeclarations.joinToWithBuffer(this, separator = "\n") { descriptor ->
append(" ")
append(declarationRenderer.render(descriptor, renderingContext))
}
}
}
}