Skip to content
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

Add option to use default Java import optimizer #1118

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.google.googlejavaformat.intellij.GoogleJavaFormatConfigurable">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="4" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
Expand All @@ -18,23 +18,31 @@
</component>
<vspacer id="19e83">
<constraints>
<grid row="2" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="3" column="0" row-span="1" col-span="2" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="c93e1" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Code style"/>
</properties>
</component>
<component id="31761" class="javax.swing.JComboBox" binding="styleComboBox" custom-create="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="1" use-parent-layout="false"/>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="1" use-parent-layout="false"/>
</constraints>
<properties/>
</component>
<component id="22ecd" class="javax.swing.JCheckBox" binding="optimizeImports">
<constraints>
<grid row="1" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Optimize imports with google-java-format"/>
</properties>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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());
}

Expand All @@ -94,13 +96,15 @@ 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()));
}

@Override
public boolean isModified() {
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
return enable.isSelected() != settings.isEnabled()
|| optimizeImports.isSelected() != settings.shouldOptimizeImports()
|| !styleComboBox.getSelectedItem().equals(UiFormatterStyle.convert(settings.getStyle()));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -41,6 +42,9 @@ public class GoogleJavaFormatFormattingService extends AsyncDocumentFormattingSe
public static final ImmutableSet<ImportOptimizer> IMPORT_OPTIMIZERS =
ImmutableSet.of(new GoogleJavaFormatImportOptimizer());

private static final ImmutableSet<ImportOptimizer> DEFAULT_OPTIMIZERS =
ImmutableSet.of(new JavaImportOptimizer());

@Override
protected FormattingTask createFormattingTask(AsyncFormattingRequest request) {
Project project = request.getContext().getProject();
Expand Down Expand Up @@ -85,7 +89,11 @@ public boolean canFormat(@NotNull PsiFile file) {

@Override
public @NotNull Set<ImportOptimizer> 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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -89,6 +97,7 @@ static class State {

private EnabledState enabled = EnabledState.UNKNOWN;
public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE;
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) {
Expand Down