Skip to content

Commit 9fbc6e2

Browse files
committed
Preserve terminal comments
Fixes google#101 MOE_MIGRATED_REVID=140052456
1 parent 04b5839 commit 9fbc6e2

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
/** A wrapper around javac's lexer. */
3333
public class JavacTokens {
3434

35+
/** The lexer eats terminal comments, so feed it one we don't care about. */
36+
// TODO(b/33103797): fix javac and remove the work-around
37+
private static final CharSequence EOF_COMMENT = "\n//EOF";
38+
3539
/** An unprocessed input token, including whitespace and comments. */
3640
static class RawTok {
3741
private final String stringVal;
@@ -74,17 +78,14 @@ public static ImmutableList<RawTok> getTokens(
7478
return ImmutableList.of();
7579
}
7680
ScannerFactory fac = ScannerFactory.instance(context);
77-
char[] buffer = source.toCharArray();
81+
char[] buffer = (source + EOF_COMMENT).toCharArray();
7882
Scanner scanner =
7983
new AccessibleScanner(fac, new CommentSavingTokenizer(fac, buffer, buffer.length));
8084
ImmutableList.Builder<RawTok> tokens = ImmutableList.builder();
8185
int last = 0;
8286
do {
8387
scanner.nextToken();
8488
Token t = scanner.token();
85-
if (stopTokens.contains(t.kind)) {
86-
break;
87-
}
8889
if (t.comments != null) {
8990
for (Comment c : Lists.reverse(t.comments)) {
9091
if (last < c.getSourcePos(0)) {
@@ -95,6 +96,9 @@ public static ImmutableList<RawTok> getTokens(
9596
last = c.getSourcePos(0) + c.getText().length();
9697
}
9798
}
99+
if (stopTokens.contains(t.kind)) {
100+
break;
101+
}
98102
if (last < t.pos) {
99103
tokens.add(new RawTok(null, null, last, t.pos));
100104
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public @interface kHz {} // No prefix defined in the annotation itself
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
public @interface kHz {} // No prefix defined in the annotation itself

0 commit comments

Comments
 (0)