Repository navigation
Conversation
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.
Kronen
reviewed
Nov 21, 2026
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.
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.
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 hasOPTIMIZE_IMPORTS, and its optimizer,PalantirJavaFormatImportOptimizer, callsFormatterService.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; theRunnableit 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
DocumentMergerto 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, andFileInEditorProcessorover 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: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 becomeimport a.b.*;, which open-java-format keeps.PalantirJavaFormatCodeStyleSettingsModifiergives 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.order="first"and no id to order against. In a project whose.editorconfigsets code style properties for Java files, these settings do not apply; itsij_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
fixImportsnever 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
openjavaformat/docs.Tests
PalantirJavaFormatImportOptimizerTestformatSourceReflowStringsAndFixImports, the command line, gives.PalantirJavaFormatCodeStyleSettingsModifierTestJavaCodeStyleManager.addImport, as auto-import adds it: a fifth class from one package stays a single-class import, and with the plugin disabled the IDE writesimport p.*;instead.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:checkpasses 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 ofCodeStyleSettingsModifier, and no new usage of deprecated API.To check in a running IDE
Not tried yet:
.editorconfig.