Merge pull request #183 from hashicorp/b-list-comment
hcl/printer: preserve list comment on first line
This commit is contained in:
commit
72ac8ca2e6
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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"},
|
||||
|
7
hcl/printer/testdata/list_comment.golden
vendored
Normal file
7
hcl/printer/testdata/list_comment.golden
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
foo = [1, # Hello
|
||||
2,
|
||||
]
|
||||
|
||||
foo = [1, # Hello
|
||||
2, # World
|
||||
]
|
6
hcl/printer/testdata/list_comment.input
vendored
Normal file
6
hcl/printer/testdata/list_comment.input
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
foo = [1, # Hello
|
||||
2]
|
||||
|
||||
foo = [1, # Hello
|
||||
2, # World
|
||||
]
|
Loading…
Reference in New Issue
Block a user