hcl/printer: newline after heredoc items in lists

This commit is contained in:
Mitchell Hashimoto 2017-01-08 16:24:08 -08:00
parent e930f8e5fe
commit 4f76b0e04a
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 35 additions and 4 deletions

View File

@ -503,7 +503,16 @@ func (p *printer) list(l *ast.ListType) []byte {
val := p.output(item)
curLen := len(val)
buf.Write(p.indent(val))
buf.WriteString(",")
// if this item is a heredoc, then we output the comma on
// the next line. This is the only case this happens.
comma := []byte{','}
if lit, ok := item.(*ast.LiteralType); ok && lit.Token.Type == token.HEREDOC {
buf.WriteByte(newline)
comma = p.indent(comma)
}
buf.Write(comma)
if lit, ok := item.(*ast.LiteralType); ok && lit.LineComment != nil {
// if the next item doesn't have any comments, do not align

View File

@ -132,7 +132,7 @@ func format(src []byte) ([]byte, error) {
// make sure formatted output is syntactically correct
if _, err := parser.Parse(formatted); err != nil {
return nil, fmt.Errorf("parse: %s\n%s", err, src)
return nil, fmt.Errorf("parse: %s\n%s", err, formatted)
}
return formatted, nil

View File

@ -25,3 +25,14 @@ foo = [
"ethiopia",
"columbia",
]
foo = [
<<EOS
one
EOS
,
<<EOS
two
EOS
,
]

View File

@ -2,14 +2,14 @@ foo = ["fatih", "arslan" ]
foo = [ "bar", "qaz", ]
foo = [ "zeynep",
foo = [ "zeynep",
"arslan", ]
foo = ["fatih", "zeynep",
"arslan", ]
foo = [
"vim-go",
"vim-go",
"golang", "hcl"]
foo = []
@ -19,3 +19,14 @@ foo = [1, 2,3, 4]
foo = [
"kenya", "ethiopia",
"columbia"]
foo = [
<<EOS
one
EOS
,
<<EOS
two
EOS
,
]