Skip to content

Commit c79f897

Browse files
plumpycushon
authored andcommitted
Add AOSP option to the IntelliJ plugin
MOE_MIGRATED_REVID=134426986
1 parent f9b5473 commit c79f897

File tree

4 files changed

+191
-27
lines changed

4 files changed

+191
-27
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.googlejavaformat.intellij;
18+
19+
import com.google.googlejavaformat.java.JavaFormatterOptions;
20+
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
21+
22+
/**
23+
* Configuration options for the formatting style.
24+
*/
25+
public enum FormatterStyle {
26+
GOOGLE("Default Google Java style", Style.GOOGLE),
27+
AOSP("Android Open Source Project (AOSP) style", Style.AOSP);
28+
29+
private final String description;
30+
private final JavaFormatterOptions javaFormatterOptions;
31+
32+
FormatterStyle(String description, JavaFormatterOptions.Style style) {
33+
this.description = description;
34+
this.javaFormatterOptions = JavaFormatterOptions.builder().style(style).build();
35+
}
36+
37+
@Override
38+
public String toString() {
39+
return description;
40+
}
41+
42+
public JavaFormatterOptions getJavaFormatterOptions() {
43+
return javaFormatterOptions;
44+
}
45+
}
Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.google.googlejavaformat.intellij.GoogleJavaFormatConfigurable">
3-
<grid id="27dc6" binding="panel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<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">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
66
<xy x="20" y="20" width="500" height="400"/>
@@ -10,17 +10,31 @@
1010
<children>
1111
<component id="4a87f" class="javax.swing.JCheckBox" binding="enable" default-binding="true">
1212
<constraints>
13-
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
13+
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
1414
</constraints>
1515
<properties>
1616
<text value="Enable google-java-format"/>
1717
</properties>
1818
</component>
1919
<vspacer id="19e83">
2020
<constraints>
21-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
21+
<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"/>
2222
</constraints>
2323
</vspacer>
24+
<component id="c93e1" class="javax.swing.JLabel">
25+
<constraints>
26+
<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"/>
27+
</constraints>
28+
<properties>
29+
<text value="Code style"/>
30+
</properties>
31+
</component>
32+
<component id="31761" class="javax.swing.JComboBox" binding="styleComboBox" custom-create="true">
33+
<constraints>
34+
<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"/>
35+
</constraints>
36+
<properties/>
37+
</component>
2438
</children>
2539
</grid>
2640
</form>

idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java

Lines changed: 91 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,26 @@
2020
import com.intellij.openapi.options.ConfigurationException;
2121
import com.intellij.openapi.options.SearchableConfigurable;
2222
import com.intellij.openapi.project.Project;
23+
import com.intellij.openapi.ui.ComboBox;
2324
import com.intellij.uiDesigner.core.GridConstraints;
2425
import com.intellij.uiDesigner.core.GridLayoutManager;
2526
import com.intellij.uiDesigner.core.Spacer;
2627
import java.awt.Insets;
2728
import javax.swing.JCheckBox;
29+
import javax.swing.JComboBox;
2830
import javax.swing.JComponent;
31+
import javax.swing.JLabel;
2932
import javax.swing.JPanel;
3033
import org.jetbrains.annotations.Nls;
3134
import org.jetbrains.annotations.NotNull;
3235
import org.jetbrains.annotations.Nullable;
3336

34-
class GoogleJavaFormatConfigurable extends BaseConfigurable
35-
implements SearchableConfigurable {
37+
class GoogleJavaFormatConfigurable extends BaseConfigurable implements SearchableConfigurable {
3638

3739
private final Project project;
3840
private JPanel panel;
3941
private JCheckBox enable;
42+
private JComboBox styleComboBox;
4043

4144
public GoogleJavaFormatConfigurable(Project project) {
4245
this.project = project;
@@ -74,26 +77,38 @@ public JComponent createComponent() {
7477

7578
@Override
7679
public void apply() throws ConfigurationException {
77-
GoogleJavaFormatSettings.getInstance(project).setEnabled(enable.isSelected());
80+
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
81+
settings.setEnabled(enable.isSelected());
82+
settings.setStyle((FormatterStyle) styleComboBox.getSelectedItem());
7883
}
7984

8085
@Override
8186
public void reset() {
82-
enable.setSelected(GoogleJavaFormatSettings.getInstance(project).isEnabled());
87+
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
88+
enable.setSelected(settings.isEnabled());
89+
styleComboBox.setSelectedItem(settings.getStyle());
8390
}
8491

8592
@Override
8693
public boolean isModified() {
87-
return enable.isSelected() != GoogleJavaFormatSettings.getInstance(project).isEnabled();
94+
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
95+
return enable.isSelected() != settings.isEnabled()
96+
|| !styleComboBox.getSelectedItem().equals(settings.getStyle());
8897
}
8998

9099
@Override
91100
public void disposeUIResources() {}
92101

102+
private void createUIComponents() {
103+
styleComboBox = new ComboBox<>(FormatterStyle.values());
104+
}
105+
106+
// IntelliJ's UI designer generated this ugly code and then google-java-format made it even
107+
// uglier. C'est la vie.
93108
{
94-
// GUI initializer generated by IntelliJ IDEA GUI Designer
95-
// >>> IMPORTANT!! <<<
96-
// DO NOT EDIT OR ADD ANY CODE HERE!
109+
// GUI initializer generated by IntelliJ IDEA GUI Designer
110+
// >>> IMPORTANT!! <<<
111+
// DO NOT EDIT OR ADD ANY CODE HERE!
97112
$$$setupUI$$$();
98113
}
99114

@@ -104,23 +119,81 @@ public void disposeUIResources() {}
104119
* @noinspection ALL
105120
*/
106121
private void $$$setupUI$$$() {
122+
createUIComponents();
107123
panel = new JPanel();
108-
panel.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1));
124+
panel.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 0, 0), -1, -1));
109125
enable = new JCheckBox();
110126
enable.setText("Enable google-java-format");
111-
panel.add(enable,
112-
new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE,
127+
panel.add(
128+
enable,
129+
new GridConstraints(
130+
0,
131+
0,
132+
1,
133+
2,
134+
GridConstraints.ANCHOR_WEST,
135+
GridConstraints.FILL_NONE,
113136
GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW,
114-
GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
137+
GridConstraints.SIZEPOLICY_FIXED,
138+
null,
139+
null,
140+
null,
141+
0,
142+
false));
115143
final Spacer spacer1 = new Spacer();
116-
panel.add(spacer1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER,
117-
GridConstraints.FILL_VERTICAL, 1, GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0,
118-
false));
144+
panel.add(
145+
spacer1,
146+
new GridConstraints(
147+
2,
148+
0,
149+
1,
150+
2,
151+
GridConstraints.ANCHOR_CENTER,
152+
GridConstraints.FILL_VERTICAL,
153+
1,
154+
GridConstraints.SIZEPOLICY_WANT_GROW,
155+
null,
156+
null,
157+
null,
158+
0,
159+
false));
160+
final JLabel label1 = new JLabel();
161+
label1.setText("Code style");
162+
panel.add(
163+
label1,
164+
new GridConstraints(
165+
1,
166+
0,
167+
1,
168+
1,
169+
GridConstraints.ANCHOR_WEST,
170+
GridConstraints.FILL_NONE,
171+
GridConstraints.SIZEPOLICY_FIXED,
172+
GridConstraints.SIZEPOLICY_FIXED,
173+
null,
174+
null,
175+
null,
176+
0,
177+
false));
178+
panel.add(
179+
styleComboBox,
180+
new GridConstraints(
181+
1,
182+
1,
183+
1,
184+
1,
185+
GridConstraints.ANCHOR_WEST,
186+
GridConstraints.FILL_HORIZONTAL,
187+
GridConstraints.SIZEPOLICY_CAN_GROW,
188+
GridConstraints.SIZEPOLICY_FIXED,
189+
null,
190+
null,
191+
null,
192+
1,
193+
false));
119194
}
120195

121-
/**
122-
* @noinspection ALL
123-
*/
196+
/** @noinspection ALL */
124197
public JComponent $$$getRootComponent$$$() {
125198
return panel;
126199
}

idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ class GoogleJavaFormatSettings extends AbstractProjectComponent
3232
implements PersistentStateComponent<GoogleJavaFormatSettings.State> {
3333

3434
private boolean enabled = false;
35+
private FormatterStyle formatterStyle = FormatterStyle.GOOGLE;
3536

3637
protected GoogleJavaFormatSettings(Project project) {
3738
super(project);
3839
}
3940

40-
public static GoogleJavaFormatSettings getInstance(Project project) {
41+
static GoogleJavaFormatSettings getInstance(Project project) {
4142
return PeriodicalTasksCloser.getInstance()
4243
.safeGetComponent(project, GoogleJavaFormatSettings.class);
4344
}
@@ -47,32 +48,63 @@ public static GoogleJavaFormatSettings getInstance(Project project) {
4748
public State getState() {
4849
State state = new State();
4950
state.setEnabled(enabled);
51+
state.setStyle(formatterStyle);
5052
return state;
5153
}
5254

5355
@Override
5456
public void loadState(State state) {
5557
setEnabled(state.isEnabled());
58+
setStyle(state.getStyle());
5659
}
5760

58-
public boolean isEnabled() {
61+
boolean isEnabled() {
5962
return enabled;
6063
}
6164

62-
public void setEnabled(boolean enabled) {
65+
void setEnabled(boolean enabled) {
6366
this.enabled = enabled;
64-
GoogleJavaFormatInstaller.installFormatter(myProject, enabled);
67+
updateFormatterState();
68+
}
69+
70+
FormatterStyle getStyle() {
71+
return formatterStyle;
72+
}
73+
74+
void setStyle(FormatterStyle formatterStyle) {
75+
// formatterStyle can be null when users upgrade to the first version of the plugin with style
76+
// support (since it was never saved before). If so, keep the default value.
77+
if (formatterStyle == null) {
78+
this.formatterStyle = FormatterStyle.GOOGLE;
79+
} else {
80+
this.formatterStyle = formatterStyle;
81+
}
82+
updateFormatterState();
83+
}
84+
85+
private void updateFormatterState() {
86+
GoogleJavaFormatInstaller.installFormatter(
87+
myProject, enabled, this.formatterStyle.getJavaFormatterOptions());
6588
}
6689

6790
static class State {
6891
private boolean enabled = false;
92+
private FormatterStyle formatterStyle = FormatterStyle.GOOGLE;
6993

70-
public boolean isEnabled() {
94+
boolean isEnabled() {
7195
return enabled;
7296
}
7397

74-
public void setEnabled(boolean enabled) {
98+
void setEnabled(boolean enabled) {
7599
this.enabled = enabled;
76100
}
101+
102+
FormatterStyle getStyle() {
103+
return formatterStyle;
104+
}
105+
106+
void setStyle(FormatterStyle formatterStyle) {
107+
this.formatterStyle = formatterStyle;
108+
}
77109
}
78110
}

0 commit comments

Comments
 (0)