void WriteSequence(YamlSequence obj, bool allowInlining) { if (allowInlining && TryInlineSequence(obj, _maximumWidth - _indentColumn, out var inlined)) { WriteInline(inlined); } else { var oldInlineColumn = _inlineColumn; _inlineColumn = _indentColumn + _indentSize; foreach (var item in obj) { WriteLine(); Write("- "); allowInlining = !TryWriteComment(item); _indentColumn += _indentSize; _inlineColumn = _indentColumn; WriteObject(item, allowInlining); _indentColumn -= _indentSize; } _inlineColumn = oldInlineColumn; } }
bool TryInlineSequence(YamlSequence obj, int maximumWidth, [MaybeNullWhen(false)] out string result) { result = null; // If any of the items have comments, do not inline as there's nowhere to write the comments. if (obj.Any(a => a?.Comment is string)) { return(false); } var sb = new StringBuilder(); sb.Append("[ "); var firstSeen = false; foreach (var value in obj) { if (firstSeen) { sb.Append(", "); } firstSeen = true; if (TryInlineObject(value, maximumWidth - sb.Length, out var valueInlined)) { sb.Append(valueInlined); } else { result = null; return(false); } } sb.Append(firstSeen ? " ]" : "]"); if (sb.Length <= maximumWidth) { result = sb.ToString(); } return(result is not null); }
bool TryInlineSequence(YamlSequence obj, int maximumWidth, out string result) { result = null; var sb = new StringBuilder(); sb.Append("[ "); var firstSeen = false; foreach (var value in obj) { if (firstSeen) { sb.Append(", "); } firstSeen = true; if (TryInlineObject(value, maximumWidth - sb.Length, out var valueInlined)) { sb.Append(valueInlined); } else { result = null; return(false); } } sb.Append(firstSeen ? " ]" : "]"); if (sb.Length <= maximumWidth) { result = sb.ToString(); } return(result != null); }