Skip to content

Commit 98daeb0

Browse files
fredywgoogle-java-format Team
authored andcommitted
Add max-line-length configuration support to the IntelliJ plugin
- Add max line length setting to GoogleJavaFormatSettings and UI. - Preserve custom maxLineLength across Google and AOSP style selection. - Add tests for settings persistence and formatting service. Fixes #1471, #1516 COPYBARA_INTEGRATE_REVIEW=#1471 from fredyw:add-max-line-length-idea-plugin a90d346 PiperOrigin-RevId: 1015351515
1 parent 056a0ac commit 98daeb0

6 files changed

Lines changed: 198 additions & 16 deletions

File tree

‎idea_plugin/src/main/java/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.form‎

Lines changed: 16 additions & 2 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="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
3+
<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">
44
<margin top="0" left="0" bottom="0" right="0"/>
55
<constraints>
66
<xy x="20" y="20" width="500" height="400"/>
@@ -18,7 +18,7 @@
1818
</component>
1919
<vspacer id="19e83">
2020
<constraints>
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"/>
21+
<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"/>
2222
</constraints>
2323
</vspacer>
2424
<component id="c93e1" class="javax.swing.JLabel">
@@ -35,6 +35,20 @@
3535
</constraints>
3636
<properties/>
3737
</component>
38+
<component id="e92a1" class="javax.swing.JLabel">
39+
<constraints>
40+
<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"/>
41+
</constraints>
42+
<properties>
43+
<text value="Max line length"/>
44+
</properties>
45+
</component>
46+
<component id="a1b2c" class="com.intellij.ui.components.JBTextField" binding="maxLineLengthField" custom-create="true">
47+
<constraints>
48+
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="1" use-parent-layout="false"/>
49+
</constraints>
50+
<properties/>
51+
</component>
3852
</children>
3953
</grid>
4054
</form>

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

Lines changed: 57 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,15 @@
1616

1717
package com.google.googlejavaformat.intellij;
1818

19+
import com.google.googlejavaformat.Doc;
1920
import com.google.googlejavaformat.intellij.GoogleJavaFormatSettings.EnabledState;
2021
import com.intellij.openapi.options.BaseConfigurable;
2122
import com.intellij.openapi.options.ConfigurationException;
2223
import com.intellij.openapi.options.SearchableConfigurable;
2324
import com.intellij.openapi.project.Project;
2425
import com.intellij.openapi.ui.ComboBox;
26+
import com.intellij.ui.SimpleListCellRenderer;
27+
import com.intellij.ui.components.JBTextField;
2528
import com.intellij.uiDesigner.core.GridConstraints;
2629
import com.intellij.uiDesigner.core.GridLayoutManager;
2730
import com.intellij.uiDesigner.core.Spacer;
@@ -41,6 +44,7 @@ class GoogleJavaFormatConfigurable extends BaseConfigurable implements Searchabl
4144
private JPanel panel;
4245
private JCheckBox enable;
4346
private JComboBox styleComboBox;
47+
private JBTextField maxLineLengthField;
4448

4549
public GoogleJavaFormatConfigurable(Project project) {
4650
this.project = project;
@@ -81,6 +85,17 @@ public void apply() throws ConfigurationException {
8185
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
8286
settings.setEnabled(enable.isSelected() ? EnabledState.ENABLED : getDisabledState());
8387
settings.setStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert());
88+
int maxLineLength;
89+
try {
90+
maxLineLength = Integer.parseInt(maxLineLengthField.getText().trim());
91+
} catch (NumberFormatException e) {
92+
throw new ConfigurationException("Max line length must be a valid integer");
93+
}
94+
if (maxLineLength <= 0 || maxLineLength >= Doc.MAX_LINE_WIDTH) {
95+
throw new ConfigurationException(
96+
String.format("Max line length must be between 1 and %d", Doc.MAX_LINE_WIDTH - 1));
97+
}
98+
settings.setMaxLineLength(maxLineLength);
8499
}
85100

86101
private EnabledState getDisabledState() {
@@ -95,20 +110,25 @@ public void reset() {
95110
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
96111
enable.setSelected(settings.isEnabled());
97112
styleComboBox.setSelectedItem(UiFormatterStyle.convert(settings.getStyle()));
113+
maxLineLengthField.setText(String.valueOf(settings.getMaxLineLength()));
98114
}
99115

100116
@Override
101117
public boolean isModified() {
102118
GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project);
103119
return enable.isSelected() != settings.isEnabled()
104-
|| !styleComboBox.getSelectedItem().equals(UiFormatterStyle.convert(settings.getStyle()));
120+
|| !styleComboBox.getSelectedItem().equals(UiFormatterStyle.convert(settings.getStyle()))
121+
|| !maxLineLengthField.getText().trim().equals(String.valueOf(settings.getMaxLineLength()));
105122
}
106123

107124
@Override
108125
public void disposeUIResources() {}
109126

110127
private void createUIComponents() {
111128
styleComboBox = new ComboBox<>(UiFormatterStyle.values());
129+
styleComboBox.setRenderer(SimpleListCellRenderer.create("", UiFormatterStyle::getDescription));
130+
maxLineLengthField = new JBTextField();
131+
maxLineLengthField.setColumns(4);
112132
}
113133

114134
{
@@ -127,7 +147,7 @@ private void createUIComponents() {
127147
private void $$$setupUI$$$() {
128148
createUIComponents();
129149
panel = new JPanel();
130-
panel.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 0, 0), -1, -1));
150+
panel.setLayout(new GridLayoutManager(4, 2, new Insets(0, 0, 0, 0), -1, -1));
131151
enable = new JCheckBox();
132152
enable.setText("Enable google-java-format");
133153
panel.add(
@@ -150,7 +170,7 @@ private void createUIComponents() {
150170
panel.add(
151171
spacer1,
152172
new GridConstraints(
153-
2,
173+
3,
154174
0,
155175
1,
156176
2,
@@ -197,6 +217,40 @@ private void createUIComponents() {
197217
null,
198218
1,
199219
false));
220+
final JLabel label2 = new JLabel();
221+
label2.setText("Max line length");
222+
panel.add(
223+
label2,
224+
new GridConstraints(
225+
2,
226+
0,
227+
1,
228+
1,
229+
GridConstraints.ANCHOR_WEST,
230+
GridConstraints.FILL_NONE,
231+
GridConstraints.SIZEPOLICY_FIXED,
232+
GridConstraints.SIZEPOLICY_FIXED,
233+
null,
234+
null,
235+
null,
236+
0,
237+
false));
238+
panel.add(
239+
maxLineLengthField,
240+
new GridConstraints(
241+
2,
242+
1,
243+
1,
244+
1,
245+
GridConstraints.ANCHOR_WEST,
246+
GridConstraints.FILL_NONE,
247+
GridConstraints.SIZEPOLICY_FIXED,
248+
GridConstraints.SIZEPOLICY_FIXED,
249+
null,
250+
null,
251+
null,
252+
1,
253+
false));
200254
}
201255

202256
/**

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,21 @@ boolean isUninitialized() {
7272
}
7373

7474
JavaFormatterOptions.Style getStyle() {
75-
return state.style;
75+
UiFormatterStyle style = state.style != null ? state.style : UiFormatterStyle.GOOGLE;
76+
return style.convert().toBuilder().maxLineLength(state.maxLineLength).build();
7677
}
7778

7879
void setStyle(JavaFormatterOptions.Style style) {
79-
state.style = style;
80+
state.style = UiFormatterStyle.convert(style);
81+
state.maxLineLength = style.maxLineLength();
82+
}
83+
84+
int getMaxLineLength() {
85+
return state.maxLineLength;
86+
}
87+
88+
void setMaxLineLength(int maxLineLength) {
89+
state.maxLineLength = maxLineLength;
8090
}
8191

8292
enum EnabledState {
@@ -88,7 +98,8 @@ enum EnabledState {
8898
static class State {
8999

90100
private EnabledState enabled = EnabledState.UNKNOWN;
91-
public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE;
101+
public UiFormatterStyle style = UiFormatterStyle.GOOGLE;
102+
public int maxLineLength = JavaFormatterOptions.Style.GOOGLE.maxLineLength();
92103

93104
// enabled used to be a boolean so we use bean property methods for backwards compatibility
94105
public void setEnabled(@Nullable String enabledStr) {

‎idea_plugin/src/main/java/com/google/googlejavaformat/intellij/UiFormatterStyle.java‎

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818

1919
import com.google.googlejavaformat.java.JavaFormatterOptions;
2020
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
21-
import java.util.Arrays;
22-
import java.util.Objects;
2321

2422
/** Configuration options for the formatting style. */
2523
enum UiFormatterStyle {
@@ -34,8 +32,7 @@ enum UiFormatterStyle {
3432
this.style = style;
3533
}
3634

37-
@Override
38-
public String toString() {
35+
public String getDescription() {
3936
return description;
4037
}
4138

@@ -44,9 +41,6 @@ public JavaFormatterOptions.Style convert() {
4441
}
4542

4643
static UiFormatterStyle convert(JavaFormatterOptions.Style style) {
47-
return Arrays.stream(UiFormatterStyle.values())
48-
.filter(value -> Objects.equals(value.style, style))
49-
.findFirst()
50-
.get();
44+
return style.isAosp() ? AOSP : GOOGLE;
5145
}
5246
}

‎idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingServiceTest.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,31 @@ public void aospStyle() throws Exception {
116116
assertThat(delegatingFormatter.wasInvoked()).isTrue();
117117
}
118118

119+
@Test
120+
public void customMaxLineLength() throws Exception {
121+
settings.setMaxLineLength(40);
122+
PsiFile file =
123+
createPsiFile(
124+
"com/foo/FormatTest.java",
125+
"package com.foo;",
126+
"public class FormatTest {",
127+
"void foo() {",
128+
"System.out.println(\"This is a long string that wraps\");",
129+
"}",
130+
"}");
131+
String origText = file.getText();
132+
CodeStyleManager manager = CodeStyleManager.getInstance(file.getProject());
133+
WriteCommandAction.runWriteCommandAction(
134+
file.getProject(), () -> manager.reformatText(file, 0, file.getTextLength()));
135+
136+
Style styleWith40 = Style.GOOGLE.toBuilder().maxLineLength(40).build();
137+
assertThat(file.getText())
138+
.isEqualTo(
139+
new Formatter(JavaFormatterOptions.builder().style(styleWith40).build())
140+
.formatSource(origText));
141+
assertThat(delegatingFormatter.wasInvoked()).isTrue();
142+
}
143+
119144
@Test
120145
public void canChangeWhitespaceOnlyDoesNotReorderModifiers() throws Exception {
121146
settings.setStyle(Style.GOOGLE);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2026 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 static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.googlejavaformat.intellij.GoogleJavaFormatSettings.State;
22+
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
23+
import com.intellij.configurationStore.XmlSerializer;
24+
import org.jdom.Element;
25+
import org.junit.Test;
26+
27+
public class GoogleJavaFormatSettingsTest {
28+
@Test
29+
public void defaultMaxLineLengthMatchesGoogleStyle() {
30+
State state = new State();
31+
assertThat(state.maxLineLength).isEqualTo(Style.GOOGLE.maxLineLength());
32+
}
33+
34+
@Test
35+
public void serializeAndDeserialize() {
36+
State state = new State();
37+
state.setEnabled("true");
38+
state.style = UiFormatterStyle.AOSP;
39+
state.maxLineLength = 120;
40+
41+
Element element = XmlSerializer.serialize(state);
42+
State loaded = XmlSerializer.deserialize(element, State.class);
43+
44+
assertThat(loaded.getEnabled()).isEqualTo("true");
45+
assertThat(loaded.style).isEqualTo(UiFormatterStyle.AOSP);
46+
assertThat(loaded.maxLineLength).isEqualTo(120);
47+
}
48+
49+
@Test
50+
public void deserializeExistingProjectSettings() {
51+
Element element = new Element("state");
52+
Element optAosp = new Element("option");
53+
optAosp.setAttribute("name", "style");
54+
optAosp.setAttribute("value", "AOSP");
55+
element.addContent(optAosp);
56+
57+
State loaded = XmlSerializer.deserialize(element, State.class);
58+
assertThat(loaded.style).isEqualTo(UiFormatterStyle.AOSP);
59+
}
60+
61+
@Test
62+
public void nullStyleFallsBackToGoogle() {
63+
State state = new State();
64+
state.style = null;
65+
GoogleJavaFormatSettings settings = new GoogleJavaFormatSettings(null);
66+
settings.loadState(state);
67+
68+
assertThat(settings.getStyle()).isEqualTo(Style.GOOGLE);
69+
}
70+
71+
@Test
72+
public void getAndSetStylePreservesMaxLineLength() {
73+
GoogleJavaFormatSettings.State state = new GoogleJavaFormatSettings.State();
74+
state.maxLineLength = 140;
75+
state.style = UiFormatterStyle.GOOGLE;
76+
77+
assertThat(
78+
state.style.convert().toBuilder()
79+
.maxLineLength(state.maxLineLength)
80+
.build()
81+
.maxLineLength())
82+
.isEqualTo(140);
83+
}
84+
}

0 commit comments

Comments
 (0)