diff --git a/rewrite-core/src/main/java/org/openrewrite/Cursor.java b/rewrite-core/src/main/java/org/openrewrite/Cursor.java index 3be9d7c15a3..7ed54841f73 100644 --- a/rewrite-core/src/main/java/org/openrewrite/Cursor.java +++ b/rewrite-core/src/main/java/org/openrewrite/Cursor.java @@ -57,7 +57,7 @@ public Cursor getRoot() { /** * @return true if this cursor is the root of the tree, false otherwise */ - final boolean isRoot() { + final public boolean isRoot() { return ROOT_VALUE.equals(value); } 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 5361955065f..3b1bf9c1355 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYaml.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYaml.java @@ -84,6 +84,7 @@ public String getDescription() { } final static String FOUND_MATCHING_ELEMENT = "FOUND_MATCHING_ELEMENT"; + final static String REMOVE_PREFIX = "REMOVE_PREFIX"; @Override public TreeVisitor getVisitor() { @@ -96,7 +97,7 @@ public TreeVisitor getVisitor() { .map(docs -> { // Any comments will have been put on the parent Document node, preserve by copying to the mapping Yaml.Document doc = docs.getDocuments().get(0); - if(doc.getBlock() instanceof Yaml.Mapping) { + 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) { @@ -110,9 +111,11 @@ public TreeVisitor getVisitor() { @Override public Yaml.Document visitDocument(Yaml.Document document, ExecutionContext ctx) { if ("$".equals(key)) { - return document.withBlock((Yaml.Block) new MergeYamlVisitor<>(document.getBlock(), yaml, - Boolean.TRUE.equals(acceptTheirs), objectIdentifyingProperty).visitNonNull(document.getBlock(), - ctx, getCursor())); + Yaml.Document d = document.withBlock((Yaml.Block) + new MergeYamlVisitor<>(document.getBlock(), yaml, Boolean.TRUE.equals(acceptTheirs), objectIdentifyingProperty) + .visitNonNull(document.getBlock(), ctx, getCursor()) + ); + return getCursor().getMessage(REMOVE_PREFIX, false) ? d.withEnd(d.getEnd().withPrefix("")) : d; } Yaml.Document d = super.visitDocument(document, ctx); if (d == document && !getCursor().getMessage(FOUND_MATCHING_ELEMENT, false)) { 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 66a5f310506..e3d24a32898 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYamlVisitor.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYamlVisitor.java @@ -15,23 +15,47 @@ */ package org.openrewrite.yaml; -import lombok.AllArgsConstructor; import lombok.RequiredArgsConstructor; import org.intellij.lang.annotations.Language; import org.jspecify.annotations.Nullable; import org.openrewrite.Cursor; import org.openrewrite.internal.ListUtils; +import org.openrewrite.style.GeneralFormatStyle; import org.openrewrite.yaml.tree.Yaml; +import org.openrewrite.yaml.tree.Yaml.Document; +import org.openrewrite.yaml.tree.Yaml.Mapping; +import org.openrewrite.yaml.tree.Yaml.Mapping.Entry; +import org.openrewrite.yaml.tree.Yaml.Scalar; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.Optional; +import java.util.regex.Pattern; import java.util.stream.Collectors; -@AllArgsConstructor +import static java.lang.System.lineSeparator; +import static org.openrewrite.Cursor.ROOT_VALUE; +import static org.openrewrite.internal.ListUtils.*; +import static org.openrewrite.yaml.MergeYaml.REMOVE_PREFIX; +/** + * Visitor class to merge two yaml files. + * + * @implNote Loops recursively through the documents, for every part a new MergeYamlVisitor instance will be created. + * As inline comments are put on the prefix of the next element (which can be an item very much higher in the tree), + * the following solutions are chosen to merge the comments as well: + *