Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extra blank line when serializing mapping inside of sequence #367

Open
bishabosha opened this issue Dec 10, 2024 · 1 comment
Open

Extra blank line when serializing mapping inside of sequence #367

bishabosha opened this issue Dec 10, 2024 · 1 comment

Comments

@bishabosha
Copy link

bishabosha commented Dec 10, 2024

when the serializer does "push and indent" for entering mapping/sequence, there is always a newline inserted see code, which leads to extra blank lines.

e.g. Map("foo" -> Seq(Map("bar" -> 123))).asYaml

goes to:

foo: 
  - 
    bar: 123

It would be nice to avoid the extra blank line specifically when the first entry of a sequence is a mapping. (it is fine for scalar values)

perhaps this could be configurable if there is a possibility of conflict

@bishabosha bishabosha changed the title Extra blank line when mapping inside of sequence Extra blank line when serializing mapping inside of sequence Dec 10, 2024
@bishabosha
Copy link
Author

bishabosha commented Dec 19, 2024

As a workaround, this function works to post-process the string afterwards:

def shiftLines(yaml: String): String =
  val lines = yaml.linesIterator.toVector
  def startsWith[A](a: A, it: Iterator[A]): Boolean = it.hasNext && it.next == a
  lines.tail
    .foldLeft(lines.head :: Nil) { (before, line) =>
      val prev :: rest = before: @unchecked
      val prevEnd = prev.reverseIterator.dropWhile(_.isWhitespace)
      val lineStart = line.iterator.dropWhile(_.isWhitespace)
      if startsWith('-', prevEnd) && !startsWith('#', lineStart) then
        val (padding, content) = line.span(_.isWhitespace)
        (prev.padTo(padding.size, ' ') ++ content) :: rest
      else line :: prev :: rest
    }
    .reverse
    .mkString("\n")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant