Skip to content

IntelliJ plugin: optimize imports with open-java-format, not the IDE's own optimizer #222

Open

Description

@abashev

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

  1. Fix FormatterService.fixImports formats the whole file out of process #221.
  2. getFeatures() adds OPTIMIZE_IMPORTS, and getImportOptimizers returns an optimizer that calls FormatterService.fixImports, as google-java-format's GoogleJavaFormatImportOptimizer calls Formatter.fixImports in process. OptimizeImportsProcessor.prepareTask runs processFile in 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.
  3. A CodeStyleSettingsModifier, as EditorConfig has, sets the Java import settings that ImportHelper reads to what open-java-format writes: CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND and NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND to 999, PACKAGES_TO_USE_IMPORT_ON_DEMAND empty, IMPORT_LAYOUT_TABLE static imports, a blank line, the others. Its mayOverrideSettingsOf makes 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

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

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions