hcl/printer: format comments across multiple lines in an object
https://github.com/hashicorp/terraform/issues/11209 See the TF issue above for an example, and test cases for a fix. This didn't affect any other formatting tests.
This commit is contained in:
parent
8c0b1f045f
commit
db65f66984
@ -263,17 +263,24 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
|
|||||||
var nextItem token.Pos
|
var nextItem token.Pos
|
||||||
var commented, newlinePrinted bool
|
var commented, newlinePrinted bool
|
||||||
for {
|
for {
|
||||||
|
// Determine the location of the next actual non-comment
|
||||||
|
// item. If we're at the end, the next item is the closing brace
|
||||||
|
if index != len(o.List.Items) {
|
||||||
|
nextItem = o.List.Items[index].Pos()
|
||||||
|
} else {
|
||||||
|
nextItem = o.Rbrace
|
||||||
|
}
|
||||||
|
|
||||||
// Print stand alone comments
|
// Go through the standalone comments in the file and print out
|
||||||
|
// the comments that we should be for this object item.
|
||||||
for _, c := range p.standaloneComments {
|
for _, c := range p.standaloneComments {
|
||||||
|
printed := false
|
||||||
|
var lastCommentPos token.Pos
|
||||||
for _, comment := range c.List {
|
for _, comment := range c.List {
|
||||||
// if we hit the end, last item should be the brace
|
// We only care about comments after the previous item
|
||||||
if index != len(o.List.Items) {
|
// we've printed so that comments are printed in the
|
||||||
nextItem = o.List.Items[index].Pos()
|
// correct locations (between two objects for example).
|
||||||
} else {
|
// And before the next item.
|
||||||
nextItem = o.Rbrace
|
|
||||||
}
|
|
||||||
|
|
||||||
if comment.Pos().After(p.prev) && comment.Pos().Before(nextItem) {
|
if comment.Pos().After(p.prev) && comment.Pos().Before(nextItem) {
|
||||||
// If there are standalone comments and the initial newline has not
|
// If there are standalone comments and the initial newline has not
|
||||||
// been printed yet, do it now.
|
// been printed yet, do it now.
|
||||||
@ -288,11 +295,33 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
|
|||||||
buf.WriteByte(newline)
|
buf.WriteByte(newline)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Store this position
|
||||||
|
lastCommentPos = comment.Pos()
|
||||||
|
|
||||||
|
// output the comment itself
|
||||||
buf.Write(p.indent(p.heredocIndent([]byte(comment.Text))))
|
buf.Write(p.indent(p.heredocIndent([]byte(comment.Text))))
|
||||||
|
|
||||||
|
// Set printed to true to note that we printed something
|
||||||
|
printed = true
|
||||||
|
|
||||||
|
/*
|
||||||
|
if index != len(o.List.Items) {
|
||||||
|
buf.WriteByte(newline) // do not print on the end
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Stuff to do if we had comments
|
||||||
|
if printed {
|
||||||
|
// Always write a newline
|
||||||
|
buf.WriteByte(newline)
|
||||||
|
|
||||||
|
// If there is another item in the object and our comment
|
||||||
|
// didn't hug it directly, then make sure there is a blank
|
||||||
|
// line separating them.
|
||||||
|
if nextItem != o.Rbrace && nextItem.Line != lastCommentPos.Line+1 {
|
||||||
buf.WriteByte(newline)
|
buf.WriteByte(newline)
|
||||||
if index != len(o.List.Items) {
|
|
||||||
buf.WriteByte(newline) // do not print on the end
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,6 +38,7 @@ var data = []entry{
|
|||||||
{"comment_multiline_no_stanza.input", "comment_multiline_no_stanza.golden"},
|
{"comment_multiline_no_stanza.input", "comment_multiline_no_stanza.golden"},
|
||||||
{"comment_multiline_stanza.input", "comment_multiline_stanza.golden"},
|
{"comment_multiline_stanza.input", "comment_multiline_stanza.golden"},
|
||||||
{"comment_newline.input", "comment_newline.golden"},
|
{"comment_newline.input", "comment_newline.golden"},
|
||||||
|
{"comment_object_multi.input", "comment_object_multi.golden"},
|
||||||
{"comment_standalone.input", "comment_standalone.golden"},
|
{"comment_standalone.input", "comment_standalone.golden"},
|
||||||
{"empty_block.input", "empty_block.golden"},
|
{"empty_block.input", "empty_block.golden"},
|
||||||
{"list_of_objects.input", "list_of_objects.golden"},
|
{"list_of_objects.input", "list_of_objects.golden"},
|
||||||
|
9
hcl/printer/testdata/comment_object_multi.golden
vendored
Normal file
9
hcl/printer/testdata/comment_object_multi.golden
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
variable "environment" {
|
||||||
|
default = {}
|
||||||
|
|
||||||
|
# default {
|
||||||
|
# "region" = "us-west-2"
|
||||||
|
# "sg" = "playground"
|
||||||
|
# "env" = "prod"
|
||||||
|
# }
|
||||||
|
}
|
9
hcl/printer/testdata/comment_object_multi.input
vendored
Normal file
9
hcl/printer/testdata/comment_object_multi.input
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
variable "environment" {
|
||||||
|
default = {}
|
||||||
|
|
||||||
|
# default {
|
||||||
|
# "region" = "us-west-2"
|
||||||
|
# "sg" = "playground"
|
||||||
|
# "env" = "prod"
|
||||||
|
# }
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user