hcl/printer: fix some edge cases around ending with a lead comment
This commit is contained in:
parent
26fb5a83d4
commit
f3182500c1
@ -448,6 +448,7 @@ func (p *printer) list(l *ast.ListType) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
insertSpaceBeforeItem := false
|
insertSpaceBeforeItem := false
|
||||||
|
lastHadLeadComment := false
|
||||||
for i, item := range l.List {
|
for i, item := range l.List {
|
||||||
if item.Pos().Line != l.Lbrack.Line {
|
if item.Pos().Line != l.Lbrack.Line {
|
||||||
// multiline list, add newline before we add each item
|
// multiline list, add newline before we add each item
|
||||||
@ -458,6 +459,16 @@ func (p *printer) list(l *ast.ListType) []byte {
|
|||||||
leadComment := false
|
leadComment := false
|
||||||
if lit, ok := item.(*ast.LiteralType); ok && lit.LeadComment != nil {
|
if lit, ok := item.(*ast.LiteralType); ok && lit.LeadComment != nil {
|
||||||
leadComment = true
|
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 {
|
for _, comment := range lit.LeadComment.List {
|
||||||
buf.Write(p.indent([]byte(comment.Text)))
|
buf.Write(p.indent([]byte(comment.Text)))
|
||||||
buf.WriteByte(newline)
|
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)
|
buf.WriteByte(newline)
|
||||||
}
|
}
|
||||||
|
|
||||||
if leadComment {
|
if leadComment && !lastItem {
|
||||||
buf.WriteByte(newline)
|
buf.WriteByte(newline)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
lastHadLeadComment = leadComment
|
||||||
} else {
|
} else {
|
||||||
if insertSpaceBeforeItem {
|
if insertSpaceBeforeItem {
|
||||||
buf.WriteByte(blank)
|
buf.WriteByte(blank)
|
||||||
|
3
hcl/printer/testdata/comment_array.golden
vendored
3
hcl/printer/testdata/comment_array.golden
vendored
@ -7,4 +7,7 @@ banana = [
|
|||||||
|
|
||||||
"c", # And C
|
"c", # And C
|
||||||
"d",
|
"d",
|
||||||
|
|
||||||
|
# And another
|
||||||
|
"e",
|
||||||
]
|
]
|
||||||
|
3
hcl/printer/testdata/comment_array.input
vendored
3
hcl/printer/testdata/comment_array.input
vendored
@ -7,4 +7,7 @@ banana = [
|
|||||||
|
|
||||||
"c", # And C
|
"c", # And C
|
||||||
"d",
|
"d",
|
||||||
|
|
||||||
|
# And another
|
||||||
|
"e",
|
||||||
]
|
]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user