Skip to content

Commit a95d669

Browse files
author
Laurens Westerlaken
authored
Yaml newline after end of document (#6035)
* Add testcase and initial implementation * Move suffix to Yaml/Documents
1 parent 21a8926 commit a95d669

4 files changed

Lines changed: 37 additions & 10 deletions

File tree

rewrite-yaml/src/main/java/org/openrewrite/yaml/YamlParser.java

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ private Yaml.Documents parseFromInput(Path sourceFile, EncodingDetectingInputStr
138138
Yaml.Document document = null;
139139
Stack<BlockBuilder> blockStack = new Stack<>();
140140
String newLine = "";
141+
String suffix = "";
141142

142143
for (Event event = parser.getEvent(); event != null; event = parser.getEvent()) {
143144
switch (event.getEventId()) {
@@ -430,14 +431,18 @@ private Yaml.Documents parseFromInput(Path sourceFile, EncodingDetectingInputStr
430431
break;
431432
}
432433
case StreamEnd: {
433-
String fmt = newLine + reader.prefix(lastEnd, event);
434-
if (document == null && !fmt.isEmpty()) {
435-
documents.add(
436-
new Yaml.Document(
437-
randomId(), fmt, Markers.EMPTY, false,
438-
new Yaml.Mapping(randomId(), Markers.EMPTY, null, emptyList(), null, null, null),
439-
new Yaml.Document.End(randomId(), "", Markers.EMPTY, false)
440-
));
434+
if (document == null) {
435+
String fmt = newLine + reader.prefix(lastEnd, event);
436+
if (!fmt.isEmpty()) {
437+
documents.add(
438+
new Yaml.Document(
439+
randomId(), fmt, Markers.EMPTY, false,
440+
new Yaml.Mapping(randomId(), Markers.EMPTY, null, emptyList(), null, null, null),
441+
new Yaml.Document.End(randomId(), "", Markers.EMPTY, false)
442+
));
443+
}
444+
} else {
445+
suffix = reader.prefix(lastEnd, event);
441446
}
442447
break;
443448
}
@@ -446,7 +451,7 @@ private Yaml.Documents parseFromInput(Path sourceFile, EncodingDetectingInputStr
446451
}
447452
}
448453

449-
Yaml.Documents result = new Yaml.Documents(randomId(), Markers.EMPTY, sourceFile, FileAttributes.fromPath(sourceFile), source.getCharset().name(), source.isCharsetBomMarked(), null, documents);
454+
Yaml.Documents result = new Yaml.Documents(randomId(), Markers.EMPTY, sourceFile, FileAttributes.fromPath(sourceFile), source.getCharset().name(), source.isCharsetBomMarked(), null, suffix, documents);
450455
if (helmTemplateByUuid.isEmpty() && variableByUuid.isEmpty()) {
451456
return result;
452457
}

rewrite-yaml/src/main/java/org/openrewrite/yaml/internal/YamlPrinter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class YamlPrinter<P> extends YamlVisitor<PrintOutputCapture<P>> {
2929
public Yaml visitDocuments(Yaml.Documents documents, PrintOutputCapture<P> p) {
3030
visitMarkers(documents.getMarkers(), p);
3131
visit(documents.getDocuments(), p);
32+
p.append(documents.getSuffix());
3233
afterSyntax(documents, p);
3334
return documents;
3435
}

rewrite-yaml/src/main/java/org/openrewrite/yaml/tree/Yaml.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class Documents implements Yaml, SourceFileWithReferences {
8686
@Nullable
8787
Checksum checksum;
8888

89+
@Nullable
90+
String suffix;
91+
8992
@Override
9093
public Charset getCharset() {
9194
return charsetName == null ? StandardCharsets.UTF_8 : Charset.forName(charsetName);
@@ -106,7 +109,7 @@ public <P> Yaml acceptYaml(YamlVisitor<P> v, P p) {
106109
@Override
107110
public Documents copyPaste() {
108111
return new Documents(randomId(), Markers.EMPTY,
109-
sourcePath, fileAttributes, charsetName, charsetBomMarked, checksum, documents.stream().map(Document::copyPaste).collect(toList()));
112+
sourcePath, fileAttributes, charsetName, charsetBomMarked, checksum, suffix, documents.stream().map(Document::copyPaste).collect(toList()));
110113
}
111114

112115
/**

rewrite-yaml/src/test/java/org/openrewrite/yaml/YamlParserTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.openrewrite.Issue;
2323
import org.openrewrite.SourceFile;
2424
import org.openrewrite.test.RewriteTest;
25+
import org.openrewrite.test.SourceSpec;
2526
import org.openrewrite.tree.ParseError;
2627
import org.openrewrite.yaml.tree.Yaml;
2728

@@ -465,6 +466,23 @@ void withAnchorSequenceOnRootLevel() {
465466
);
466467
}
467468

469+
@Issue("https://github.com/moderneinc/customer-requests/issues/1471")
470+
@Test
471+
void yamlWithDocumentEndMarker() {
472+
rewriteRun(
473+
yaml(
474+
"""
475+
---
476+
applications:
477+
- name: modified-app-name
478+
memory: 1G
479+
...
480+
""",
481+
SourceSpec::noTrim
482+
)
483+
);
484+
}
485+
468486
@Test
469487
void helmTemplateMatchingDocumentEndParsesCorrectly() {
470488
rewriteRun(

0 commit comments

Comments
 (0)