Repository navigation
Let a write action interrupt the IntelliJ plugin's import optimizer while the formatter runs - #267
Merged
abashev merged 4 commits intoNov 23, 2026
Conversation
… runs With "Optimize imports on the fly" ticked, IntelliJ IDEA 2024.2 to 2025.1 run the plugin's import optimizer in a non-blocking read action after a highlighting pass, and the optimizer called fixImports right there. A non-blocking read action gives way to a write action only when it checks for cancellation, which the formatter never does, so the write action of the next keystroke waited for the formatter: about 0.3 s with a JVM of its own, 20 ms with the native binary (#253). From 2025.2 the IDE optimizes on the fly with its own JavaImportOptimizer and never asks the plugin. fixImports now runs on a pooled thread, and the optimizer waits for it with ProgressIndicatorUtils.awaitWithCheckCanceled. A pending write action cancels the read action, and the platform runs it again after the write. The call already started runs to its end, and one that is still running is shared, by formatter and text, with the calls that ask for the same text meanwhile: a read action run again after a write elsewhere waits for it instead of starting another JVM. A call is kept only while it runs. What the formatter throws comes back from its thread as a value: awaitWithCheckCanceled would wrap a FormatterException in a RuntimeException, and the optimizer would report a file it cannot parse as a fault of the plugin. Fixes #253
With debug logging on, the log could tell how long fixImports took but not whether typing waited for it, which is what #253 is about. The optimizer now logs the thread it was asked on, and whether it started fixImports or waits for the call already running on the same text. Once debug logging is on, it also listens to write actions: while an optimization waits for fixImports, it logs how long each write action waited for the read lock, from beforeWriteActionStart to writeActionStarted. Other read actions may hold the lock too, and their time counts as well. The listener stays until the IDE restarts, which the plugin needs to unload.
…the plugin ADR-003 listed, from the sources of IntelliJ IDEA 2024.2.6, every way of optimizing imports as going through OptimizeImportsProcessor.collectOptimizers, and so through the plugin's optimizer. From 2025.2 two of them do not: "Optimize imports on the fly", and the quick fix for missorted imports. JetBrains made that fix a ModCommandAction (IDEA-370071), whose perform runs the IDE's own JavaImportOptimizer; the classes of 2026.3 do, those of 2025.1.7.2 still take the old path. The decision now says on which paths the plugin's optimizer runs in which versions, and the consequences what imports optimized on the fly look like from 2025.2. The list also named the quick fix for an unused import, which even in 2024.2.6 is "Remove unused imports": RemoveAllUnusedImportsFix deletes the imports it highlights itself, and never asks an optimizer. The quick fix that went through collectOptimizers was the one for missorted imports. The consequence about the optimizer's time inside a read action says that typing no longer waits for it (#253).
abashev
deleted the
abashev/intellij-plugin-optimize-imports-on-the-fly-hold
branch
November 23, 2026 21:26
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Based on
21.X.X; independent of the other open pull requests. Fixes #253.With "Optimize imports on the fly" ticked, IntelliJ IDEA 2024.2 to 2025.1 run the plugin's import optimizer in a non-blocking read action after a highlighting pass, and the optimizer called
FormatterService.fixImportsright there. A non-blocking read action gives way to a write action only atProgressManager.checkCanceled(), which the formatter never calls, so the write action of the next keystroke waited for the formatter: about 0.3 s with a JVM of its own, 20 ms with the native binary.What changes
fixImportsruns on a pooled thread, and the optimizer waits for it withProgressIndicatorUtils.awaitWithCheckCanceled. A pending write action cancels the read action, and the platform runs it again after the write. The call already started runs to its end.awaitWithCheckCanceledwould wrap aFormatterExceptionin aRuntimeException, and the optimizer would report a file it cannot parse as a fault of the plugin.Optimizing imports of file=…:asked on thread DefaultDispatcher-worker-25started fixImports, orwaits for the fixImports already running on the same textwaited 3 ms for fixImports, length=2935, text changed: true(wasfixImports ran in …), orcancelled after waiting 12 ms for fixImports, for a write action or by the userfixImports,Optimizing imports: a write action waited 2 ms for the read lock, measured frombeforeWriteActionStarttowriteActionStarted. The listener is registered once debug logging is on, and stays until the IDE restarts.IntelliJ IDEA 2025.2 and later
From 2025.2, on the fly never reaches the plugin. JetBrains turned the fix into a
ModCommandAction(IDEA-370071, JetBrains/intellij-community@7ddc26cb5427, first in 252), whoseperformruns the IDE's ownJavaImportOptimizeron a copy of the file, in a non-blocking read action that expires once the file changes. The same fix is behind Alt+Enter "Optimize imports" on missorted imports. The quick fix for an unused import, "Remove unused imports", never asked an optimizer, in 2024.2 either:RemoveAllUnusedImportsFixdeletes the imports it highlights itself. Optimize Imports, Reformat with "Optimize imports", Actions on Save and "Optimize imports" before a commit still go through the plugin's optimizer.So on the fly in those IDEs follows the IDE's rules with the layout that
PalantirJavaFormatCodeStyleSettingsModifiersets: it removes an unusedimport a.b.*;, which open-java-format keeps, and it cannot hold up typing. ADR-003 now says on which paths the plugin's optimizer runs in which versions, and drops the quick fix for an unused import from them. The note in #265 that on the fly asks the optimizer on every highlighting pass holds for 2024.2 to 2025.1.What the log showed
With the plugin built from this branch, in IntelliJ IDEA Community 2024.2.6 (
./gradlew :open-java-format-idea-plugin:runIde), typing with "Optimize imports on the fly" ticked did not wait for the formatter.In IntelliJ IDEA 2026.3:
DefaultDispatcher-worker-*threads. In the IDE processfixImportstook 3 to 4 ms once warm, in a separate JVM 351 ms.Possible downsides
RuntimeExceptionfrom the formatter now reaches the error report wrapped in aRuntimeExceptionand anExecutionException; the cause is in the chain.Tests
a_write_action_does_not_wait_for_the_formatter_to_fix_the_imports: the formatter blocks,processFileruns inReadAction.nonBlocking(…)as on the fly, and a write action comes from the EDT. It timed out after 10 s before the change, and passes in 0.2 s with it.optimizing_the_same_text_again_waits_for_the_fix_already_running: two calls on the same text call the formatter once. Without the sharing it fails with 2 calls. The formatter is released once the second call waits, which a progress indicator tells fromawaitWithCheckCanceledon the stack.optimize_imports_leaves_the_file_alone_when_the_formatter_fails_on_it: aFormatterExceptionfrom the pooled thread is still a warning. Waiting on a plainFuture<String>makes it fail withTestLoggerAssertionError.measures_how_long_a_write_action_waits_for_the_read_lock: with a read action held for 300 ms, the write action's wait is measured above 200 ms../gradlew :open-java-format-idea-plugin:checkpasses, 138 tests.The last two commits update ADR-003 only, the second of them accepting it: #221 and #222 are in.