Merge pull request #163 from hashicorp/sethvargo/failing_fmt

Add failing format example
This commit is contained in:
Mitchell Hashimoto 2016-11-01 11:00:25 -07:00 committed by GitHub
commit 6e968a3fcd
6 changed files with 65 additions and 13 deletions

View File

@ -118,17 +118,27 @@ func (p *printer) output(n interface{}) []byte {
defer un(trace(p, "ObjectList"))
var index int
var nextItem token.Pos
for {
// TODO(arslan): refactor below comment printing, we have the same in objectType
for _, c := range p.standaloneComments {
for _, comment := range c.List {
if index != len(t.Items) {
nextItem = t.Items[index].Pos()
} else {
nextItem = token.Pos{Offset: infinity, Line: infinity}
}
// Determine the location of the next actual non-comment
// item. If we're at the end, the next item is at "infinity"
var nextItem token.Pos
if index != len(t.Items) {
nextItem = t.Items[index].Pos()
} else {
nextItem = token.Pos{Offset: infinity, Line: infinity}
}
// 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 {
// Go through all the comments in the group. The group
// should be printed together, not separated by double newlines.
printed := false
for _, comment := range c.List {
// 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 we hit the end add newlines so we can print the comment
// we don't do this if prev is invalid which means the
@ -138,14 +148,21 @@ func (p *printer) output(n interface{}) []byte {
buf.Write([]byte{newline, newline})
}
// Write the actual comment.
buf.WriteString(comment.Text)
buf.WriteByte(newline)
if index != len(t.Items) {
buf.WriteByte(newline)
}
// Set printed to true to note that we printed something
printed = true
}
}
// If we're not at the last item, write a new line so
// that there is a newline separating this comment from
// the next object.
if printed && index != len(t.Items) {
buf.WriteByte(newline)
}
}
if index == len(t.Items) {

View File

@ -34,6 +34,8 @@ var data = []entry{
{"comment.input", "comment.golden"},
{"comment_aligned.input", "comment_aligned.golden"},
{"comment_array.input", "comment_array.golden"},
{"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_standalone.input", "comment_standalone.golden"},
{"empty_block.input", "empty_block.golden"},

View File

@ -0,0 +1,7 @@
# This is a multiline comment
# That has values like this:
#
# ami-abcd1234
#
# Do not delete this comment

View File

@ -0,0 +1,6 @@
# This is a multiline comment
# That has values like this:
#
# ami-abcd1234
#
# Do not delete this comment

View File

@ -0,0 +1,10 @@
# This is a multiline comment
# That has values like this:
#
# ami-abcd1234
#
# Do not delete this comment
resource "aws_instance" "web" {
ami_id = "ami-abcd1234"
}

View File

@ -0,0 +1,10 @@
# This is a multiline comment
# That has values like this:
#
# ami-abcd1234
#
# Do not delete this comment
resource "aws_instance" "web" {
ami_id = "ami-abcd1234"
}