From 78fd56bdf0d4ca6dc2a2a59b232ee17e78521301 Mon Sep 17 00:00:00 2001 From: Ilya Kirillov Date: Tue, 30 Apr 2019 15:55:08 +0300 Subject: [PATCH] New J2K: Fix running some post processings in incorrect thread #KT-31234 fixed --- .../jetbrains/kotlin/nj2k/J2kPostProcessor.kt | 53 +++++++++++-------- .../kotlin/nj2k/NewJavaToKotlinConverter.kt | 4 +- 2 files changed, 32 insertions(+), 25 deletions(-) diff --git a/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/J2kPostProcessor.kt b/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/J2kPostProcessor.kt index 933a2bc4dd7..e66afcfc7d7 100644 --- a/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/J2kPostProcessor.kt +++ b/nj2k/nj2k-services/src/org/jetbrains/kotlin/nj2k/J2kPostProcessor.kt @@ -18,6 +18,7 @@ package org.jetbrains.kotlin.nj2k import com.intellij.openapi.application.ApplicationManager import com.intellij.openapi.application.ModalityState +import com.intellij.openapi.command.CommandProcessor import com.intellij.openapi.editor.RangeMarker import com.intellij.openapi.util.TextRange import com.intellij.psi.PsiElement @@ -73,21 +74,22 @@ class NewJ2kPostProcessor(private val formatCode: Boolean) : PostProcessor { collectAvailableActions(this, file, converterContext, rangeMarker) } withContext(EDT) { - for ((element, action, _, writeActionNeeded) in elementToActions) { - if (element.isValid) { - if (writeActionNeeded) { - runWriteAction { + CommandProcessor.getInstance().runUndoTransparentAction { + for ((element, action, _, writeActionNeeded) in elementToActions) { + if (element.isValid) { + if (writeActionNeeded) { + runWriteAction { + runAction(action, element) + } + } else { runAction(action, element) } } else { - runAction(action, element) + modificationStamp = null } - } else { - modificationStamp = null } } } - return modificationStamp != file.modificationStamp && elementToActions.isNotEmpty() } @@ -125,31 +127,38 @@ class NewJ2kPostProcessor(private val formatCode: Boolean) : PostProcessor { ).fixNullability(AnalysisScope(file, rangeMarker)) } withContext(EDT) { - runWriteAction { - if (rangeMarker != null) { - ShortenReferences.DEFAULT.process(file, rangeMarker.startOffset, rangeMarker.endOffset) - } else { - ShortenReferences.DEFAULT.process(file) + CommandProcessor.getInstance().runUndoTransparentAction { + runWriteAction { + if (rangeMarker != null) { + ShortenReferences.DEFAULT.process(file, rangeMarker.startOffset, rangeMarker.endOffset) + } else { + ShortenReferences.DEFAULT.process(file) + } + } } } NewJ2KPostProcessingRegistrar.mainProcessings.runProcessings(file, converterContext as NewJ2kConverterContext, rangeMarker) withContext(EDT) { - runWriteAction { - file.commitAndUnblockDocument() + CommandProcessor.getInstance().runUndoTransparentAction { + runWriteAction { + file.commitAndUnblockDocument() + } } } withContext(EDT) { runWriteAction { - val codeStyleManager = CodeStyleManager.getInstance(file.project) - if (rangeMarker != null) { - if (rangeMarker.isValid) { - codeStyleManager.reformatRange(file, rangeMarker.startOffset, rangeMarker.endOffset) + CommandProcessor.getInstance().runUndoTransparentAction { + val codeStyleManager = CodeStyleManager.getInstance(file.project) + if (rangeMarker != null) { + if (rangeMarker.isValid) { + codeStyleManager.reformatRange(file, rangeMarker.startOffset, rangeMarker.endOffset) + } + } else { + codeStyleManager.reformat(file) } - } else { - codeStyleManager.reformat(file) + Unit } - Unit } } } diff --git a/nj2k/src/org/jetbrains/kotlin/nj2k/NewJavaToKotlinConverter.kt b/nj2k/src/org/jetbrains/kotlin/nj2k/NewJavaToKotlinConverter.kt index a7ff6604087..522ce1833bf 100644 --- a/nj2k/src/org/jetbrains/kotlin/nj2k/NewJavaToKotlinConverter.kt +++ b/nj2k/src/org/jetbrains/kotlin/nj2k/NewJavaToKotlinConverter.kt @@ -72,9 +72,7 @@ class NewJavaToKotlinConverter( } } - CommandProcessor.getInstance().runUndoTransparentAction { - AfterConversionPass(project, postProcessor).run(kotlinFile, context, range = null) - } + AfterConversionPass(project, postProcessor).run(kotlinFile, context, range = null) kotlinFile.text } catch (e: ProcessCanceledException) {