Skip to content

Commit a4c75f6

Browse files
cushongoogle-java-format Team
authored and
google-java-format Team
committed
Format type annotation as part of the type, not part of the modifiers list
Given e.g. `@Deprecated @nullable Object foo() {}`, prefer this: ``` @deprecated @nullable Object foo() {} ``` instead of: ``` @deprecated @nullable Object foo() {} ``` The implementation is complicated by the fact that the AST doesn't store source position information for modifiers, and there's no requirement that declaration annotations, modifiers, and type annotations appear in any particular order in source. To work around this, we examine the token stream to figure out the ordering of the modifiers and annotations. #5 PiperOrigin-RevId: 392459885
1 parent 0051153 commit a4c75f6

File tree

7 files changed

+398
-68
lines changed

7 files changed

+398
-68
lines changed

core/pom.xml

+5-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@
5151
<artifactId>error_prone_annotations</artifactId>
5252
<optional>true</optional>
5353
</dependency>
54-
54+
<dependency>
55+
<groupId>com.google.auto.value</groupId>
56+
<artifactId>auto-value-annotations</artifactId>
57+
<optional>true</optional>
58+
</dependency>
5559

5660
<!-- Test dependencies -->
5761
<dependency>

core/src/main/java/com/google/googlejavaformat/OpsBuilder.java

+24
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import static java.lang.Math.min;
1919

2020
import com.google.common.base.MoreObjects;
21+
import com.google.common.base.Preconditions;
22+
import com.google.common.base.Predicate;
2123
import com.google.common.collect.ArrayListMultimap;
2224
import com.google.common.collect.ImmutableList;
2325
import com.google.common.collect.Iterables;
@@ -281,6 +283,28 @@ public final Optional<String> peekToken(int skip) {
281283
: Optional.empty();
282284
}
283285

286+
/**
287+
* Returns the {@link Input.Tok}s starting at the current source position, which are satisfied by
288+
* the given predicate.
289+
*/
290+
public ImmutableList<Tok> peekTokens(int startPosition, Predicate<Input.Tok> predicate) {
291+
ImmutableList<? extends Input.Token> tokens = input.getTokens();
292+
Preconditions.checkState(
293+
tokens.get(tokenI).getTok().getPosition() == startPosition,
294+
"Expected the current token to be at position %s, found: %s",
295+
startPosition,
296+
tokens.get(tokenI));
297+
ImmutableList.Builder<Tok> result = ImmutableList.builder();
298+
for (int idx = tokenI; idx < tokens.size(); idx++) {
299+
Tok tok = tokens.get(idx).getTok();
300+
if (!predicate.apply(tok)) {
301+
break;
302+
}
303+
result.add(tok);
304+
}
305+
return result.build();
306+
}
307+
284308
/**
285309
* Emit an optional token iff it exists on the input. This is used to emit tokens whose existence
286310
* has been lost in the AST.

0 commit comments

Comments
 (0)