Skip to content

Commit 48c6ccf

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

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
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,
@@ -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
}

tests/com/goide/inspections/GoTestSignaturesInspectionTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@
2121
import com.intellij.testFramework.LightProjectDescriptor;
2222

2323
public class GoTestSignaturesInspectionTest extends GoQuickFixTestBase {
24-
@Override
25-
protected void tearDown() throws Exception {
26-
GoCodeInsightSettings.getInstance().setOptimizeImportsOnTheFly(true);
27-
super.tearDown();
28-
}
29-
3024
@Override
3125
protected void setUp() throws Exception {
3226
super.setUp();
@@ -35,6 +29,12 @@ protected void setUp() throws Exception {
3529
GoCodeInsightSettings.getInstance().setOptimizeImportsOnTheFly(false);
3630
}
3731

32+
@Override
33+
protected void tearDown() throws Exception {
34+
GoCodeInsightSettings.getInstance().setOptimizeImportsOnTheFly(true);
35+
super.tearDown();
36+
}
37+
3838
@Override
3939
protected LightProjectDescriptor getProjectDescriptor() {
4040
return createMockProjectDescriptor();

0 commit comments

Comments
 (0)