compact sequence indentation option

This commit is contained in:
Natasha Sarkar
2021-06-22 18:13:31 -07:00
committed by Katrina Verey
parent d0ae8fba13
commit d5a2009d3f
5 changed files with 50 additions and 7 deletions

View File

@@ -226,7 +226,7 @@ func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_
}
// Increase the indentation level.
func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool) bool {
func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool, compact_seq bool) bool {
emitter.indents = append(emitter.indents, emitter.indent)
if emitter.indent < 0 {
if flow {
@@ -243,6 +243,9 @@ func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool
// Everything else aligns to the chosen indentation.
emitter.indent = emitter.best_indent*((emitter.indent+emitter.best_indent)/emitter.best_indent)
}
if compact_seq {
emitter.indent = emitter.indent - 2
}
}
return true
}
@@ -534,7 +537,7 @@ func yaml_emitter_emit_flow_sequence_item(emitter *yaml_emitter_t, event *yaml_e
if !yaml_emitter_write_indicator(emitter, []byte{'['}, true, true, false) {
return false
}
if !yaml_emitter_increase_indent(emitter, true, false) {
if !yaml_emitter_increase_indent(emitter, true, false, false) {
return false
}
emitter.flow_level++
@@ -617,7 +620,7 @@ func yaml_emitter_emit_flow_mapping_key(emitter *yaml_emitter_t, event *yaml_eve
if !yaml_emitter_write_indicator(emitter, []byte{'{'}, true, true, false) {
return false
}
if !yaml_emitter_increase_indent(emitter, true, false) {
if !yaml_emitter_increase_indent(emitter, true, false, false) {
return false
}
emitter.flow_level++
@@ -728,7 +731,9 @@ func yaml_emitter_emit_flow_mapping_value(emitter *yaml_emitter_t, event *yaml_e
// Expect a block item node.
func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
if first {
if !yaml_emitter_increase_indent(emitter, false, false) {
seq := emitter.mapping_context && (emitter.column == 0 || !emitter.indention) &&
emitter.compact_sequence_indent
if !yaml_emitter_increase_indent(emitter, false, false, seq){
return false
}
}
@@ -764,7 +769,7 @@ func yaml_emitter_emit_block_sequence_item(emitter *yaml_emitter_t, event *yaml_
// Expect a block key node.
func yaml_emitter_emit_block_mapping_key(emitter *yaml_emitter_t, event *yaml_event_t, first bool) bool {
if first {
if !yaml_emitter_increase_indent(emitter, false, false) {
if !yaml_emitter_increase_indent(emitter, false, false, false) {
return false
}
}
@@ -896,7 +901,7 @@ func yaml_emitter_emit_scalar(emitter *yaml_emitter_t, event *yaml_event_t) bool
if !yaml_emitter_process_tag(emitter) {
return false
}
if !yaml_emitter_increase_indent(emitter, true, false) {
if !yaml_emitter_increase_indent(emitter, true, false, false) {
return false
}
if !yaml_emitter_process_scalar(emitter) {

View File

@@ -656,6 +656,37 @@ func (s *S) TestSetIndent(c *C) {
c.Assert(buf.String(), Equals, "a:\n b:\n c: d\n")
}
func (s *S) TestCompactSeqIndentDefault(c *C) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.CompactSeqIndent()
err := enc.Encode(map[string]interface{}{"a": []string{"b", "c"}})
c.Assert(err, Equals, nil)
err = enc.Close()
c.Assert(err, Equals, nil)
// The default indent is 4, so these sequence elements get 2 indents as before
c.Assert(buf.String(), Equals, `a:
- b
- c
`)
}
func (s *S) TestCompactSequenceWithSetIndent(c *C) {
var buf bytes.Buffer
enc := yaml.NewEncoder(&buf)
enc.CompactSeqIndent()
enc.SetIndent(2)
err := enc.Encode(map[string]interface{}{"a": []string{"b", "c"}})
c.Assert(err, Equals, nil)
err = enc.Close()
c.Assert(err, Equals, nil)
// The sequence indent is 2, so these sequence elements don't get indented at all
c.Assert(buf.String(), Equals, `a:
- b
- c
`)
}
func (s *S) TestSortedOutput(c *C) {
order := []interface{}{
false,

View File

@@ -2,4 +2,4 @@ module "gopkg.in/yaml.v3"
require (
"gopkg.in/check.v1" v0.0.0-20161208181325-20d25e280405
)
)

View File

@@ -278,6 +278,11 @@ func (e *Encoder) SetIndent(spaces int) {
e.encoder.indent = spaces
}
// CompactSeqIndent makes it so that '- ' is considered part of the indentation.
func (e *Encoder) CompactSeqIndent() {
e.encoder.emitter.compact_sequence_indent = true
}
// Close closes the encoder by writing any remaining data.
// It does not write a stream terminating string "...".
func (e *Encoder) Close() (err error) {

View File

@@ -742,6 +742,8 @@ type yaml_emitter_t struct {
indent int // The current indentation level.
compact_sequence_indent bool // Is '- ' is considered part of the indentation for sequence elements?
flow_level int // The current flow level.
root_context bool // Is it the document root context?