From fcd12732d4f531831e01f7c3816cf067eb037a56 Mon Sep 17 00:00:00 2001 From: Ohle Claussen Date: Fri, 5 Jul 2024 09:19:09 +0200 Subject: [PATCH 1/3] Add a checkbox to opt out of optimizing imports --- .../intellij/GoogleJavaFormatConfigurable.form | 16 ++++++++++++---- .../intellij/GoogleJavaFormatConfigurable.java | 4 ++++ .../GoogleJavaFormatImportOptimizer.java | 5 ++++- .../intellij/GoogleJavaFormatSettings.java | 9 +++++++++ 4 files changed, 29 insertions(+), 5 deletions(-) diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.form b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.form index 1db1d7920..cae5a5db0 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.form +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.form @@ -1,6 +1,6 @@
- + @@ -18,12 +18,12 @@ - + - + @@ -31,10 +31,18 @@ - + + + + + + + + + diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java index 759decc0f..7b73d0638 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java @@ -40,6 +40,7 @@ class GoogleJavaFormatConfigurable extends BaseConfigurable implements Searchabl private final Project project; private JPanel panel; private JCheckBox enable; + private JCheckBox optimizeImports; private JComboBox styleComboBox; public GoogleJavaFormatConfigurable(Project project) { @@ -80,6 +81,7 @@ public JComponent createComponent() { public void apply() throws ConfigurationException { GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); settings.setEnabled(enable.isSelected() ? EnabledState.ENABLED : getDisabledState()); + settings.setOptimizeImports(optimizeImports.isSelected()); settings.setStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert()); } @@ -94,6 +96,7 @@ private EnabledState getDisabledState() { public void reset() { GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); enable.setSelected(settings.isEnabled()); + optimizeImports.setSelected(settings.shouldOptimizeImports()); styleComboBox.setSelectedItem(UiFormatterStyle.convert(settings.getStyle())); } @@ -101,6 +104,7 @@ public void reset() { public boolean isModified() { GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); return enable.isSelected() != settings.isEnabled() + || optimizeImports.isSelected() != settings.shouldOptimizeImports() || !styleComboBox.getSelectedItem().equals(UiFormatterStyle.convert(settings.getStyle())); } diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java index 425124219..dc56975fc 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatImportOptimizer.java @@ -34,8 +34,11 @@ public class GoogleJavaFormatImportOptimizer implements ImportOptimizer { @Override public boolean supports(@NotNull PsiFile file) { + GoogleJavaFormatSettings projectSettings = + GoogleJavaFormatSettings.getInstance(file.getProject()); return JavaFileType.INSTANCE.equals(file.getFileType()) - && GoogleJavaFormatSettings.getInstance(file.getProject()).isEnabled(); + && projectSettings.isEnabled() + && projectSettings.shouldOptimizeImports(); } @Override diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java index 4546f68a3..b866fea76 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java @@ -79,6 +79,14 @@ void setStyle(JavaFormatterOptions.Style style) { state.style = style; } + boolean shouldOptimizeImports() { + return state.optimizeImports; + } + + void setOptimizeImports(boolean optimizeImports) { + state.optimizeImports = optimizeImports; + } + enum EnabledState { UNKNOWN, ENABLED, @@ -89,6 +97,7 @@ static class State { private EnabledState enabled = EnabledState.UNKNOWN; public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE; + boolean optimizeImports = true; // enabled used to be a boolean so we use bean property methods for backwards compatibility public void setEnabled(@Nullable String enabledStr) { From 209cb12587855b6317a7d503c4d24942e4e24386 Mon Sep 17 00:00:00 2001 From: Ohle Claussen Date: Fri, 5 Jul 2024 09:19:09 +0200 Subject: [PATCH 2/3] Fall back on default java imports optimizer --- .../intellij/GoogleJavaFormatFormattingService.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java index 9d2d7a595..6db650877 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingService.java @@ -27,6 +27,7 @@ import com.intellij.formatting.service.AsyncFormattingRequest; import com.intellij.ide.highlighter.JavaFileType; import com.intellij.lang.ImportOptimizer; +import com.intellij.lang.java.JavaImportOptimizer; import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; import com.intellij.psi.PsiFile; @@ -41,6 +42,9 @@ public class GoogleJavaFormatFormattingService extends AsyncDocumentFormattingSe public static final ImmutableSet IMPORT_OPTIMIZERS = ImmutableSet.of(new GoogleJavaFormatImportOptimizer()); + private static final ImmutableSet DEFAULT_OPTIMIZERS = + ImmutableSet.of(new JavaImportOptimizer()); + @Override protected FormattingTask createFormattingTask(AsyncFormattingRequest request) { Project project = request.getContext().getProject(); @@ -85,7 +89,11 @@ public boolean canFormat(@NotNull PsiFile file) { @Override public @NotNull Set getImportOptimizers(@NotNull PsiFile file) { - return IMPORT_OPTIMIZERS; + if (GoogleJavaFormatSettings.getInstance(file.getProject()).shouldOptimizeImports()) { + return IMPORT_OPTIMIZERS; + } else { + return DEFAULT_OPTIMIZERS; + } } private static final class GoogleJavaFormatFormattingTask implements FormattingTask { From 94eb60861f97151758a7469ae3045f7abceaf91e Mon Sep 17 00:00:00 2001 From: Ohle Claussen Date: Fri, 5 Jul 2024 09:19:09 +0200 Subject: [PATCH 3/3] Make sure setting can be persisted --- .../googlejavaformat/intellij/GoogleJavaFormatSettings.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java index b866fea76..5a0d1e9a3 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java @@ -97,7 +97,7 @@ static class State { private EnabledState enabled = EnabledState.UNKNOWN; public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE; - boolean optimizeImports = true; + public boolean optimizeImports = true; // enabled used to be a boolean so we use bean property methods for backwards compatibility public void setEnabled(@Nullable String enabledStr) {