Enable suppression actions for Kotlin inspections
This commit is contained in:
committed by
Nikolay Krasko
parent
94c0ccf2bf
commit
712d2bdec0
+8
-5
@@ -46,9 +46,12 @@ class KotlinSuppressableWarningProblemGroup(
|
||||
|
||||
}
|
||||
|
||||
fun createSuppressWarningActions(element: PsiElement, diagnosticFactory: DiagnosticFactory<*>): List<SuppressIntentionAction> {
|
||||
if (diagnosticFactory.getSeverity() != Severity.WARNING)
|
||||
return Collections.emptyList()
|
||||
fun createSuppressWarningActions(element: PsiElement, diagnosticFactory: DiagnosticFactory<*>): List<SuppressIntentionAction> =
|
||||
createSuppressWarningActions(element, diagnosticFactory.severity, diagnosticFactory.name)
|
||||
|
||||
|
||||
fun createSuppressWarningActions(element: PsiElement, severity: Severity, suppressionKey: String): List<SuppressIntentionAction> {
|
||||
if (severity != Severity.WARNING) return Collections.emptyList()
|
||||
|
||||
val actions = arrayListOf<SuppressIntentionAction>()
|
||||
var current: PsiElement? = element
|
||||
@@ -58,7 +61,7 @@ fun createSuppressWarningActions(element: PsiElement, diagnosticFactory: Diagnos
|
||||
val declaration = current
|
||||
val kind = DeclarationKindDetector.detect(declaration)
|
||||
if (kind != null) {
|
||||
actions.add(KotlinSuppressIntentionAction(declaration, diagnosticFactory, kind))
|
||||
actions.add(KotlinSuppressIntentionAction(declaration, suppressionKey, kind))
|
||||
}
|
||||
suppressAtStatementAllowed = false
|
||||
}
|
||||
@@ -66,7 +69,7 @@ fun createSuppressWarningActions(element: PsiElement, diagnosticFactory: Diagnos
|
||||
// Add suppress action at first statement
|
||||
if (current.parent is KtBlockExpression || current.parent is KtDestructuringDeclaration) {
|
||||
val kind = if (current.parent is KtBlockExpression) "statement" else "initializer"
|
||||
actions.add(KotlinSuppressIntentionAction(current, diagnosticFactory,
|
||||
actions.add(KotlinSuppressIntentionAction(current, suppressionKey,
|
||||
AnnotationHostKind(kind, "", true)))
|
||||
suppressAtStatementAllowed = false
|
||||
}
|
||||
|
||||
+4
-2
@@ -17,18 +17,20 @@
|
||||
package org.jetbrains.kotlin.idea.inspections
|
||||
|
||||
import com.intellij.codeHighlighting.HighlightDisplayLevel
|
||||
import com.intellij.codeInsight.daemon.HighlightDisplayKey
|
||||
import com.intellij.codeInspection.CustomSuppressableInspectionTool
|
||||
import com.intellij.codeInspection.LocalInspectionTool
|
||||
import com.intellij.codeInspection.SuppressIntentionAction
|
||||
import com.intellij.codeInspection.SuppressManager
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.diagnostics.Severity
|
||||
import org.jetbrains.kotlin.idea.highlighter.createSuppressWarningActions
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.KotlinCacheService
|
||||
|
||||
public abstract class AbstractKotlinInspection: LocalInspectionTool(), CustomSuppressableInspectionTool {
|
||||
public override fun getSuppressActions(element: PsiElement?): Array<SuppressIntentionAction>? {
|
||||
return SuppressManager.getInstance()!!.createSuppressActions(HighlightDisplayKey.find(getShortName())!!)
|
||||
if (element == null) return emptyArray()
|
||||
|
||||
return createSuppressWarningActions(element, toSeverity(defaultLevel), this.shortName).toTypedArray()
|
||||
}
|
||||
|
||||
public override fun isSuppressedFor(element: PsiElement): Boolean {
|
||||
|
||||
+8
-9
@@ -16,31 +16,30 @@
|
||||
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.codeInspection.SuppressIntentionAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.diagnostics.DiagnosticFactory
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||
import com.intellij.codeInspection.SuppressIntentionAction
|
||||
import org.jetbrains.kotlin.idea.util.PsiPrecedences
|
||||
import org.jetbrains.kotlin.idea.KotlinBundle
|
||||
import org.jetbrains.kotlin.idea.caches.resolve.analyze
|
||||
import org.jetbrains.kotlin.idea.util.PsiPrecedences
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.resolve.BindingContext
|
||||
|
||||
public class KotlinSuppressIntentionAction(
|
||||
private val suppressAt: KtExpression,
|
||||
private val diagnosticFactory: DiagnosticFactory<*>,
|
||||
private val suppressKey: String,
|
||||
private val kind: AnnotationHostKind
|
||||
) : SuppressIntentionAction() {
|
||||
|
||||
override fun getFamilyName() = KotlinBundle.message("suppress.warnings.family")
|
||||
override fun getText() = KotlinBundle.message("suppress.warning.for", diagnosticFactory.getName(), kind.kind, kind.name)
|
||||
override fun getText() = KotlinBundle.message("suppress.warning.for", suppressKey, kind.kind, kind.name)
|
||||
|
||||
override fun isAvailable(project: Project, editor: Editor?, element: PsiElement) = element.isValid()
|
||||
|
||||
override fun invoke(project: Project, editor: Editor?, element: PsiElement) {
|
||||
val id = "\"${diagnosticFactory.getName()}\""
|
||||
val id = "\"$suppressKey\""
|
||||
if (suppressAt is KtModifierListOwner) {
|
||||
suppressAtModifierListOwner(suppressAt, id)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user