Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -284,7 +285,7 @@ private static RangeMap<Integer, String> 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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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++;
Expand All @@ -188,7 +190,7 @@ private void indentTextBlocks(
TreeRangeMap<Integer, String> replacements, List<Tree> 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);

Expand Down Expand Up @@ -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++;
Expand All @@ -264,7 +266,7 @@ private void wrapLongStrings(
// Get the original source text of the string literals, excluding `"` and `+`.
ImmutableList<String> 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()));
}
}
Expand All @@ -280,7 +282,7 @@ private static ImmutableList<String> 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))) {
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions core/src/main/java/com/google/googlejavaformat/java/Trees.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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. */
Expand Down
Loading