Repository navigation
Optimize imports with open-java-format in IntelliJ IDEA #255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abashev
wants to merge
6
commits into
21.X.X
Choose a base branch
from
abashev/intellij-plugin-optimize-imports-with-open-java
base: 21.X.X
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+888
−24
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d5ece4
Optimize imports with open-java-format in IntelliJ IDEA
abashev 3f9f609
Give the imports IntelliJ IDEA inserts open-java-format's layout
abashev 1b8de42
Merge branch '21.X.X' into abashev/intellij-plugin-optimize-imports-w…
abashev 4dde8f4
Say why the IDE plugin still calls TrustedProjects.isTrusted
abashev d00e3d5
Optimize no imports in an untrusted project without a balloon
abashev f57eeba
Leave the classpath out of the untrusted-project message
abashev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
88 changes: 88 additions & 0 deletions
88
...in/java/com/palantir/javaformat/intellij/PalantirJavaFormatCodeStyleSettingsModifier.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| /* | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package com.palantir.javaformat.intellij; | ||
|
|
||
| import com.intellij.ide.highlighter.JavaFileType; | ||
| import com.intellij.openapi.project.Project; | ||
| import com.intellij.psi.PsiFile; | ||
| import com.intellij.psi.codeStyle.CodeStyleSettings; | ||
| import com.intellij.psi.codeStyle.JavaCodeStyleSettings; | ||
| import com.intellij.psi.codeStyle.PackageEntry; | ||
| import com.intellij.psi.codeStyle.PackageEntryTable; | ||
| import com.intellij.psi.codeStyle.modifier.CodeStyleSettingsModifier; | ||
| import com.intellij.psi.codeStyle.modifier.CodeStyleStatusBarUIContributor; | ||
| import com.intellij.psi.codeStyle.modifier.TransientCodeStyleSettings; | ||
| import java.util.function.Consumer; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| /** | ||
| * Gives the Java files the plugin formats the import settings that open-java-format writes by. The IDE inserts imports | ||
| * through auto-import, Alt+Enter and completion by these settings, and no import optimizer sees them: with the IDE's | ||
| * defaults, five classes from one package become {@code import java.util.*;}, which open-java-format keeps. | ||
| * | ||
| * <p>The platform applies only the first modifier that changes a file's settings, and EditorConfig's is registered | ||
| * first: in a project whose {@code .editorconfig} sets code style properties for Java files, these settings do not | ||
| * apply, and its {@code ij_java_*} properties can set them there instead. | ||
| */ | ||
| final class PalantirJavaFormatCodeStyleSettingsModifier implements CodeStyleSettingsModifier { | ||
| // More names than a file imports from one package, so that an import on demand is never written. | ||
| private static final int NEVER_IMPORT_ON_DEMAND = 999; | ||
|
|
||
| @Override | ||
| public boolean modifySettings(TransientCodeStyleSettings settings, PsiFile file) { | ||
| if (!JavaFileType.INSTANCE.equals(file.getFileType()) | ||
| || !PalantirJavaFormatSettings.getInstance(file.getProject()).isEnabled()) { | ||
| return false; | ||
| } | ||
| var javaSettings = settings.getCustomSettings(JavaCodeStyleSettings.class); | ||
| javaSettings.USE_SINGLE_CLASS_IMPORTS = true; | ||
| javaSettings.CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND = NEVER_IMPORT_ON_DEMAND; | ||
| javaSettings.NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND = NEVER_IMPORT_ON_DEMAND; | ||
| javaSettings.PACKAGES_TO_USE_IMPORT_ON_DEMAND.copyFrom(new PackageEntryTable()); | ||
| // Static imports, a blank line, then the others, each group in the order of its names: ImportOrderer's. | ||
| javaSettings.LAYOUT_STATIC_IMPORTS_SEPARATELY = true; | ||
| var importLayout = new PackageEntryTable(); | ||
| importLayout.addEntry(PackageEntry.ALL_OTHER_STATIC_IMPORTS_ENTRY); | ||
| importLayout.addEntry(PackageEntry.BLANK_LINE_ENTRY); | ||
| importLayout.addEntry(PackageEntry.ALL_OTHER_IMPORTS_ENTRY); | ||
| javaSettings.IMPORT_LAYOUT_TABLE.copyFrom(importLayout); | ||
| return true; | ||
| } | ||
|
|
||
| // The Code Style page then says that its settings may be overridden by open-java-format. | ||
| @Override | ||
| public boolean mayOverrideSettingsOf(Project project) { | ||
| return PalantirJavaFormatSettings.getInstance(project).isEnabled(); | ||
| } | ||
|
|
||
| @Override | ||
| public String getName() { | ||
| return "open-java-format"; | ||
| } | ||
|
|
||
| @Override | ||
| @Nullable | ||
| public CodeStyleStatusBarUIContributor getStatusBarUiContributor(TransientCodeStyleSettings transientSettings) { | ||
| return null; | ||
| } | ||
|
|
||
| // What the Disable link next to that message runs: these settings come with the plugin, so it switches the plugin | ||
| // off for the project, as unticking it on the plugin's settings page does. | ||
| @Override | ||
| public Consumer<CodeStyleSettings> getDisablingFunction(Project project) { | ||
| return settings -> PalantirJavaFormatSettings.getInstance(project) | ||
| .setEnabled(PalantirJavaFormatSettings.disabledState(project)); | ||
| } | ||
| } |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.