Stop MergeYamlVisitor from grafting the document end comment onto inserted entries - #8432
Merged
Conversation
…erted entries An inline comment on the last line of a document is stored on `Yaml.Document.End`, so it has to be moved onto the first newly inserted entry to stay on its own line. Removing the original copy was left entirely to `MergeYaml`, which meant the comment was rendered twice when `MergeYamlVisitor` was used directly. It now consumes those cursor messages itself for the documents and mappings it traverses. Only the part of the document end prefix before the first line break is treated as an inline comment now; a comment on a line of its own stays where it is, and a trailing line break is no longer swallowed. Fixes #8416
timtebeek
force-pushed
the
tim/cape-town-v3
branch
from
August 8, 2026 20:54
fc2672a to
3062bc4
Compare
The `createNewKeys` branch of `visitDocument` returned before the cleanup ran, so a trailing inline comment was duplicated onto the key that was created on the spot. Both branches now fall through to a single tail.
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 #8416
An inline comment on the last line of a document is stored on
Yaml.Document.End. WhenMergeYamlVisitorappends entries to the block that comment trails, it has to copy the comment onto the first newly inserted entry, otherwise the comment slides down to the last line of the inserted block. Removing the copy it moved away from was left entirely toMergeYaml, through cursor messages on ancestors thatMergeYamlVisitordoes not traverse itself, so anyone using the visitor directly got the comment rendered twice.MergeYamlVisitornow consumes those messages for the documents and mappings it does traverse, so applying it to the enclosingYaml.Documents/Yaml.Documentproduces the same result as theMergeYamlrecipe. That is now stated in the class javadoc. Merging into a block without passing the enclosing document remains unable to clean upYaml.Document.End— the visitor returns only the block it was given — hence the javadoc note.While confirming the recipe path, three related defects surfaced in how the document end prefix is treated. The first two only show up on documents ending in a line break, which
RewriteTeststrips from its text blocks, which is why no existing test caught them:value: true # inline\ninserted a blank line before the new entry and dropped the file's trailing line break. Only the part before the first line break is taken now, and the rest is retained.createNewKeysbranch ofMergeYaml.visitDocumentreturned before the cleanup ran, so the comment was duplicated onto the key created on the spot. Both branches now fall through to a single tail.