Skip to content

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

Description

@abashev

What

Reported upstream as google/google-java-format#562 ("Running with --replace does not converge", 2021). The reporter's comment has a no-break space (U+00A0) right after the opening quote, which is easy to miss. It reproduces here on main:

public class T {
    //String testString = "<U+00A0>thisisnotaHYPERLINKandsoitshouldntbetruncated… \"http://…\"";
}

where the word after the no-break space is longer than the line. The first run splits the comment in front of the no-break space, and every later run adds one more empty // line above the second half:

    // String testString = "
    //
    //<U+00A0>thisisnotaHYPERLINKandsoitshouldntbetruncated…

So the file never becomes stable, and a format check fails after every --replace.

Why it happens

JavaCommentsHelper.wrapLineComments looks for a break with CharMatcher.whitespace(), which counts U+00A0 as whitespace, so the continuation line starts with // and the no-break space. On the next run LINE_COMMENT_MISSING_SPACE_PREFIX, whose \s does not match U+00A0, reads that as a comment with no space after the slashes and inserts one. The wrap then breaks at the no-break space again, the inserted space stays behind on a line of its own, and trim() turns that line into an empty //.

Fix

Break line comments only at breaking whitespace, CharMatcher.breakingWhitespace(), which leaves out U+00A0, U+2007 and U+202F. A no-break space is by definition not a place to break a line. Comments without a no-break space are not affected.

Done when

  • the comment above formats in one run the way the next run leaves it;
  • a golden covers a long line comment whose only break opportunity is a no-break space.

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions