add more quickfixes to cleanup action

This commit is contained in:
Dmitry Jemerov
2015-05-20 14:42:48 +02:00
parent 6e7ea662c8
commit e2dbfebf32
4 changed files with 54 additions and 9 deletions
@@ -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<JetAnnotationEntry>() ?: 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<*>
}
}
@@ -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
@@ -18,3 +18,15 @@ val f = { (a: Int, b: Int) -> a + b }
annotation class Ann(val arg1: Class<*>, val arg2: Class<out Any?>)
Ann(javaClass<String>(), javaClass<Int>()) class MyClass
class A private()
val x = fun foo(x: String) { }
fun foo() {
@loop
for (i in 1..100) {
/* comment */
continue@loop
}
}
@@ -20,3 +20,15 @@ val f = { a: Int, b: Int -> a + b }
annotation class Ann(val arg1: KClass<*>, val arg2: KClass<out Any?>)
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
}
}