18
18
19
19
import com .intellij .codeInsight .daemon .impl .DefaultHighlightInfoProcessor ;
20
20
import com .intellij .codeInsight .daemon .impl .ProgressableTextEditorHighlightingPass ;
21
+ import com .intellij .openapi .application .ApplicationManager ;
22
+ import com .intellij .openapi .command .undo .UndoManager ;
21
23
import com .intellij .openapi .editor .Editor ;
22
24
import com .intellij .openapi .progress .ProgressIndicator ;
23
25
import com .intellij .openapi .project .Project ;
26
+ import com .intellij .psi .PsiDocumentManager ;
24
27
import com .intellij .psi .PsiFile ;
25
28
import com .intellij .util .DocumentUtil ;
26
29
import org .jetbrains .annotations .NotNull ;
27
30
28
31
public class GoOptimizeImportsPass extends ProgressableTextEditorHighlightingPass {
29
32
@ NotNull private final PsiFile myFile ;
33
+ private Runnable myRunnableFix ;
30
34
31
35
public GoOptimizeImportsPass (@ NotNull Project project , @ NotNull PsiFile file , @ NotNull Editor editor ) {
32
36
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
36
40
37
41
@ Override
38
42
protected void collectInformationWithProgress (@ NotNull ProgressIndicator progress ) {
43
+ myRunnableFix = new GoImportOptimizer ().processFile (myFile );
39
44
progress .checkCanceled ();
40
45
}
41
46
42
47
@ Override
43
48
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
+ });
46
61
}
47
62
}
0 commit comments