1818
1919import com .intellij .codeInsight .daemon .impl .DefaultHighlightInfoProcessor ;
2020import com .intellij .codeInsight .daemon .impl .ProgressableTextEditorHighlightingPass ;
21+ import com .intellij .openapi .application .ApplicationManager ;
22+ import com .intellij .openapi .command .undo .UndoManager ;
2123import com .intellij .openapi .editor .Editor ;
2224import com .intellij .openapi .progress .ProgressIndicator ;
2325import com .intellij .openapi .project .Project ;
26+ import com .intellij .psi .PsiDocumentManager ;
2427import com .intellij .psi .PsiFile ;
2528import com .intellij .util .DocumentUtil ;
2629import org .jetbrains .annotations .NotNull ;
2730
2831public class GoOptimizeImportsPass extends ProgressableTextEditorHighlightingPass {
2932 @ NotNull private final PsiFile myFile ;
33+ private Runnable myRunnableFix ;
3034
3135 public GoOptimizeImportsPass (@ NotNull Project project , @ NotNull PsiFile file , @ NotNull Editor editor ) {
3236 super (project , editor .getDocument (), "Go Optimize Imports Pass" , file , editor , file .getTextRange (), false ,
@@ -36,12 +40,23 @@ public GoOptimizeImportsPass(@NotNull Project project, @NotNull PsiFile file, @N
3640
3741 @ Override
3842 protected void collectInformationWithProgress (@ NotNull ProgressIndicator progress ) {
43+ myRunnableFix = new GoImportOptimizer ().processFile (myFile );
3944 progress .checkCanceled ();
4045 }
4146
4247 @ Override
4348 protected void applyInformationWithProgress () {
44- final Runnable runnable = new GoImportOptimizer ().processFile (myFile );
45- DocumentUtil .writeInRunUndoTransparentAction (runnable );
49+ final Project project = myFile .getProject ();
50+ if (PsiDocumentManager .getInstance (project ).getDocument (myFile ) == null ) return ;
51+ ApplicationManager .getApplication ().invokeLater (new Runnable () {
52+ @ Override
53+ public void run () {
54+ if (project .isDisposed ()) return ;
55+ UndoManager undoManager = UndoManager .getInstance (project );
56+ if (undoManager .isUndoInProgress () || undoManager .isRedoInProgress ()) return ;
57+ PsiDocumentManager .getInstance (project ).commitAllDocuments ();
58+ DocumentUtil .writeInRunUndoTransparentAction (myRunnableFix );
59+ }
60+ });
4661 }
4762}
0 commit comments