From 343d9aa7276b2c681d0be4239ae38d6730b2395a Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Wed, 3 Dec 2025 06:59:03 -0800 Subject: [PATCH] Consolidate end position handling in google-java-format PiperOrigin-RevId: 839739629 --- .../java/RemoveUnusedImports.java | 3 ++- .../googlejavaformat/java/StringWrapper.java | 22 +++++++------------ .../google/googlejavaformat/java/Trees.java | 10 +++++++-- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java b/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java index 4035ce319..a1c722b7e 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java +++ b/core/src/main/java/com/google/googlejavaformat/java/RemoveUnusedImports.java @@ -17,6 +17,7 @@ package com.google.googlejavaformat.java; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.googlejavaformat.java.Trees.getEndPosition; import static java.lang.Math.max; import static java.nio.charset.StandardCharsets.UTF_8; @@ -284,7 +285,7 @@ private static RangeMap buildReplacements( continue; } // delete the import - int endPosition = importTree.getEndPosition(unit.endPositions); + int endPosition = getEndPosition(importTree, unit); endPosition = max(CharMatcher.isNot(' ').indexIn(contents, endPosition), endPosition); String sep = Newlines.guessLineSeparator(contents); if (endPosition + sep.length() < contents.length() diff --git a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java index f1014e52f..9d917cc4a 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java +++ b/core/src/main/java/com/google/googlejavaformat/java/StringWrapper.java @@ -16,6 +16,8 @@ import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.Iterables.getLast; +import static com.google.googlejavaformat.java.Trees.getEndPosition; +import static com.google.googlejavaformat.java.Trees.getStartPosition; import static java.lang.Math.min; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.stream.Collectors.joining; @@ -171,7 +173,7 @@ public Void visitLiteral(LiteralTree literalTree, Void aVoid) { && ((MemberSelectTree) parent).getExpression().equals(literalTree)) { return null; } - int endPosition = getEndPosition(unit, literalTree); + int endPosition = getEndPosition(literalTree, unit); int lineEnd = endPosition; while (Newlines.hasNewlineAt(input, lineEnd) == -1) { lineEnd++; @@ -188,7 +190,7 @@ private void indentTextBlocks( TreeRangeMap replacements, List textBlocks) { for (Tree tree : textBlocks) { int startPosition = lineMap.getStartPosition(lineMap.getLineNumber(getStartPosition(tree))); - int endPosition = getEndPosition(unit, tree); + int endPosition = getEndPosition(tree, unit); String text = input.substring(startPosition, endPosition); int leadingWhitespace = CharMatcher.whitespace().negate().indexIn(text); @@ -254,7 +256,7 @@ private void wrapLongStrings( // Handling leaving trailing non-string tokens at the end of the literal, // e.g. the trailing `);` in `foo("...");`. - int end = getEndPosition(unit, getLast(flat)); + int end = getEndPosition(getLast(flat), unit); int lineEnd = end; while (Newlines.hasNewlineAt(input, lineEnd) == -1) { lineEnd++; @@ -264,7 +266,7 @@ private void wrapLongStrings( // Get the original source text of the string literals, excluding `"` and `+`. ImmutableList components = stringComponents(input, unit, flat); replacements.put( - Range.closedOpen(getStartPosition(flat.get(0)), getEndPosition(unit, getLast(flat))), + Range.closedOpen(getStartPosition(flat.get(0)), getEndPosition(getLast(flat), unit)), reflow(separator, columnLimit, startColumn, trailing, components, first.get())); } } @@ -280,7 +282,7 @@ private static ImmutableList stringComponents( StringBuilder piece = new StringBuilder(); for (Tree tree : flat) { // adjust for leading and trailing double quotes - String text = input.substring(getStartPosition(tree) + 1, getEndPosition(unit, tree) - 1); + String text = input.substring(getStartPosition(tree) + 1, getEndPosition(tree, unit) - 1); int start = 0; for (int idx = 0; idx < text.length(); idx++) { if (CharMatcher.whitespace().matches(text.charAt(idx))) { @@ -453,20 +455,12 @@ && noComments(input, unit, flat.get(endIdx - 1), flat.get(endIdx))) { private static boolean noComments( String input, JCTree.JCCompilationUnit unit, Tree one, Tree two) { return STRING_CONCAT_DELIMITER.matchesAllOf( - input.subSequence(getEndPosition(unit, one), getStartPosition(two))); + input.subSequence(getEndPosition(one, unit), getStartPosition(two))); } public static final CharMatcher STRING_CONCAT_DELIMITER = CharMatcher.whitespace().or(CharMatcher.anyOf("\"+")); - private static int getEndPosition(JCTree.JCCompilationUnit unit, Tree tree) { - return ((JCTree) tree).getEndPosition(unit.endPositions); - } - - private static int getStartPosition(Tree tree) { - return ((JCTree) tree).getStartPosition(); - } - /** * Returns true if any lines in the given Java source exceed the column limit, or contain a {@code * """} that could indicate a text block. diff --git a/core/src/main/java/com/google/googlejavaformat/java/Trees.java b/core/src/main/java/com/google/googlejavaformat/java/Trees.java index ded219644..a16ce17c0 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/Trees.java +++ b/core/src/main/java/com/google/googlejavaformat/java/Trees.java @@ -15,6 +15,7 @@ package com.google.googlejavaformat.java; import com.sun.source.tree.ClassTree; +import com.sun.source.tree.CompilationUnitTree; import com.sun.source.tree.CompoundAssignmentTree; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.IdentifierTree; @@ -24,6 +25,7 @@ import com.sun.source.tree.Tree; import com.sun.source.util.TreePath; import com.sun.tools.javac.tree.JCTree; +import com.sun.tools.javac.tree.JCTree.JCCompilationUnit; import com.sun.tools.javac.tree.Pretty; import com.sun.tools.javac.tree.TreeInfo; import java.io.IOError; @@ -44,8 +46,12 @@ static int getStartPosition(Tree expression) { /** Returns the source end position of the node. */ static int getEndPosition(Tree expression, TreePath path) { - return ((JCTree) expression) - .getEndPosition(((JCTree.JCCompilationUnit) path.getCompilationUnit()).endPositions); + return getEndPosition(expression, path.getCompilationUnit()); + } + + /** Returns the source end position of the node. */ + public static int getEndPosition(Tree tree, CompilationUnitTree unit) { + return ((JCTree) tree).getEndPosition(((JCCompilationUnit) unit).endPositions); } /** Returns the source text for the node. */