Merge pull request #183 from hashicorp/b-list-comment

hcl/printer: preserve list comment on first line
This commit is contained in:
Mitchell Hashimoto 2017-01-24 16:52:30 -08:00 committed by GitHub
commit 72ac8ca2e6
4 changed files with 30 additions and 1 deletions

View File

@ -579,7 +579,10 @@ func (p *printer) list(l *ast.ListType) []byte {
}
// Output the item itself
buf.Write(p.output(item))
// also indent each line
val := p.output(item)
curLen := len(val)
buf.Write(val)
// If this is a heredoc item we always have to output a newline
// so that it parses properly.
@ -592,6 +595,18 @@ func (p *printer) list(l *ast.ListType) []byte {
buf.WriteString(",")
insertSpaceBeforeItem = true
}
if lit, ok := item.(*ast.LiteralType); ok && lit.LineComment != nil {
// if the next item doesn't have any comments, do not align
buf.WriteByte(blank) // align one space
for i := 0; i < longestLine-curLen; i++ {
buf.WriteByte(blank)
}
for _, comment := range lit.LineComment.List {
buf.WriteString(comment.Text)
}
}
}
}

View File

@ -31,6 +31,7 @@ type entry struct {
var data = []entry{
{"complexhcl.input", "complexhcl.golden"},
{"list.input", "list.golden"},
{"list_comment.input", "list_comment.golden"},
{"comment.input", "comment.golden"},
{"comment_aligned.input", "comment_aligned.golden"},
{"comment_array.input", "comment_array.golden"},

View File

@ -0,0 +1,7 @@
foo = [1, # Hello
2,
]
foo = [1, # Hello
2, # World
]

View File

@ -0,0 +1,6 @@
foo = [1, # Hello
2]
foo = [1, # Hello
2, # World
]