diff --git a/core/src/main/java/com/google/googlejavaformat/java/SnippetFormatter.java b/core/src/main/java/com/google/googlejavaformat/java/SnippetFormatter.java index 3827ef958..2f7a2dd06 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/SnippetFormatter.java +++ b/core/src/main/java/com/google/googlejavaformat/java/SnippetFormatter.java @@ -20,6 +20,7 @@ import com.google.common.collect.Range; import com.google.common.collect.RangeSet; import com.google.common.collect.TreeRangeSet; +import com.google.googlejavaformat.java.JavaFormatterOptions.Style; import java.util.ArrayList; import java.util.List; @@ -56,16 +57,26 @@ public void closeBraces(int initialIndent) { } } - private static final int INDENTATION_SIZE = 2; - private final Formatter formatter = new Formatter(); private static final CharMatcher NOT_WHITESPACE = CharMatcher.whitespace().negate(); + private final Formatter formatter; + private final int indentationSize; + + public SnippetFormatter() { + this(Style.GOOGLE); + } + + public SnippetFormatter(Style style) { + this.formatter = new Formatter(JavaFormatterOptions.builder().style(style).build()); + this.indentationSize = 2 * style.indentationMultiplier(); + } + public String createIndentationString(int indentationLevel) { Preconditions.checkArgument( indentationLevel >= 0, "Indentation level cannot be less than zero. Given: %s", indentationLevel); - int spaces = indentationLevel * INDENTATION_SIZE; + int spaces = indentationLevel * indentationSize; StringBuilder buf = new StringBuilder(spaces); for (int i = 0; i < spaces; i++) { buf.append(' '); diff --git a/core/src/test/java/com/google/googlejavaformat/java/SnippetFormatterTest.java b/core/src/test/java/com/google/googlejavaformat/java/SnippetFormatterTest.java index 6cb737d4c..75a5ed13e 100644 --- a/core/src/test/java/com/google/googlejavaformat/java/SnippetFormatterTest.java +++ b/core/src/test/java/com/google/googlejavaformat/java/SnippetFormatterTest.java @@ -18,6 +18,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Range; +import com.google.googlejavaformat.java.JavaFormatterOptions.Style; import com.google.googlejavaformat.java.SnippetFormatter.SnippetKind; import java.util.List; import org.junit.Test; @@ -104,4 +105,36 @@ public void compilationWithComments() throws FormatterException { .containsExactly( Replacement.create(Range.closedOpen(0, 24), "/** a b */\nclass Test {}\n")); } + + @Test + public void classWithMethodGoogleStyle() throws FormatterException { + String input = "class Test {\nvoid f() {}\n}"; + List replacements = + new SnippetFormatter(Style.GOOGLE) + .format( + SnippetKind.COMPILATION_UNIT, + input, + ImmutableList.of(Range.closedOpen(0, input.length())), + 4, + true); + assertThat(replacements) + .containsExactly( + Replacement.create(Range.closedOpen(0, 26), "class Test {\n void f() {}\n}\n")); + } + + @Test + public void classWithMethodAospStyle() throws FormatterException { + String input = "class Test {\nvoid f() {}\n}"; + List replacements = + new SnippetFormatter(Style.AOSP) + .format( + SnippetKind.COMPILATION_UNIT, + input, + ImmutableList.of(Range.closedOpen(0, input.length())), + 4, + true); + assertThat(replacements) + .containsExactly( + Replacement.create(Range.closedOpen(0, 26), "class Test {\n void f() {}\n}\n")); + } }