|
22 | 22 | import com.google.common.base.CharMatcher; |
23 | 23 | import com.google.common.base.Verify; |
24 | 24 | import com.google.common.collect.ImmutableList; |
| 25 | +import com.google.common.collect.ImmutableRangeSet; |
25 | 26 | import com.google.common.collect.Range; |
| 27 | +import com.google.common.collect.RangeSet; |
26 | 28 | import com.google.common.collect.TreeRangeMap; |
27 | 29 | import com.google.googlejavaformat.Newlines; |
28 | 30 | import com.sun.source.tree.BinaryTree; |
@@ -58,20 +60,36 @@ public static String wrap(String input, Formatter formatter) throws FormatterExc |
58 | 60 | /// Reflows string literals in the given Java source code that extend past the given column limit. |
59 | 61 | static String wrap(final int columnLimit, String input, Formatter formatter) |
60 | 62 | throws FormatterException { |
| 63 | + return wrap( |
| 64 | + columnLimit, input, formatter, ImmutableRangeSet.of(Range.closedOpen(0, input.length()))); |
| 65 | + } |
| 66 | + |
| 67 | + /// Reflows string literals in the given character ranges of the given Java source code that |
| 68 | + /// extend past the given column limit. |
| 69 | + static String wrap( |
| 70 | + final int columnLimit, String input, Formatter formatter, RangeSet<Integer> characterRanges) |
| 71 | + throws FormatterException { |
61 | 72 | JavaFormatterOptions options = formatter.options(); |
62 | 73 | if (!needWrapping(columnLimit, input, options)) { |
63 | 74 | // fast path |
64 | 75 | return input; |
65 | 76 | } |
66 | 77 |
|
67 | | - TreeRangeMap<Integer, String> replacements = getReflowReplacements(columnLimit, input, options); |
68 | | - String firstPass = formatter.formatSource(input, replacements.asMapOfRanges().keySet()); |
| 78 | + TreeRangeMap<Integer, String> replacements = |
| 79 | + getReflowReplacements(columnLimit, input, options, characterRanges); |
| 80 | + if (replacements.asMapOfRanges().isEmpty()) { |
| 81 | + return input; |
| 82 | + } |
| 83 | + Edit firstPass = |
| 84 | + Edit.apply( |
| 85 | + input, formatter.getFormatReplacements(input, replacements.asMapOfRanges().keySet())); |
69 | 86 |
|
70 | | - if (!firstPass.equals(input)) { |
| 87 | + if (!firstPass.contents().equals(input)) { |
71 | 88 | // If formatting the replacement ranges resulted in a change, recalculate the replacements on |
72 | 89 | // the updated input. |
73 | | - input = firstPass; |
74 | | - replacements = getReflowReplacements(columnLimit, input, options); |
| 90 | + input = firstPass.contents(); |
| 91 | + characterRanges = firstPass.mapRanges(characterRanges.asRanges()); |
| 92 | + replacements = getReflowReplacements(columnLimit, input, options, characterRanges); |
75 | 93 | } |
76 | 94 |
|
77 | 95 | String result = applyReplacements(input, replacements); |
@@ -111,24 +129,34 @@ private static FormatterException somethingHasGoneTerriblyWrong( |
111 | 129 | } |
112 | 130 |
|
113 | 131 | private static TreeRangeMap<Integer, String> getReflowReplacements( |
114 | | - int columnLimit, final String input, JavaFormatterOptions options) throws FormatterException { |
115 | | - return new Reflower(columnLimit, input, options).getReflowReplacements(); |
| 132 | + int columnLimit, |
| 133 | + final String input, |
| 134 | + JavaFormatterOptions options, |
| 135 | + RangeSet<Integer> characterRanges) |
| 136 | + throws FormatterException { |
| 137 | + return new Reflower(columnLimit, input, options, characterRanges).getReflowReplacements(); |
116 | 138 | } |
117 | 139 |
|
118 | 140 | private static class Reflower { |
119 | 141 |
|
120 | 142 | private final String input; |
121 | 143 | private final int columnLimit; |
122 | 144 | private final JavaFormatterOptions options; |
| 145 | + private final RangeSet<Integer> characterRanges; |
123 | 146 | private final String separator; |
124 | 147 | private final JCTree.JCCompilationUnit unit; |
125 | 148 | private final Position.LineMap lineMap; |
126 | 149 |
|
127 | | - Reflower(int columnLimit, String input, JavaFormatterOptions options) |
| 150 | + Reflower( |
| 151 | + int columnLimit, |
| 152 | + String input, |
| 153 | + JavaFormatterOptions options, |
| 154 | + RangeSet<Integer> characterRanges) |
128 | 155 | throws FormatterException { |
129 | 156 | this.columnLimit = columnLimit; |
130 | 157 | this.input = input; |
131 | 158 | this.options = options; |
| 159 | + this.characterRanges = characterRanges; |
132 | 160 | this.separator = Newlines.guessLineSeparator(input); |
133 | 161 | this.unit = parse(input, /* allowStringFolding= */ false); |
134 | 162 | this.lineMap = unit.getLineMap(); |
@@ -170,6 +198,10 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) { |
170 | 198 | return null; |
171 | 199 | } |
172 | 200 | int endPosition = getEndPosition(literalTree, unit); |
| 201 | + if (!characterRanges.intersects( |
| 202 | + Range.closedOpen(getStartPosition(literalTree), endPosition))) { |
| 203 | + return null; |
| 204 | + } |
173 | 205 | int lineEnd = endPosition; |
174 | 206 | while (Newlines.hasNewlineAt(input, lineEnd) == -1) { |
175 | 207 | lineEnd++; |
|
0 commit comments