diff --git a/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessor.kt b/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessor.kt index 00f3a19512a..4790d71af75 100644 --- a/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessor.kt +++ b/idea/src/org/jetbrains/kotlin/idea/j2k/J2kPostProcessor.kt @@ -59,12 +59,28 @@ class J2kPostProcessor( PROCESS } - override fun doAdditionalProcessing(file: KtFile, rangeMarker: RangeMarker?) = - runBlocking(EDT.ModalityStateElement(ModalityState.defaultModalityState())) { + + override fun doAdditionalProcessing(file: KtFile, rangeMarker: RangeMarker?) { + fun Processing.flattenToGroups(): List> = + when (this) { + is SingleProcessing -> listOf(listOf(this.processing)) + is ProcessingGroup -> + if (processings.all { it is SingleProcessing }) { + listOf(processings.map { (it as SingleProcessing).processing }) + } else { + processings.flatMap { it.flattenToGroups() } + } + else -> error("Processing tree is corrupted") + } + + val groupsOfProcessings = postProcessingRegistrar.mainProcessings.flattenToGroups() + + runBlocking(EDT.ModalityStateElement(ModalityState.defaultModalityState())) { + for (processings in groupsOfProcessings) { do { var modificationStamp: Long? = file.modificationStamp val elementToActions = runReadAction { - collectAvailableActions(file, rangeMarker) + collectAvailableActions(processings, file, rangeMarker) } withContext(EDT) { @@ -74,44 +90,45 @@ class J2kPostProcessor( runWriteAction { action() } - } - else { + } else { action() } - } - else { + } else { modificationStamp = null } } } if (modificationStamp == file.modificationStamp) break - } - while (elementToActions.isNotEmpty()) + } while (elementToActions.isNotEmpty()) + } - - if (formatCode) { - withContext(EDT) { - runWriteAction { - val codeStyleManager = CodeStyleManager.getInstance(file.project) - if (rangeMarker != null) { - if (rangeMarker.isValid) { - codeStyleManager.reformatRange(file, rangeMarker.startOffset, rangeMarker.endOffset) - } + if (formatCode) { + withContext(EDT) { + runWriteAction { + val codeStyleManager = CodeStyleManager.getInstance(file.project) + if (rangeMarker != null) { + if (rangeMarker.isValid) { + codeStyleManager.reformatRange(file, rangeMarker.startOffset, rangeMarker.endOffset) } - else { - codeStyleManager.reformat(file) - } - Unit + } else { + codeStyleManager.reformat(file) } + Unit } } } + } + } private data class ActionData(val element: KtElement, val action: () -> Unit, val priority: Int, val writeActionNeeded: Boolean) - private fun collectAvailableActions(file: KtFile, rangeMarker: RangeMarker?): List { + private fun collectAvailableActions( + processings: Collection, + file: KtFile, + rangeMarker: RangeMarker? + ): List { val diagnostics = analyzeFileRange(file, rangeMarker) val availableActions = ArrayList() @@ -125,12 +142,16 @@ class J2kPostProcessor( super.visitElement(element) if (rangeResult == RangeFilterResult.PROCESS) { - postProcessingRegistrar.processings.forEach { processing -> + processings.forEach { processing -> val action = processing.createAction(element, diagnostics) if (action != null) { - availableActions.add(ActionData(element, action, - postProcessingRegistrar.priority(processing), - processing.writeActionNeeded)) + availableActions.add( + ActionData( + element, action, + postProcessingRegistrar.priority(processing), + processing.writeActionNeeded + ) + ) } } }