Skip to content

Keep a no-break space right after // instead of padding it with a regular space #249

Closed

Description

@abashev

Upstream: google/google-java-format#1530
Kind: bug | Priority: low

google-java-format#1530 (fixes google-java-format#562, merged 2026-11-18) makes line-comment handling treat U+00A0 as non-breaking. It does three things: wraps only on CharMatcher.breakingWhitespace(), compiles the "missing space after //" pattern with Pattern.UNICODE_CHARACTER_CLASS, and trims lines with CharMatcher.whitespace().trimFrom(...) instead of String.trim(). This is the follow-up to the baseline test in google-java-format#1529 that we were watching.

We already have the first part: wrapLineComments in our JavaCommentsHelper breaks only on breakingWhitespace() and carries a comment explaining the non-convergence. We do not have the other two. Our LINE_COMMENT_MISSING_SPACE_PREFIX is a plain ^(//+)[^\s/], where \s is ASCII-only, so a no-break space directly after the slashes counts as "not a space" and a regular space is inserted before it. The result is stable after one pass (no endless growth), but it rewrites the comment's characters, and it differs from upstream's new output. Our indentLineComments also uses String.trim(), which does not strip U+00A0 at the line ends (trailing U+00A0 is already removed earlier by trimTrailingFrom).

Examples (input is a comment starting with // followed by U+00A0; "Before" derived by reading our code, not by running it):

Before (how it is formatted today, our code):

class T {
  //  non-breaking space after prefix
}

After (with the upstream change):

class T {
  // non-breaking space after prefix
}

Affected files in our repo: open-java-format/src/main/java/com/palantir/javaformat/java/JavaCommentsHelper.java (LINE_COMMENT_MISSING_SPACE_PREFIX, indentLineComments, wrapLineComments), plus the matching comment test inputs/outputs.

Suggested action: adopt-the-idea. Small, low-risk change (about 5 lines): add Pattern.UNICODE_CHARACTER_CLASS to the prefix pattern and switch trim() to CharMatcher.whitespace().trimFrom(...) in indentLineComments, then add the upstream-style test with a no-break space after the prefix and mid-comment. Check that our /// markdown-comment handling is unaffected by the flag. Low priority because the real non-convergence bug is already fixed on our side.


Filed from an upstream-watch ticket after manual review.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions