Skip to content

Stop breaking line comments at a no-break space - #41

Merged
abashev merged 1 commit into
mainfrom
comment-nbsp-wrap
Sep 23, 2026
Merged

abashev merged 1 commit into
mainfrom
comment-nbsp-wrap

Conversation

@abashev

@abashev abashev commented Sep 23, 2026

Copy link
Copy Markdown
Collaborator

Closes #38. Upstream report: google/google-java-format#562.

What changes

JavaCommentsHelper.wrapLineComments breaks a line comment that is too long only at breaking whitespace, CharMatcher.breakingWhitespace(), and no longer at a no-break space (U+00A0, U+2007, U+202F). Breaking there left a continuation line that starts with // and the no-break space. The next run took that for a missing space, padded it, broke at the no-break space again and left an empty // line behind, one more on every run.

A number grouped with no-break spaces, such as 1 000 000, now moves to the next line whole instead of being cut inside.

Checked

  • The new golden ojf-issue-38-nbsp-line-comment fails without the change (the comment is cut before each no-break space) and passes with it. It covers a number grouped with no-break spaces and the shape from Running with --replace does not converge google/google-java-format#562.
  • ./gradlew :open-java-format:test on JDK 21: 1421 tests, all green.
  • The 15,747 files of the JDK 21 sources format byte for byte as before. None of them contains a no-break space, so they cannot show the change.

JavaCommentsHelper wraps a line comment that is too long at the last
whitespace before the limit, and CharMatcher.whitespace() counts the
no-break space U+00A0 as whitespace. The continuation line then starts
with "//" and that no-break space. LINE_COMMENT_MISSING_SPACE_PREFIX,
whose \s does not match it, takes that for a comment with no space after
the slashes, so the next run pads it, breaks at the no-break space again
and leaves the padding behind as an empty "//" line. Every run adds one,
and the file never settles (#38, from google/google-java-format#562).

Line comments now break only at breaking whitespace, which leaves out
U+00A0, U+2007 and U+202F: a no-break space is by definition not a place
to break a line. A number grouped with no-break spaces now moves to the
next line whole instead of being cut inside.

The new golden covers both shapes. The 15,747 files of the JDK 21
sources format exactly as before; none of them contains a no-break
space.
@abashev
abashev enabled auto-merge September 23, 2026 07:24
@abashev
abashev disabled auto-merge September 23, 2026 08:00
@abashev
abashev merged commit 0ef517a into main Sep 23, 2026
10 checks passed
@abashev
abashev deleted the comment-nbsp-wrap branch September 23, 2026 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A line comment with a no-break space gains an empty // line on every run

1 participant