Merge pull request #244 from octo/multiline-assign-comment
printer: Fix handling of line comments in multi-line statements.
This commit is contained in:
commit
e9ccac6b7f
@ -205,6 +205,12 @@ func (p *Parser) objectItem() (*ast.ObjectItem, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// key=#comment
|
||||
// val
|
||||
if p.lineComment != nil {
|
||||
o.LineComment, p.lineComment = p.lineComment, nil
|
||||
}
|
||||
|
||||
// do a look-ahead for line comment
|
||||
p.scan()
|
||||
if len(keys) > 0 && o.Val.Pos().Line == keys[0].Pos().Line && p.lineComment != nil {
|
||||
|
@ -252,6 +252,14 @@ func (p *printer) objectItem(o *ast.ObjectItem) []byte {
|
||||
}
|
||||
}
|
||||
|
||||
// If key and val are on different lines, treat line comments like lead comments.
|
||||
if o.LineComment != nil && o.Val.Pos().Line != o.Keys[0].Pos().Line {
|
||||
for _, comment := range o.LineComment.List {
|
||||
buf.WriteString(comment.Text)
|
||||
buf.WriteByte(newline)
|
||||
}
|
||||
}
|
||||
|
||||
for i, k := range o.Keys {
|
||||
buf.WriteString(k.Token.Text)
|
||||
buf.WriteByte(blank)
|
||||
@ -265,7 +273,7 @@ func (p *printer) objectItem(o *ast.ObjectItem) []byte {
|
||||
|
||||
buf.Write(p.output(o.Val))
|
||||
|
||||
if o.Val.Pos().Line == o.Keys[0].Pos().Line && o.LineComment != nil {
|
||||
if o.LineComment != nil && o.Val.Pos().Line == o.Keys[0].Pos().Line {
|
||||
buf.WriteByte(blank)
|
||||
for _, comment := range o.LineComment.List {
|
||||
buf.WriteString(comment.Text)
|
||||
|
@ -153,6 +153,7 @@ func TestFormatValidOutput(t *testing.T) {
|
||||
cases := []string{
|
||||
"#\x00",
|
||||
"#\ue123t",
|
||||
"x=//\n0y=<<_\n_\n",
|
||||
"Y=<<4\n4/\n\n\n/4/@=4/\n\n\n/4000000004\r\r\n00004\n",
|
||||
"x=<<_\n_\r\r\n_\n",
|
||||
"X=<<-\n\r\r\n",
|
||||
|
3
hcl/printer/testdata/comment.golden
vendored
3
hcl/printer/testdata/comment.golden
vendored
@ -34,3 +34,6 @@ variable = {
|
||||
foo {
|
||||
bar = "fatih" // line comment 2
|
||||
} // line comment 3
|
||||
|
||||
// comment
|
||||
multiline = "assignment"
|
||||
|
2
hcl/printer/testdata/comment.input
vendored
2
hcl/printer/testdata/comment.input
vendored
@ -35,3 +35,5 @@ foo {
|
||||
bar = "fatih" // line comment 2
|
||||
} // line comment 3
|
||||
|
||||
multiline = // comment
|
||||
"assignment"
|
||||
|
2
hcl/printer/testdata/comment_crlf.input
vendored
2
hcl/printer/testdata/comment_crlf.input
vendored
@ -35,3 +35,5 @@ foo {
|
||||
bar = "fatih" // line comment 2
|
||||
} // line comment 3
|
||||
|
||||
multiline = // comment
|
||||
"assignment"
|
||||
|
Loading…
Reference in New Issue
Block a user