IDEA-285011 [java]: make optimizing imports on the fly undo transparent

(cherry picked from commit f79dc536b7bbd516005524118a4451291b408e80)

IJ-CR-210513

GitOrigin-RevId: 416984570dc3bc838ff6520d867f2b206c5b6eae
This commit is contained in:
Bas Leijdekkers
2026-07-01 11:58:59 +00:00
committed by intellij-monorepo-bot
parent 22bc65d75b
commit 82f42832c9
2 changed files with 42 additions and 5 deletions
@@ -1,7 +1,6 @@
// Copyright 2000-2023 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.daemon.impl.analysis;
import com.intellij.codeInsight.CodeInsightBundle;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
import com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx;
import com.intellij.modcommand.ActionContext;
@@ -117,9 +116,8 @@ public final class OptimizeImportRestarter implements Disposable {
ReadAction.nonBlocking(() -> optimizeFix.getPresentation(context) != null ? optimizeFix.perform(context) : ModCommand.nop())
.expireWhen(() -> myProject.isDisposed() || psiFile.getModificationStamp() != request.modificationStampBefore())
.finishOnUiThread(ModalityState.defaultModalityState(),
command -> CommandProcessor.getInstance().executeCommand(
myProject, () -> ModCommandExecutor.getInstance().executeInBatch(context, command),
CodeInsightBundle.message("process.optimize.imports"), null))
command -> CommandProcessor.getInstance()
.runUndoTransparentAction(() -> ModCommandExecutor.getInstance().executeInBatch(context, command)))
.submit(AppExecutorUtil.getAppExecutorService());
scheduledFutures.add(future);
future.onProcessed(_->scheduledFutures.remove(future));
@@ -734,6 +734,45 @@ public class ImportHelperTest extends ProductionDaemonAnalyzerTestCase {
assertNoImportsAdded();
}
public void testUndoAfterOptimizeImportsOnTheFly() {
assertNotNull(JavaPsiFacade.getInstance(getProject()).findClass("java.util.Date", GlobalSearchScope.allScope(getProject())));
@Language("JAVA") String text = """
import java.util.*;
final class UndoTestJava {
static void main() {
<caret>Date d = new Date();// Comment out this line then try undo
}
}
""";
configureByText(text);
CodeInsightWorkspaceSettings.getInstance(getProject()).setOptimizeImportsOnTheFly(true, getTestRootDisposable());
EditorTestUtil.executeAction(getEditor(), IdeActions.ACTION_COMMENT_LINE);
List<HighlightInfo> errors = myTestDaemonCodeAnalyzer.waitHighlightingSurviveCancellations(getFile(), HighlightSeverity.ERROR);
waitForAutoOptimizeImports();
assertEmpty(errors);
assertEquals("""
final class UndoTestJava {
static void main() {
// Date d = new Date();// Comment out this line then try undo
}
}
""", getFile().getText());
EditorTestUtil.executeAction(getEditor(), IdeActions.ACTION_UNDO);
errors = myTestDaemonCodeAnalyzer.waitHighlightingSurviveCancellations(getFile(), HighlightSeverity.ERROR);
waitForAutoOptimizeImports();
assertEmpty(errors);
assertEquals("""
import java.util.*;
final class UndoTestJava {
static void main() {
Date d = new Date();// Comment out this line then try undo
}
}
""", getFile().getText());
}
private void waitForAutoOptimizeImports() {
PlatformTestUtil.dispatchAllInvocationEventsInIdeEventQueue();
TestDaemonCodeAnalyzerImpl.waitWhilePumping(ApplicationManager.getApplication().executeOnPooledThread(() -> {