Skip to content

Commit 365e36f

Browse files
cushonronshapiro
authored andcommitted
Refactor max line length handling
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=244858188
1 parent 94275bf commit 365e36f

File tree

8 files changed

+23
-54
lines changed

8 files changed

+23
-54
lines changed

core/src/main/java/com/google/googlejavaformat/java/FormatFileCallable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public String call() throws FormatterException {
4545
new Formatter(options).formatSource(input, characterRanges(input).asRanges());
4646
formatted = fixImports(formatted);
4747
if (parameters.reflowLongStrings()) {
48-
formatted = StringWrapper.wrap(options.maxLineLength(), formatted);
48+
formatted = StringWrapper.wrap(Formatter.MAX_LINE_LENGTH, formatted);
4949
}
5050
return formatted;
5151
}

core/src/main/java/com/google/googlejavaformat/java/Formatter.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@
8787
@Immutable
8888
public final class Formatter {
8989

90+
public static final int MAX_LINE_LENGTH = 100;
91+
9092
static final Range<Integer> EMPTY_RANGE = Range.closedOpen(-1, -1);
9193

9294
private final JavaFormatterOptions options;
@@ -156,8 +158,7 @@ public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOExcept
156158
builder.sync(javaInput.getText().length());
157159
builder.drain();
158160
Doc doc = new DocBuilder().withOps(builder.build()).build();
159-
doc.computeBreaks(
160-
javaOutput.getCommentsHelper(), options.maxLineLength(), new Doc.State(+0, 0));
161+
doc.computeBreaks(javaOutput.getCommentsHelper(), MAX_LINE_LENGTH, new Doc.State(+0, 0));
161162
doc.write(javaOutput);
162163
javaOutput.flush();
163164
}

core/src/main/java/com/google/googlejavaformat/java/JavaCommentsHelper.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,10 @@
2929
/** {@code JavaCommentsHelper} extends {@link CommentsHelper} to rewrite Java comments. */
3030
public final class JavaCommentsHelper implements CommentsHelper {
3131

32-
private final JavaFormatterOptions options;
3332
private final String lineSeparator;
3433

3534
public JavaCommentsHelper(String lineSeparator, JavaFormatterOptions options) {
3635
this.lineSeparator = lineSeparator;
37-
this.options = options;
3836
}
3937

4038
@Override
@@ -44,7 +42,7 @@ public String rewrite(Tok tok, int maxWidth, int column0) {
4442
}
4543
String text = tok.getOriginalText();
4644
if (tok.isJavadocComment()) {
47-
text = JavadocFormatter.formatJavadoc(text, column0, options);
45+
text = JavadocFormatter.formatJavadoc(text, column0);
4846
}
4947
List<String> lines = new ArrayList<>();
5048
Iterator<String> it = Newlines.lineIterator(text);
@@ -92,7 +90,7 @@ private String preserveIndentation(List<String> lines, int column0) {
9290

9391
// Wraps and re-indents line comments.
9492
private String indentLineComments(List<String> lines, int column0) {
95-
lines = wrapLineComments(lines, column0, options);
93+
lines = wrapLineComments(lines, column0);
9694
StringBuilder builder = new StringBuilder();
9795
builder.append(lines.get(0).trim());
9896
String indentString = Strings.repeat(" ", column0);
@@ -107,8 +105,7 @@ private String indentLineComments(List<String> lines, int column0) {
107105
private static final Pattern LINE_COMMENT_MISSING_SPACE_PREFIX =
108106
Pattern.compile("^(//+)(?!noinspection|\\$NON-NLS-\\d+\\$)[^\\s/]");
109107

110-
private List<String> wrapLineComments(
111-
List<String> lines, int column0, JavaFormatterOptions options) {
108+
private List<String> wrapLineComments(List<String> lines, int column0) {
112109
List<String> result = new ArrayList<>();
113110
for (String line : lines) {
114111
// Add missing leading spaces to line comments: `//foo` -> `// foo`.
@@ -122,8 +119,8 @@ private List<String> wrapLineComments(
122119
result.add(line);
123120
continue;
124121
}
125-
while (line.length() + column0 > options.maxLineLength()) {
126-
int idx = options.maxLineLength() - column0;
122+
while (line.length() + column0 > Formatter.MAX_LINE_LENGTH) {
123+
int idx = Formatter.MAX_LINE_LENGTH - column0;
127124
// only break on whitespace characters, and ignore the leading `// `
128125
while (idx >= 2 && !CharMatcher.whitespace().matches(line.charAt(idx))) {
129126
idx--;

core/src/main/java/com/google/googlejavaformat/java/JavaFormatterOptions.java

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.google.googlejavaformat.java;
1616

1717
import com.google.errorprone.annotations.Immutable;
18-
import com.google.googlejavaformat.java.javadoc.JavadocOptions;
1918

2019
/**
2120
* Options for a google-java-format invocation.
@@ -28,9 +27,7 @@
2827
* preferences, and in fact it would work directly against our primary goals.
2928
*/
3029
@Immutable
31-
public class JavaFormatterOptions implements JavadocOptions {
32-
33-
static final int DEFAULT_MAX_LINE_LENGTH = 100;
30+
public class JavaFormatterOptions {
3431

3532
public enum Style {
3633

@@ -57,12 +54,6 @@ private JavaFormatterOptions(Style style) {
5754
this.style = style;
5855
}
5956

60-
/** Returns the maximum formatted width */
61-
@Override
62-
public int maxLineLength() {
63-
return DEFAULT_MAX_LINE_LENGTH;
64-
}
65-
6657
/** Returns the multiplier for the unit of indent */
6758
public int indentationMultiplier() {
6859
return style.indentationMultiplier();

core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
public final class StringWrapper {
6565
/** Reflows long string literals in the given Java source code. */
6666
public static String wrap(String input) throws FormatterException {
67-
return StringWrapper.wrap(JavaFormatterOptions.defaultOptions().maxLineLength(), input);
67+
return StringWrapper.wrap(Formatter.MAX_LINE_LENGTH, input);
6868
}
6969

7070
/**

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocFormatter.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,23 +35,26 @@
3535
* single blank line if it's empty.
3636
*/
3737
public final class JavadocFormatter {
38+
39+
static final int MAX_LINE_LENGTH = 100;
40+
3841
/**
3942
* Formats the given Javadoc comment, which must start with ∕✱✱ and end with ✱∕. The output will
4043
* start and end with the same characters.
4144
*/
42-
public static String formatJavadoc(String input, int blockIndent, JavadocOptions options) {
45+
public static String formatJavadoc(String input, int blockIndent) {
4346
ImmutableList<Token> tokens;
4447
try {
4548
tokens = lex(input);
4649
} catch (LexException e) {
4750
return input;
4851
}
49-
String result = render(tokens, blockIndent, options);
50-
return makeSingleLineIfPossible(blockIndent, result, options);
52+
String result = render(tokens, blockIndent);
53+
return makeSingleLineIfPossible(blockIndent, result);
5154
}
5255

53-
private static String render(List<Token> input, int blockIndent, JavadocOptions options) {
54-
JavadocWriter output = new JavadocWriter(blockIndent, options);
56+
private static String render(List<Token> input, int blockIndent) {
57+
JavadocWriter output = new JavadocWriter(blockIndent);
5558
for (Token token : input) {
5659
switch (token.getType()) {
5760
case BEGIN_JAVADOC:
@@ -162,9 +165,8 @@ private static Token standardize(Token token, Token standardToken) {
162165
* Returns the given string or a one-line version of it (e.g., "∕✱✱ Tests for foos. ✱∕") if it
163166
* fits on one line.
164167
*/
165-
private static String makeSingleLineIfPossible(
166-
int blockIndent, String input, JavadocOptions options) {
167-
int oneLinerContentLength = options.maxLineLength() - "/** */".length() - blockIndent;
168+
private static String makeSingleLineIfPossible(int blockIndent, String input) {
169+
int oneLinerContentLength = MAX_LINE_LENGTH - "/** */".length() - blockIndent;
168170
Matcher matcher = ONE_CONTENT_LINE_PATTERN.matcher(input);
169171
if (matcher.matches() && matcher.group(1).isEmpty()) {
170172
return "/** */";

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocOptions.java

Lines changed: 0 additions & 20 deletions
This file was deleted.

core/src/main/java/com/google/googlejavaformat/java/javadoc/JavadocWriter.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
*/
4141
final class JavadocWriter {
4242
private final int blockIndent;
43-
private final JavadocOptions options;
4443
private final StringBuilder output = new StringBuilder();
4544
/**
4645
* Whether we are inside an {@code <li>} element, excluding the case in which the {@code <li>}
@@ -60,9 +59,8 @@ final class JavadocWriter {
6059
private int indentForMoeEndStripComment;
6160
private boolean wroteAnythingSignificant;
6261

63-
JavadocWriter(int blockIndent, JavadocOptions options) {
62+
JavadocWriter(int blockIndent) {
6463
this.blockIndent = blockIndent;
65-
this.options = checkNotNull(options);
6664
}
6765

6866
/**
@@ -375,7 +373,7 @@ private void writeNewline(AutoIndent autoIndent) {
375373
appendSpaces(blockIndent + 1);
376374
output.append("*");
377375
appendSpaces(1);
378-
remainingOnLine = options.maxLineLength() - blockIndent - 3;
376+
remainingOnLine = JavadocFormatter.MAX_LINE_LENGTH - blockIndent - 3;
379377
if (autoIndent == AUTO_INDENT) {
380378
appendSpaces(innerIndent());
381379
remainingOnLine -= innerIndent();

0 commit comments

Comments
 (0)