Fix YAML block indentation after leading newlines (#1990)

This commit is contained in:
0z5a authored and GitHub committed 2026-11-15 19:10:05 -08:00
1 parent ad7dedeecd
commit 1a80881874
3 files changed
+27 -4

No files matched your search

@@ -292,12 +292,20 @@ public abstract class YamlEmitter {
private void emitMultilineString(String str, int newlineIndex, StringBuilder currIndent) {
currIndent.append(indent);
var length = str.length();
// YAML detects the indentation of a block scalar from its first non-empty line.
// If that line starts with a space, emit an explicit indentation indicator.
var firstNonNewlineIndex = 0;
while (firstNonNewlineIndex < length && str.charAt(firstNonNewlineIndex) == '\n') {
firstNonNewlineIndex++;
}
builder.append('|');
if (str.charAt(0) == ' ') {
if (firstNonNewlineIndex < length && str.charAt(firstNonNewlineIndex) == ' ') {
builder.append(indent.length());
}
var length = str.length();
if (str.charAt(length - 1) == '\n') {
if (length == 1 || str.charAt(length - 2) == '\n') {
builder.append('+');
@@ -59,6 +59,9 @@ i3 = "one\u{7}two\nthree"
j1 = " one\ntwo"
j2 = " one\ntwo"
j3 = "one\n two"
j4 = "\n one"
j5 = "\n\n one\ntwo"
j6 = "\n \none"
// trailing spaces in multiline strings
k1 = "one\ntwo "
@@ -74,6 +74,18 @@ j2: |2-
j3: |-
one
two
j4: |2-
one
j5: |2-
one
two
j6: |2-
one
k1: |-
one
two
@@ -83,7 +95,7 @@ k2: |-
k3: |-
one
two
l1: |-
l1: |2-
l2: |2
@@ -91,7 +103,7 @@ l2: |2
l3: |2-
l4: |
l4: |2
m1: 'foo:'