From c395ee4e463682ec295bf8347cfbdbc4c1241816 Mon Sep 17 00:00:00 2001 From: kuzyanov Date: Tue, 17 Dec 2019 00:21:15 +0300 Subject: [PATCH] configurable max line length --- .gitignore | 2 ++ .../java/FormatFileCallable.java | 2 +- .../googlejavaformat/java/Formatter.java | 6 ++--- .../java/JavaCommentsHelper.java | 4 ++-- .../java/JavaFormatterOptions.java | 19 ++++++++++++++-- .../googlejavaformat/java/StringWrapper.java | 4 ++-- idea_plugin/build.gradle | 3 ++- idea_plugin/resources/META-INF/plugin.xml | 2 ++ .../GoogleJavaFormatCodeStyleManager.java | 7 ++++-- .../GoogleJavaFormatConfigurable.form | 22 +++++++++++++++++-- .../GoogleJavaFormatConfigurable.java | 19 +++++++++++----- .../intellij/GoogleJavaFormatSettings.java | 18 +++++++++++++++ 12 files changed, 86 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 235f69d3d..ca386bdfd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .idea/ *.ims *.iml +**/.gradle .classpath .project @@ -12,4 +13,5 @@ target/ bin/ out/ +idea_plugin/build eclipse_plugin/lib/ diff --git a/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java b/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java index 9d8ae41ca..26b26f962 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java +++ b/core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java @@ -45,7 +45,7 @@ public String call() throws FormatterException { String formatted = formatter.formatSource(input, characterRanges(input).asRanges()); formatted = fixImports(formatted); if (parameters.reflowLongStrings()) { - formatted = StringWrapper.wrap(Formatter.MAX_LINE_LENGTH, formatted, formatter); + formatted = StringWrapper.wrap(options.maxLineLength(), formatted, formatter); } return formatted; } diff --git a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java index e53f79fcf..363c2a679 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Formatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Formatter.java @@ -87,8 +87,6 @@ @Immutable public final class Formatter { - public static final int MAX_LINE_LENGTH = 100; - static final Range EMPTY_RANGE = Range.closedOpen(-1, -1); private final JavaFormatterOptions options; @@ -158,7 +156,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept builder.sync(javaInput.getText().length()); builder.drain(); Doc doc = new DocBuilder().withOps(builder.build()).build(); - doc.computeBreaks(javaOutput.getCommentsHelper(), MAX_LINE_LENGTH, new Doc.State(+0, 0)); + doc.computeBreaks(javaOutput.getCommentsHelper(), options.maxLineLength(), new Doc.State(+0, 0)); doc.write(javaOutput); javaOutput.flush(); } @@ -219,7 +217,7 @@ public String formatSourceAndFixImports(String input) throws FormatterException input = ImportOrderer.reorderImports(input, options.style()); input = RemoveUnusedImports.removeUnusedImports(input); String formatted = formatSource(input); - formatted = StringWrapper.wrap(formatted, this); + formatted = StringWrapper.wrap(formatted, this, options); return formatted; } diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java b/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java index 346324a45..4bc262cf5 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java @@ -121,8 +121,8 @@ private List wrapLineComments(List lines, int column0) { result.add(line); continue; } - while (line.length() + column0 > Formatter.MAX_LINE_LENGTH) { - int idx = Formatter.MAX_LINE_LENGTH - column0; + while (line.length() + column0 > options.maxLineLength()) { + int idx = options.maxLineLength() - column0; // only break on whitespace characters, and ignore the leading `// ` while (idx >= 2 && !CharMatcher.whitespace().matches(line.charAt(idx))) { idx--; diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java index 4d3d30d4c..382e269e7 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java @@ -29,6 +29,8 @@ @Immutable public class JavaFormatterOptions { + public static final int DEFAULT_MAX_LINE_LENGTH = 100; + public enum Style { /** The default Google Java Style configuration. */ GOOGLE(1), @@ -49,10 +51,12 @@ int indentationMultiplier() { private final Style style; private final boolean formatJavadoc; + private final int maxLineLength; - private JavaFormatterOptions(Style style, boolean formatJavadoc) { + private JavaFormatterOptions(Style style, boolean formatJavadoc, int maxLineLength) { this.style = style; this.formatJavadoc = formatJavadoc; + this.maxLineLength = maxLineLength; } /** Returns the multiplier for the unit of indent. */ @@ -69,6 +73,11 @@ public Style style() { return style; } + /** Returns the max line length. */ + public int maxLineLength() { + return maxLineLength; + } + /** Returns the default formatting options. */ public static JavaFormatterOptions defaultOptions() { return builder().build(); @@ -83,6 +92,7 @@ public static Builder builder() { public static class Builder { private Style style = Style.GOOGLE; private boolean formatJavadoc = true; + private int maxLineLength = DEFAULT_MAX_LINE_LENGTH; private Builder() {} @@ -96,8 +106,13 @@ Builder formatJavadoc(boolean formatJavadoc) { return this; } + public Builder maxLineLength(int maxLineLength) { + this.maxLineLength = maxLineLength; + return this; + } + public JavaFormatterOptions build() { - return new JavaFormatterOptions(style, formatJavadoc); + return new JavaFormatterOptions(style, formatJavadoc, maxLineLength); } } } diff --git a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java index 6dfca5335..e32ac30c8 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -62,8 +62,8 @@ /** Wraps string literals that exceed the column limit. */ public final class StringWrapper { /** Reflows long string literals in the given Java source code. */ - public static String wrap(String input, Formatter formatter) throws FormatterException { - return StringWrapper.wrap(Formatter.MAX_LINE_LENGTH, input, formatter); + public static String wrap(String input, Formatter formatter, JavaFormatterOptions options) throws FormatterException { + return StringWrapper.wrap(options.maxLineLength(), input, formatter); } /** diff --git a/idea_plugin/build.gradle b/idea_plugin/build.gradle index 23691f62a..a6cf2de45 100644 --- a/idea_plugin/build.gradle +++ b/idea_plugin/build.gradle @@ -20,10 +20,11 @@ plugins { repositories { mavenCentral() + mavenLocal() } ext { - googleJavaFormatVersion = '1.7' + googleJavaFormatVersion = '1.8-SNAPSHOT' } apply plugin: 'org.jetbrains.intellij' diff --git a/idea_plugin/resources/META-INF/plugin.xml b/idea_plugin/resources/META-INF/plugin.xml index 8b4bb1f70..487ce588f 100644 --- a/idea_plugin/resources/META-INF/plugin.xml +++ b/idea_plugin/resources/META-INF/plugin.xml @@ -12,6 +12,8 @@ +
1.8-SNAPSHOT.0.3
+
Configurable max line length.
1.7.0.3
Fixed the plugin on 2019.3 IDEs.
1.7.0.2
diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java index 52424c2c8..d22cc51b1 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatCodeStyleManager.java @@ -126,8 +126,11 @@ private void formatInternal(PsiFile file, Collection ranges) { * formatter (usually using {@link #performReplacements(Document, Map)}. */ private void format(Document document, Collection ranges) { - Style style = GoogleJavaFormatSettings.getInstance(getProject()).getStyle(); - Formatter formatter = new Formatter(JavaFormatterOptions.builder().style(style).build()); + GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(getProject()); + Style style = settings.getStyle(); + int maxLineLength = settings.getMaxLineLength(); + JavaFormatterOptions options = JavaFormatterOptions.builder().style(style).maxLineLength(maxLineLength).build(); + Formatter formatter = new Formatter(options); performReplacements( document, FormatterUtil.getReplacements(formatter, document.getText(), ranges)); } diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.form b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.form index 1db1d7920..60c43d811 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.form +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.form @@ -1,6 +1,6 @@
- + @@ -18,7 +18,7 @@ - + @@ -35,6 +35,24 @@ + + + + + + + + + + + + + + + + + + diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java index 79859581a..ab831c3ab 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatConfigurable.java @@ -26,12 +26,10 @@ import com.intellij.uiDesigner.core.GridLayoutManager; import com.intellij.uiDesigner.core.Spacer; import java.awt.Insets; +import java.text.NumberFormat; import javax.annotation.Nullable; -import javax.swing.JCheckBox; -import javax.swing.JComboBox; -import javax.swing.JComponent; -import javax.swing.JLabel; -import javax.swing.JPanel; +import javax.swing.*; + import org.jetbrains.annotations.Nls; import org.jetbrains.annotations.NotNull; @@ -41,6 +39,7 @@ class GoogleJavaFormatConfigurable extends BaseConfigurable implements Searchabl private JPanel panel; private JCheckBox enable; private JComboBox styleComboBox; + private JFormattedTextField maxLineLengthField; public GoogleJavaFormatConfigurable(Project project) { this.project = project; @@ -81,6 +80,7 @@ public void apply() throws ConfigurationException { GoogleJavaFormatSettings settings = GoogleJavaFormatSettings.getInstance(project); settings.setEnabled(enable.isSelected() ? EnabledState.ENABLED : getDisabledState()); settings.setStyle(((UiFormatterStyle) styleComboBox.getSelectedItem()).convert()); + settings.setMaxLineLength(getMaxLengthFieldValue()); } private EnabledState getDisabledState() { @@ -95,13 +95,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())) + || getMaxLengthFieldValue() != settings.getMaxLineLength(); } @Override @@ -109,6 +111,11 @@ public void disposeUIResources() {} private void createUIComponents() { styleComboBox = new ComboBox<>(UiFormatterStyle.values()); + maxLineLengthField = new JFormattedTextField(NumberFormat.getIntegerInstance()); + } + + private Integer getMaxLengthFieldValue() { + return Integer.parseInt(maxLineLengthField.getText()); } { diff --git a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java index 15449d701..9a3b573c1 100644 --- a/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java +++ b/idea_plugin/src/com/google/googlejavaformat/intellij/GoogleJavaFormatSettings.java @@ -23,6 +23,7 @@ import com.intellij.openapi.components.Storage; import com.intellij.openapi.project.Project; import javax.annotation.Nullable; +import java.util.Optional; @State( name = "GoogleJavaFormatSettings", @@ -70,6 +71,14 @@ void setStyle(JavaFormatterOptions.Style style) { state.style = style; } + int getMaxLineLength() { + return state.maxLineLength; + } + + void setMaxLineLength(Integer maxLineLength) { + state.setMaxLineLength(maxLineLength); + } + enum EnabledState { UNKNOWN, ENABLED, @@ -80,6 +89,7 @@ static class State { private EnabledState enabled = EnabledState.UNKNOWN; public JavaFormatterOptions.Style style = JavaFormatterOptions.Style.GOOGLE; + private int maxLineLength = JavaFormatterOptions.DEFAULT_MAX_LINE_LENGTH; // enabled used to be a boolean so we use bean property methods for backwards compatibility public void setEnabled(@Nullable String enabledStr) { @@ -102,5 +112,13 @@ public String getEnabled() { return null; } } + + public int getMaxLineLength() { + return maxLineLength; + } + + public void setMaxLineLength(Integer maxLineLength) { + this.maxLineLength = Optional.ofNullable(maxLineLength).orElse(JavaFormatterOptions.DEFAULT_MAX_LINE_LENGTH); + } } }