|
| 1 | +package com.google.googlejavaformat.java; |
| 2 | + |
| 3 | +import com.google.common.base.Preconditions; |
| 4 | +import com.google.common.collect.Range; |
| 5 | +import com.google.googlejavaformat.java.SnippetFormatter.SnippetKind; |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import org.eclipse.jdt.core.dom.ASTParser; |
| 9 | +import org.eclipse.jdt.core.formatter.CodeFormatter; |
| 10 | +import org.eclipse.jface.text.IRegion; |
| 11 | +import org.eclipse.jface.text.Region; |
| 12 | +import org.eclipse.text.edits.MultiTextEdit; |
| 13 | +import org.eclipse.text.edits.ReplaceEdit; |
| 14 | +import org.eclipse.text.edits.TextEdit; |
| 15 | + |
| 16 | +/** Runs the Google Java formatter on the given code. */ |
| 17 | +public class GoogleJavaFormatter extends CodeFormatter { |
| 18 | + |
| 19 | + private static final int INDENTATION_SIZE = 2; |
| 20 | + |
| 21 | + @Override |
| 22 | + public TextEdit format( |
| 23 | + int kind, String source, int offset, int length, int indentationLevel, String lineSeparator) { |
| 24 | + IRegion[] regions = new IRegion[] {new Region(offset, length)}; |
| 25 | + return formatInternal(kind, source, regions, indentationLevel); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + public TextEdit format( |
| 30 | + int kind, String source, IRegion[] regions, int indentationLevel, String lineSeparator) { |
| 31 | + return formatInternal(kind, source, regions, indentationLevel); |
| 32 | + } |
| 33 | + |
| 34 | + @Override |
| 35 | + public String createIndentationString(int indentationLevel) { |
| 36 | + Preconditions.checkArgument( |
| 37 | + indentationLevel >= 0, |
| 38 | + "Indentation level cannot be less than zero. Given: %s", |
| 39 | + indentationLevel); |
| 40 | + int spaces = indentationLevel * INDENTATION_SIZE; |
| 41 | + StringBuilder buf = new StringBuilder(spaces); |
| 42 | + for (int i = 0; i < spaces; i++) { |
| 43 | + buf.append(' '); |
| 44 | + } |
| 45 | + return buf.toString(); |
| 46 | + } |
| 47 | + |
| 48 | + /** Runs the Google Java formatter on the given source, with only the given ranges specified. */ |
| 49 | + private TextEdit formatInternal(int kind, String source, IRegion[] regions, int initialIndent) { |
| 50 | + try { |
| 51 | + boolean includeComments = |
| 52 | + (kind & CodeFormatter.F_INCLUDE_COMMENTS) == CodeFormatter.F_INCLUDE_COMMENTS; |
| 53 | + kind &= ~CodeFormatter.F_INCLUDE_COMMENTS; |
| 54 | + SnippetKind snippetKind; |
| 55 | + switch (kind) { |
| 56 | + case ASTParser.K_EXPRESSION: |
| 57 | + snippetKind = SnippetKind.EXPRESSION; |
| 58 | + break; |
| 59 | + case ASTParser.K_STATEMENTS: |
| 60 | + snippetKind = SnippetKind.STATEMENTS; |
| 61 | + break; |
| 62 | + case ASTParser.K_CLASS_BODY_DECLARATIONS: |
| 63 | + snippetKind = SnippetKind.CLASS_BODY_DECLARATIONS; |
| 64 | + break; |
| 65 | + case ASTParser.K_COMPILATION_UNIT: |
| 66 | + snippetKind = SnippetKind.COMPILATION_UNIT; |
| 67 | + break; |
| 68 | + default: |
| 69 | + throw new IllegalArgumentException(String.format("Unknown snippet kind: %d", kind)); |
| 70 | + } |
| 71 | + List<Replacement> replacements = |
| 72 | + new SnippetFormatter() |
| 73 | + .format( |
| 74 | + snippetKind, source, rangesFromRegions(regions), initialIndent, includeComments); |
| 75 | + if (idempotent(source, regions, replacements)) { |
| 76 | + // Do not create edits if there's no diff. |
| 77 | + return null; |
| 78 | + } |
| 79 | + // Convert replacements to text edits. |
| 80 | + return editFromReplacements(replacements); |
| 81 | + } catch (IllegalArgumentException | FormatterException exception) { |
| 82 | + // Do not format on errors. |
| 83 | + return null; |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + private List<Range<Integer>> rangesFromRegions(IRegion[] regions) { |
| 88 | + List<Range<Integer>> ranges = new ArrayList<>(); |
| 89 | + for (IRegion region : regions) { |
| 90 | + ranges.add(Range.closedOpen(region.getOffset(), region.getOffset() + region.getLength())); |
| 91 | + } |
| 92 | + return ranges; |
| 93 | + } |
| 94 | + |
| 95 | + /** @return {@code true} if input and output texts are equal, else {@code false}. */ |
| 96 | + private boolean idempotent(String source, IRegion[] regions, List<Replacement> replacements) { |
| 97 | + // This implementation only checks for single replacement. |
| 98 | + if (replacements.size() == 1) { |
| 99 | + Replacement replacement = replacements.get(0); |
| 100 | + String output = replacement.getReplacementString(); |
| 101 | + // Entire source case: input = output, nothing changed. |
| 102 | + if (output.equals(source)) { |
| 103 | + return true; |
| 104 | + } |
| 105 | + // Single region and single replacement case: if they are equal, nothing changed. |
| 106 | + if (regions.length == 1) { |
| 107 | + Range<Integer> range = replacement.getReplaceRange(); |
| 108 | + String snippet = source.substring(range.lowerEndpoint(), range.upperEndpoint()); |
| 109 | + if (output.equals(snippet)) { |
| 110 | + return true; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return false; |
| 115 | + } |
| 116 | + |
| 117 | + private TextEdit editFromReplacements(List<Replacement> replacements) { |
| 118 | + // Split the replacements that cross line boundaries. |
| 119 | + TextEdit edit = new MultiTextEdit(); |
| 120 | + for (Replacement replacement : replacements) { |
| 121 | + Range<Integer> replaceRange = replacement.getReplaceRange(); |
| 122 | + edit.addChild( |
| 123 | + new ReplaceEdit( |
| 124 | + replaceRange.lowerEndpoint(), |
| 125 | + replaceRange.upperEndpoint() - replaceRange.lowerEndpoint(), |
| 126 | + replacement.getReplacementString())); |
| 127 | + } |
| 128 | + return edit; |
| 129 | + } |
| 130 | +} |
0 commit comments