Skip to content

Commit

Permalink
Make SnippetFormatter support other styles
Browse files Browse the repository at this point in the history
An alternative approach would be to pass in the formatter; see
google#251.
  • Loading branch information
Stephan202 committed Jan 27, 2019
1 parent 148f08d commit ff4c2dd
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(' ');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Replacement> 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<Replacement> 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"));
}
}

0 comments on commit ff4c2dd

Please sign in to comment.