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:
Mitchell Hashimoto 2017-01-24 16:44:23 -08:00
parent 8c0b1f045f
commit db65f66984
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
4 changed files with 59 additions and 11 deletions

View File

@ -263,17 +263,24 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
var nextItem token.Pos
var commented, newlinePrinted bool
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 {
printed := false
var lastCommentPos token.Pos
for _, comment := range c.List {
// if we hit the end, last item should be the brace
if index != len(o.List.Items) {
nextItem = o.List.Items[index].Pos()
} else {
nextItem = o.Rbrace
}
// We only care about comments after the previous item
// we've printed so that comments are printed in the
// correct locations (between two objects for example).
// And before the next item.
if comment.Pos().After(p.prev) && comment.Pos().Before(nextItem) {
// If there are standalone comments and the initial newline has not
// been printed yet, do it now.
@ -288,11 +295,33 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
buf.WriteByte(newline)
}
// Store this position
lastCommentPos = comment.Pos()
// output the comment itself
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)
if index != len(o.List.Items) {
buf.WriteByte(newline) // do not print on the end
}
}
}
}

View File

@ -38,6 +38,7 @@ var data = []entry{
{"comment_multiline_no_stanza.input", "comment_multiline_no_stanza.golden"},
{"comment_multiline_stanza.input", "comment_multiline_stanza.golden"},
{"comment_newline.input", "comment_newline.golden"},
{"comment_object_multi.input", "comment_object_multi.golden"},
{"comment_standalone.input", "comment_standalone.golden"},
{"empty_block.input", "empty_block.golden"},
{"list_of_objects.input", "list_of_objects.golden"},

View File

@ -0,0 +1,9 @@
variable "environment" {
default = {}
# default {
# "region" = "us-west-2"
# "sg" = "playground"
# "env" = "prod"
# }
}

View File

@ -0,0 +1,9 @@
variable "environment" {
default = {}
# default {
# "region" = "us-west-2"
# "sg" = "playground"
# "env" = "prod"
# }
}