From e2dbfebf32fb3d84f1c2763d783d48d37970d752 Mon Sep 17 00:00:00 2001 From: Dmitry Jemerov Date: Wed, 20 May 2015 14:42:48 +0200 Subject: [PATCH] add more quickfixes to cleanup action --- .../inspections/KotlinCleanupInspection.kt | 25 +++++++++++++++++-- .../quickfix/ReplaceObsoleteLabelSyntaxFix.kt | 14 +++++------ idea/testData/inspections/cleanup/cleanup.kt | 12 +++++++++ .../inspections/cleanup/cleanup.kt.after | 12 +++++++++ 4 files changed, 54 insertions(+), 9 deletions(-) diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt index 40ae98bd759..dd807460617 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/KotlinCleanupInspection.kt @@ -16,6 +16,7 @@ package org.jetbrains.kotlin.idea.inspections +import com.intellij.codeInsight.intention.IntentionAction import com.intellij.codeInspection.* import com.intellij.openapi.progress.ProcessCanceledException import com.intellij.psi.PsiElement @@ -27,9 +28,13 @@ import org.jetbrains.kotlin.diagnostics.rendering.DefaultErrorMessages import org.jetbrains.kotlin.idea.caches.resolve.analyzeFullyAndGetResult import org.jetbrains.kotlin.idea.highlighter.JetPsiChecker import org.jetbrains.kotlin.idea.quickfix.JetWholeProjectModalAction +import org.jetbrains.kotlin.idea.quickfix.ReplaceObsoleteLabelSyntaxFix +import org.jetbrains.kotlin.idea.quickfix.looksLikeObsoleteLabel import org.jetbrains.kotlin.idea.util.ProjectRootsUtil +import org.jetbrains.kotlin.psi.JetAnnotationEntry import org.jetbrains.kotlin.psi.JetFile import org.jetbrains.kotlin.psi.JetTreeVisitorVoid +import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspectionTool { @@ -52,7 +57,7 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe super.visitElement(element) val collection = diagnostics.forElement(element) collection.forEach { - if (it.getFactory().isCleanup()) { + if (it.isCleanup()) { problemDescriptors.add(it.toProblemDescriptor(file, manager)) } } @@ -61,18 +66,27 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe return problemDescriptors.toTypedArray() } + private fun Diagnostic.isCleanup() = getFactory().isCleanup() || isObsoleteLabel() + private fun DiagnosticFactory<*>.isCleanup() = this == Errors.DEPRECATED_TRAIT_KEYWORD || this == Errors.DEPRECATED_ANNOTATION_SYNTAX || this == Errors.ENUM_ENTRY_USES_DEPRECATED_OR_NO_DELIMITER || this == Errors.ENUM_ENTRY_USES_DEPRECATED_SUPER_CONSTRUCTOR || this == Errors.DEPRECATED_LAMBDA_SYNTAX || + this == Errors.MISSING_CONSTRUCTOR_KEYWORD || + this == Errors.FUNCTION_EXPRESSION_WITH_NAME || this == Errors.JAVA_LANG_CLASS_PARAMETER_IN_ANNOTATION || this == ErrorsJvm.JAVA_LANG_CLASS_ARGUMENT_IN_ANNOTATION + private fun Diagnostic.isObsoleteLabel(): Boolean { + val annotationEntry = getPsiElement().getNonStrictParentOfType() ?: return false + return annotationEntry.looksLikeObsoleteLabel() + } + private fun Diagnostic.toProblemDescriptor(file: JetFile, manager: InspectionManager): ProblemDescriptor? { val quickFixes = JetPsiChecker.createQuickfixes(this) - .filter { it !is JetWholeProjectModalAction<*> } + .filter { it.isCleanupFix(this) } .map { IntentionWrapper(it, file) } return manager.createProblemDescriptor(getPsiElement(), @@ -81,4 +95,11 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe quickFixes.toTypedArray(), ProblemHighlightType.GENERIC_ERROR_OR_WARNING) } + + private fun IntentionAction.isCleanupFix(diagnostic: Diagnostic): Boolean { + if (diagnostic.getFactory() == Errors.UNRESOLVED_REFERENCE) { + return this is ReplaceObsoleteLabelSyntaxFix + } + return this !is JetWholeProjectModalAction<*> + } } diff --git a/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceObsoleteLabelSyntaxFix.kt b/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceObsoleteLabelSyntaxFix.kt index 0373b25b56e..fdda73e9a46 100644 --- a/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceObsoleteLabelSyntaxFix.kt +++ b/idea/src/org/jetbrains/kotlin/idea/quickfix/ReplaceObsoleteLabelSyntaxFix.kt @@ -78,13 +78,6 @@ public class ReplaceObsoleteLabelSyntaxFix(element: JetAnnotationEntry?) : JetIn } && analyze().getDiagnostics().forElement(nameExpression).any { it.getFactory() == Errors.UNRESOLVED_REFERENCE } } - private fun JetAnnotationEntry.looksLikeObsoleteLabel() = - getAtSymbol() != null && - getParent() is JetAnnotatedExpression && - (getParent() as JetAnnotatedExpression).getAnnotationEntries().size() == 1 && - getValueArgumentList() == null && - getCalleeExpression()?.getConstructorReferenceExpression()?.getIdentifier() != null - private fun replaceWithLabel(annotation: JetAnnotationEntry) { val labelName = annotation.getCalleeExpression()?.getConstructorReferenceExpression()?.getReferencedName() ?: return val annotatedExpression = annotation.getParent() as? JetAnnotatedExpression ?: return @@ -103,3 +96,10 @@ public class ReplaceObsoleteLabelSyntaxFix(element: JetAnnotationEntry?) : JetIn } } } + +public fun JetAnnotationEntry.looksLikeObsoleteLabel(): Boolean = + getAtSymbol() != null && + getParent() is JetAnnotatedExpression && + (getParent() as JetAnnotatedExpression).getAnnotationEntries().size() == 1 && + getValueArgumentList() == null && + getCalleeExpression()?.getConstructorReferenceExpression()?.getIdentifier() != null diff --git a/idea/testData/inspections/cleanup/cleanup.kt b/idea/testData/inspections/cleanup/cleanup.kt index d1cf1f50c7c..9cef11cddd8 100644 --- a/idea/testData/inspections/cleanup/cleanup.kt +++ b/idea/testData/inspections/cleanup/cleanup.kt @@ -18,3 +18,15 @@ val f = { (a: Int, b: Int) -> a + b } annotation class Ann(val arg1: Class<*>, val arg2: Class) Ann(javaClass(), javaClass()) class MyClass + +class A private() + +val x = fun foo(x: String) { } + +fun foo() { + @loop + for (i in 1..100) { + /* comment */ + continue@loop + } +} diff --git a/idea/testData/inspections/cleanup/cleanup.kt.after b/idea/testData/inspections/cleanup/cleanup.kt.after index 8fa7a99f0bc..4ae57e05f26 100644 --- a/idea/testData/inspections/cleanup/cleanup.kt.after +++ b/idea/testData/inspections/cleanup/cleanup.kt.after @@ -20,3 +20,15 @@ val f = { a: Int, b: Int -> a + b } annotation class Ann(val arg1: KClass<*>, val arg2: KClass) Ann(String::class, Int::class) class MyClass + +class A private constructor() + +val x = fun(x: String) { } + +fun foo() { + loop@ + for (i in 1..100) { + /* comment */ + continue@loop + } +}