printer: don't write a newline on empty objects
This is to match the same criteria as in gofmt. Fixes #94 Signed-off-by: Miquel Sabaté Solà <msabate@suse.com>
This commit is contained in:
parent
78101eb3cb
commit
0c3742819f
@ -221,12 +221,12 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
|
|||||||
defer un(trace(p, "ObjectType"))
|
defer un(trace(p, "ObjectType"))
|
||||||
var buf bytes.Buffer
|
var buf bytes.Buffer
|
||||||
buf.WriteString("{")
|
buf.WriteString("{")
|
||||||
buf.WriteByte(newline)
|
|
||||||
|
|
||||||
var index int
|
var index int
|
||||||
var nextItem token.Pos
|
var nextItem token.Pos
|
||||||
var commented bool
|
var commented, newlinePrinted bool
|
||||||
for {
|
for {
|
||||||
|
|
||||||
// Print stand alone comments
|
// Print stand alone comments
|
||||||
for _, c := range p.standaloneComments {
|
for _, c := range p.standaloneComments {
|
||||||
for _, comment := range c.List {
|
for _, comment := range c.List {
|
||||||
@ -238,6 +238,13 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
// been printed yet, do it now.
|
||||||
|
if !newlinePrinted {
|
||||||
|
newlinePrinted = true
|
||||||
|
buf.WriteByte(newline)
|
||||||
|
}
|
||||||
|
|
||||||
// add newline if it's between other printed nodes
|
// add newline if it's between other printed nodes
|
||||||
if index > 0 {
|
if index > 0 {
|
||||||
commented = true
|
commented = true
|
||||||
@ -258,6 +265,14 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// At this point we are sure that it's not a totally empty block: print
|
||||||
|
// the initial newline if it hasn't been printed yet by the previous
|
||||||
|
// block about standalone comments.
|
||||||
|
if !newlinePrinted {
|
||||||
|
buf.WriteByte(newline)
|
||||||
|
newlinePrinted = true
|
||||||
|
}
|
||||||
|
|
||||||
// check if we have adjacent one liner items. If yes we'll going to align
|
// check if we have adjacent one liner items. If yes we'll going to align
|
||||||
// the comments.
|
// the comments.
|
||||||
var aligned []*ast.ObjectItem
|
var aligned []*ast.ObjectItem
|
||||||
|
@ -29,6 +29,7 @@ var data = []entry{
|
|||||||
{"comment.input", "comment.golden"},
|
{"comment.input", "comment.golden"},
|
||||||
{"comment_aligned.input", "comment_aligned.golden"},
|
{"comment_aligned.input", "comment_aligned.golden"},
|
||||||
{"comment_standalone.input", "comment_standalone.golden"},
|
{"comment_standalone.input", "comment_standalone.golden"},
|
||||||
|
{"empty_block.input", "empty_block.golden"},
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestFiles(t *testing.T) {
|
func TestFiles(t *testing.T) {
|
||||||
|
13
hcl/printer/testdata/empty_block.golden
vendored
Normal file
13
hcl/printer/testdata/empty_block.golden
vendored
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
variable "foo" {}
|
||||||
|
|
||||||
|
variable "foo" {}
|
||||||
|
|
||||||
|
variable "foo" {
|
||||||
|
# Standalone comment should be still here
|
||||||
|
}
|
||||||
|
|
||||||
|
foo {}
|
||||||
|
|
||||||
|
foo {
|
||||||
|
bar = "mssola"
|
||||||
|
}
|
14
hcl/printer/testdata/empty_block.input
vendored
Normal file
14
hcl/printer/testdata/empty_block.input
vendored
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
variable "foo" {}
|
||||||
|
variable "foo" {
|
||||||
|
}
|
||||||
|
|
||||||
|
variable "foo" {
|
||||||
|
# Standalone comment should be still here
|
||||||
|
}
|
||||||
|
|
||||||
|
foo {
|
||||||
|
}
|
||||||
|
|
||||||
|
foo {
|
||||||
|
bar = "mssola"
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user