|
49 | 49 | import com.google.common.base.Verify; |
50 | 50 | import com.google.common.collect.HashMultiset; |
51 | 51 | import com.google.common.collect.ImmutableList; |
| 52 | +import com.google.common.collect.ImmutableRangeMap; |
52 | 53 | import com.google.common.collect.ImmutableSet; |
53 | 54 | import com.google.common.collect.ImmutableSortedSet; |
54 | 55 | import com.google.common.collect.Iterables; |
|
84 | 85 | import java.util.Map; |
85 | 86 | import java.util.Optional; |
86 | 87 | import java.util.Set; |
87 | | -import java.util.function.IntFunction; |
88 | 88 | import java.util.regex.Pattern; |
89 | 89 | import java.util.stream.Stream; |
90 | 90 | import javax.annotation.Nullable; |
@@ -3304,13 +3304,24 @@ private Integer actualColumn(ExpressionTree expression) { |
3304 | 3304 |
|
3305 | 3305 | /** How many lines does this node take up in the input. Returns at least 1. */ |
3306 | 3306 | int lineSpan(Tree node) { |
3307 | | - IntFunction<Integer> lineNumberAt = tokenPosition -> { |
3308 | | - Input.Token token = builder.getInput().getPositionTokenMap().get(tokenPosition); |
3309 | | - return builder.getInput().getLineNumber(token.getTok().getPosition()); |
3310 | | - }; |
3311 | | - return lineNumberAt.apply(getEndPosition(node, getCurrentPath())) |
3312 | | - - lineNumberAt.apply(getStartPosition(node)) |
3313 | | - + 1; |
| 3307 | + ImmutableRangeMap<Integer, ? extends Input.Token> positionTokenMap = |
| 3308 | + builder.getInput().getPositionTokenMap(); |
| 3309 | + |
| 3310 | + int startPosition = getStartPosition(node); |
| 3311 | + int endPosition = getEndPosition(node, getCurrentPath()); |
| 3312 | + // The last token will not be in the range map if it's whitespace, because of JavaOutput.endTok's filtering. |
| 3313 | + // Thus, we go back until we find a "real" last token. |
| 3314 | + // If all tokens down from endPosition are null, then we are guaranteed stop at startPosition. |
| 3315 | + while (endPosition > startPosition && positionTokenMap.get(endPosition) == null) { |
| 3316 | + endPosition--; |
| 3317 | + } |
| 3318 | + Input.Token startToken = positionTokenMap.get(startPosition); |
| 3319 | + Input.Token endToken = positionTokenMap.get(endPosition); |
| 3320 | + return lineNumberAt(endToken) - lineNumberAt(startToken) + 1; |
| 3321 | + } |
| 3322 | + |
| 3323 | + private int lineNumberAt(Input.Token token) { |
| 3324 | + return builder.getInput().getLineNumber(token.getTok().getPosition()); |
3314 | 3325 | } |
3315 | 3326 |
|
3316 | 3327 | /** Returns true if {@code atLeastM} of the expressions in the given column are the same kind. */ |
|
0 commit comments