Skip to content

Commit ac03825

Browse files
committed
Process imports in background thread
1 parent 5680c65 commit ac03825

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/com/goide/codeInsight/imports/GoOptimizeImportsPass.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,19 @@
1818

1919
import com.intellij.codeInsight.daemon.impl.DefaultHighlightInfoProcessor;
2020
import com.intellij.codeInsight.daemon.impl.ProgressableTextEditorHighlightingPass;
21+
import com.intellij.openapi.application.ApplicationManager;
22+
import com.intellij.openapi.command.undo.UndoManager;
2123
import com.intellij.openapi.editor.Editor;
2224
import com.intellij.openapi.progress.ProgressIndicator;
2325
import com.intellij.openapi.project.Project;
26+
import com.intellij.psi.PsiDocumentManager;
2427
import com.intellij.psi.PsiFile;
2528
import com.intellij.util.DocumentUtil;
2629
import org.jetbrains.annotations.NotNull;
2730

2831
public 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,
@@ -37,11 +41,22 @@ public GoOptimizeImportsPass(@NotNull Project project, @NotNull PsiFile file, @N
3741
@Override
3842
protected void collectInformationWithProgress(@NotNull ProgressIndicator progress) {
3943
progress.checkCanceled();
44+
myRunnableFix = new GoImportOptimizer().processFile(myFile);
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

Comments
 (0)