hcl/printer: fix some edge cases around ending with a lead comment

This commit is contained in:
Mitchell Hashimoto 2016-10-08 15:06:49 +08:00
parent 26fb5a83d4
commit f3182500c1
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
3 changed files with 22 additions and 2 deletions

View File

@ -448,6 +448,7 @@ func (p *printer) list(l *ast.ListType) []byte {
}
insertSpaceBeforeItem := false
lastHadLeadComment := false
for i, item := range l.List {
if item.Pos().Line != l.Lbrack.Line {
// multiline list, add newline before we add each item
@ -458,6 +459,16 @@ func (p *printer) list(l *ast.ListType) []byte {
leadComment := false
if lit, ok := item.(*ast.LiteralType); ok && lit.LeadComment != nil {
leadComment = true
// If this isn't the first item and the previous element
// didn't have a lead comment, then we need to add an extra
// newline to properly space things out. If it did have a
// lead comment previously then this would be done
// automatically.
if i > 0 && !lastHadLeadComment {
buf.WriteByte(newline)
}
for _, comment := range lit.LeadComment.List {
buf.Write(p.indent([]byte(comment.Text)))
buf.WriteByte(newline)
@ -482,13 +493,16 @@ func (p *printer) list(l *ast.ListType) []byte {
}
}
if i == len(l.List)-1 {
lastItem := i == len(l.List)-1
if lastItem {
buf.WriteByte(newline)
}
if leadComment {
if leadComment && !lastItem {
buf.WriteByte(newline)
}
lastHadLeadComment = leadComment
} else {
if insertSpaceBeforeItem {
buf.WriteByte(blank)

View File

@ -7,4 +7,7 @@ banana = [
"c", # And C
"d",
# And another
"e",
]

View File

@ -7,4 +7,7 @@ banana = [
"c", # And C
"d",
# And another
"e",
]