Skip to content

Commit 1a00656

Browse files
nreid260google-java-format Team
authored and
google-java-format Team
committed
Allow blank lines inserted before comments.
Previously, blank lines could only be emitted before comments if they were there in the input. This was irrespective of BlankLineWanted.YES hints in the OpBuilder. These hints are already being set by some paths in GJF, so now that they're respected, blank lines will be added to some existing files. Eval in unknown commit PiperOrigin-RevId: 514817708
1 parent 20527aa commit 1a00656

File tree

5 files changed

+18
-1
lines changed

5 files changed

+18
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public void append(String text, Range<Integer> range) {
111111
* there's a blank line here and it's a comment.
112112
*/
113113
BlankLineWanted wanted = blankLines.getOrDefault(lastK, BlankLineWanted.NO);
114-
if (isComment(text) ? sawNewlines : wanted.wanted().orElse(sawNewlines)) {
114+
if ((sawNewlines && isComment(text)) || wanted.wanted().orElse(sawNewlines)) {
115115
++newlinesPending;
116116
}
117117
}

core/src/test/java/com/google/googlejavaformat/java/PartialFormattingTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ public void noTokensOnLine() throws Exception {
679679
String input =
680680
lines(
681681
" package com.google.googlejavaformat.java;",
682+
"",
682683
"/*",
683684
" * Copyright 2015 Google Inc.",
684685
" *",

core/src/test/resources/com/google/googlejavaformat/java/testdata/B27246427.output

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ enum TrailingComment {
66

77
/** a */
88
Object a;
9+
910
/** b */
1011
Object b;
1112
}

core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.input

+7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
class Fields {
22

3+
int a = 1;
4+
int b = 1;
5+
6+
int c = 1;
7+
/** Javadoc */
8+
int d = 1;
9+
310
int x = 1;
411

512
int y = 1;

core/src/test/resources/com/google/googlejavaformat/java/testdata/Fields.output

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
class Fields {
22

3+
int a = 1;
4+
int b = 1;
5+
6+
int c = 1;
7+
8+
/** Javadoc */
9+
int d = 1;
10+
311
int x = 1;
412

513
int y = 1;

0 commit comments

Comments
 (0)