google / yamlfmt

An extensible command line tool or library to format yaml files.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add option to remove indentation on arrays

pmialon opened this issue · comments

Yaml support indented or not array.

array:
  - a
  - b
array:
- a
- b

It would be great if the basic formater had an option to remove spaces before hyphens.

Thanks for making an issue!

I should be able to do this in the next release.

The pertinent section of the YAML specification is §8.2.1, Block Sequences. The specification shows its examples there with indentation before the sequence entries.

From §6.1:

The “-”, “?” and “:” characters used to denote block collection entries are perceived by people to be part of the indentation. This is handled on a case-by-case basis by the relevant productions.

Later in §8.2.3, handling one of those cases:

Since people perceive the “-” indicator as indentation, nested block sequences may be indented by one less space to compensate, except, of course, if nested inside another block sequence ([BLOCK-OUT context] versus [BLOCK-IN context]).

I'm pretty sure that that leads to production 201:

seq-space(n,BLOCK-OUT) ::= l+block-sequence(n-1)

It's that -1 that tolerates dropping the minimum indentation from one to zero.

Hi @seh, yes it's definitely valid yaml. The library simply doesn't expose an option to change to the unindented style. I have seen there's an example of someone else adding this option in their own fork of the same library, I can likely use that as inspiration to add the same thing. After that I will add an option in yamlfmt for it.

commented

Thanks @braydonk !
yamlfmt is now perfect for me.