Merge pull request #163 from hashicorp/sethvargo/failing_fmt
Add failing format example
This commit is contained in:
commit
6e968a3fcd
@ -118,17 +118,27 @@ func (p *printer) output(n interface{}) []byte {
|
|||||||
defer un(trace(p, "ObjectList"))
|
defer un(trace(p, "ObjectList"))
|
||||||
|
|
||||||
var index int
|
var index int
|
||||||
var nextItem token.Pos
|
|
||||||
for {
|
for {
|
||||||
// TODO(arslan): refactor below comment printing, we have the same in objectType
|
// Determine the location of the next actual non-comment
|
||||||
for _, c := range p.standaloneComments {
|
// item. If we're at the end, the next item is at "infinity"
|
||||||
for _, comment := range c.List {
|
var nextItem token.Pos
|
||||||
if index != len(t.Items) {
|
if index != len(t.Items) {
|
||||||
nextItem = t.Items[index].Pos()
|
nextItem = t.Items[index].Pos()
|
||||||
} else {
|
} else {
|
||||||
nextItem = token.Pos{Offset: infinity, Line: infinity}
|
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 comment.Pos().After(p.prev) && comment.Pos().Before(nextItem) {
|
||||||
// if we hit the end add newlines so we can print the comment
|
// 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
|
// we don't do this if prev is invalid which means the
|
||||||
@ -138,13 +148,20 @@ func (p *printer) output(n interface{}) []byte {
|
|||||||
buf.Write([]byte{newline, newline})
|
buf.Write([]byte{newline, newline})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Write the actual comment.
|
||||||
buf.WriteString(comment.Text)
|
buf.WriteString(comment.Text)
|
||||||
|
buf.WriteByte(newline)
|
||||||
|
|
||||||
buf.WriteByte(newline)
|
// Set printed to true to note that we printed something
|
||||||
if index != len(t.Items) {
|
printed = true
|
||||||
buf.WriteByte(newline)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,6 +34,8 @@ var data = []entry{
|
|||||||
{"comment.input", "comment.golden"},
|
{"comment.input", "comment.golden"},
|
||||||
{"comment_aligned.input", "comment_aligned.golden"},
|
{"comment_aligned.input", "comment_aligned.golden"},
|
||||||
{"comment_array.input", "comment_array.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_newline.input", "comment_newline.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"},
|
||||||
|
7
hcl/printer/testdata/comment_multiline_no_stanza.golden
vendored
Normal file
7
hcl/printer/testdata/comment_multiline_no_stanza.golden
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
# This is a multiline comment
|
||||||
|
# That has values like this:
|
||||||
|
#
|
||||||
|
# ami-abcd1234
|
||||||
|
#
|
||||||
|
# Do not delete this comment
|
||||||
|
|
6
hcl/printer/testdata/comment_multiline_no_stanza.input
vendored
Normal file
6
hcl/printer/testdata/comment_multiline_no_stanza.input
vendored
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# This is a multiline comment
|
||||||
|
# That has values like this:
|
||||||
|
#
|
||||||
|
# ami-abcd1234
|
||||||
|
#
|
||||||
|
# Do not delete this comment
|
10
hcl/printer/testdata/comment_multiline_stanza.golden
vendored
Normal file
10
hcl/printer/testdata/comment_multiline_stanza.golden
vendored
Normal 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"
|
||||||
|
}
|
10
hcl/printer/testdata/comment_multiline_stanza.input
vendored
Normal file
10
hcl/printer/testdata/comment_multiline_stanza.input
vendored
Normal 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"
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user