Keep XML comments trailing an element on the same line - #8353
Merged
Conversation
`LineBreaksVisitor.visitComment` unconditionally prepended a newline to every comment prefix, so `<exclude>a</exclude> <!--why a-->` was reflowed onto its own line, silently re-associating the comment with the *next* element. Skip the line break when the comment already sits on the same line as a preceding sibling; a comment that is the first content of its tag (or already on its own line) keeps the existing behavior.
timtebeek
marked this pull request as ready for review
July 30, 2026 12:05
This was referenced Jul 30, 2026
timtebeek
added a commit
to openrewrite/rewrite-migrate-java
that referenced
this pull request
Jul 30, 2026
Restores the pre-existing expectations, which assert that auto-format moves a trailing comment onto its own line. All 20 cases fail against the current rewrite snapshot: openrewrite/rewrite#8353 now keeps such comments on the line of the element they trail. Leaving the expectations as the desired behaviour rather than absorbing the new output into the test. Red until the formatting is addressed.
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.
Fixes https://github.com/moderneinc/customer-requests/issues/2926
What
XML auto-format moved a comment that trailed an element on the same line onto its own following line:
Besides being pure review noise on a migration PR, this silently changes what each comment documents: by XML convention the reflowed comment now reads as annotating the next
<exclude>, and the last one dangles with nothing after it.Why
The XML LST has no notion of a trailing comment — the whitespace separating it from the preceding sibling lives in the comment's own prefix, so that prefix is the only carrier of the author's intent.
LineBreaksVisitor.visitCommentcalledminimumLines(..., 1)unconditionally, which prepends"\n"to any newline-free prefix;TabsAndIndentsVisitorthen indented the now-multiline prefix. Latent since XML auto-format was written (0b1efb7706, 2022), and untested — greppingrewrite-xml/rewrite-maven/rewrite-gradlefor</tag> <!--returned zero fixtures.How
Skip the line break when the comment is on the same line as a preceding sibling — i.e. its prefix contains no newline and it is not the first content of its parent tag. Existing behavior is preserved for:
<dependencies><!--comment-->), which the existingLineBreaksTest#commentscase asserts should reflow;Tests
LineBreaksTest#trailingCommentsStayOnSameLineAutoFormatTest#trailingCommentStaysOnSameLine(the reproducer from the issue)AutoFormatTest#trailingCommentAfterTagContentStaysOnSameLineAutoFormatTest#misindentedTrailingCommentIsStillIndented(guards the contrast case)All three new same-line tests fail on
mainand pass with the fix.:rewrite-xml:test,:rewrite-maven:testand:rewrite-gradle:testare green.Not covered here
The issue also asks which recipe auto-formatted a scope far wider than it changed (
OrderPomElements,AddAnnotationProcessorover the whole<plugins>block are the likely candidates). This fix stops the comment reflow regardless of the caller; the over-wide auto-format scope is a separate question.