Rewrite the missing-space rule without a negative lookahead (google-java-format #1460) - #104
Merged
Merged
Conversation
`//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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
//foogets a space after its slashes unless the comment is an IDE marker,//noinspectionor//$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.repeattoString.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
lineCommentGetsItsMissingSpaceExceptIdeMarkersinFormatterTestwas written first. It is a refactoring, so the test passes before and after; it pins both markers, a marker followed by text,//footo// foo,///footo/// foo, and comments that already have their space. The goldensI202,i221andNON-NLScover the markers in context.Checks
:open-java-format:teston JDK 21: 1598 tests, 0 failures.