New J2K: group post-processings to named groups

This commit is contained in:
Ilya Kirillov
2019-06-10 09:46:16 +03:00
parent cacb4ed9f3
commit 110611d690
6 changed files with 230 additions and 209 deletions
@@ -67,10 +67,12 @@ class NewJ2kPostProcessor : PostProcessor {
onPhaseChanged: ((Int, String) -> Unit)? onPhaseChanged: ((Int, String) -> Unit)?
) { ) {
runBlocking(EDT.ModalityStateElement(ModalityState.defaultModalityState())) { runBlocking(EDT.ModalityStateElement(ModalityState.defaultModalityState())) {
for ((i, processing) in processings.withIndex()) { for ((i, group) in processings.withIndex()) {
onPhaseChanged?.invoke(i, processing.description) onPhaseChanged?.invoke(i + 1, group.description)
processing.runProcessing(file, rangeMarker, converterContext as NewJ2kConverterContext) for (processing in group.processings) {
commitFile(file) processing.runProcessing(file, rangeMarker, converterContext as NewJ2kConverterContext)
commitFile(file)
}
} }
} }
} }
@@ -86,199 +88,221 @@ class NewJ2kPostProcessor : PostProcessor {
} }
} }
private val processings: List<GeneralPostProcessing> = listOf( private val processings: List<NamedPostProcessingGroup> = listOf(
nullabilityProcessing, NamedPostProcessingGroup(
formatCodeProcessing, "Inferring declarations nullability",
shortenReferencesProcessing, listOf(nullabilityProcessing)
InspectionLikeProcessingGroup(ConvertGettersAndSettersToPropertyProcessing.DESCRIPTION, VarToValProcessing()),
ConvertGettersAndSettersToPropertyProcessing(),
InspectionLikeProcessingGroup(
ConvertGettersAndSettersToPropertyProcessing.DESCRIPTION,
MoveGetterAndSetterAnnotationsToPropertyProcessing()
), ),
InspectionLikeProcessingGroup( NamedPostProcessingGroup(
ConvertGettersAndSettersToPropertyProcessing.DESCRIPTION, "Formatting code",
generalInspectionBasedProcessing(RedundantGetterInspection()), listOf(formatCodeProcessing)
generalInspectionBasedProcessing(RedundantSetterInspection())
), ),
ConvertToDataClassProcessing(), NamedPostProcessingGroup(
InspectionLikeProcessingGroup( "Shortening fully-qualified references",
listOf(shortenReferencesProcessing)
),
NamedPostProcessingGroup(
"Converting POJOs to data classes",
listOf(
InspectionLikeProcessingGroup(VarToValProcessing()),
ConvertGettersAndSettersToPropertyProcessing(),
InspectionLikeProcessingGroup(MoveGetterAndSetterAnnotationsToPropertyProcessing()),
InspectionLikeProcessingGroup(
generalInspectionBasedProcessing(RedundantGetterInspection()),
generalInspectionBasedProcessing(RedundantSetterInspection())
),
ConvertToDataClassProcessing()
)
),
NamedPostProcessingGroup(
"Cleaning up Kotlin code", "Cleaning up Kotlin code",
RemoveRedundantVisibilityModifierProcessing(), listOf(
RemoveRedundantModalityModifierProcessing(), InspectionLikeProcessingGroup(
RemoveRedundantConstructorKeywordProcessing(), RemoveRedundantVisibilityModifierProcessing(),
diagnosticBasedProcessing(Errors.REDUNDANT_OPEN_IN_INTERFACE) { element: KtDeclaration, _ -> RemoveRedundantModalityModifierProcessing(),
element.removeModifier(KtTokens.OPEN_KEYWORD) RemoveRedundantConstructorKeywordProcessing(),
}, diagnosticBasedProcessing(Errors.REDUNDANT_OPEN_IN_INTERFACE) { element: KtDeclaration, _ ->
RemoveExplicitOpenInInterfaceProcessing(), element.removeModifier(KtTokens.OPEN_KEYWORD)
generalInspectionBasedProcessing(ExplicitThisInspection()), },
RemoveExplicitTypeArgumentsProcessing(), RemoveExplicitOpenInInterfaceProcessing(),
RemoveRedundantOverrideVisibilityProcessing(), generalInspectionBasedProcessing(ExplicitThisInspection()),
inspectionBasedProcessing(MoveLambdaOutsideParenthesesInspection()), RemoveExplicitTypeArgumentsProcessing(),
generalInspectionBasedProcessing(RedundantCompanionReferenceInspection()), RemoveRedundantOverrideVisibilityProcessing(),
FixObjectStringConcatenationProcessing(), inspectionBasedProcessing(MoveLambdaOutsideParenthesesInspection()),
ConvertToStringTemplateProcessing(), generalInspectionBasedProcessing(RedundantCompanionReferenceInspection()),
UsePropertyAccessSyntaxProcessing(), FixObjectStringConcatenationProcessing(),
UninitializedVariableReferenceFromInitializerToThisReferenceProcessing(), ConvertToStringTemplateProcessing(),
UnresolvedVariableReferenceFromInitializerToThisReferenceProcessing(), UsePropertyAccessSyntaxProcessing(),
RemoveRedundantSamAdaptersProcessing(), UninitializedVariableReferenceFromInitializerToThisReferenceProcessing(),
RemoveRedundantCastToNullableProcessing(), UnresolvedVariableReferenceFromInitializerToThisReferenceProcessing(),
inspectionBasedProcessing(ReplacePutWithAssignmentInspection()), RemoveRedundantSamAdaptersProcessing(),
UseExpressionBodyProcessing(), RemoveRedundantCastToNullableProcessing(),
inspectionBasedProcessing(UnnecessaryVariableInspection()), inspectionBasedProcessing(ReplacePutWithAssignmentInspection()),
RemoveExplicitPropertyTypeWithInspectionProcessing(), UseExpressionBodyProcessing(),
generalInspectionBasedProcessing(RedundantUnitReturnTypeInspection()), inspectionBasedProcessing(UnnecessaryVariableInspection()),
JavaObjectEqualsToEqOperatorProcessing(), RemoveExplicitPropertyTypeWithInspectionProcessing(),
RemoveExplicitPropertyTypeProcessing(), generalInspectionBasedProcessing(RedundantUnitReturnTypeInspection()),
RemoveRedundantNullabilityProcessing(), JavaObjectEqualsToEqOperatorProcessing(),
generalInspectionBasedProcessing(CanBeValInspection(ignoreNotUsedVals = false)), RemoveExplicitPropertyTypeProcessing(),
inspectionBasedProcessing(FoldInitializerAndIfToElvisInspection()), RemoveRedundantNullabilityProcessing(),
generalInspectionBasedProcessing(RedundantSemicolonInspection()), generalInspectionBasedProcessing(CanBeValInspection(ignoreNotUsedVals = false)),
intentionBasedProcessing(RemoveEmptyClassBodyIntention()), inspectionBasedProcessing(FoldInitializerAndIfToElvisInspection()),
intentionBasedProcessing( generalInspectionBasedProcessing(RedundantSemicolonInspection()),
RemoveRedundantCallsOfConversionMethodsIntention() intentionBasedProcessing(RemoveEmptyClassBodyIntention()),
), intentionBasedProcessing(
inspectionBasedProcessing(JavaMapForEachInspection()), RemoveRedundantCallsOfConversionMethodsIntention()
),
inspectionBasedProcessing(JavaMapForEachInspection()),
intentionBasedProcessing(FoldIfToReturnIntention()) { it.then.isTrivialStatementBody() && it.`else`.isTrivialStatementBody() }, intentionBasedProcessing(FoldIfToReturnIntention()) { it.then.isTrivialStatementBody() && it.`else`.isTrivialStatementBody() },
intentionBasedProcessing(FoldIfToReturnAsymmetricallyIntention()) { intentionBasedProcessing(FoldIfToReturnAsymmetricallyIntention()) {
it.then.isTrivialStatementBody() && (KtPsiUtil.skipTrailingWhitespacesAndComments( it.then.isTrivialStatementBody() && (KtPsiUtil.skipTrailingWhitespacesAndComments(
it it
) as KtReturnExpression).returnedExpression.isTrivialStatementBody() ) as KtReturnExpression).returnedExpression.isTrivialStatementBody()
}, },
inspectionBasedProcessing(IfThenToSafeAccessInspection()), inspectionBasedProcessing(IfThenToSafeAccessInspection()),
inspectionBasedProcessing(IfThenToSafeAccessInspection()), inspectionBasedProcessing(IfThenToSafeAccessInspection()),
inspectionBasedProcessing(IfThenToElvisInspection(true)), inspectionBasedProcessing(IfThenToElvisInspection(true)),
inspectionBasedProcessing(SimplifyNegatedBinaryExpressionInspection()), inspectionBasedProcessing(SimplifyNegatedBinaryExpressionInspection()),
inspectionBasedProcessing(ReplaceGetOrSetInspection()), inspectionBasedProcessing(ReplaceGetOrSetInspection()),
inspectionBasedProcessing(AddOperatorModifierInspection()), inspectionBasedProcessing(AddOperatorModifierInspection()),
intentionBasedProcessing(ObjectLiteralToLambdaIntention()), intentionBasedProcessing(ObjectLiteralToLambdaIntention()),
intentionBasedProcessing(AnonymousFunctionToLambdaIntention()), intentionBasedProcessing(AnonymousFunctionToLambdaIntention()),
intentionBasedProcessing(RemoveUnnecessaryParenthesesIntention()), intentionBasedProcessing(RemoveUnnecessaryParenthesesIntention()),
intentionBasedProcessing(DestructureIntention()), intentionBasedProcessing(DestructureIntention()),
inspectionBasedProcessing(SimplifyAssertNotNullInspection()), inspectionBasedProcessing(SimplifyAssertNotNullInspection()),
intentionBasedProcessing( intentionBasedProcessing(
RemoveRedundantCallsOfConversionMethodsIntention() RemoveRedundantCallsOfConversionMethodsIntention()
), ),
generalInspectionBasedProcessing(LiftReturnOrAssignmentInspection(skipLongExpressions = false)), generalInspectionBasedProcessing(LiftReturnOrAssignmentInspection(skipLongExpressions = false)),
generalInspectionBasedProcessing(MayBeConstantInspection()), generalInspectionBasedProcessing(MayBeConstantInspection()),
intentionBasedProcessing(RemoveEmptyPrimaryConstructorIntention()), intentionBasedProcessing(RemoveEmptyPrimaryConstructorIntention()),
diagnosticBasedProcessing(Errors.PLATFORM_CLASS_MAPPED_TO_KOTLIN) { element: KtDotQualifiedExpression, _ -> diagnosticBasedProcessing(Errors.PLATFORM_CLASS_MAPPED_TO_KOTLIN) { element: KtDotQualifiedExpression, _ ->
val parent = element.parent as? KtImportDirective ?: return@diagnosticBasedProcessing val parent = element.parent as? KtImportDirective ?: return@diagnosticBasedProcessing
parent.delete() parent.delete()
}, },
diagnosticBasedProcessing( diagnosticBasedProcessing(
Errors.UNSAFE_CALL, Errors.UNSAFE_CALL,
Errors.UNSAFE_INFIX_CALL, Errors.UNSAFE_INFIX_CALL,
Errors.UNSAFE_OPERATOR_CALL Errors.UNSAFE_OPERATOR_CALL
) { element: PsiElement, diagnostic -> ) { element: PsiElement, diagnostic ->
val action = val action =
AddExclExclCallFix.createActions(diagnostic).singleOrNull() AddExclExclCallFix.createActions(diagnostic).singleOrNull()
?: return@diagnosticBasedProcessing ?: return@diagnosticBasedProcessing
action.invoke(element.project, null, element.containingFile) action.invoke(element.project, null, element.containingFile)
}, },
diagnosticBasedProcessingWithFixFactory( diagnosticBasedProcessingWithFixFactory(
MissingIteratorExclExclFixFactory, MissingIteratorExclExclFixFactory,
Errors.ITERATOR_ON_NULLABLE Errors.ITERATOR_ON_NULLABLE
), ),
diagnosticBasedProcessingWithFixFactory( diagnosticBasedProcessingWithFixFactory(
SmartCastImpossibleExclExclFixFactory, SmartCastImpossibleExclExclFixFactory,
Errors.SMARTCAST_IMPOSSIBLE Errors.SMARTCAST_IMPOSSIBLE
), ),
diagnosticBasedProcessing(Errors.TYPE_MISMATCH) { element: PsiElement, diagnostic -> diagnosticBasedProcessing(Errors.TYPE_MISMATCH) { element: PsiElement, diagnostic ->
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val diagnosticWithParameters = val diagnosticWithParameters =
diagnostic as? DiagnosticWithParameters2<KtExpression, KotlinType, KotlinType> diagnostic as? DiagnosticWithParameters2<KtExpression, KotlinType, KotlinType>
?: return@diagnosticBasedProcessing ?: return@diagnosticBasedProcessing
val expectedType = diagnosticWithParameters.a val expectedType = diagnosticWithParameters.a
val realType = diagnosticWithParameters.b val realType = diagnosticWithParameters.b
when { when {
realType.makeNotNullable().isSubtypeOf(expectedType.makeNotNullable()) realType.makeNotNullable().isSubtypeOf(expectedType.makeNotNullable())
&& realType.isNullable() && realType.isNullable()
&& !expectedType.isNullable() && !expectedType.isNullable()
-> { -> {
val factory = KtPsiFactory(element) val factory = KtPsiFactory(element)
element.replace(factory.createExpressionByPattern("($0)!!", element.text)) element.replace(factory.createExpressionByPattern("($0)!!", element.text))
} }
element is KtExpression element is KtExpression
&& realType.isSignedOrUnsignedNumberType() && realType.isSignedOrUnsignedNumberType()
&& expectedType.isSignedOrUnsignedNumberType() -> { && expectedType.isSignedOrUnsignedNumberType() -> {
val fix = NumberConversionFix(element, expectedType, disableIfAvailable = null) val fix = NumberConversionFix(element, expectedType, disableIfAvailable = null)
fix.invoke(element.project, null, element.containingFile) fix.invoke(element.project, null, element.containingFile)
} }
}
},
diagnosticBasedProcessingWithFixFactory(
ReplacePrimitiveCastWithNumberConversionFix,
Errors.CAST_NEVER_SUCCEEDS
),
diagnosticBasedProcessingWithFixFactory(
ChangeCallableReturnTypeFix.ReturnTypeMismatchOnOverrideFactory,
Errors.RETURN_TYPE_MISMATCH_ON_OVERRIDE
),
diagnosticBasedProcessing<KtBinaryExpressionWithTypeRHS>(Errors.USELESS_CAST) { element, _ ->
if (element.left.isNullExpression()) return@diagnosticBasedProcessing
val expression = RemoveUselessCastFix.invoke(element)
val variable = expression.parent as? KtProperty
if (variable != null && expression == variable.initializer && variable.isLocal) {
val ref = ReferencesSearch.search(variable, LocalSearchScope(variable.containingFile)).findAll().singleOrNull()
if (ref != null && ref.element is KtSimpleNameExpression) {
ref.element.replace(expression)
variable.delete()
}
}
},
diagnosticBasedProcessingWithFixFactory(
RemoveModifierFix.createRemoveProjectionFactory(true),
Errors.REDUNDANT_PROJECTION
),
diagnosticBasedProcessingWithFixFactory(
AddModifierFix.createFactory(KtTokens.OVERRIDE_KEYWORD),
Errors.VIRTUAL_MEMBER_HIDDEN
),
diagnosticBasedProcessingWithFixFactory(
RemoveModifierFix.createRemoveModifierFromListOwnerFactory(KtTokens.OPEN_KEYWORD),
Errors.NON_FINAL_MEMBER_IN_FINAL_CLASS, Errors.NON_FINAL_MEMBER_IN_OBJECT
),
diagnosticBasedProcessingWithFixFactory(
MakeVisibleFactory,
Errors.INVISIBLE_MEMBER
),
diagnosticBasedProcessingFactory(
Errors.VAL_REASSIGNMENT, Errors.CAPTURED_VAL_INITIALIZATION, Errors.CAPTURED_MEMBER_VAL_INITIALIZATION
) { element: KtSimpleNameExpression, _: Diagnostic ->
val property = element.mainReference.resolve() as? KtProperty
if (property == null) {
null
} else {
val action = {
if (!property.isVar) {
property.valOrVarKeyword.replace(KtPsiFactory(element.project).createVarKeyword())
} }
} },
action
}
},
diagnosticBasedProcessing<KtSimpleNameExpression>(Errors.UNNECESSARY_NOT_NULL_ASSERTION) { element, _ -> diagnosticBasedProcessingWithFixFactory(
val exclExclExpr = element.parent as KtUnaryExpression ReplacePrimitiveCastWithNumberConversionFix,
val baseExpression = exclExclExpr.baseExpression!! Errors.CAST_NEVER_SUCCEEDS
val context = baseExpression.analyze(BodyResolveMode.PARTIAL_WITH_DIAGNOSTICS) ),
if (context.diagnostics.forElement(element).any { it.factory == Errors.UNNECESSARY_NOT_NULL_ASSERTION }) { diagnosticBasedProcessingWithFixFactory(
exclExclExpr.replace(baseExpression) ChangeCallableReturnTypeFix.ReturnTypeMismatchOnOverrideFactory,
} Errors.RETURN_TYPE_MISMATCH_ON_OVERRIDE
}, ),
RemoveForExpressionLoopParameterTypeProcessing()
diagnosticBasedProcessing<KtBinaryExpressionWithTypeRHS>(Errors.USELESS_CAST) { element, _ ->
if (element.left.isNullExpression()) return@diagnosticBasedProcessing
val expression = RemoveUselessCastFix.invoke(element)
val variable = expression.parent as? KtProperty
if (variable != null && expression == variable.initializer && variable.isLocal) {
val ref = ReferencesSearch.search(variable, LocalSearchScope(variable.containingFile)).findAll().singleOrNull()
if (ref != null && ref.element is KtSimpleNameExpression) {
ref.element.replace(expression)
variable.delete()
}
}
},
diagnosticBasedProcessingWithFixFactory(
RemoveModifierFix.createRemoveProjectionFactory(true),
Errors.REDUNDANT_PROJECTION
),
diagnosticBasedProcessingWithFixFactory(
AddModifierFix.createFactory(KtTokens.OVERRIDE_KEYWORD),
Errors.VIRTUAL_MEMBER_HIDDEN
),
diagnosticBasedProcessingWithFixFactory(
RemoveModifierFix.createRemoveModifierFromListOwnerFactory(KtTokens.OPEN_KEYWORD),
Errors.NON_FINAL_MEMBER_IN_FINAL_CLASS, Errors.NON_FINAL_MEMBER_IN_OBJECT
),
diagnosticBasedProcessingWithFixFactory(
MakeVisibleFactory,
Errors.INVISIBLE_MEMBER
),
diagnosticBasedProcessingFactory(
Errors.VAL_REASSIGNMENT, Errors.CAPTURED_VAL_INITIALIZATION, Errors.CAPTURED_MEMBER_VAL_INITIALIZATION
) { element: KtSimpleNameExpression, _: Diagnostic ->
val property = element.mainReference.resolve() as? KtProperty
if (property == null) {
null
} else {
val action = {
if (!property.isVar) {
property.valOrVarKeyword.replace(KtPsiFactory(element.project).createVarKeyword())
}
}
action
}
},
diagnosticBasedProcessing<KtSimpleNameExpression>(Errors.UNNECESSARY_NOT_NULL_ASSERTION) { element, _ ->
val exclExclExpr = element.parent as KtUnaryExpression
val baseExpression = exclExclExpr.baseExpression!!
val context = baseExpression.analyze(BodyResolveMode.PARTIAL_WITH_DIAGNOSTICS)
if (context.diagnostics.forElement(element).any { it.factory == Errors.UNNECESSARY_NOT_NULL_ASSERTION }) {
exclExclExpr.replace(baseExpression)
}
},
RemoveForExpressionLoopParameterTypeProcessing()
)
)
), ),
formatCodeProcessing, NamedPostProcessingGroup(
optimizeImportsProcessing, "Optimizing imports",
shortenReferencesProcessing listOf(
optimizeImportsProcessing,
shortenReferencesProcessing
)
),
NamedPostProcessingGroup(
"Formatting code",
listOf(formatCodeProcessing)
)
) )
@@ -41,13 +41,9 @@ import kotlin.reflect.KClass
import kotlin.reflect.full.isSubclassOf import kotlin.reflect.full.isSubclassOf
class InspectionLikeProcessingGroup( class InspectionLikeProcessingGroup(val inspectionLikeProcessings: List<InspectionLikeProcessing>) : ProcessingGroup {
override val description: String,
val inspectionLikeProcessings: List<InspectionLikeProcessing>
) : ProcessingGroup {
constructor(description: String, vararg inspectionLikeProcessings: InspectionLikeProcessing) : constructor(vararg inspectionLikeProcessings: InspectionLikeProcessing) : this(inspectionLikeProcessings.toList())
this(description, inspectionLikeProcessings.toList())
private val processingsToPriorityMap = inspectionLikeProcessings.mapToIndex() private val processingsToPriorityMap = inspectionLikeProcessings.mapToIndex()
fun priority(processing: InspectionLikeProcessing): Int = processingsToPriorityMap.getValue(processing) fun priority(processing: InspectionLikeProcessing): Int = processingsToPriorityMap.getValue(processing)
@@ -18,11 +18,10 @@ import org.jetbrains.kotlin.psi.psiUtil.elementsInRange
interface GeneralPostProcessing { interface GeneralPostProcessing {
val description: String
suspend fun runProcessing(file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext) suspend fun runProcessing(file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext)
} }
abstract class SimplePostProcessing(override val description: String) : GeneralPostProcessing { abstract class SimplePostProcessing() : GeneralPostProcessing {
final override suspend fun runProcessing(file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext) { final override suspend fun runProcessing(file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext) {
withContext(EDT) { withContext(EDT) {
CommandProcessor.getInstance().runUndoTransparentAction { CommandProcessor.getInstance().runUndoTransparentAction {
@@ -36,7 +35,7 @@ abstract class SimplePostProcessing(override val description: String) : GeneralP
abstract fun applySimpleProcessing(file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext) abstract fun applySimpleProcessing(file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext)
} }
abstract class ElementsBasedPostProcessing(description: String) : SimplePostProcessing(description) { abstract class ElementsBasedPostProcessing() : SimplePostProcessing() {
final override fun applySimpleProcessing(file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext) { final override fun applySimpleProcessing(file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext) {
val elements = val elements =
rangeMarker?.let { marker -> rangeMarker?.let { marker ->
@@ -50,12 +49,17 @@ abstract class ElementsBasedPostProcessing(description: String) : SimplePostProc
interface ProcessingGroup : GeneralPostProcessing interface ProcessingGroup : GeneralPostProcessing
data class NamedPostProcessingGroup(
val description: String,
val processings: List<GeneralPostProcessing>
)
fun postProcessing( fun postProcessing(
description: String,
action: (file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext) -> Unit action: (file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext) -> Unit
): GeneralPostProcessing = ): GeneralPostProcessing =
object : SimplePostProcessing(description) { object : SimplePostProcessing() {
override fun applySimpleProcessing(file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext) { override fun applySimpleProcessing(file: KtFile, rangeMarker: RangeMarker?, converterContext: NewJ2kConverterContext) {
action(file, rangeMarker, converterContext) action(file, rangeMarker, converterContext)
} }
} }
@@ -43,7 +43,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull
import org.jetbrains.kotlin.utils.addToStdlib.safeAs import org.jetbrains.kotlin.utils.addToStdlib.safeAs
import org.jetbrains.kotlin.utils.mapToIndex import org.jetbrains.kotlin.utils.mapToIndex
class ConvertGettersAndSettersToPropertyProcessing : ElementsBasedPostProcessing(DESCRIPTION) { class ConvertGettersAndSettersToPropertyProcessing : ElementsBasedPostProcessing() {
private fun KtNamedFunction.hasOverrides(): Boolean = private fun KtNamedFunction.hasOverrides(): Boolean =
toLightMethods().singleOrNull()?.let { lightMethod -> toLightMethods().singleOrNull()?.let { lightMethod ->
OverridingMethodsSearch.search(lightMethod).findFirst() OverridingMethodsSearch.search(lightMethod).findFirst()
@@ -525,9 +525,6 @@ class ConvertGettersAndSettersToPropertyProcessing : ElementsBasedPostProcessing
} }
} }
} }
companion object {
const val DESCRIPTION = "Converting POJOs to data classes"
}
} }
@@ -25,7 +25,7 @@ import org.jetbrains.kotlin.psi.psiUtil.containingClass
import org.jetbrains.kotlin.psi.psiUtil.visibilityModifierTypeOrDefault import org.jetbrains.kotlin.psi.psiUtil.visibilityModifierTypeOrDefault
import org.jetbrains.kotlin.types.typeUtil.makeNotNullable import org.jetbrains.kotlin.types.typeUtil.makeNotNullable
class ConvertToDataClassProcessing : ElementsBasedPostProcessing(ConvertGettersAndSettersToPropertyProcessing.DESCRIPTION) { class ConvertToDataClassProcessing : ElementsBasedPostProcessing() {
private fun KtCallableDeclaration.rename(newName: String) { private fun KtCallableDeclaration.rename(newName: String) {
val factory = KtPsiFactory(this) val factory = KtPsiFactory(this)
val escapedName = newName.escaped() val escapedName = newName.escaped()
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.psi.KtPackageDirective
import org.jetbrains.kotlin.psi.psiUtil.elementsInRange import org.jetbrains.kotlin.psi.psiUtil.elementsInRange
val formatCodeProcessing = val formatCodeProcessing =
postProcessing("Formating code") { file, rangeMarker, _ -> postProcessing { file, rangeMarker, _ ->
file.commitAndUnblockDocument() file.commitAndUnblockDocument()
val codeStyleManager = CodeStyleManager.getInstance(file.project) val codeStyleManager = CodeStyleManager.getInstance(file.project)
if (rangeMarker != null) { if (rangeMarker != null) {
@@ -35,7 +35,7 @@ val formatCodeProcessing =
} }
val nullabilityProcessing = val nullabilityProcessing =
postProcessing("Inferring declarations nullability") { file, rangeMarker, converterContext -> postProcessing { file, rangeMarker, converterContext ->
NullabilityAnalysisFacade( NullabilityAnalysisFacade(
converterContext, converterContext,
getTypeElementNullability = { nullabilityByUndefinedNullabilityComment(it, converterContext) }, getTypeElementNullability = { nullabilityByUndefinedNullabilityComment(it, converterContext) },
@@ -45,7 +45,7 @@ val nullabilityProcessing =
} }
val shortenReferencesProcessing = val shortenReferencesProcessing =
postProcessing("Shortening fully-qualified references") { file, rangeMarker, _ -> postProcessing { file, rangeMarker, _ ->
if (rangeMarker != null) { if (rangeMarker != null) {
ShortenReferences.DEFAULT.process(file, rangeMarker.startOffset, rangeMarker.endOffset) ShortenReferences.DEFAULT.process(file, rangeMarker.startOffset, rangeMarker.endOffset)
} else { } else {
@@ -54,7 +54,7 @@ val shortenReferencesProcessing =
} }
val optimizeImportsProcessing = val optimizeImportsProcessing =
postProcessing("Optimizing imports") { file, rangeMarker, _ -> postProcessing { file, rangeMarker, _ ->
val elements = if (rangeMarker != null) { val elements = if (rangeMarker != null) {
file.elementsInRange(TextRange(rangeMarker.startOffset, rangeMarker.endOffset)) file.elementsInRange(TextRange(rangeMarker.startOffset, rangeMarker.endOffset))
} else file.children.asList() } else file.children.asList()