Skip to content

Commit

Permalink
Do not insert newline before Yaml SequenceEntry lacking dash
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Jul 8, 2024
1 parent 64e814d commit da3f0a4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ public class MinimumViableSpacingVisitor<P> extends YamlIsoVisitor<P> {
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;
}
}
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2024 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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
"""
)
);
}
}

0 comments on commit da3f0a4

Please sign in to comment.