Skip to content

Commit

Permalink
Improve yaml handling of comments in sequences.
Browse files Browse the repository at this point in the history
  • Loading branch information
sambsnyd committed Oct 16, 2024
1 parent 9da3321 commit 6602fe7
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
if(doc.getBlock() instanceof Yaml.Mapping) {
Yaml.Mapping m = (Yaml.Mapping) doc.getBlock();
return m.withEntries(ListUtils.mapFirst(m.getEntries(), entry -> entry.withPrefix(doc.getPrefix())));
} else if (doc.getBlock() instanceof Yaml.Sequence) {
Yaml.Sequence s = (Yaml.Sequence) doc.getBlock();
return s.withEntries(ListUtils.mapFirst(s.getEntries(), entry -> entry.withPrefix(doc.getPrefix())));
}
return doc.getBlock().withPrefix(doc.getPrefix());
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ public MergeYamlVisitor(Yaml scope, @Language("yml") String yamlString, boolean
if(doc.getBlock() instanceof Yaml.Mapping) {
Yaml.Mapping m = (Yaml.Mapping) doc.getBlock();
return m.withEntries(ListUtils.mapFirst(m.getEntries(), entry -> entry.withPrefix(doc.getPrefix())));
} else if (doc.getBlock() instanceof Yaml.Sequence) {
Yaml.Sequence s = (Yaml.Sequence) doc.getBlock();
return s.withEntries(ListUtils.mapFirst(s.getEntries(), entry -> entry.withPrefix(doc.getPrefix())));
}
return doc.getBlock().withPrefix(doc.getPrefix());
})
Expand Down Expand Up @@ -172,11 +175,20 @@ private Yaml.Sequence mergeSequence(Yaml.Sequence s1, Yaml.Sequence s2, P p, Cur
for (Yaml.Sequence.Entry existingEntry : s1.getEntries()) {
Yaml.Mapping existingMapping = (Yaml.Mapping) existingEntry.getBlock();
if (keyMatches(existingMapping, incomingMapping)) {
return existingEntry.withBlock(mergeMapping(existingMapping, incomingMapping, p, cursor));
Yaml.Sequence.Entry e1 = existingEntry.withBlock(mergeMapping(existingMapping, incomingMapping, p, cursor));
if(e1 == existingEntry) {
// Made no change, no need to consider the entry "mutated"
//noinspection DataFlowIssue
return null;
}
return e1;
}
}
return entry;
});
if (mutatedEntries.isEmpty()) {
return s1;
}

List<Yaml.Sequence.Entry> entries = ListUtils.concatAll(
s1.getEntries().stream().filter(entry -> !mutatedEntries.contains(entry))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ public IndentsVisitor(IndentsStyle style, @Nullable Tree stopAfter) {
}

private boolean isUnindentedTopLevel() {
return getCursor().getParentOrThrow().getValue() instanceof Yaml.Document ||
return getCursor().getValue() instanceof Yaml.Documents ||
getCursor().getParentOrThrow().getValue() instanceof Yaml.Document ||
getCursor().getParentOrThrow(2).getValue() instanceof Yaml.Document;
}

Expand All @@ -123,10 +124,12 @@ private String indentTo(String prefix, int column) {
}

int indent = findIndent(prefix);
prefix = indentComments(prefix, indent);
if (indent != column) {
int shift = column - indent;
prefix = indent(prefix, shift);
prefix = indentComments(prefix, shift);
} else {
prefix = indentComments(prefix, indent);
}

return prefix;
Expand Down
33 changes: 33 additions & 0 deletions rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1168,4 +1168,37 @@ void comment() {
)
);
}

@Test
void commentInList() {
rewriteRun(
spec -> spec.recipe(
new MergeYaml(
"$.groups",
//language=yaml
"""
# comment
- id: 3
""",
false,
"id",
null
)),
yaml(
"""
groups:
- id: 1
- id: 2
""",
"""
groups:
- id: 1
- id: 2
# comment
- id: 3
"""
)
);
}
}

0 comments on commit 6602fe7

Please sign in to comment.