Skip to content

Rewrite the missing-space rule without a negative lookahead (google-java-format #1460) - #104

Merged
abashev merged 2 commits into
mainfrom
gjf-1460-no-lookahead-regex
Oct 4, 2026
Merged

abashev merged 2 commits into
mainfrom
gjf-1460-no-lookahead-regex

Conversation

@abashev

@abashev abashev commented Oct 3, 2026

Copy link
Copy Markdown
Collaborator

Port of the applicable part of google/google-java-format#1460 (Éamonn McManus, author kept): the missing-space rule for line comments no longer uses a negative lookahead. //foo gets a space after its slashes unless the comment is an IDE marker, //noinspection or //$NON-NLS-n$, which the tools recognise only as written. The exemption moves from a lookahead inside the pattern to a second pattern that names the markers. Both forms accept the same lines: the first pattern needs a non-space, non-slash character right after the slashes, so the lookahead was always evaluated after the whole run of slashes, which is where the second pattern looks too.

Why take it: regex engines without backtracking do not support lookahead, which is upstream's reason, and a pattern that names the markers is the piece #23 wants to share with doc/Comment, which today adds the space unconditionally.

Not taken: Strings.repeat to String.repeat, already done here, and the markdown javadoc branch, which belongs to google-java-format's own /// handling; this fork wraps /// runs (see #102).

TDD

lineCommentGetsItsMissingSpaceExceptIdeMarkers in FormatterTest was written first. It is a refactoring, so the test passes before and after; it pins both markers, a marker followed by text, //foo to // foo, ///foo to /// foo, and comments that already have their space. The goldens I202, i221 and NON-NLS cover the markers in context.

Checks

  • :open-java-format:test on JDK 21: 1598 tests, 0 failures.
  • The JDK 21 sources (15,747 files), formatted with main's jar and this branch's jar, are identical.

`//foo` gets a space after its slashes unless the comment is an IDE
marker, `//noinspection` or `//$NON-NLS-n$`, which the tools recognise
only as written. The exemption was a negative lookahead inside the
matching pattern; it is now a second pattern that names the markers,
checked after the first one matches. The two forms accept the same
lines: the first pattern needs a non-space, non-slash character right
after the slashes, so the lookahead was always evaluated after the whole
run of slashes, which is where the second pattern looks as well.

Taken from google/google-java-format#1460, whose motivation stands here
too: regex engines without backtracking do not support lookahead, and
the markers are easier to read, and to share with doc/Comment as #23
asks, as a pattern of their own. The rest of that change does not apply:
`Strings.repeat` is already `String.repeat` here, and the markdown
branch belongs to google-java-format's own `///` handling.

FormatterTest pins the behaviour before and after: both markers, a
marker followed by text, `//foo`, `///foo`, and comments that already
have their space.
@abashev
abashev merged commit 6d17c15 into main Oct 4, 2026
16 checks passed
@abashev
abashev deleted the gjf-1460-no-lookahead-regex branch October 4, 2026 10:19
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.

2 participants