1616
1717import static com .google .common .collect .ImmutableList .toImmutableList ;
1818import 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 ;
1921import static java .lang .Math .min ;
2022import static java .nio .charset .StandardCharsets .UTF_8 ;
2123import 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.
0 commit comments