K2 LL API: implement automatic diagnostic suppression
This commit is contained in:
+5
-1
@@ -27,6 +27,10 @@ internal class FileStructureElementDiagnosticsCollector private constructor(priv
|
||||
useExtendedCheckers,
|
||||
)
|
||||
collector.collectDiagnostics(firDeclaration, reporter)
|
||||
return FileStructureElementDiagnosticList(reporter.diagnostics)
|
||||
val source = firDeclaration.source
|
||||
if (source != null) {
|
||||
reporter.checkAndCommitReportsOn(source, null)
|
||||
}
|
||||
return FileStructureElementDiagnosticList(reporter.committedDiagnostics)
|
||||
}
|
||||
}
|
||||
|
||||
+28
-2
@@ -6,11 +6,13 @@
|
||||
package org.jetbrains.kotlin.analysis.low.level.api.fir.diagnostics
|
||||
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.AbstractKtSourceElement
|
||||
import org.jetbrains.kotlin.analysis.low.level.api.fir.util.addValueFor
|
||||
import org.jetbrains.kotlin.diagnostics.*
|
||||
|
||||
internal class LLFirDiagnosticReporter : DiagnosticReporter() {
|
||||
val diagnostics = mutableMapOf<PsiElement, MutableList<KtPsiDiagnostic>>()
|
||||
private val pendingDiagnostics = mutableMapOf<PsiElement, MutableList<KtPsiDiagnostic>>()
|
||||
val committedDiagnostics = mutableMapOf<PsiElement, MutableList<KtPsiDiagnostic>>()
|
||||
|
||||
override fun report(diagnostic: KtDiagnostic?, context: DiagnosticContext) {
|
||||
if (diagnostic == null) return
|
||||
@@ -21,7 +23,31 @@ internal class LLFirDiagnosticReporter : DiagnosticReporter() {
|
||||
is KtLightDiagnostic -> diagnostic.toPsiDiagnostic()
|
||||
else -> error("Unknown diagnostic type ${diagnostic::class.simpleName}")
|
||||
}
|
||||
diagnostics.addValueFor(psiDiagnostic.psiElement, psiDiagnostic)
|
||||
pendingDiagnostics.addValueFor(psiDiagnostic.psiElement, psiDiagnostic)
|
||||
}
|
||||
|
||||
override fun checkAndCommitReportsOn(element: AbstractKtSourceElement, context: DiagnosticContext?) {
|
||||
val commitEverything = context == null
|
||||
for ((diagnosticElement, pendingList) in pendingDiagnostics) {
|
||||
val committedList = committedDiagnostics.getOrPut(diagnosticElement) { mutableListOf() }
|
||||
val iterator = pendingList.iterator()
|
||||
while (iterator.hasNext()) {
|
||||
val diagnostic = iterator.next()
|
||||
when {
|
||||
context?.isDiagnosticSuppressed(diagnostic as KtDiagnostic) == true -> {
|
||||
if (diagnostic.element == element ||
|
||||
diagnostic.element.startOffset >= element.startOffset && diagnostic.element.endOffset <= element.endOffset
|
||||
) {
|
||||
iterator.remove()
|
||||
}
|
||||
}
|
||||
diagnostic.element == element || commitEverything -> {
|
||||
iterator.remove()
|
||||
committedList += diagnostic
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user