From 6602fe7ddc634787070bd58c1aa96081fb6c6c7c Mon Sep 17 00:00:00 2001 From: Sam Snyder Date: Tue, 15 Oct 2024 20:57:14 -0700 Subject: [PATCH] Improve yaml handling of comments in sequences. --- .../java/org/openrewrite/yaml/MergeYaml.java | 3 ++ .../openrewrite/yaml/MergeYamlVisitor.java | 14 +++++++- .../yaml/format/IndentsVisitor.java | 7 ++-- .../org/openrewrite/yaml/MergeYamlTest.java | 33 +++++++++++++++++++ 4 files changed, 54 insertions(+), 3 deletions(-) diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYaml.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYaml.java index 742edeff870..5361955065f 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYaml.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYaml.java @@ -99,6 +99,9 @@ public TreeVisitor 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()); }) diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYamlVisitor.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYamlVisitor.java index 05e1eb43316..66a5f310506 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYamlVisitor.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYamlVisitor.java @@ -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()); }) @@ -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 entries = ListUtils.concatAll( s1.getEntries().stream().filter(entry -> !mutatedEntries.contains(entry)) diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/format/IndentsVisitor.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/format/IndentsVisitor.java index 63dd01d20f8..d1a1ecd8a2d 100755 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/format/IndentsVisitor.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/format/IndentsVisitor.java @@ -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; } @@ -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; diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java index 59418ad5c34..fa8c3ba91c5 100644 --- a/rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java +++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java @@ -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 + """ + ) + ); + } }