Skip to content

Commit ff4c2dd

Browse files
committed
Make SnippetFormatter support other styles
An alternative approach would be to pass in the formatter; see google#251.
1 parent 148f08d commit ff4c2dd

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.Range;
2121
import com.google.common.collect.RangeSet;
2222
import com.google.common.collect.TreeRangeSet;
23+
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526

@@ -56,16 +57,26 @@ public void closeBraces(int initialIndent) {
5657
}
5758
}
5859

59-
private static final int INDENTATION_SIZE = 2;
60-
private final Formatter formatter = new Formatter();
6160
private static final CharMatcher NOT_WHITESPACE = CharMatcher.whitespace().negate();
6261

62+
private final Formatter formatter;
63+
private final int indentationSize;
64+
65+
public SnippetFormatter() {
66+
this(Style.GOOGLE);
67+
}
68+
69+
public SnippetFormatter(Style style) {
70+
this.formatter = new Formatter(JavaFormatterOptions.builder().style(style).build());
71+
this.indentationSize = 2 * style.indentationMultiplier();
72+
}
73+
6374
public String createIndentationString(int indentationLevel) {
6475
Preconditions.checkArgument(
6576
indentationLevel >= 0,
6677
"Indentation level cannot be less than zero. Given: %s",
6778
indentationLevel);
68-
int spaces = indentationLevel * INDENTATION_SIZE;
79+
int spaces = indentationLevel * indentationSize;
6980
StringBuilder buf = new StringBuilder(spaces);
7081
for (int i = 0; i < spaces; i++) {
7182
buf.append(' ');

core/src/test/java/com/google/googlejavaformat/java/SnippetFormatterTest.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.collect.ImmutableList;
2020
import com.google.common.collect.Range;
21+
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
2122
import com.google.googlejavaformat.java.SnippetFormatter.SnippetKind;
2223
import java.util.List;
2324
import org.junit.Test;
@@ -104,4 +105,36 @@ public void compilationWithComments() throws FormatterException {
104105
.containsExactly(
105106
Replacement.create(Range.closedOpen(0, 24), "/** a b */\nclass Test {}\n"));
106107
}
108+
109+
@Test
110+
public void classWithMethodGoogleStyle() throws FormatterException {
111+
String input = "class Test {\nvoid f() {}\n}";
112+
List<Replacement> replacements =
113+
new SnippetFormatter(Style.GOOGLE)
114+
.format(
115+
SnippetKind.COMPILATION_UNIT,
116+
input,
117+
ImmutableList.of(Range.closedOpen(0, input.length())),
118+
4,
119+
true);
120+
assertThat(replacements)
121+
.containsExactly(
122+
Replacement.create(Range.closedOpen(0, 26), "class Test {\n void f() {}\n}\n"));
123+
}
124+
125+
@Test
126+
public void classWithMethodAospStyle() throws FormatterException {
127+
String input = "class Test {\nvoid f() {}\n}";
128+
List<Replacement> replacements =
129+
new SnippetFormatter(Style.AOSP)
130+
.format(
131+
SnippetKind.COMPILATION_UNIT,
132+
input,
133+
ImmutableList.of(Range.closedOpen(0, input.length())),
134+
4,
135+
true);
136+
assertThat(replacements)
137+
.containsExactly(
138+
Replacement.create(Range.closedOpen(0, 26), "class Test {\n void f() {}\n}\n"));
139+
}
107140
}

0 commit comments

Comments
 (0)