diff --git a/idea_plugin/build.gradle.kts b/idea_plugin/build.gradle.kts index 2b753a5c9..8459b7aaf 100644 --- a/idea_plugin/build.gradle.kts +++ b/idea_plugin/build.gradle.kts @@ -30,8 +30,8 @@ repositories { } // https://github.com/google/google-java-format/releases -val googleJavaFormatVersion = "1.36.1" -val pluginPatchVersion = "1" +val googleJavaFormatVersion = "1.37.0" +val pluginPatchVersion = "0" java { toolchain { languageVersion = JavaLanguageVersion.of(21) } 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..7f299f589 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 @@
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 3a98c2eb9..06867e1d5 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 @@ -16,12 +16,14 @@ package com.google.googlejavaformat.intellij; +import com.google.googlejavaformat.Doc; import com.google.googlejavaformat.intellij.GoogleJavaFormatSettings.EnabledState; import com.intellij.openapi.options.BaseConfigurable; import com.intellij.openapi.options.ConfigurationException; import com.intellij.openapi.options.SearchableConfigurable; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.ComboBox; +import com.intellij.ui.components.JBTextField; import com.intellij.uiDesigner.core.GridConstraints; import com.intellij.uiDesigner.core.GridLayoutManager; import com.intellij.uiDesigner.core.Spacer; @@ -41,6 +43,7 @@ class GoogleJavaFormatConfigurable extends BaseConfigurable implements Searchabl private JPanel panel; private JCheckBox enable; private JComboBox styleComboBox; + private JBTextField maxLineLengthField; public GoogleJavaFormatConfigurable(Project project) { this.project = project; @@ -81,6 +84,17 @@ public void apply() throws ConfigurationException { GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); settings.setEnabled(enable.isSelected() ? EnabledState.ENABLED : getDisabledState()); settings.setStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert()); + int maxLineLength; + try { + maxLineLength = Integer.parseInt(maxLineLengthField.getText().trim()); + } catch (NumberFormatException e) { + throw new ConfigurationException("Max line length must be a valid integer"); + } + if (maxLineLength <= 0 || maxLineLength >= Doc.MAX_LINE_WIDTH) { + throw new ConfigurationException( + String.format("Max line length must be between 1 and %d", Doc.MAX_LINE_WIDTH - 1)); + } + settings.setMaxLineLength(maxLineLength); } private EnabledState getDisabledState() { @@ -95,13 +109,15 @@ public void reset() { GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); enable.setSelected(settings.isEnabled()); styleComboBox.setSelectedItem(UiFormatterStyle.convert(settings.getStyle())); + maxLineLengthField.setText(String.valueOf(settings.getMaxLineLength())); } @Override public boolean isModified() { GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); return enable.isSelected() != settings.isEnabled() - || !styleComboBox.getSelectedItem().equals(UiFormatterStyle.convert(settings.getStyle())); + || !styleComboBox.getSelectedItem().equals(UiFormatterStyle.convert(settings.getStyle())) + || !maxLineLengthField.getText().trim().equals(String.valueOf(settings.getMaxLineLength())); } @Override @@ -109,6 +125,8 @@ public void disposeUIResources() {} private void createUIComponents() { styleComboBox = new ComboBox<>(UiFormatterStyle.values()); + maxLineLengthField = new JBTextField(); + maxLineLengthField.setColumns(4); } { @@ -127,7 +145,7 @@ private void createUIComponents() { private void $$$setupUI$$$() { createUIComponents(); panel = new JPanel(); - panel.setLayout(new GridLayoutManager(3, 2, new Insets(0, 0, 0, 0), -1, -1)); + panel.setLayout(new GridLayoutManager(4, 2, new Insets(0, 0, 0, 0), -1, -1)); enable = new JCheckBox(); enable.setText("Enable google-java-format"); panel.add( @@ -150,7 +168,7 @@ private void createUIComponents() { panel.add( spacer1, new GridConstraints( - 2, + 3, 0, 1, 2, @@ -197,6 +215,40 @@ private void createUIComponents() { null, 1, false)); + final JLabel label2 = new JLabel(); + label2.setText("Max line length"); + panel.add( + label2, + new GridConstraints( + 2, + 0, + 1, + 1, + GridConstraints.ANCHOR_WEST, + GridConstraints.FILL_NONE, + GridConstraints.SIZEPOLICY_FIXED, + GridConstraints.SIZEPOLICY_FIXED, + null, + null, + null, + 0, + false)); + panel.add( + maxLineLengthField, + new GridConstraints( + 2, + 1, + 1, + 1, + GridConstraints.ANCHOR_WEST, + GridConstraints.FILL_NONE, + GridConstraints.SIZEPOLICY_FIXED, + GridConstraints.SIZEPOLICY_FIXED, + null, + null, + null, + 1, + false)); } /** 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..d2f865bf5 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 @@ -72,11 +72,20 @@ boolean isUninitialized() { } JavaFormatterOptions.Style getStyle() { - return state.style; + return state.style.convert().toBuilder().maxLineLength(state.maxLineLength).build(); } void setStyle(JavaFormatterOptions.Style style) { - state.style = style; + state.style = UiFormatterStyle.convert(style); + state.maxLineLength = style.maxLineLength(); + } + + int getMaxLineLength() { + return state.maxLineLength; + } + + void setMaxLineLength(int maxLineLength) { + state.maxLineLength = maxLineLength; } enum EnabledState { @@ -88,7 +97,8 @@ enum EnabledState { static class State { private EnabledState enabled = EnabledState.UNKNOWN; - public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE; + public UiFormatterStyle style = UiFormatterStyle.GOOGLE; + public int maxLineLength = JavaFormatterOptions.Style.GOOGLE.maxLineLength(); // enabled used to be a boolean so we use bean property methods for backwards compatibility public void setEnabled(@Nullable String enabledStr) { diff --git a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/UiFormatterStyle.java b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/UiFormatterStyle.java index 24ed6f62c..251bf0a95 100644 --- a/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/UiFormatterStyle.java +++ b/idea_plugin/src/main/java/com/google/googlejavaformat/intellij/UiFormatterStyle.java @@ -18,8 +18,6 @@ import com.google.googlejavaformat.java.JavaFormatterOptions; import com.google.googlejavaformat.java.JavaFormatterOptions.Style; -import java.util.Arrays; -import java.util.Objects; /** Configuration options for the formatting style. */ enum UiFormatterStyle { @@ -44,9 +42,6 @@ public JavaFormatterOptions.Style convert() { } static UiFormatterStyle convert(JavaFormatterOptions.Style style) { - return Arrays.stream(UiFormatterStyle.values()) - .filter(value -> Objects.equals(value.style, style)) - .findFirst() - .get(); + return style.isAosp() ? AOSP : GOOGLE; } } diff --git a/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingServiceTest.java b/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingServiceTest.java index bdf50a934..4a90079e7 100644 --- a/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingServiceTest.java +++ b/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatFormattingServiceTest.java @@ -116,6 +116,31 @@ public void aospStyle() throws Exception { assertThat(delegatingFormatter.wasInvoked()).isTrue(); } + @Test + public void customMaxLineLength() throws Exception { + settings.setMaxLineLength(40); + PsiFile file = + createPsiFile( + "com/foo/FormatTest.java", + "package com.foo;", + "public class FormatTest {", + "void foo() {", + "System.out.println(\"This is a long string that wraps\");", + "}", + "}"); + String origText = file.getText(); + CodeStyleManager manager = CodeStyleManager.getInstance(file.getProject()); + WriteCommandAction.runWriteCommandAction( + file.getProject(), () -> manager.reformatText(file, 0, file.getTextLength())); + + Style styleWith40 = Style.GOOGLE.toBuilder().maxLineLength(40).build(); + assertThat(file.getText()) + .isEqualTo( + new Formatter(JavaFormatterOptions.builder().style(styleWith40).build()) + .formatSource(origText)); + assertThat(delegatingFormatter.wasInvoked()).isTrue(); + } + @Test public void canChangeWhitespaceOnlyDoesNotReorderModifiers() throws Exception { settings.setStyle(Style.GOOGLE); diff --git a/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettingsTest.java b/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettingsTest.java new file mode 100644 index 000000000..0aa2d30bf --- /dev/null +++ b/idea_plugin/src/test/java/com/google/googlejavaformat/intellij/GoogleJavaFormatSettingsTest.java @@ -0,0 +1,46 @@ +package com.google.googlejavaformat.intellij; + +import static com.google.common.truth.Truth.assertThat; + +import com.google.googlejavaformat.intellij.GoogleJavaFormatSettings.State; +import com.google.googlejavaformat.java.JavaFormatterOptions.Style; +import com.intellij.configurationStore.XmlSerializer; +import org.jdom.Element; +import org.junit.Test; + +public class GoogleJavaFormatSettingsTest { + @Test + public void defaultMaxLineLengthMatchesGoogleStyle() { + State state = new State(); + assertThat(state.maxLineLength).isEqualTo(Style.GOOGLE.maxLineLength()); + } + + @Test + public void serializeAndDeserialize() { + State state = new State(); + state.setEnabled("true"); + state.style = UiFormatterStyle.AOSP; + state.maxLineLength = 120; + + Element element = XmlSerializer.serialize(state); + State loaded = XmlSerializer.deserialize(element, State.class); + + assertThat(loaded.getEnabled()).isEqualTo("true"); + assertThat(loaded.style).isEqualTo(UiFormatterStyle.AOSP); + assertThat(loaded.maxLineLength).isEqualTo(120); + } + + @Test + public void getAndSetStylePreservesMaxLineLength() { + GoogleJavaFormatSettings.State state = new GoogleJavaFormatSettings.State(); + state.maxLineLength = 140; + state.style = UiFormatterStyle.GOOGLE; + + assertThat( + state.style.convert().toBuilder() + .maxLineLength(state.maxLineLength) + .build() + .maxLineLength()) + .isEqualTo(140); + } +}