Skip to content

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

Done

Description

@abashev

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 (RemoveUnusedImports never removes import a.b.*;), or a used single-type import from java.lang, which it keeps on purpose (JLS 6.4.1).
  • Imports out of the IDE's layout (findEntryIndex going 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, and javax.* and java.* after every other package. The plugin's CodeStyleSettingsModifier gives Java files open-java-format's layout, but EditorConfig's modifier is registered first and wins in a project whose .editorconfig sets code style properties for Java files. There, a file that open-java-format wrote with a static import, or with java.* above org.*, 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 fixImports on a pooled thread and wait for it with ProgressIndicatorUtils.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. awaitWithCheckCanceled wraps an exception thrown by the task, so FormatterException has 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions