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

org.jetbrains.kotlin.diagnostics.KtDiagnosticFactoryToRendererMap.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

import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticParameterRenderer

class KtDiagnosticFactoryToRendererMap(val name: String) {
    private val renderersMap: MutableMap = mutableMapOf()

    operator fun get(factory: AbstractKtDiagnosticFactory): KtDiagnosticRenderer? = renderersMap[factory]

    fun containsKey(factory: AbstractKtDiagnosticFactory): Boolean {
        return renderersMap.containsKey(factory)
    }

    fun put(factory: KtDiagnosticFactory0, message: String) {
        put(factory, SimpleKtDiagnosticRenderer(message))
    }

    fun  put(
        factory: KtDiagnosticFactory1,
        message: String,
        rendererA: DiagnosticParameterRenderer?
    ) {
        put(factory, KtDiagnosticWithParameters1Renderer(message, rendererA))
    }

    fun  put(
        factory: KtDiagnosticFactory2,
        message: String,
        rendererA: DiagnosticParameterRenderer?,
        rendererB: DiagnosticParameterRenderer?
    ) {
        put(factory, KtDiagnosticWithParameters2Renderer(message, rendererA, rendererB))
    }

    fun  put(
        factory: KtDiagnosticFactory3,
        message: String,
        rendererA: DiagnosticParameterRenderer?,
        rendererB: DiagnosticParameterRenderer?,
        rendererC: DiagnosticParameterRenderer?
    ) {
        put(factory, KtDiagnosticWithParameters3Renderer(message, rendererA, rendererB, rendererC))
    }

    fun  put(
        factory: KtDiagnosticFactory4,
        message: String,
        rendererA: DiagnosticParameterRenderer?,
        rendererB: DiagnosticParameterRenderer?,
        rendererC: DiagnosticParameterRenderer?,
        rendererD: DiagnosticParameterRenderer?
    ) {
        put(factory, KtDiagnosticWithParameters4Renderer(message, rendererA, rendererB, rendererC, rendererD))
    }

    fun put(factory: KtDiagnosticFactoryForDeprecation0, message: String) {
        put(factory.errorFactory, SimpleKtDiagnosticRenderer(message))
        put(factory.warningFactory, SimpleKtDiagnosticRenderer(factory.warningMessage(message)))
    }

    fun  put(
        factory: KtDiagnosticFactoryForDeprecation1,
        message: String,
        rendererA: DiagnosticParameterRenderer?
    ) {
        put(factory.errorFactory, KtDiagnosticWithParameters1Renderer(message, rendererA))
        put(factory.warningFactory, KtDiagnosticWithParameters1Renderer(factory.warningMessage(message), rendererA))
    }

    fun  put(
        factory: KtDiagnosticFactoryForDeprecation2,
        message: String,
        rendererA: DiagnosticParameterRenderer?,
        rendererB: DiagnosticParameterRenderer?
    ) {
        put(factory.errorFactory, KtDiagnosticWithParameters2Renderer(message, rendererA, rendererB))
        put(factory.warningFactory, KtDiagnosticWithParameters2Renderer(factory.warningMessage(message), rendererA, rendererB))
    }

    fun  put(
        factory: KtDiagnosticFactoryForDeprecation3,
        message: String,
        rendererA: DiagnosticParameterRenderer?,
        rendererB: DiagnosticParameterRenderer?,
        rendererC: DiagnosticParameterRenderer?
    ) {
        put(factory.errorFactory, KtDiagnosticWithParameters3Renderer(message, rendererA, rendererB, rendererC))
        put(factory.warningFactory, KtDiagnosticWithParameters3Renderer(factory.warningMessage(message), rendererA, rendererB, rendererC))
    }

    fun  put(
        factory: KtDiagnosticFactoryForDeprecation4,
        message: String,
        rendererA: DiagnosticParameterRenderer?,
        rendererB: DiagnosticParameterRenderer?,
        rendererC: DiagnosticParameterRenderer?,
        rendererD: DiagnosticParameterRenderer?
    ) {
        put(factory.errorFactory, KtDiagnosticWithParameters4Renderer(message, rendererA, rendererB, rendererC, rendererD))
        put(factory.warningFactory, KtDiagnosticWithParameters4Renderer(factory.warningMessage(message), rendererA, rendererB, rendererC, rendererD))
    }

    private fun put(factory: AbstractKtDiagnosticFactory, renderer: KtDiagnosticRenderer) {
        if (renderersMap.containsKey(factory)) {
            throw IllegalStateException("Diagnostic renderer is already initialized for $factory")
        }
        renderersMap[factory] = renderer
    }

    private fun KtDiagnosticFactoryForDeprecation<*>.warningMessage(errorMessage: String): String {
        return buildString {
            append(errorMessage)
            when {
                errorMessage.endsWith(".") -> append(" ")
                errorMessage.lastOrNull()?.isWhitespace() == true -> {}
                else -> append(". ")
            }
            append("This will become an error")
            val sinceVersion = deprecatingFeature.sinceVersion
            if (sinceVersion != null) {
                append(" in Kotlin ")
                append(sinceVersion.versionString)
            } else {
                append(" in a future release")
            }
            append(".")
        }
    }
}