diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java b/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java index f59a387a9fb4..19f37c372305 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatProcessor.java @@ -223,9 +223,7 @@ class FormatProcessor { return; } - while (myCurrentState.getStateId() == FormattingStateId.PROCESSING_BLOCKS && !myCurrentState.isDone()) { - myCurrentState.iteration(); - } + doIterationsSynchronously(FormattingStateId.PROCESSING_BLOCKS); } private void reset() { @@ -246,15 +244,27 @@ class FormatProcessor { assert !myDisposed; myCurrentState.setNext(new ApplyChangesState(model)); - if (!sequentially) { + if (sequentially) { return; } - while (myCurrentState.getStateId() == FormattingStateId.APPLYING_CHANGES && !myCurrentState.isDone()) { + doIterationsSynchronously(FormattingStateId.APPLYING_CHANGES); + } + + /** + * Perform iterations against the {@link #myCurrentState current state} until it's {@link FormattingStateId type} + * is {@link FormattingStateId#getPreviousStates() less} or equal to the given state. + * + * @param state target state to process + */ + private void doIterationsSynchronously(@NotNull FormattingStateId state) { + while ((myCurrentState.getStateId() == state || state.getPreviousStates().contains(myCurrentState.getStateId())) + && !myCurrentState.isDone()) + { myCurrentState.iteration(); } } - + public void setJavaIndentOptions(final CodeStyleSettings.IndentOptions javaIndentOptions) { myJavaIndentOptions = javaIndentOptions; } diff --git a/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java b/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java index 052a2ca8a559..02a7cc6ff350 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormatterImpl.java @@ -214,6 +214,7 @@ public class FormatterImpl extends FormatterEx } finally { enableFormatting(); + myProgressIndicator = null; } } else { diff --git a/platform/lang-impl/src/com/intellij/formatting/FormattingProgressIndicatorImpl.java b/platform/lang-impl/src/com/intellij/formatting/FormattingProgressIndicatorImpl.java index 6916a1dcedcb..1a57f2c4a1bc 100644 --- a/platform/lang-impl/src/com/intellij/formatting/FormattingProgressIndicatorImpl.java +++ b/platform/lang-impl/src/com/intellij/formatting/FormattingProgressIndicatorImpl.java @@ -35,6 +35,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import javax.swing.*; +import java.lang.ref.WeakReference; import java.lang.reflect.InvocationTargetException; import java.util.Collection; import java.util.HashMap; @@ -81,10 +82,9 @@ public class FormattingProgressIndicatorImpl extends Task.Modal implements Forma private final Map> myCallbacks = new HashMap>(); - @Nullable - private final VirtualFile myFile; - private final Document myDocument; - private final int myFileTextLength; + private final WeakReference myFile; + private final WeakReference myDocument; + private final int myFileTextLength; @NotNull private FormattingStateId myLastState = FormattingStateId.WRAPPING_BLOCKS; @@ -98,8 +98,8 @@ public class FormattingProgressIndicatorImpl extends Task.Modal implements Forma public FormattingProgressIndicatorImpl(@Nullable Project project, @NotNull PsiFile file, @NotNull Document document) { super(project, getTitle(file), true); - myFile = file.getVirtualFile(); - myDocument = document; + myFile = new WeakReference(file.getVirtualFile()); + myDocument = new WeakReference(document); myFileTextLength = file.getTextLength(); addCallback(EventType.CANCEL, new MyCancelCallback()); } @@ -140,7 +140,10 @@ public class FormattingProgressIndicatorImpl extends Task.Modal implements Forma UIUtil.invokeAndWaitIfNeeded(new Runnable() { @Override public void run() { - myDocumentModificationStampBefore = myDocument.getModificationStamp(); + Document document = myDocument.get(); + if (document != null) { + myDocumentModificationStampBefore = document.getModificationStamp(); + } task.prepare(); } }); @@ -282,16 +285,18 @@ public class FormattingProgressIndicatorImpl extends Task.Modal implements Forma @Override public void run() { myRunning = false; - if (myFile == null || myDocumentModificationStampBefore < 0) { + VirtualFile file = myFile.get(); + Document document = myDocument.get(); + if (file == null || document == null || myDocumentModificationStampBefore < 0) { return; } - FileEditor editor = FileEditorManager.getInstance(myProject).getSelectedEditor(myFile); + FileEditor editor = FileEditorManager.getInstance(myProject).getSelectedEditor(file); if (editor == null) { return; } UndoManager manager = UndoManager.getInstance(myProject); - while (manager.isUndoAvailable(editor) && myDocument.getModificationStamp() != myDocumentModificationStampBefore) { + while (manager.isUndoAvailable(editor) && document.getModificationStamp() != myDocumentModificationStampBefore) { manager.undo(editor); } }