Skip to content

Optimize imports with open-java-format in IntelliJ IDEA - #255

Open
abashev wants to merge 6 commits into
21.X.Xfrom
abashev/intellij-plugin-optimize-imports-with-open-java
Open

abashev wants to merge 6 commits into
21.X.Xfrom
abashev/intellij-plugin-optimize-imports-with-open-java

Conversation

@abashev

@abashev abashev commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

Closes #222. Builds on #221, which is merged; independent of the other open pull requests; based on 21.X.X.

With the plugin enabled, IntelliJ IDEA formatted with open-java-format but optimized imports with its own JavaImportOptimizer, by the IDE's import layout, and the command line then sorted them again. The formatting service now has OPTIMIZE_IMPORTS, and its optimizer, PalantirJavaFormatImportOptimizer, calls FormatterService.fixImports: the unused imports removed, the others sorted, the rest of the file as it was. Every way the IDE optimizes imports goes through it: Optimize Imports, Reformat Code and Reformat Files with "Optimize imports", Actions on Save, "Optimize imports" before a commit, the quick fix for an unused import and "Optimize imports on the fly".

The formatter runs in the optimizer's processFile, which the IDE calls under a read action, in the background on most paths; the Runnable it returns, which runs on the EDT under a write action, only writes the text, and only if the document has not changed since.

Reformat with "Optimize imports"

Reformat starts the plugin's formatter in the background, and the platform applies its text later, only if the document has not changed since the request started: IntelliJ IDEA registers no DocumentMerger to do anything else (2024.2.6). Reformat with "Optimize imports" starts the optimizer right after the formatter, on the same document, without waiting for it (ReformatCodeAction, and FileInEditorProcessor over a whole file). So whichever wrote second lost: the formatted text, or the imports.

PendingFormat, kept on the document by each formatting request, settles it:

  • While the request is formatting, the optimizer hands the imports over to it and writes nothing. The request fixes the imports of the text it formatted, after the layout, as the command line does: the order ADR-004 keeps until Fix the imports before the layout or after it? #224 decides it.
  • Once the request has its text, until the document next changes, the optimizer writes nothing, so the formatted text is not dropped. Imports optimized in that moment are not fixed.

As ADR-003 decided, Reformat alone still never changes imports: the request fixes them only when "Optimize imports" handed them over.

Imports the IDE inserts

Auto-import, Alt+Enter and completion insert imports through the IDE's ImportHelper, by the Java code style settings, and no optimizer sees them: by default five classes from one package become import a.b.*;, which open-java-format keeps. PalantirJavaFormatCodeStyleSettingsModifier gives Java files open-java-format's import settings while the plugin is enabled: single-class imports up to 999 from a package, no packages always imported on demand, and the static imports, a blank line, then the others.

  • The Code Style page says "Settings may be overridden by open-java-format". Its Disable link switches the plugin off for the project, as unticking it on the plugin's page does; without a disabling function the link did nothing.
  • Switching the plugin on or off on its page refreshes the code style of open files, which the IDE caches for each file.
  • EditorConfig wins. The platform applies only the first modifier that changes a file's settings, and EditorConfig's is registered first, with order="first" and no id to order against. In a project whose .editorconfig sets code style properties for Java files, these settings do not apply; its ij_java_* properties can set them there.

Not done here

"Optimize imports on the fly" calls the optimizer after every highlighting pass that finds an import the IDE would change, inside a read action that fixImports never lets go of, so typing waits for it: about 0.3 s with the Java-based formatter, about 20 ms with the native binary. In a project where EditorConfig's settings win, that includes every file open-java-format wrote with a static import. #253 has the details and a proposal; #223 makes one call short enough not to matter.

What changes for users

  • Optimize Imports and everything that runs it give the imports the command line gives, and Reformat with "Optimize imports" gives the command line's file.
  • An import inserted by the IDE is a single-class import in open-java-format's layout, unless EditorConfig's settings win.
  • The plugin's description on the Marketplace mentions Optimize Imports.
  • The openjavaformat.dev page for IntelliJ IDEA needs a follow-up in openjavaformat/docs.

Tests

  • PalantirJavaFormatImportOptimizerTest
    • Optimize Imports removes the unused import and sorts the others, leaving the rest of the file as it was.
    • Reformat with "Optimize imports" gives exactly what formatSourceReflowStringsAndFixImports, the command line, gives.
    • With a formatting request still running on the document, Optimize Imports writes nothing, and the request returns the command line's file.
    • With a request whose text is not applied yet, Optimize Imports writes nothing; once the text is in, it fixes the imports.
    • With the plugin disabled, the IDE's own optimizer runs.
  • PalantirJavaFormatCodeStyleSettingsModifierTest
    • The modifier's settings, and that it leaves other files, and every file with the plugin disabled, alone.
    • An import added through JavaCodeStyleManager.addImport, as auto-import adds it: a fifth class from one package stays a single-class import, and with the plugin disabled the IDE writes import p.*; instead.
    • The Disable link switches the plugin off.
  • PalantirJavaFormatConfigurableTest: switching the plugin off bumps the code style's modification tracker, and applying the page unchanged does not.

The two hand-over tests fail with the hand-over taken out. The headless test IDE formats synchronously, so no test reproduces the timing of a running IDE; those two drive the sequence by hand.

./gradlew :open-java-format-idea-plugin:check passes on JDK 21, and the first commit's tests pass on their own. verifyPlugin: compatible with IC-242.26775.15 and IU-263.7307.200; the new usages of experimental API are those of CodeStyleSettingsModifier, and no new usage of deprecated API.

To check in a running IDE

Not tried yet:

  • Reformat Code (Ctrl+Alt+L) with "Optimize imports" ticked, with the Java-based formatter and with the native binary.
  • Reformat Code on a directory with "Optimize imports".
  • Actions on Save with "Reformat code" and "Optimize imports".
  • Alt+Enter on a fifth class from one package, in a project with and without an .editorconfig.

The formatting service now has OPTIMIZE_IMPORTS, so every way the IDE
optimizes imports asks its optimizer instead of the IDE's own
JavaImportOptimizer: Optimize Imports, Reformat with "Optimize
imports", Actions on Save, the check before a commit, the quick fix for
an unused import and "Optimize imports on the fly". The optimizer calls
FormatterService.fixImports, which since #221 fixes the imports alone:
the unused ones removed, the others sorted, as the command line does.

Reformat with "Optimize imports" starts the formatter in the background
and the optimizer right after it, on the same document. The platform
applies the formatter's text only if the document has not changed
since the request started, and IntelliJ IDEA registers no
DocumentMerger, so whichever wrote second lost: the formatting or the
imports. While a format of the plugin is running on a document, the
optimizer now hands the imports over to it, and the format fixes them
after the layout, as the command line does. Once the format has its
text, the optimizer leaves the document alone until the text is in.

The optimizer holds a read action while the formatter runs, which
"Optimize imports on the fly" makes typing wait for (#253).
Auto-import, Alt+Enter and completion insert imports through the IDE's
ImportHelper, by the Java code style settings, and no import optimizer
sees them: by default five classes from one package become an import
on demand, which open-java-format keeps. A CodeStyleSettingsModifier
now gives Java files open-java-format's import settings while the
plugin is enabled: single-class imports up to 999 from a package, no
packages always imported on demand, and the static imports, a blank
line, then the others.

The Code Style page then says that open-java-format may override its
settings, and its Disable link switches the plugin off for the project,
as unticking it on the plugin's page does. Switching the plugin on or
off there refreshes the code style of open files, which the IDE caches
for each file.

The platform applies only the first modifier that changes a file's
settings, and EditorConfig's is registered first: in a project whose
.editorconfig sets code style properties for Java files, these
settings do not apply.
The comment said that its replacement,
com.intellij.ide.trustedProjects.TrustedProjects, is
@ApiStatus.Internal. It is in 2024.2, but from 2025.1 that class is
public, and its isProjectTrusted replaces isTrusted, deprecated since.
The plugin still supports 2024.2, so the call stays until sinceBuild
reaches 251.
Safe mode is a normal state, not a fault, yet the import optimizer
logged a warning and showed a balloon each time it ran in an untrusted
project, and "Optimize imports on the fly" runs it on every highlighting
pass while the file has unused imports. It now returns a
CollectingInfoRunnable that changes nothing: on the fly nothing shows,
and an explicit Optimize Imports shows the reason in its hint instead
of "No unused imports found".
The message said that a trusted project is required to load the
external formatter classpath, which only the Java-based formatter
does. Users of the native binary got it too, since the trust check
comes before the formatter is chosen. It now says that open-java-format
requires a trusted project to run its formatter.
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 with open-java-format, not the IDE's own optimizer

2 participants