New J2K: group post-processings to named groups
This commit is contained in:
+39
-15
@@ -67,13 +67,15 @@ 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)
|
||||||
|
for (processing in group.processings) {
|
||||||
processing.runProcessing(file, rangeMarker, converterContext as NewJ2kConverterContext)
|
processing.runProcessing(file, rangeMarker, converterContext as NewJ2kConverterContext)
|
||||||
commitFile(file)
|
commitFile(file)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private suspend fun commitFile(file: KtFile) {
|
private suspend fun commitFile(file: KtFile) {
|
||||||
withContext(EDT) {
|
withContext(EDT) {
|
||||||
@@ -86,24 +88,36 @@ 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()
|
|
||||||
),
|
),
|
||||||
|
NamedPostProcessingGroup(
|
||||||
|
"Formatting code",
|
||||||
|
listOf(formatCodeProcessing)
|
||||||
|
),
|
||||||
|
NamedPostProcessingGroup(
|
||||||
|
"Shortening fully-qualified references",
|
||||||
|
listOf(shortenReferencesProcessing)
|
||||||
|
),
|
||||||
|
NamedPostProcessingGroup(
|
||||||
|
"Converting POJOs to data classes",
|
||||||
|
listOf(
|
||||||
|
InspectionLikeProcessingGroup(VarToValProcessing()),
|
||||||
|
ConvertGettersAndSettersToPropertyProcessing(),
|
||||||
|
InspectionLikeProcessingGroup(MoveGetterAndSetterAnnotationsToPropertyProcessing()),
|
||||||
InspectionLikeProcessingGroup(
|
InspectionLikeProcessingGroup(
|
||||||
ConvertGettersAndSettersToPropertyProcessing.DESCRIPTION,
|
|
||||||
generalInspectionBasedProcessing(RedundantGetterInspection()),
|
generalInspectionBasedProcessing(RedundantGetterInspection()),
|
||||||
generalInspectionBasedProcessing(RedundantSetterInspection())
|
generalInspectionBasedProcessing(RedundantSetterInspection())
|
||||||
),
|
),
|
||||||
ConvertToDataClassProcessing(),
|
ConvertToDataClassProcessing()
|
||||||
InspectionLikeProcessingGroup(
|
)
|
||||||
|
),
|
||||||
|
NamedPostProcessingGroup(
|
||||||
"Cleaning up Kotlin code",
|
"Cleaning up Kotlin code",
|
||||||
|
listOf(
|
||||||
|
InspectionLikeProcessingGroup(
|
||||||
RemoveRedundantVisibilityModifierProcessing(),
|
RemoveRedundantVisibilityModifierProcessing(),
|
||||||
RemoveRedundantModalityModifierProcessing(),
|
RemoveRedundantModalityModifierProcessing(),
|
||||||
RemoveRedundantConstructorKeywordProcessing(),
|
RemoveRedundantConstructorKeywordProcessing(),
|
||||||
@@ -277,8 +291,18 @@ private val processings: List<GeneralPostProcessing> = listOf(
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
RemoveForExpressionLoopParameterTypeProcessing()
|
RemoveForExpressionLoopParameterTypeProcessing()
|
||||||
|
)
|
||||||
|
)
|
||||||
),
|
),
|
||||||
formatCodeProcessing,
|
NamedPostProcessingGroup(
|
||||||
|
"Optimizing imports",
|
||||||
|
listOf(
|
||||||
optimizeImportsProcessing,
|
optimizeImportsProcessing,
|
||||||
shortenReferencesProcessing
|
shortenReferencesProcessing
|
||||||
|
)
|
||||||
|
),
|
||||||
|
NamedPostProcessingGroup(
|
||||||
|
"Formatting code",
|
||||||
|
listOf(formatCodeProcessing)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
+2
-6
@@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-4
@@ -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"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -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()
|
||||||
|
|||||||
+4
-4
@@ -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()
|
||||||
|
|||||||
Reference in New Issue
Block a user