mirror of
https://github.com/goccy/go-yaml.git
synced 2025-12-08 06:09:57 +00:00
Add indentation to flow values on new lines (#759)
This commit is contained in:
parent
0040ab4161
commit
07c09c0287
2 changed files with 61 additions and 2 deletions
13
ast/ast.go
13
ast/ast.go
|
|
@ -1450,16 +1450,25 @@ func (n *MappingValueNode) toString() string {
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), value)
|
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), value)
|
||||||
} else if keyIndentLevel < valueIndentLevel && !n.IsFlowStyle {
|
} else if keyIndentLevel < valueIndentLevel && !n.IsFlowStyle {
|
||||||
|
valueStr := n.Value.String()
|
||||||
|
// For flow-style values indented on the next line, we need to add the proper indentation
|
||||||
|
if m, ok := n.Value.(*MappingNode); ok && m.IsFlowStyle {
|
||||||
|
valueIndent := strings.Repeat(" ", n.Value.GetToken().Position.Column-1)
|
||||||
|
valueStr = valueIndent + valueStr
|
||||||
|
} else if s, ok := n.Value.(*SequenceNode); ok && s.IsFlowStyle {
|
||||||
|
valueIndent := strings.Repeat(" ", n.Value.GetToken().Position.Column-1)
|
||||||
|
valueStr = valueIndent + valueStr
|
||||||
|
}
|
||||||
if keyComment != nil {
|
if keyComment != nil {
|
||||||
return fmt.Sprintf(
|
return fmt.Sprintf(
|
||||||
"%s%s: %s\n%s",
|
"%s%s: %s\n%s",
|
||||||
space,
|
space,
|
||||||
n.Key.stringWithoutComment(),
|
n.Key.stringWithoutComment(),
|
||||||
keyComment.String(),
|
keyComment.String(),
|
||||||
n.Value.String(),
|
valueStr,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s%s:\n%s", space, n.Key.String(), n.Value.String())
|
return fmt.Sprintf("%s%s:\n%s", space, n.Key.String(), valueStr)
|
||||||
} else if m, ok := n.Value.(*MappingNode); ok && (m.IsFlowStyle || len(m.Values) == 0) {
|
} else if m, ok := n.Value.(*MappingNode); ok && (m.IsFlowStyle || len(m.Values) == 0) {
|
||||||
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
|
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
|
||||||
} else if s, ok := n.Value.(*SequenceNode); ok && (s.IsFlowStyle || len(s.Values) == 0) {
|
} else if s, ok := n.Value.(*SequenceNode); ok && (s.IsFlowStyle || len(s.Values) == 0) {
|
||||||
|
|
|
||||||
|
|
@ -290,6 +290,30 @@ r: s
|
||||||
- b
|
- b
|
||||||
- c - d - e
|
- c - d - e
|
||||||
- f
|
- f
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`
|
||||||
|
elem1:
|
||||||
|
- elem2:
|
||||||
|
{a: b, c: d}
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
elem1:
|
||||||
|
- elem2:
|
||||||
|
{a: b, c: d}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`
|
||||||
|
elem1:
|
||||||
|
- elem2:
|
||||||
|
[a, b, c, d]
|
||||||
|
`,
|
||||||
|
`
|
||||||
|
elem1:
|
||||||
|
- elem2:
|
||||||
|
[a, b, c, d]
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
@ -1520,6 +1544,32 @@ foo2: &anchor text # anchor comment
|
||||||
# foo3 comment
|
# foo3 comment
|
||||||
# foo3 comment2
|
# foo3 comment2
|
||||||
foo3: *anchor # alias comment
|
foo3: *anchor # alias comment
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "flow map with inline key comment",
|
||||||
|
yaml: `
|
||||||
|
elem1:
|
||||||
|
- elem2: # comment
|
||||||
|
{a: b, c: d}
|
||||||
|
`,
|
||||||
|
expected: `
|
||||||
|
elem1:
|
||||||
|
- elem2: # comment
|
||||||
|
{a: b, c: d}
|
||||||
|
`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "flow sequence with inline key comment",
|
||||||
|
yaml: `
|
||||||
|
elem1:
|
||||||
|
- elem2: # comment
|
||||||
|
[a, b, c, d]
|
||||||
|
`,
|
||||||
|
expected: `
|
||||||
|
elem1:
|
||||||
|
- elem2: # comment
|
||||||
|
[a, b, c, d]
|
||||||
`,
|
`,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue