Skip to content

Commit 343d9aa

Browse files
cushongoogle-java-format Team
authored andcommitted
Consolidate end position handling in google-java-format
PiperOrigin-RevId: 839739629
1 parent 8e8db81 commit 343d9aa

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.google.googlejavaformat.java;
1818

1919
import static com.google.common.base.Preconditions.checkArgument;
20+
import static com.google.googlejavaformat.java.Trees.getEndPosition;
2021
import static java.lang.Math.max;
2122
import static java.nio.charset.StandardCharsets.UTF_8;
2223

@@ -284,7 +285,7 @@ private static RangeMap<Integer, String> buildReplacements(
284285
continue;
285286
}
286287
// delete the import
287-
int endPosition = importTree.getEndPosition(unit.endPositions);
288+
int endPosition = getEndPosition(importTree, unit);
288289
endPosition = max(CharMatcher.isNot(' ').indexIn(contents, endPosition), endPosition);
289290
String sep = Newlines.guessLineSeparator(contents);
290291
if (endPosition + sep.length() < contents.length()

core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
import static com.google.common.collect.ImmutableList.toImmutableList;
1818
import static com.google.common.collect.Iterables.getLast;
19+
import static com.google.googlejavaformat.java.Trees.getEndPosition;
20+
import static com.google.googlejavaformat.java.Trees.getStartPosition;
1921
import static java.lang.Math.min;
2022
import static java.nio.charset.StandardCharsets.UTF_8;
2123
import static java.util.stream.Collectors.joining;
@@ -171,7 +173,7 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) {
171173
&& ((MemberSelectTree) parent).getExpression().equals(literalTree)) {
172174
return null;
173175
}
174-
int endPosition = getEndPosition(unit, literalTree);
176+
int endPosition = getEndPosition(literalTree, unit);
175177
int lineEnd = endPosition;
176178
while (Newlines.hasNewlineAt(input, lineEnd) == -1) {
177179
lineEnd++;
@@ -188,7 +190,7 @@ private void indentTextBlocks(
188190
TreeRangeMap<Integer, String> replacements, List<Tree> textBlocks) {
189191
for (Tree tree : textBlocks) {
190192
int startPosition = lineMap.getStartPosition(lineMap.getLineNumber(getStartPosition(tree)));
191-
int endPosition = getEndPosition(unit, tree);
193+
int endPosition = getEndPosition(tree, unit);
192194
String text = input.substring(startPosition, endPosition);
193195
int leadingWhitespace = CharMatcher.whitespace().negate().indexIn(text);
194196

@@ -254,7 +256,7 @@ private void wrapLongStrings(
254256

255257
// Handling leaving trailing non-string tokens at the end of the literal,
256258
// e.g. the trailing `);` in `foo("...");`.
257-
int end = getEndPosition(unit, getLast(flat));
259+
int end = getEndPosition(getLast(flat), unit);
258260
int lineEnd = end;
259261
while (Newlines.hasNewlineAt(input, lineEnd) == -1) {
260262
lineEnd++;
@@ -264,7 +266,7 @@ private void wrapLongStrings(
264266
// Get the original source text of the string literals, excluding `"` and `+`.
265267
ImmutableList<String> components = stringComponents(input, unit, flat);
266268
replacements.put(
267-
Range.closedOpen(getStartPosition(flat.get(0)), getEndPosition(unit, getLast(flat))),
269+
Range.closedOpen(getStartPosition(flat.get(0)), getEndPosition(getLast(flat), unit)),
268270
reflow(separator, columnLimit, startColumn, trailing, components, first.get()));
269271
}
270272
}
@@ -280,7 +282,7 @@ private static ImmutableList<String> stringComponents(
280282
StringBuilder piece = new StringBuilder();
281283
for (Tree tree : flat) {
282284
// adjust for leading and trailing double quotes
283-
String text = input.substring(getStartPosition(tree) + 1, getEndPosition(unit, tree) - 1);
285+
String text = input.substring(getStartPosition(tree) + 1, getEndPosition(tree, unit) - 1);
284286
int start = 0;
285287
for (int idx = 0; idx < text.length(); idx++) {
286288
if (CharMatcher.whitespace().matches(text.charAt(idx))) {
@@ -453,20 +455,12 @@ && noComments(input, unit, flat.get(endIdx - 1), flat.get(endIdx))) {
453455
private static boolean noComments(
454456
String input, JCTree.JCCompilationUnit unit, Tree one, Tree two) {
455457
return STRING_CONCAT_DELIMITER.matchesAllOf(
456-
input.subSequence(getEndPosition(unit, one), getStartPosition(two)));
458+
input.subSequence(getEndPosition(one, unit), getStartPosition(two)));
457459
}
458460

459461
public static final CharMatcher STRING_CONCAT_DELIMITER =
460462
CharMatcher.whitespace().or(CharMatcher.anyOf("\"+"));
461463

462-
private static int getEndPosition(JCTree.JCCompilationUnit unit, Tree tree) {
463-
return ((JCTree) tree).getEndPosition(unit.endPositions);
464-
}
465-
466-
private static int getStartPosition(Tree tree) {
467-
return ((JCTree) tree).getStartPosition();
468-
}
469-
470464
/**
471465
* Returns true if any lines in the given Java source exceed the column limit, or contain a {@code
472466
* """} that could indicate a text block.

core/src/main/java/com/google/googlejavaformat/java/Trees.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package com.google.googlejavaformat.java;
1616

1717
import com.sun.source.tree.ClassTree;
18+
import com.sun.source.tree.CompilationUnitTree;
1819
import com.sun.source.tree.CompoundAssignmentTree;
1920
import com.sun.source.tree.ExpressionTree;
2021
import com.sun.source.tree.IdentifierTree;
@@ -24,6 +25,7 @@
2425
import com.sun.source.tree.Tree;
2526
import com.sun.source.util.TreePath;
2627
import com.sun.tools.javac.tree.JCTree;
28+
import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
2729
import com.sun.tools.javac.tree.Pretty;
2830
import com.sun.tools.javac.tree.TreeInfo;
2931
import java.io.IOError;
@@ -44,8 +46,12 @@ static int getStartPosition(Tree expression) {
4446

4547
/** Returns the source end position of the node. */
4648
static int getEndPosition(Tree expression, TreePath path) {
47-
return ((JCTree) expression)
48-
.getEndPosition(((JCTree.JCCompilationUnit) path.getCompilationUnit()).endPositions);
49+
return getEndPosition(expression, path.getCompilationUnit());
50+
}
51+
52+
/** Returns the source end position of the node. */
53+
public static int getEndPosition(Tree tree, CompilationUnitTree unit) {
54+
return ((JCTree) tree).getEndPosition(((JCCompilationUnit) unit).endPositions);
4955
}
5056

5157
/** Returns the source text for the node. */

0 commit comments

Comments
 (0)