Skip to content

Fix MergeYaml sequence entry indentation with objectIdentifyingProperty#7113

Closed
timtebeek wants to merge 5 commits intomainfrom
tim/discussion-7107-reply
Closed

Fix MergeYaml sequence entry indentation with objectIdentifyingProperty#7113
timtebeek wants to merge 5 commits intomainfrom
tim/discussion-7107-reply

Conversation

@timtebeek
Copy link
Copy Markdown
Member

@timtebeek timtebeek commented Mar 24, 2026

Summary

  • When merging new entries into a YAML sequence via nested MergeYamlVisitor calls (e.g. key: $ with a nested sequence), autoFormat produced incorrect indentation, causing new entries to appear as children of the last existing entry's sub-sequence rather than as siblings.
  • Fix by re-indenting the auto-formatted prefix to match existing sequence entries, while preserving comments and blank lines.
  • Added a test reproducing the exact scenario from the discussion.

…ty (#7107)

When merging new entries into a YAML sequence via nested MergeYamlVisitor
calls (e.g. key: $ with a nested sequence), autoFormat produced incorrect
indentation causing new entries to appear as children of the last existing
entry's sub-sequence. Fix by re-indenting the auto-formatted prefix to
match existing sequence entries while preserving comments and blank lines.
@timtebeek timtebeek added bug Something isn't working recipe Requested Recipe yaml labels Mar 24, 2026
AutoFormatVisitor forks the cursor (discarding messages) and IndentsVisitor
lacks sequence-specific indent awareness, so fixing the root cause would
require changes to shared YAML formatting infrastructure. Document this
trade-off in the method javadoc.
@timtebeek timtebeek requested a review from Jenson3210 March 24, 2026 13:38
List<Yaml.Sequence.Entry> entries = concatAll(
filter(s1.getEntries(), it -> !mutatedEntries.contains(it)),
map(mutatedEntries, it -> autoFormat(it, p, cursor)),
map(mutatedEntries, it -> reindentEntry(autoFormat(it, p, cursor), sequenceEntryIndent)),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we would pass the correct parent entry cursor here, would that make the formatting not do the correct things here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went through some iterations here that culminated in the Javadoc in reindentEntry. Welcome to have a go as well if you think the cursor approach is doable too; that had my preference, but didn't want any ripple effects from a small change here.

Add a test merging a new entry with nested content (specs sequence) into
an existing sequence with objectIdentifyingProperty, as requested in PR
review.

Extend reindentEntry to also shift indentation of nested content within
the entry's block via ShiftIndentVisitor, so that mapping entries and
inner sequences are corrected by the same delta as the entry prefix.

Note: autoFormat always uses indented-sequence style (- indented by
indentSize relative to parent key), even when the existing document uses
same-column style. The shift corrects the absolute column but cannot
change the relative style.
@timtebeek timtebeek marked this pull request as draft March 24, 2026 15:45
- Replace replaceFirst("^\n", "") with startsWith/substring
- Replace replaceAll("^\\s+", "") with StringUtils.indexOfNonWhitespace
- Replace char[]/Arrays.fill with StringUtils.repeat, matching IndentsVisitor
@Jenson3210
Copy link
Copy Markdown
Contributor

@timtebeek I wrote a unit test that verifies we simply do not support same-column sequences in any formatting. Questioning if we should not try to add support rather than only have some backport that works on this one?

@Jenson3210
Copy link
Copy Markdown
Contributor

@timtebeek timtebeek closed this Mar 25, 2026
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Mar 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working recipe Requested Recipe yaml

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants