Skip to content

Commit

Permalink
v3: Don't count seq indicator as part of indent.
Browse files Browse the repository at this point in the history
  • Loading branch information
monopole committed Jun 14, 2021
1 parent 2d3372c commit 020d876
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 35 deletions.
24 changes: 17 additions & 7 deletions emitterc.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,15 @@ func yaml_emitter_append_tag_directive(emitter *yaml_emitter_t, value *yaml_tag_
return true
}

// Don't count the sequenceIndicator as part of sequence indentation.
// This maintains sequence indentation consistency with gopkg.in/yaml.v2
// (https://pkg.go.dev/gopkg.in/yaml.v2#readme-api-documentation)
// and conforms to the YAML 1.2 spec comment
// > both the “-” indicator and the following spaces are considered
// > to be part of the indentation of the nested collection.
// at https://yaml.org/spec/1.2/spec.html#id2772075
const lenSequenceIndicator = len(`- `)

// Increase the indentation level.
func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool) bool {
emitter.indents = append(emitter.indents, emitter.indent)
Expand All @@ -235,13 +244,10 @@ func yaml_emitter_increase_indent(emitter *yaml_emitter_t, flow, indentless bool
emitter.indent = 0
}
} else if !indentless {
// [Go] This was changed so that indentations are more regular.
if emitter.states[len(emitter.states)-1] == yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE {
// The first indent inside a sequence will just skip the "- " indicator.
emitter.indent += 2
} else {
// Everything else aligns to the chosen indentation.
emitter.indent = emitter.best_indent*((emitter.indent+emitter.best_indent)/emitter.best_indent)
emitter.indent += emitter.best_indent
// If inside a block sequence item, discount the space taken by the indicator.
if emitter.best_indent > lenSequenceIndicator && emitter.states[len(emitter.states)-1] == yaml_EMIT_BLOCK_SEQUENCE_ITEM_STATE {
emitter.indent -= lenSequenceIndicator
}
}
return true
Expand Down Expand Up @@ -728,9 +734,13 @@ 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 {
originalIndent := emitter.indent
if !yaml_emitter_increase_indent(emitter, false, false) {
return false
}
if emitter.indent > originalIndent+lenSequenceIndicator {
emitter.indent -= lenSequenceIndicator
}
}
if event.typ == yaml_SEQUENCE_END_EVENT {
emitter.indent = emitter.indents[len(emitter.indents)-1]
Expand Down
56 changes: 28 additions & 28 deletions encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,25 +138,25 @@ v: ""
}, {
map[string][]string{"v": []string{"A", "B"}}, `
v:
- A
- B
- A
- B
`,
}, {
map[string][]string{"v": []string{"A", "B\nC"}}, `
v:
- A
- |-
B
C
- A
- |-
B
C
`,
}, {
map[string][]interface{}{"v": []interface{}{"A", 1, map[string][]int{"B": []int{2, 3}}}}, `
v:
- A
- 1
- B:
- 2
- 3
- A
- 1
- B:
- 2
- 3
`,
}, {
map[string]interface{}{"a": map[interface{}]interface{}{"b": "c"}}, `
Expand Down Expand Up @@ -214,14 +214,14 @@ a: 1
}, {
&struct{ A []int }{[]int{1, 2}}, `
a:
- 1
- 2
- 1
- 2
`,
}, {
&struct{ A [2]int }{[2]int{1, 2}}, `
a:
- 1
- 2
- 1
- 2
`,
}, {
&struct {
Expand Down Expand Up @@ -530,8 +530,8 @@ true
map[string]interface{}{"a": map[string]interface{}{"b": []map[string]int{{"c": 1, "d": 2}}}}, `
a:
b:
- c: 1
d: 2
- c: 1
d: 2
`,
},

Expand Down Expand Up @@ -713,7 +713,7 @@ var marshalerTests = []struct {
value interface{}
}{
{"_:\n hi: there\n", map[interface{}]interface{}{"hi": "there"}},
{"_:\n - 1\n - A\n", []interface{}{1, "A"}},
{"_:\n - 1\n - A\n", []interface{}{1, "A"}},
{"_: 10\n", 10},
{"_: null\n", nil},
{"_: BAR!\n", "BAR!"},
Expand Down Expand Up @@ -794,24 +794,24 @@ a:
b:
c: d
e:
- 1
- 2
- 3
- 1
- 2
- 3
f:
- g
- h
- g
- h
`
indentEight = `
a:
b:
c: d
e:
- 1
- 2
- 3
- 1
- 2
- 3
f:
- g
- h
- g
- h
`
)

Expand Down

0 comments on commit 020d876

Please sign in to comment.