Adapted code cleanup feature implementation to the recent changes in IDEA
This commit is contained in:
@@ -29,16 +29,15 @@ 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.CleanupFix
|
||||
import org.jetbrains.kotlin.idea.quickfix.replaceWith.DeprecatedSymbolUsageFix
|
||||
import org.jetbrains.kotlin.idea.quickfix.JetIntentionAction
|
||||
import org.jetbrains.kotlin.idea.quickfix.ReplaceObsoleteLabelSyntaxFix
|
||||
import org.jetbrains.kotlin.idea.quickfix.replaceWith.DeprecatedSymbolUsageFix
|
||||
import org.jetbrains.kotlin.idea.util.ProjectRootsUtil
|
||||
import org.jetbrains.kotlin.psi.JetAnnotationEntry
|
||||
import org.jetbrains.kotlin.psi.JetFile
|
||||
import org.jetbrains.kotlin.psi.JetImportDirective
|
||||
import org.jetbrains.kotlin.psi.psiUtil.forEachDescendantOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.getNonStrictParentOfType
|
||||
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
|
||||
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
|
||||
|
||||
public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspectionTool {
|
||||
@@ -57,17 +56,13 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe
|
||||
|
||||
val diagnostics = analysisResult.bindingContext.getDiagnostics()
|
||||
|
||||
class ProblemData(val problemDescriptor: ProblemDescriptor, elementToBeInvalidated: PsiElement) {
|
||||
val depth = elementToBeInvalidated.parentsWithSelf.takeWhile { it !is PsiFile }.count()
|
||||
}
|
||||
|
||||
val problems = arrayListOf<ProblemData>()
|
||||
val problemDescriptors = arrayListOf<ProblemDescriptor>()
|
||||
|
||||
val importsToRemove = file.importDirectives.filter { DeprecatedSymbolUsageFix.isImportToBeRemoved(it) }
|
||||
for (import in importsToRemove) {
|
||||
val removeImportFix = RemoveImportFix(import)
|
||||
val problemDescriptor = createProblemDescriptor(import, removeImportFix.text, listOf(removeImportFix), file, manager)
|
||||
problems.add(ProblemData(problemDescriptor, import))
|
||||
problemDescriptors.add(problemDescriptor)
|
||||
}
|
||||
|
||||
file.forEachDescendantOfType<PsiElement> { element ->
|
||||
@@ -75,16 +70,13 @@ public class KotlinCleanupInspection(): LocalInspectionTool(), CleanupLocalInspe
|
||||
if (diagnostic.isCleanup()) {
|
||||
val fixes = diagnostic.toCleanupFixes()
|
||||
if (fixes.isNotEmpty()) {
|
||||
val problemDescriptor = diagnostic.toProblemDescriptor(fixes, file, manager)
|
||||
//TODO: not quite correct for multiple
|
||||
val elementToBeInvalidated = fixes.map { it.elementToBeInvalidated() }.filterNotNull().firstOrNull() ?: element
|
||||
problems.add(ProblemData(problemDescriptor, elementToBeInvalidated))
|
||||
problemDescriptors.add(diagnostic.toProblemDescriptor(fixes, file, manager))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return problems.sortBy { it.depth }.map { it.problemDescriptor }.toTypedArray()
|
||||
return problemDescriptors.toTypedArray()
|
||||
}
|
||||
|
||||
private fun Diagnostic.isCleanup() = getFactory() in cleanupDiagnosticsFactories || isObsoleteLabel()
|
||||
|
||||
@@ -17,14 +17,12 @@
|
||||
package org.jetbrains.kotlin.idea.quickfix
|
||||
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.psi.PsiElement
|
||||
|
||||
/**
|
||||
* Marker interface for quickfixes that can be used as part of the "Cleanup Code" action. The diagnostics
|
||||
* that produce these quickfixes need to be added to KotlinCleanupInspection.cleanupDiagnosticsFactories.
|
||||
*/
|
||||
public interface CleanupFix : IntentionAction {
|
||||
public fun elementToBeInvalidated(): PsiElement? = null
|
||||
}
|
||||
// TODO(yole): add isSafeToApply() method here to get rid of filtering by diagnostics factories in
|
||||
// KotlinCleanupInspection
|
||||
|
||||
@@ -91,12 +91,5 @@ public class RemoveRightPartOfBinaryExpressionFix<T extends JetExpression> exten
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// TODO: drop it when converted to Kotlin
|
||||
@Nullable
|
||||
@Override
|
||||
public PsiElement elementToBeInvalidated() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-15
@@ -20,13 +20,13 @@ import com.intellij.codeInsight.intention.HighPriorityAction
|
||||
import com.intellij.codeInsight.intention.IntentionAction
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.PsiElement
|
||||
import org.jetbrains.kotlin.diagnostics.Diagnostic
|
||||
import org.jetbrains.kotlin.idea.core.targetDescriptors
|
||||
import org.jetbrains.kotlin.idea.quickfix.CleanupFix
|
||||
import org.jetbrains.kotlin.idea.quickfix.JetSingleIntentionActionFactory
|
||||
import org.jetbrains.kotlin.idea.quickfix.moveCaret
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.psi.JetImportDirective
|
||||
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
|
||||
import org.jetbrains.kotlin.resolve.calls.callUtil.getCalleeExpressionIfAny
|
||||
|
||||
public class DeprecatedSymbolUsageFix(
|
||||
@@ -44,19 +44,6 @@ public class DeprecatedSymbolUsageFix(
|
||||
editor?.moveCaret(offset)
|
||||
}
|
||||
|
||||
override fun elementToBeInvalidated(): PsiElement? {
|
||||
val parent = element.parent
|
||||
return when (parent) {
|
||||
is JetCallExpression -> {
|
||||
val qualified = parent.parent as? JetQualifiedExpression
|
||||
if (parent == qualified?.selectorExpression) qualified else parent
|
||||
}
|
||||
is JetQualifiedExpression -> if (element == parent.selectorExpression) parent else element
|
||||
is JetOperationExpression -> if (element == parent.operationReference) parent else element
|
||||
else -> element
|
||||
}
|
||||
}
|
||||
|
||||
companion object : JetSingleIntentionActionFactory() {
|
||||
override fun createAction(diagnostic: Diagnostic): IntentionAction? {
|
||||
val (nameExpression, replacement) = DeprecatedSymbolUsageFixBase.extractDataFromDiagnostic(diagnostic) ?: return null
|
||||
|
||||
+1
-1
@@ -14,7 +14,7 @@ val x = fun foo(x: String) { }
|
||||
fun foo() {
|
||||
@loop
|
||||
for (i in 1..100) {
|
||||
val v = oldFun2(i as Int)/* as Int*/
|
||||
val v = oldFun2(i as Int) as Int
|
||||
/* comment */
|
||||
continue@loop
|
||||
}
|
||||
|
||||
+1
-1
@@ -13,7 +13,7 @@ val x = fun(x: String) { }
|
||||
fun foo() {
|
||||
loop@
|
||||
for (i in 1..100) {
|
||||
val v = bar(i + 2)/* as Int*/
|
||||
val v = bar(i + 2)
|
||||
/* comment */
|
||||
continue@loop
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user