Repository navigation
Description
What happens
With the plugin enabled, IntelliJ IDEA formats with open-java-format, but its own JavaImportOptimizer still optimizes the imports, by the IDE's import layout. The command line, the GitHub Action and the build plugins then sort them again with ImportOrderer, and the file changes once more.
Every path that optimizes imports goes through OptimizeImportsProcessor: Optimize Imports, Reformat Code and Reformat Files with "Optimize imports" ticked, Actions on Save, "Optimize imports" before a commit, the quick fix for an unused import, and "Optimize imports on the fly". Separately, auto-import, Alt+Enter and completion insert imports through ImportHelper, by the Java code style settings: by default five classes from a package become import java.util.*;, which open-java-format keeps.
Why
PalantirJavaFormatFormattingService.getFeatures() returns FORMAT_FRAGMENTS only, and getImportOptimizers comes from AbstractDocumentFormattingService, which returns no optimizers. OptimizeImportsProcessor.collectOptimizers asks FormattingServiceUtil.findImportsOptimizingService(file) for a service with OPTIMIZE_IMPORTS, finds CoreFormattingService, and gets JavaImportOptimizer from LanguageImportStatements. All of this is in the sources of IntelliJ IDEA 2024.2.6, the platform the plugin is built against.
The plugin had an optimizer for three weeks: palantir#1078 added one on 2024-04-26, as google-java-format did in google/google-java-format@84b2c9a2 on 2023-03-02, and palantir#1089 removed it on 2024-05-21 as too slow. It called FormatterService.fixImports, which formatted the whole file in a new JVM each time (#221).
Proposal
- Fix FormatterService.fixImports formats the whole file out of process #221.
getFeatures()addsOPTIMIZE_IMPORTS, andgetImportOptimizersreturns an optimizer that callsFormatterService.fixImports, as google-java-format'sGoogleJavaFormatImportOptimizercallsFormatter.fixImportsin process.OptimizeImportsProcessor.prepareTaskrunsprocessFilein a background task under a read action and applies its result on the EDT, so the time of one call counts: about 20 ms with the native binary and 0.3 s with a new JVM on a small file. How the formatter runs is IntelliJ plugin: format in the IDE's JVM, without a new JVM for each call #223.- A
CodeStyleSettingsModifier, as EditorConfig has, sets the Java import settings thatImportHelperreads to what open-java-format writes:CLASS_COUNT_TO_USE_IMPORT_ON_DEMANDandNAMES_COUNT_TO_USE_IMPORT_ON_DEMANDto 999,PACKAGES_TO_USE_IMPORT_ON_DEMANDempty,IMPORT_LAYOUT_TABLEstatic imports, a blank line, the others. ItsmayOverrideSettingsOfmakes the Code Style page say that open-java-format overrides them.
Open question
Reformat Code with "Optimize imports" runs the optimizer before formatting, which fixes the imports before the layout (#224). A Reformat of the whole file could instead call formatSourceReflowStringsAndFixImports once and give exactly what the command line gives, at the price of fixing the imports when the box is not ticked, as the command line always does.
Activity