Title: IntelliJ plugin: optimize imports with open-java-format, not the IDE's own optimizer · Issue #222 · openjavaformat/open-java-format · GitHub
Open Graph Title: IntelliJ plugin: optimize imports with open-java-format, not the IDE's own optimizer · Issue #222 · openjavaformat/open-java-format
X Title: IntelliJ plugin: optimize imports with open-java-format, not the IDE's own optimizer · Issue #222 · openjavaformat/open-java-format
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 so...
Open Graph 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 Git...
X 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...
Opengraph URL: https://github.com/openjavaformat/open-java-format/issues/222
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"IntelliJ plugin: optimize imports with open-java-format, not the IDE's own optimizer","articleBody":"### What happens\n\nWith 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.\n\nEvery 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.\n\n### Why\n\n`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.\n\nThe plugin had an optimizer for three weeks: palantir/palantir-java-format#1078 added one on 2024-04-26, as google-java-format did in google/google-java-format@84b2c9a2 on 2023-03-02, and palantir/palantir-java-format#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).\n\n### Proposal\n\n1. Fix #221.\n2. `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 #223.\n3. 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.\n\n### Open question\n\nReformat 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.\n","author":{"url":"https://github.com/abashev","@type":"Person","name":"abashev"},"datePublished":"2026-11-13T20:35:22Z","url":"https://github.com/openjavaformat/open-java-format/issues/222"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:494e4150-34aa-00d1-4808-61f107243296 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | D782:6188:2BB27E:39FE8A:6AFBB795 |
| html-safe-nonce | dc14135ad72e3e65ba373018d770e97c69927f2ced836ae6aebf262dff3e18ea |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJENzgyOjYxODg6MkJCMjdFOjM5RkU4QTo2QUZCQjc5NSIsInZpc2l0b3JfaWQiOiI1MTYxODE4MzYxMDA4MjA3NzY1IiwicmVnaW9uX2VkZ2UiOiJpYWQiLCJyZWdpb25fcmVuZGVyIjoiaWFkIn0= |
| visitor-hmac | c9f7d3bf5a34f8800f4b61d266e8d56ee7705ceb2b9f83ecf25b97135a36fa04 |
| hovercard-subject-tag | issue:6379498909 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/openjavaformat/open-java-format/222/issue_layout |
| twitter:image | https://opengraph.githubassets.com/3e2a29ad8ef67b84e12e408148fa94b28acd375b7ba522d3aa811cc62c322f7c/openjavaformat/open-java-format/issues/222 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/3e2a29ad8ef67b84e12e408148fa94b28acd375b7ba522d3aa811cc62c322f7c/openjavaformat/open-java-format/issues/222 |
| og:image:alt | 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 Git... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | abashev |
| hostname | github.com |
| expected-hostname | github.com |
| None | 2b2ef9c1578c18d8ed27027940544c958743213cf4461659f14d3ccdf51e162c |
| turbo-cache-control | no-preview |
| go-import | github.com/openjavaformat/open-java-format git https://github.com/openjavaformat/open-java-format.git |
| octolytics-dimension-user_id | 325177626 |
| octolytics-dimension-user_login | openjavaformat |
| octolytics-dimension-repository_id | 1288055459 |
| octolytics-dimension-repository_nwo | openjavaformat/open-java-format |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | true |
| octolytics-dimension-repository_parent_id | 214273098 |
| octolytics-dimension-repository_parent_nwo | palantir/palantir-java-format |
| octolytics-dimension-repository_network_root_id | 214273098 |
| octolytics-dimension-repository_network_root_nwo | palantir/palantir-java-format |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e4cce0e99cf61481a318ff12c2e8580153a6c7c6 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width