Extract UnboundDiagnostic, DiagnosticFactory/Renderer to frontend-common

This commit is contained in:
Mikhail Glukhikh
2020-11-19 10:23:02 +03:00
parent 52a07e31c7
commit 6f8947dd04
8 changed files with 53 additions and 73 deletions
@@ -1,25 +1,14 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* 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.diagnostics
import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticRenderer
import java.lang.IllegalArgumentException
import java.lang.SafeVarargs
abstract class DiagnosticFactory<D : Diagnostic> protected constructor(
abstract class DiagnosticFactory<D : UnboundDiagnostic> protected constructor(
open var name: String?,
open val severity: Severity
) {
@@ -28,7 +17,12 @@ abstract class DiagnosticFactory<D : Diagnostic> protected constructor(
protected constructor(severity: Severity) : this(null, severity)
fun cast(diagnostic: Diagnostic): D {
@Suppress("UNCHECKED_CAST")
fun initDefaultRenderer(defaultRenderer: DiagnosticRenderer<*>?) {
this.defaultRenderer = defaultRenderer as DiagnosticRenderer<D>?
}
fun cast(diagnostic: UnboundDiagnostic): D {
require(!(diagnostic.factory !== this)) { "Factory mismatch: expected " + this + " but was " + diagnostic.factory }
@Suppress("UNCHECKED_CAST")
return diagnostic as D
@@ -40,11 +34,11 @@ abstract class DiagnosticFactory<D : Diagnostic> protected constructor(
companion object {
@SafeVarargs
fun <D : Diagnostic> cast(diagnostic: Diagnostic, vararg factories: DiagnosticFactory<out D>): D {
fun <D : UnboundDiagnostic> cast(diagnostic: UnboundDiagnostic, vararg factories: DiagnosticFactory<out D>): D {
return cast(diagnostic, listOf(*factories))
}
fun <D : Diagnostic> cast(diagnostic: Diagnostic, factories: Collection<DiagnosticFactory<out D>>): D {
fun <D : UnboundDiagnostic> cast(diagnostic: UnboundDiagnostic, factories: Collection<DiagnosticFactory<out D>>): D {
for (factory in factories) {
if (diagnostic.factory === factory) return factory.cast(diagnostic)
}
@@ -0,0 +1,12 @@
/*
* 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.diagnostics
enum class Severity {
INFO,
ERROR,
WARNING
}
@@ -0,0 +1,15 @@
/*
* 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.diagnostics
import com.intellij.openapi.util.TextRange
interface UnboundDiagnostic {
val factory: DiagnosticFactory<*>
val severity: Severity
val textRanges: List<TextRange>
val isValid: Boolean
}
@@ -0,0 +1,12 @@
/*
* 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.diagnostics.rendering
import org.jetbrains.kotlin.diagnostics.UnboundDiagnostic
interface DiagnosticRenderer<in D : UnboundDiagnostic> {
fun render(diagnostic: D): String
}
@@ -15,15 +15,10 @@
*/
package org.jetbrains.kotlin.diagnostics
import com.intellij.openapi.util.TextRange
import com.intellij.psi.PsiElement
import com.intellij.psi.PsiFile
interface Diagnostic {
val factory: DiagnosticFactory<*>
val severity: Severity
interface Diagnostic : UnboundDiagnostic {
val psiElement: PsiElement
val textRanges: List<TextRange>
val psiFile: PsiFile?
val isValid: Boolean
}
@@ -1,23 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.diagnostics;
public enum Severity {
INFO,
ERROR,
WARNING
}
@@ -1,25 +0,0 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.diagnostics.rendering;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.kotlin.diagnostics.Diagnostic;
public interface DiagnosticRenderer<D extends Diagnostic> {
@NotNull
String render(@NotNull D diagnostic);
}
@@ -20,7 +20,7 @@ import org.jetbrains.kotlin.diagnostics.*
import java.text.MessageFormat
abstract class AbstractDiagnosticWithParametersRenderer<D : Diagnostic> protected constructor(message: String) : DiagnosticRenderer<D> {
abstract class AbstractDiagnosticWithParametersRenderer<in D : Diagnostic> protected constructor(message: String) : DiagnosticRenderer<D> {
private val messageFormat = MessageFormat(message)
override fun render(diagnostic: D): String {