Skip to content

Commit

Permalink
configurable max line length
Browse files Browse the repository at this point in the history
  • Loading branch information
kuzyanov committed Dec 16, 2019
1 parent d1f6d89 commit c395ee4
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 22 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.idea/
*.ims
*.iml
**/.gradle

.classpath
.project
Expand All @@ -12,4 +13,5 @@ target/

bin/
out/
idea_plugin/build
eclipse_plugin/lib/
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@
@Immutable
public final class Formatter {

public static final int MAX_LINE_LENGTH = 100;

static final Range<Integer> EMPTY_RANGE = Range.closedOpen(-1, -1);

private final JavaFormatterOptions options;
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ private List<String> wrapLineComments(List<String> 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--;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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. */
Expand All @@ -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();
Expand All @@ -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() {}

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

/**
Expand Down
3 changes: 2 additions & 1 deletion idea_plugin/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ plugins {

repositories {
mavenCentral()
mavenLocal()
}

ext {
googleJavaFormatVersion = '1.7'
googleJavaFormatVersion = '1.8-SNAPSHOT'
}

apply plugin: 'org.jetbrains.intellij'
Expand Down
2 changes: 2 additions & 0 deletions idea_plugin/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

<change-notes><![CDATA[
<dl>
<dt>1.8-SNAPSHOT.0.3</dt>
<dd>Configurable max line length.</dd>
<dt>1.7.0.3</dt>
<dd>Fixed the plugin on 2019.3 IDEs.</dd>
<dt>1.7.0.2</dt>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,11 @@ private void formatInternal(PsiFile file, Collection<TextRange> ranges) {
* formatter (usually using {@link #performReplacements(Document, Map)}.
*/
private void format(Document document, Collection<TextRange> 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));
}
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,24 @@
</constraints>
<properties/>
</component>
<component id="88ea0" class="javax.swing.JFormattedTextField" binding="maxLineLengthField" custom-create="true">
<constraints>
<grid row="2" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="150" height="-1"/>
</grid>
</constraints>
<properties>
<text value="100"/>
</properties>
</component>
<component id="3a8fe" 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>
</children>
</grid>
</form>
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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() {
Expand All @@ -95,20 +95,27 @@ 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
public void disposeUIResources() {}

private void createUIComponents() {
styleComboBox = new ComboBox<>(UiFormatterStyle.values());
maxLineLengthField = new JFormattedTextField(NumberFormat.getIntegerInstance());
}

private Integer getMaxLengthFieldValue() {
return Integer.parseInt(maxLineLengthField.getText());
}

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

0 comments on commit c395ee4

Please sign in to comment.