diff --git a/rewrite-yaml/src/main/java/org/openrewrite/yaml/format/MinimumViableSpacingVisitor.java b/rewrite-yaml/src/main/java/org/openrewrite/yaml/format/MinimumViableSpacingVisitor.java index e8b1f45214b..bb010ddfcf4 100644 --- a/rewrite-yaml/src/main/java/org/openrewrite/yaml/format/MinimumViableSpacingVisitor.java +++ b/rewrite-yaml/src/main/java/org/openrewrite/yaml/format/MinimumViableSpacingVisitor.java @@ -30,9 +30,9 @@ public class MinimumViableSpacingVisitor

extends YamlIsoVisitor

{ public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, P p) { Yaml.Mapping.Entry e = super.visitMappingEntry(entry, p); if (!e.getPrefix().contains("\n")) { - if(getCursor().getParentOrThrow(2).getValue() instanceof Yaml.Document) { + if (getCursor().getParentOrThrow(2).getValue() instanceof Yaml.Document) { Yaml.Mapping mapping = getCursor().getParentOrThrow().getValue(); - if(mapping.getEntries().isEmpty() || mapping.getEntries().get(0) == e) { + if (mapping.getEntries().isEmpty() || mapping.getEntries().get(0) == e) { return e; } } @@ -52,7 +52,7 @@ public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, P p) { @Override public Yaml.Sequence.Entry visitSequenceEntry(Yaml.Sequence.Entry entry, P p) { Yaml.Sequence.Entry e = super.visitSequenceEntry(entry, p); - if (!e.getPrefix().contains("\n")) { + if (!e.getPrefix().contains("\n") && e.isDash()) { return e.withPrefix("\n"); } return e; diff --git a/rewrite-yaml/src/test/java/org/openrewrite/yaml/format/MinimumViableSpacingVisitorTest.java b/rewrite-yaml/src/test/java/org/openrewrite/yaml/format/MinimumViableSpacingVisitorTest.java new file mode 100644 index 00000000000..83453ad628e --- /dev/null +++ b/rewrite-yaml/src/test/java/org/openrewrite/yaml/format/MinimumViableSpacingVisitorTest.java @@ -0,0 +1,68 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.yaml.format; + +import org.junit.jupiter.api.Test; +import org.openrewrite.ExecutionContext; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; +import org.openrewrite.yaml.YamlIsoVisitor; +import org.openrewrite.yaml.tree.Yaml; + +import static org.openrewrite.test.RewriteTest.toRecipe; +import static org.openrewrite.yaml.Assertions.yaml; + +class MinimumViableSpacingVisitorTest implements RewriteTest { + @Override + public void defaults(RecipeSpec spec) { + spec.recipe( + toRecipe(() -> new YamlIsoVisitor<>() { + @Override + public Yaml.Documents visitDocuments(Yaml.Documents documents, ExecutionContext ctx) { + return new MinimumViableSpacingVisitor<>(null).visitDocuments(documents, ctx); + } + } + ) + ); + } + + @Test + void sameLineArrayNotChanged() { + rewriteRun( + yaml( + """ + build: + existing: value + steps: [run: publish] + """ + ) + ); + } + + @Test + void nextLineArrayNotChanged() { + rewriteRun( + yaml( + """ + build: + existing: value + steps: + - run: publish + """ + ) + ); + } +}