diff --git a/jvm/jvm-analysis-quickFix/src/com/intellij/jvm/analysis/quickFix/CompositeModCommandQuickFix.kt b/jvm/jvm-analysis-quickFix/src/com/intellij/jvm/analysis/quickFix/CompositeModCommandQuickFix.kt index 7d32912e085a..7c1022786c8f 100644 --- a/jvm/jvm-analysis-quickFix/src/com/intellij/jvm/analysis/quickFix/CompositeModCommandQuickFix.kt +++ b/jvm/jvm-analysis-quickFix/src/com/intellij/jvm/analysis/quickFix/CompositeModCommandQuickFix.kt @@ -5,7 +5,6 @@ import com.intellij.codeInsight.intention.IntentionAction import com.intellij.lang.jvm.JvmModifiersOwner import com.intellij.modcommand.ActionContext import com.intellij.modcommand.ModCommandExecutor -import com.intellij.modcommand.ModUpdateFileText import com.intellij.modcommand.PsiUpdateModCommandQuickFix import com.intellij.openapi.project.Project import com.intellij.psi.PsiDocumentManager @@ -39,17 +38,8 @@ abstract class CompositeModCommandQuickFix : PsiUpdateModCommandQuickFix() { val manager = PsiDocumentManager.getInstance(project) manager.doPostponedOperationsAndUnblockDocument(document) if (modCommandAction != null) { - for (command in modCommandAction.perform(ActionContext.from(null, containingFile)).unpack()) { - if (command is ModUpdateFileText) { - if (command.file != containingFile.originalFile.virtualFile) { - throw UnsupportedOperationException("The command ${action.familyName} updates non-current file") - } - ModCommandExecutor.getInstance().updateText(project, document, command) - } - else { - throw UnsupportedOperationException("Unexpected command performed by ${action.familyName}: " + command) - } - } + ModCommandExecutor.getInstance() + .executeForFileCopy(modCommandAction.perform(ActionContext.from(null, containingFile)), containingFile) } else { action.invoke(project, null, containingFile) diff --git a/platform/analysis-api/src/com/intellij/modcommand/ModCommandExecutor.java b/platform/analysis-api/src/com/intellij/modcommand/ModCommandExecutor.java index f42ba55bb495..cca0e91e504f 100644 --- a/platform/analysis-api/src/com/intellij/modcommand/ModCommandExecutor.java +++ b/platform/analysis-api/src/com/intellij/modcommand/ModCommandExecutor.java @@ -7,6 +7,7 @@ import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.Editor; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.NlsContexts; +import com.intellij.psi.PsiFile; import com.intellij.util.concurrency.annotations.RequiresEdt; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nls; @@ -40,17 +41,13 @@ public interface ModCommandExecutor { @NotNull BatchExecutionResult executeInBatch(@NotNull ActionContext context, @NotNull ModCommand command); /** - * Apply {@link ModUpdateFileText} command to specific document. The caller must ensure that the document is writable. - * This API is not intended to be used outside the platform and may change without notice. - * - * @param project current project - * @param document document to apply changes to - * @param updateTextCommand command to apply - * @throws IllegalStateException if the current document text does not match the expected + * Apply a command for non-physical file copy. + * + * @param command command to apply + * @param file a non-physical file copy to apply the command to + * @throws UnsupportedOperationException if the command does something except modifying the specified file */ - @ApiStatus.Internal - void updateText(@NotNull Project project, @NotNull Document document, @NotNull ModUpdateFileText updateTextCommand) - throws IllegalStateException; + void executeForFileCopy(@NotNull ModCommand command, @NotNull PsiFile file) throws UnsupportedOperationException; /** * @return an instance of this service diff --git a/platform/lang-impl/src/com/intellij/lang/impl/modcommand/ModCommandExecutorImpl.java b/platform/lang-impl/src/com/intellij/lang/impl/modcommand/ModCommandExecutorImpl.java index c860f9e8dcb6..bc80893993ce 100644 --- a/platform/lang-impl/src/com/intellij/lang/impl/modcommand/ModCommandExecutorImpl.java +++ b/platform/lang-impl/src/com/intellij/lang/impl/modcommand/ModCommandExecutorImpl.java @@ -662,7 +662,21 @@ public class ModCommandExecutorImpl implements ModCommandExecutor { } @Override - public void updateText(@NotNull Project project, @NotNull Document document, @NotNull ModUpdateFileText upd) + public void executeForFileCopy(@NotNull ModCommand command, @NotNull PsiFile file) { + for (ModCommand cmd : command.unpack()) { + if (cmd instanceof ModUpdateFileText updateFileText) { + if (!updateFileText.file().equals(file.getOriginalFile().getVirtualFile())) { + throw new UnsupportedOperationException("The command updates non-current file"); + } + updateText(file.getProject(), file.getViewProvider().getDocument(), updateFileText); + } + else if (!(cmd instanceof ModNavigate) && !(cmd instanceof ModHighlight)) { + throw new UnsupportedOperationException("Unexpected command: " + command); + } + } + } + + private void updateText(@NotNull Project project, @NotNull Document document, @NotNull ModUpdateFileText upd) throws IllegalStateException { String oldText = upd.oldText(); if (!document.getText().equals(oldText)) {