Skip to content
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
4 changes: 2 additions & 2 deletions idea_plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down
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,7 +18,7 @@
</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">
Expand All @@ -35,6 +35,20 @@
</constraints>
<properties/>
</component>
<component id="e92a1" class="javax.swing.JLabel">
<constraints>
<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="Max line length"/>
</properties>
</component>
<component id="a1b2c" class="com.intellij.ui.components.JBTextField" binding="maxLineLengthField" custom-create="true">
<constraints>
<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"/>
</constraints>
<properties/>
</component>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -95,20 +109,24 @@ 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
public void disposeUIResources() {}

private void createUIComponents() {
styleComboBox = new ComboBox<>(UiFormatterStyle.values());
maxLineLengthField = new JBTextField();
maxLineLengthField.setColumns(4);
}

{
Expand All @@ -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(
Expand All @@ -150,7 +168,7 @@ private void createUIComponents() {
panel.add(
spacer1,
new GridConstraints(
2,
3,
0,
1,
2,
Expand Down Expand Up @@ -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));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading