New J2K: Fix grouping post processing

This commit is contained in:
Ilya Kirillov
2018-12-18 14:28:55 +03:00
committed by Ilya Kirillov
parent 763ea3480d
commit d75549f443
@@ -59,12 +59,28 @@ class J2kPostProcessor(
PROCESS PROCESS
} }
override fun doAdditionalProcessing(file: KtFile, rangeMarker: RangeMarker?) =
runBlocking(EDT.ModalityStateElement(ModalityState.defaultModalityState())) { override fun doAdditionalProcessing(file: KtFile, rangeMarker: RangeMarker?) {
fun Processing.flattenToGroups(): List<List<J2kPostProcessing>> =
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 { do {
var modificationStamp: Long? = file.modificationStamp var modificationStamp: Long? = file.modificationStamp
val elementToActions = runReadAction { val elementToActions = runReadAction {
collectAvailableActions(file, rangeMarker) collectAvailableActions(processings, file, rangeMarker)
} }
withContext(EDT) { withContext(EDT) {
@@ -74,44 +90,45 @@ class J2kPostProcessor(
runWriteAction { runWriteAction {
action() action()
} }
} } else {
else {
action() action()
} }
} } else {
else {
modificationStamp = null modificationStamp = null
} }
} }
} }
if (modificationStamp == file.modificationStamp) break if (modificationStamp == file.modificationStamp) break
} } while (elementToActions.isNotEmpty())
while (elementToActions.isNotEmpty()) }
if (formatCode) {
if (formatCode) { withContext(EDT) {
withContext(EDT) { runWriteAction {
runWriteAction { val codeStyleManager = CodeStyleManager.getInstance(file.project)
val codeStyleManager = CodeStyleManager.getInstance(file.project) if (rangeMarker != null) {
if (rangeMarker != null) { if (rangeMarker.isValid) {
if (rangeMarker.isValid) { codeStyleManager.reformatRange(file, rangeMarker.startOffset, rangeMarker.endOffset)
codeStyleManager.reformatRange(file, rangeMarker.startOffset, rangeMarker.endOffset)
}
} }
else { } else {
codeStyleManager.reformat(file) codeStyleManager.reformat(file)
}
Unit
} }
Unit
} }
} }
} }
}
}
private data class ActionData(val element: KtElement, val action: () -> Unit, val priority: Int, val writeActionNeeded: Boolean) private data class ActionData(val element: KtElement, val action: () -> Unit, val priority: Int, val writeActionNeeded: Boolean)
private fun collectAvailableActions(file: KtFile, rangeMarker: RangeMarker?): List<ActionData> { private fun collectAvailableActions(
processings: Collection<J2kPostProcessing>,
file: KtFile,
rangeMarker: RangeMarker?
): List<ActionData> {
val diagnostics = analyzeFileRange(file, rangeMarker) val diagnostics = analyzeFileRange(file, rangeMarker)
val availableActions = ArrayList<ActionData>() val availableActions = ArrayList<ActionData>()
@@ -125,12 +142,16 @@ class J2kPostProcessor(
super.visitElement(element) super.visitElement(element)
if (rangeResult == RangeFilterResult.PROCESS) { if (rangeResult == RangeFilterResult.PROCESS) {
postProcessingRegistrar.processings.forEach { processing -> processings.forEach { processing ->
val action = processing.createAction(element, diagnostics) val action = processing.createAction(element, diagnostics)
if (action != null) { if (action != null) {
availableActions.add(ActionData(element, action, availableActions.add(
postProcessingRegistrar.priority(processing), ActionData(
processing.writeActionNeeded)) element, action,
postProcessingRegistrar.priority(processing),
processing.writeActionNeeded
)
)
} }
} }
} }