Extract UnboundDiagnostic, DiagnosticFactory/Renderer to frontend-common
This commit is contained in:
+12
-18
@@ -1,25 +1,14 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2015 JetBrains s.r.o.
|
* 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.
|
||||||
* 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
|
package org.jetbrains.kotlin.diagnostics
|
||||||
|
|
||||||
import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticRenderer
|
import org.jetbrains.kotlin.diagnostics.rendering.DiagnosticRenderer
|
||||||
import java.lang.IllegalArgumentException
|
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 var name: String?,
|
||||||
open val severity: Severity
|
open val severity: Severity
|
||||||
) {
|
) {
|
||||||
@@ -28,7 +17,12 @@ abstract class DiagnosticFactory<D : Diagnostic> protected constructor(
|
|||||||
|
|
||||||
protected constructor(severity: Severity) : this(null, severity)
|
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 }
|
require(!(diagnostic.factory !== this)) { "Factory mismatch: expected " + this + " but was " + diagnostic.factory }
|
||||||
@Suppress("UNCHECKED_CAST")
|
@Suppress("UNCHECKED_CAST")
|
||||||
return diagnostic as D
|
return diagnostic as D
|
||||||
@@ -40,11 +34,11 @@ abstract class DiagnosticFactory<D : Diagnostic> protected constructor(
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@SafeVarargs
|
@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))
|
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) {
|
for (factory in factories) {
|
||||||
if (diagnostic.factory === factory) return factory.cast(diagnostic)
|
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
|
||||||
|
}
|
||||||
+12
@@ -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
|
package org.jetbrains.kotlin.diagnostics
|
||||||
|
|
||||||
import com.intellij.openapi.util.TextRange
|
|
||||||
import com.intellij.psi.PsiElement
|
import com.intellij.psi.PsiElement
|
||||||
import com.intellij.psi.PsiFile
|
import com.intellij.psi.PsiFile
|
||||||
|
|
||||||
interface Diagnostic {
|
interface Diagnostic : UnboundDiagnostic {
|
||||||
val factory: DiagnosticFactory<*>
|
|
||||||
val severity: Severity
|
|
||||||
val psiElement: PsiElement
|
val psiElement: PsiElement
|
||||||
val textRanges: List<TextRange>
|
|
||||||
val psiFile: PsiFile?
|
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
|
|
||||||
}
|
|
||||||
-25
@@ -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);
|
|
||||||
}
|
|
||||||
+1
-1
@@ -20,7 +20,7 @@ import org.jetbrains.kotlin.diagnostics.*
|
|||||||
import java.text.MessageFormat
|
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)
|
private val messageFormat = MessageFormat(message)
|
||||||
|
|
||||||
override fun render(diagnostic: D): String {
|
override fun render(diagnostic: D): String {
|
||||||
|
|||||||
Reference in New Issue
Block a user