From 5174180befefa13e084af4048b9c747875100030 Mon Sep 17 00:00:00 2001 From: ingop Date: Wed, 23 May 2018 04:12:03 -0700 Subject: [PATCH] Fix handling of type-annotated varargs MOE_MIGRATED_REVID=197703737 --- .../java/com/google/googlejavaformat/OpsBuilder.java | 10 ++++++++-- .../googlejavaformat/java/JavaInputAstVisitor.java | 3 +++ .../google/googlejavaformat/java/testdata/i282.input | 2 ++ .../google/googlejavaformat/java/testdata/i282.output | 2 ++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java b/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java index 4375a90ce..5f20e5c18 100644 --- a/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java +++ b/core/src/main/java/com/google/googlejavaformat/OpsBuilder.java @@ -266,9 +266,15 @@ public final void close() { /** Return the text of the next {@link Input.Token}, or absent if there is none. */ public final Optional peekToken() { + return peekToken(0); + } + + /** Return the text of an upcoming {@link Input.Token}, or absent if there is none. */ + public final Optional peekToken(int skip) { ImmutableList tokens = input.getTokens(); - return tokenI < tokens.size() - ? Optional.of(tokens.get(tokenI).getTok().getOriginalText()) + int idx = tokenI + skip; + return idx < tokens.size() + ? Optional.of(tokens.get(idx).getTok().getOriginalText()) : Optional.absent(); } diff --git a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java index 6b700d574..62ed5cab1 100644 --- a/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java +++ b/core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java @@ -3324,6 +3324,9 @@ private void maybeAddDims( lastWasAnnotation = false; break; case ".": + if (!builder.peekToken().get().equals(".") || !builder.peekToken(1).get().equals(".")) { + return; + } if (lastWasAnnotation) { builder.breakToFill(" "); } else { diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/i282.input b/core/src/test/resources/com/google/googlejavaformat/java/testdata/i282.input index 2c5d1194e..a8bf796c2 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/i282.input +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/i282.input @@ -6,4 +6,6 @@ public class ArrayVsVarargs { void i(String @B... arg) {} void j(String @C [] @D [] arg) {} void k(String @E [] @F... arg) {} + + Class c = byte[].class; } diff --git a/core/src/test/resources/com/google/googlejavaformat/java/testdata/i282.output b/core/src/test/resources/com/google/googlejavaformat/java/testdata/i282.output index bfe401612..6c92e513c 100644 --- a/core/src/test/resources/com/google/googlejavaformat/java/testdata/i282.output +++ b/core/src/test/resources/com/google/googlejavaformat/java/testdata/i282.output @@ -11,4 +11,6 @@ public class ArrayVsVarargs { void j(String @C [] @D [] arg) {} void k(String @E [] @F ... arg) {} + + Class c = byte[].class; }