Skip to content

Commit

Permalink
Merge pull request hashicorp#177 from hashicorp/b-fmt-heredoc
Browse files Browse the repository at this point in the history
hcl/printer: newline after heredoc items in lists
  • Loading branch information
mitchellh committed Jan 9, 2017
2 parents e930f8e + 4f76b0e commit eb6f65b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 4 deletions.
11 changes: 10 additions & 1 deletion hcl/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hcl/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions hcl/printer/testdata/list.golden
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,14 @@ foo = [
"ethiopia",
"columbia",
]

foo = [
<<EOS
one
EOS
,
<<EOS
two
EOS
,
]
15 changes: 13 additions & 2 deletions hcl/printer/testdata/list.input
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -19,3 +19,14 @@ foo = [1, 2,3, 4]
foo = [
"kenya", "ethiopia",
"columbia"]

foo = [
<<EOS
one
EOS
,
<<EOS
two
EOS
,
]

0 comments on commit eb6f65b

Please sign in to comment.