Skip to content

Commit

Permalink
Merge pull request hashicorp#182 from hashicorp/b-comment
Browse files Browse the repository at this point in the history
hcl/printer: format comments across multiple lines in an object
  • Loading branch information
mitchellh committed Jan 25, 2017
2 parents dfbfb1c + db65f66 commit 4f21d59
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 11 deletions.
51 changes: 40 additions & 11 deletions hcl/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions hcl/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down
9 changes: 9 additions & 0 deletions hcl/printer/testdata/comment_object_multi.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "environment" {
default = {}

# default {
# "region" = "us-west-2"
# "sg" = "playground"
# "env" = "prod"
# }
}
9 changes: 9 additions & 0 deletions hcl/printer/testdata/comment_object_multi.input
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
variable "environment" {
default = {}

# default {
# "region" = "us-west-2"
# "sg" = "playground"
# "env" = "prod"
# }
}

0 comments on commit 4f21d59

Please sign in to comment.