All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.kotlin.diagnostics.rendering.CommonRenderers.kt Maven / Gradle / Ivy

There is a newer version: 2.1.0-RC
Show newest version
/*
 * 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))
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy