SnippetFormatter { public ImmutableList<Replacement> format( SnippetKind kind, String source, List<Range<Integer>> ranges, int initialIndent, boolean includeComments) throws FormatterException { RangeSet<Integer> rangeSet = TreeRangeSet.create(); for (Range<Integer> range : ranges) { rangeSet.add(range); } if (includeComments) { if (kind != SnippetKind.COMPILATION_UNIT) { throw new IllegalArgumentException( "comment formatting is only supported for compilation units"); } return formatter.getFormatReplacements(source, ranges); } SnippetWrapper wrapper = snippetWrapper(kind, source, initialIndent); ranges = offsetRanges(ranges, wrapper.offset); String replacement = formatter.formatSource(wrapper.contents.toString(), ranges); replacement = replacement.substring( wrapper.offset, replacement.length() - (wrapper.contents.length() - wrapper.offset - source.length())); return toReplacements(source, replacement).stream() .filter(r -> rangeSet.encloses(r.getReplaceRange())) .collect(toImmutableList()); }  String createIndentationString(int indentationLevel); ImmutableList<Replacement> format(
      SnippetKind kind,
      String source,
      List<Range<Integer>> ranges,
      int initialIndent,
      boolean includeComments);  }
@Test public void expression() throws FormatterException { String input = "x\n=42"; List<Replacement> replacements = new SnippetFormatter() .format( SnippetKind.EXPRESSION, input, ImmutableList.of(Range.closedOpen(0, input.length())), 4, false); assertThat(replacements) .containsExactly(Replacement.create(1, 2, " "), Replacement.create(3, 3, " ")); }
@Test public void statement() throws FormatterException { String input = "int x\n=42;"; List<Replacement> replacements = new SnippetFormatter() .format( SnippetKind.STATEMENTS, input, ImmutableList.of(Range.closedOpen(0, input.length())), 4, false); assertThat(replacements) .containsExactly(Replacement.create(5, 6, " "), Replacement.create(7, 7, " ")); }
@Test public void classMember() throws FormatterException { String input = "void f() {\n}"; List<Replacement> replacements = new SnippetFormatter() .format( SnippetKind.CLASS_BODY_DECLARATIONS, input, ImmutableList.of(Range.closedOpen(0, input.length())), 4, false); assertThat(replacements).containsExactly(Replacement.create(10, 11, "")); }
@Test public void compilation() throws FormatterException { String input = "\nclass Test {\n}"; List<Replacement> replacements = new SnippetFormatter() .format( SnippetKind.COMPILATION_UNIT, input, ImmutableList.of(Range.closedOpen(input.indexOf("class"), input.length())), 4, false); assertThat(replacements).containsExactly(Replacement.create(22, 23, "")); }
@Test public void compilationWithComments() throws FormatterException { String input = "\nclass Test {\n}"; List<Replacement> replacements = new SnippetFormatter() .format( SnippetKind.COMPILATION_UNIT, input, ImmutableList.of(Range.closedOpen(0, input.length())), 4, true); assertThat(replacements) .containsExactly(Replacement.create(0, 24, "\nclass Test {}\n")); }