Repository navigation
Description
What happens
With "Optimize imports on the fly" ticked (Settings → Editor → General → Auto Import, off by default), IntelliJ IDEA runs the import optimizer of #222 after every highlighting pass that finds an import it would change. The optimizer calls FormatterService.fixImports inside a read action, and the call never checks for cancellation, so the write action of the next keystroke waits until the formatter returns: about 0.3 s with a new JVM for each call, about 20 ms with the native binary, on a small file (the times of #222).
This is read from the sources of IntelliJ IDEA 2024.2.6, the platform the plugin is built against, and not measured in a running IDE yet. The optimizer is PalantirJavaFormatImportOptimizer.processFile on the branch of #222, not merged yet.
How it fires
UnusedImportsVisitor.collectHighlights hands an OptimizeImportsFix to OptimizeImportRestarter, which invokes it when the daemon finishes, if the file has not changed since. QuickFixFactoryImpl.invokeOnTheFlyImportOptimizer runs OptimizeImportsProcessor, whose background task calls processFile in ReadAction.nonBlocking(...).executeSynchronously(). A non-blocking read action gives way to a write action only at ProgressManager.checkCanceled(), and fixImports waits for its process without calling it.
The visitor creates the fix in two cases, and in both open-java-format leaves the file as it is, so the fix comes back on the pass after the next edit:
- An import the IDE calls unused or redundant, and open-java-format keeps: an unused import on demand (
RemoveUnusedImportsnever removesimport a.b.*;), or a used single-type import fromjava.lang, which it keeps on purpose (JLS 6.4.1). - Imports out of the IDE's layout (
findEntryIndexgoing down): open-java-format writes the static imports first and the others in one alphabetical block; the IDE's default layout has the static imports last, andjavax.*andjava.*after every other package. The plugin'sCodeStyleSettingsModifiergives Java files open-java-format's layout, but EditorConfig's modifier is registered first and wins in a project whose.editorconfigsets code style properties for Java files. There, a file that open-java-format wrote with a static import, or withjava.*aboveorg.*, is out of order to the IDE.
Optimize Imports, Reformat with "Optimize imports", Actions on Save and "Optimize imports" before a commit call the optimizer once per action, as Reformat calls the formatter.
Proposal
- Wait for the formatter in a way a write action can interrupt: run
fixImportson a pooled thread and wait for it withProgressIndicatorUtils.awaitWithCheckCanceled. A pending write action then cancels the read action, and the platform runs it again after the write. The process already started runs to its end and its result is dropped, so a burst of typing costs CPU instead of stalling.awaitWithCheckCanceledwraps an exception thrown by the task, soFormatterExceptionhas to come back as a value rather than be thrown across the thread. - IntelliJ plugin: format in the IDE's JVM, without a new JVM for each call #223 brings one call to about 9 ms in the IDE's JVM once warm, too short to notice. A new JVM for each call stays its last fallback, so the first point still matters.
Activity
Say in ADR-003 which IntelliJ IDEA versions optimize imports through …