Skip to content

Commit

Permalink
Allow blank lines inserted before comments.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
nreid260 authored and google-java-format Team committed Mar 7, 2023
1 parent 20527aa commit 1a00656
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public void append(String text, Range<Integer> range) {
* there's a blank line here and it's a comment.
*/
BlankLineWanted wanted = blankLines.getOrDefault(lastK, BlankLineWanted.NO);
if (isComment(text) ? sawNewlines : wanted.wanted().orElse(sawNewlines)) {
if ((sawNewlines && isComment(text)) || wanted.wanted().orElse(sawNewlines)) {
++newlinesPending;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ public void noTokensOnLine() throws Exception {
String input =
lines(
" package com.google.googlejavaformat.java;",
"",
"/*",
" * Copyright 2015 Google Inc.",
" *",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ enum TrailingComment {

/** a */
Object a;

/** b */
Object b;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
class Fields {

int a = 1;
int b = 1;

int c = 1;
/** Javadoc */
int d = 1;

int x = 1;

int y = 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
class Fields {

int a = 1;
int b = 1;

int c = 1;

/** Javadoc */
int d = 1;

int x = 1;

int y = 1;
Expand Down

0 comments on commit 1a00656

Please sign in to comment.