Skip to content

Let a write action interrupt the IntelliJ plugin's import optimizer while the formatter runs - #267

Merged
abashev merged 4 commits into
21.X.Xfrom
abashev/intellij-plugin-optimize-imports-on-the-fly-hold
Nov 23, 2026
Merged

abashev merged 4 commits into
21.X.Xfrom
abashev/intellij-plugin-optimize-imports-on-the-fly-hold

Conversation

@abashev

@abashev abashev commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

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.fixImports right there. A non-blocking read action gives way to a write action only at ProgressManager.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

  • fixImports 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.
  • A call still running is shared among the calls for the same formatter and text: a read action run again after a write elsewhere (a save, a VFS refresh, another file) waits for it instead of starting another JVM. A call is kept only while it runs. Typing in the file itself changes the text, so each keystroke there still starts a call of its own.
  • What the formatter throws comes back 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.
  • Debug lines, each starting with Optimizing imports of file=…:
    • asked on thread DefaultDispatcher-worker-25
    • started fixImports, or waits for the fixImports already running on the same text
    • waited 3 ms for fixImports, length=2935, text changed: true (was fixImports ran in …), or cancelled after waiting 12 ms for fixImports, for a write action or by the user
    • and, while an optimization waits for fixImports, Optimizing imports: a write action waited 2 ms for the read lock, measured from beforeWriteActionStart to writeActionStarted. 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), whose perform runs the IDE's own JavaImportOptimizer on 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: RemoveAllUnusedImportsFix deletes 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 PalantirJavaFormatCodeStyleSettingsModifier sets: it removes an unused import 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:

  • Saves with "Reformat code" and "Optimize imports" in Actions on Save asked the optimizer on DefaultDispatcher-worker-* threads. In the IDE process fixImports took 3 to 4 ms once warm, in a separate JVM 351 ms.
  • One write action came while the optimizer waited for the formatter, and waited 2 ms for the read lock. The read action it cancelled was run again on the new text, and that run's imports were applied.
  • Typing with "Optimize imports on the fly" ticked asked the plugin nothing, as above.

Possible downsides

  • A call that was given up on still runs to its end: CPU, and a JVM in that mode. While typing in the file being optimized, that is one call per keystroke made while one runs.
  • A RuntimeException from the formatter now reaches the error report wrapped in a RuntimeException and an ExecutionException; the cause is in the chain.

Tests

  • a_write_action_does_not_wait_for_the_formatter_to_fix_the_imports: the formatter blocks, processFile runs in ReadAction.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 from awaitWithCheckCanceled on the stack.
  • optimize_imports_leaves_the_file_alone_when_the_formatter_fails_on_it: a FormatterException from the pooled thread is still a warning. Waiting on a plain Future<String> makes it fail with TestLoggerAssertionError.
  • 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:check passes, 138 tests.

The last two commits update ADR-003 only, the second of them accepting it: #221 and #222 are in.

… 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).
Both of its decisions are in: fixImports passes --fix-imports-only out
of process (#221, in #236), and the plugin's optimizer replaces the
IDE's (#222, in #255).
@abashev
abashev merged commit c7f7277 into 21.X.X Nov 23, 2026
16 checks passed
@abashev
abashev deleted the abashev/intellij-plugin-optimize-imports-on-the-fly-hold branch November 23, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

IntelliJ plugin: "Optimize imports on the fly" holds up typing while the formatter runs

1 participant