New J2K: get rid of generalInspectionPostProcessing

This commit is contained in:
Ilya Kirillov
2019-09-17 22:33:40 +03:00
parent a4cf7a912b
commit e6f2e0041c
4 changed files with 148 additions and 87 deletions
@@ -152,7 +152,7 @@ private val addOrRemoveModifiersProcessingGroup =
RemoveRedundantVisibilityModifierProcessing(), RemoveRedundantVisibilityModifierProcessing(),
RemoveRedundantModalityModifierProcessing(), RemoveRedundantModalityModifierProcessing(),
inspectionBasedProcessing(AddOperatorModifierInspection()), inspectionBasedProcessing(AddOperatorModifierInspection()),
generalInspectionBasedProcessing(RedundantUnitReturnTypeInspection()) RemoveExplicitUnitTypeProcessing()
) )
) )
@@ -162,8 +162,8 @@ private val removeRedundantElementsProcessingGroup =
processings = listOf( processings = listOf(
RemoveExplicitTypeArgumentsProcessing(), RemoveExplicitTypeArgumentsProcessing(),
RemoveJavaStreamsCollectCallTypeArgumentsProcessing(), RemoveJavaStreamsCollectCallTypeArgumentsProcessing(),
generalInspectionBasedProcessing(RedundantCompanionReferenceInspection()), RedundantCompanionReferenceInspectionBasedProcessing(),
generalInspectionBasedProcessing(ExplicitThisInspection()), ExplicitThisInspectionBasedProcessing(),
intentionBasedProcessing(RemoveEmptyClassBodyIntention()) intentionBasedProcessing(RemoveEmptyClassBodyIntention())
) )
) )
@@ -173,7 +173,7 @@ private val removeRedundantSemicolonProcessing =
runSingleTime = true, runSingleTime = true,
acceptNonKtElements = true, acceptNonKtElements = true,
processings = listOf( processings = listOf(
generalInspectionBasedProcessing(RedundantSemicolonInspection()) RedundantSemicolonInspectionBasedProcessing()
) )
) )
@@ -195,11 +195,11 @@ private val inspectionLikePostProcessingGroup =
inspectionBasedProcessing(ReplacePutWithAssignmentInspection()), inspectionBasedProcessing(ReplacePutWithAssignmentInspection()),
UseExpressionBodyProcessing(), UseExpressionBodyProcessing(),
inspectionBasedProcessing(UnnecessaryVariableInspection()), inspectionBasedProcessing(UnnecessaryVariableInspection()),
generalInspectionBasedProcessing(RedundantExplicitTypeInspection()), RedundantExplicitTypeInspectionBasedProcessing(),
JavaObjectEqualsToEqOperatorProcessing(), JavaObjectEqualsToEqOperatorProcessing(),
RemoveExplicitPropertyTypeProcessing(), RemoveExplicitPropertyTypeProcessing(),
RemoveRedundantNullabilityProcessing(), RemoveRedundantNullabilityProcessing(),
generalInspectionBasedProcessing(CanBeValInspection(ignoreNotUsedVals = false)), CanBeValInspectionBasedProcessing(),
inspectionBasedProcessing(FoldInitializerAndIfToElvisInspection()), inspectionBasedProcessing(FoldInitializerAndIfToElvisInspection()),
intentionBasedProcessing(RemoveRedundantCallsOfConversionMethodsIntention()), intentionBasedProcessing(RemoveRedundantCallsOfConversionMethodsIntention()),
inspectionBasedProcessing(JavaMapForEachInspection()), inspectionBasedProcessing(JavaMapForEachInspection()),
@@ -219,9 +219,10 @@ private val inspectionLikePostProcessingGroup =
intentionBasedProcessing(DestructureIntention()), intentionBasedProcessing(DestructureIntention()),
inspectionBasedProcessing(SimplifyAssertNotNullInspection()), inspectionBasedProcessing(SimplifyAssertNotNullInspection()),
intentionBasedProcessing(RemoveRedundantCallsOfConversionMethodsIntention()), intentionBasedProcessing(RemoveRedundantCallsOfConversionMethodsIntention()),
generalInspectionBasedProcessing(LiftReturnOrAssignmentInspection(skipLongExpressions = false)), LiftReturnInspectionBasedProcessing(),
LiftAssignmentInspectionBasedProcessing(),
intentionBasedProcessing(RemoveEmptyPrimaryConstructorIntention()), intentionBasedProcessing(RemoveEmptyPrimaryConstructorIntention()),
generalInspectionBasedProcessing(MayBeConstantInspection()), MayBeConstantInspectionBasedProcessing(),
RemoveForExpressionLoopParameterTypeProcessing(), RemoveForExpressionLoopParameterTypeProcessing(),
intentionBasedProcessing(ReplaceMapGetOrDefaultIntention()), intentionBasedProcessing(ReplaceMapGetOrDefaultIntention()),
inspectionBasedProcessing(ReplaceGuardClauseWithFunctionCallInspection()) inspectionBasedProcessing(ReplaceGuardClauseWithFunctionCallInspection())
@@ -243,7 +244,7 @@ private val processings: List<NamedPostProcessingGroup> = listOf(
InspectionLikeProcessingGroup( InspectionLikeProcessingGroup(
processings = listOf( processings = listOf(
VarToValProcessing(), VarToValProcessing(),
generalInspectionBasedProcessing(CanBeValInspection(ignoreNotUsedVals = false)) CanBeValInspectionBasedProcessing()
), ),
runSingleTime = true runSingleTime = true
), ),
@@ -267,8 +268,8 @@ private val processings: List<NamedPostProcessingGroup> = listOf(
ConvertGettersAndSettersToPropertyProcessing(), ConvertGettersAndSettersToPropertyProcessing(),
InspectionLikeProcessingGroup(MoveGetterAndSetterAnnotationsToPropertyProcessing()), InspectionLikeProcessingGroup(MoveGetterAndSetterAnnotationsToPropertyProcessing()),
InspectionLikeProcessingGroup( InspectionLikeProcessingGroup(
generalInspectionBasedProcessing(RedundantGetterInspection()), RemoveExplicitGetterInspectionBasedProcessing(),
generalInspectionBasedProcessing(RedundantSetterInspection()) RemoveExplicitSetterInspectionBasedProcessing()
), ),
ConvertToDataClassProcessing() ConvertToDataClassProcessing()
) )
@@ -182,76 +182,6 @@ inline fun <reified TElement : PsiElement, TIntention : SelfTargetingRangeIntent
} }
fun <TInspection : AbstractKotlinInspection> generalInspectionBasedProcessing(
inspection: TInspection,
acceptInformationLevel: Boolean = false
) = (object : InspectionLikeProcessing {
override val writeActionNeeded = false
fun <D : CommonProblemDescriptor> QuickFix<D>.applyFixSmart(element: PsiElement, descriptor: D) {
if (!element.isValid) return
if (descriptor is ProblemDescriptor) {
if (this is IntentionWrapper) {
@Suppress("NOT_YET_SUPPORTED_IN_INLINE")
fun applySelfTargetingIntention(action: SelfTargetingIntention<PsiElement>) {
val target = action.getTarget(descriptor.psiElement.startOffset, descriptor.psiElement.containingFile) ?: return
if (!action.isApplicableTo(target, descriptor.psiElement.startOffset)) return
action.applyTo(target, null)
}
@Suppress("NOT_YET_SUPPORTED_IN_INLINE")
fun applyQuickFixActionBase(action: QuickFixActionBase<PsiElement>) {
if (!action.isAvailable(element.project, null, descriptor.psiElement.containingFile)) return
action.invoke(element.project, null, descriptor.psiElement.containingFile)
}
@Suppress("NOT_YET_SUPPORTED_IN_INLINE")
fun applyIntention() {
@Suppress("UNCHECKED_CAST")
when (val action = this.action) {
is SelfTargetingIntention<*> -> applySelfTargetingIntention(action as SelfTargetingIntention<PsiElement>)
is QuickFixActionBase<*> -> applyQuickFixActionBase(action)
}
}
if (this.startInWriteAction()) {
ApplicationManager.getApplication().runWriteAction(::applyIntention)
} else {
applyIntention()
}
}
}
ApplicationManager.getApplication().runWriteAction {
this.applyFix(element.project, descriptor)
}
}
override fun createAction(element: PsiElement, settings: ConverterSettings?): (() -> Unit)? {
val holder = ProblemsHolder(InspectionManager.getInstance(element.project), element.containingFile, false)
val visitor = inspection.buildVisitor(
holder,
false,
LocalInspectionToolSession(element.containingFile, 0, element.containingFile.endOffset)
)
element.accept(visitor)
if (!holder.hasResults()) return null
return {
holder.results.clear()
element.accept(visitor)
if (holder.hasResults()) {
holder.results
.filter { acceptInformationLevel || it.highlightType != ProblemHighlightType.INFORMATION }
.forEach { it.fixes?.firstOrNull()?.applyFixSmart(element, it) }
}
}
}
})
inline fun <reified TElement : PsiElement, TInspection : AbstractApplicabilityBasedInspection<TElement>> inline fun <reified TElement : PsiElement, TInspection : AbstractApplicabilityBasedInspection<TElement>>
inspectionBasedProcessing(inspection: TInspection, acceptInformationLevel: Boolean = false) = inspectionBasedProcessing(inspection: TInspection, acceptInformationLevel: Boolean = false) =
object : InspectionLikeProcessing { object : InspectionLikeProcessing {
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.nj2k.postProcessing.processings package org.jetbrains.kotlin.nj2k.postProcessing.processings
import com.intellij.psi.PsiElement
import com.intellij.psi.search.searches.ReferencesSearch import com.intellij.psi.search.searches.ReferencesSearch
import org.jetbrains.kotlin.descriptors.CallableDescriptor import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.VariableDescriptor import org.jetbrains.kotlin.descriptors.VariableDescriptor
@@ -17,11 +18,13 @@ import org.jetbrains.kotlin.idea.core.implicitVisibility
import org.jetbrains.kotlin.idea.core.replaced import org.jetbrains.kotlin.idea.core.replaced
import org.jetbrains.kotlin.idea.core.setVisibility import org.jetbrains.kotlin.idea.core.setVisibility
import org.jetbrains.kotlin.idea.debugger.sequence.psi.callName import org.jetbrains.kotlin.idea.debugger.sequence.psi.callName
import org.jetbrains.kotlin.idea.inspections.RedundantSamConstructorInspection import org.jetbrains.kotlin.idea.inspections.*
import org.jetbrains.kotlin.idea.inspections.UseExpressionBodyInspection
import org.jetbrains.kotlin.idea.inspections.collections.isCalling import org.jetbrains.kotlin.idea.inspections.collections.isCalling
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArgumentsIntention import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeArgumentsIntention
import org.jetbrains.kotlin.idea.intentions.RemoveExplicitTypeIntention
import org.jetbrains.kotlin.idea.intentions.addUseSiteTarget import org.jetbrains.kotlin.idea.intentions.addUseSiteTarget
import org.jetbrains.kotlin.idea.intentions.branchedTransformations.BranchedFoldingUtils
import org.jetbrains.kotlin.idea.quickfix.AddConstModifierFix
import org.jetbrains.kotlin.idea.references.KtSimpleNameReference import org.jetbrains.kotlin.idea.references.KtSimpleNameReference
import org.jetbrains.kotlin.idea.references.mainReference import org.jetbrains.kotlin.idea.references.mainReference
import org.jetbrains.kotlin.idea.references.readWriteAccess import org.jetbrains.kotlin.idea.references.readWriteAccess
@@ -341,3 +344,130 @@ class MoveGetterAndSetterAnnotationsToPropertyProcessing : ApplicabilityBasedIns
} }
} }
} }
class RedundantExplicitTypeInspectionBasedProcessing : ApplicabilityBasedInspectionLikeProcessing<KtProperty>(KtProperty::class) {
override fun isApplicableTo(element: KtProperty, settings: ConverterSettings?): Boolean =
RedundantExplicitTypeInspection.hasRedundantType(element)
override fun apply(element: KtProperty) {
RemoveExplicitTypeIntention.removeExplicitType(element)
}
}
class CanBeValInspectionBasedProcessing : ApplicabilityBasedInspectionLikeProcessing<KtDeclaration>(KtDeclaration::class) {
override fun isApplicableTo(element: KtDeclaration, settings: ConverterSettings?): Boolean =
CanBeValInspection.canBeVal(element, ignoreNotUsedVals = false)
override fun apply(element: KtDeclaration) {
if (element !is KtValVarKeywordOwner) return
element.valOrVarKeyword?.replace(KtPsiFactory(element).createValKeyword())
}
}
class MayBeConstantInspectionBasedProcessing : ApplicabilityBasedInspectionLikeProcessing<KtProperty>(KtProperty::class) {
override fun isApplicableTo(element: KtProperty, settings: ConverterSettings?): Boolean =
with(MayBeConstantInspection) {
val status = element.getStatus()
status == MayBeConstantInspection.Status.MIGHT_BE_CONST
|| status == MayBeConstantInspection.Status.JVM_FIELD_MIGHT_BE_CONST
}
override fun apply(element: KtProperty) {
AddConstModifierFix.addConstModifier(element)
}
}
class RemoveExplicitUnitTypeProcessing : ApplicabilityBasedInspectionLikeProcessing<KtNamedFunction>(KtNamedFunction::class) {
override fun isApplicableTo(element: KtNamedFunction, settings: ConverterSettings?): Boolean {
val typeReference = element.typeReference?.typeElement ?: return false
if (!typeReference.textMatches("Unit")) return false
return RedundantUnitReturnTypeInspection.hasRedundantUnitReturnType(element)
}
override fun apply(element: KtNamedFunction) {
RemoveExplicitTypeIntention.removeExplicitType(element)
}
}
class RemoveExplicitGetterInspectionBasedProcessing :
ApplicabilityBasedInspectionLikeProcessing<KtPropertyAccessor>(KtPropertyAccessor::class) {
override fun isApplicableTo(element: KtPropertyAccessor, settings: ConverterSettings?): Boolean =
element.isRedundantGetter()
override fun apply(element: KtPropertyAccessor) {
RemoveRedundantGetterFix.removeRedundantGetter(element)
}
}
class RemoveExplicitSetterInspectionBasedProcessing :
ApplicabilityBasedInspectionLikeProcessing<KtPropertyAccessor>(KtPropertyAccessor::class) {
override fun isApplicableTo(element: KtPropertyAccessor, settings: ConverterSettings?): Boolean =
element.isRedundantSetter()
override fun apply(element: KtPropertyAccessor) {
RemoveRedundantSetterFix.removeRedundantSetter(element)
}
}
class RedundantSemicolonInspectionBasedProcessing :
ApplicabilityBasedInspectionLikeProcessing<PsiElement>(PsiElement::class) {
override fun isApplicableTo(element: PsiElement, settings: ConverterSettings?): Boolean =
element.node.elementType == KtTokens.SEMICOLON
&& RedundantSemicolonInspection.isRedundantSemicolon(element)
override fun apply(element: PsiElement) {
element.delete()
}
}
class RedundantCompanionReferenceInspectionBasedProcessing :
ApplicabilityBasedInspectionLikeProcessing<KtReferenceExpression>(KtReferenceExpression::class) {
override fun isApplicableTo(element: KtReferenceExpression, settings: ConverterSettings?): Boolean =
RedundantCompanionReferenceInspection.isRedundantCompanionReference(element)
override fun apply(element: KtReferenceExpression) {
RemoveRedundantCompanionReferenceFix.removeRedundantCompanionReference(element)
}
}
class ExplicitThisInspectionBasedProcessing :
ApplicabilityBasedInspectionLikeProcessing<KtExpression>(KtExpression::class) {
override fun isApplicableTo(element: KtExpression, settings: ConverterSettings?): Boolean =
ExplicitThisInspection.hasExplicitThis(element)
override fun apply(element: KtExpression) {
ExplicitThisExpressionFix.removeExplicitThisExpression(
with(ExplicitThisInspection) {
element.thisAsReceiverOrNull() ?: return
}
)
}
}
class LiftReturnInspectionBasedProcessing :
ApplicabilityBasedInspectionLikeProcessing<KtExpression>(KtExpression::class) {
override fun isApplicableTo(element: KtExpression, settings: ConverterSettings?): Boolean =
LiftReturnOrAssignmentInspection.getState(element, false)?.any {
it.liftType == LiftReturnOrAssignmentInspection.Companion.LiftType.LIFT_RETURN_OUT
} ?: false
override fun apply(element: KtExpression) {
BranchedFoldingUtils.foldToReturn(element)
}
}
class LiftAssignmentInspectionBasedProcessing :
ApplicabilityBasedInspectionLikeProcessing<KtExpression>(KtExpression::class) {
override fun isApplicableTo(element: KtExpression, settings: ConverterSettings?): Boolean =
LiftReturnOrAssignmentInspection.getState(element, false)?.any {
it.liftType == LiftReturnOrAssignmentInspection.Companion.LiftType.LIFT_ASSIGNMENT_OUT
} ?: false
override fun apply(element: KtExpression) {
BranchedFoldingUtils.foldToAssignment(element)
}
}
+4 -4
View File
@@ -1,13 +1,13 @@
fun foo(i: Int, j: Int): String { fun foo(i: Int, j: Int): String {
when (i) { return when (i) {
0 -> { 0 -> {
when (j) { when (j) {
1 -> return "0, 1" 1 -> return "0, 1"
2 -> return "0, 2" 2 -> return "0, 2"
} }
return "1, x" "1, x"
} }
1 -> return "1, x" 1 -> "1, x"
else -> return "x, x" else -> "x, x"
} }
} }