hcl/printer: format single line objects to not have newline between

Fixes #161

See #161 and test cases for examples.
This commit is contained in:
Mitchell Hashimoto 2017-01-24 17:01:59 -08:00
parent 72ac8ca2e6
commit e2dbf84eb0
No known key found for this signature in database
GPG Key ID: 744E147AA52F5B0A
5 changed files with 76 additions and 2 deletions

View File

@ -171,7 +171,15 @@ func (p *printer) output(n interface{}) []byte {
buf.Write(p.output(t.Items[index]))
if index != len(t.Items)-1 {
buf.Write([]byte{newline, newline})
// Always write a newline to separate us from the next item
buf.WriteByte(newline)
// If the next item is an object that is exactly one line,
// then we don't output another newline.
next := t.Items[index+1]
if next.Pos().Line != t.Items[index].Pos().Line+1 || !p.isSingleLineObject(next) {
buf.WriteByte(newline)
}
}
index++
}
@ -683,6 +691,36 @@ func (p *printer) heredocIndent(buf []byte) []byte {
return res
}
// isSingleLineObject tells whether the given object item is a single
// line object such as "obj {}".
//
// A single line object:
//
// * has no lead comments (hence multi-line)
// * has no assignment
// * has no values in the stanza (within {})
//
func (p *printer) isSingleLineObject(val *ast.ObjectItem) bool {
// If there is a lead comment, can't be one line
if val.LeadComment != nil {
return false
}
// If there is assignment, we always break by line
if val.Assign.IsValid() {
return false
}
// If it isn't an object type, then its not a single line object
ot, ok := val.Val.(*ast.ObjectType)
if !ok {
return false
}
// If the object has no items, it is single line!
return len(ot.List.Items) == 0
}
func lines(txt string) int {
endline := 1
for i := 0; i < len(txt); i++ {

View File

@ -44,6 +44,7 @@ var data = []entry{
{"empty_block.input", "empty_block.golden"},
{"list_of_objects.input", "list_of_objects.golden"},
{"multiline_string.input", "multiline_string.golden"},
{"object_singleline.input", "object_singleline.golden"},
{"object_with_heredoc.input", "object_with_heredoc.golden"},
}

View File

@ -1,5 +1,4 @@
variable "foo" {}
variable "foo" {}
variable "foo" {

View File

@ -0,0 +1,20 @@
variable "foo" {}
variable "bar" {}
variable "baz" {}
variable "qux" {}
variable "foo" {
foo = "bar"
}
variable "foo" {}
# lead comment
variable "bar" {}
# Purposeful newline check below:
variable "foo" {}
variable "purposeful-newline" {}

View File

@ -0,0 +1,16 @@
variable "foo" {}
variable "bar" {}
variable "baz" {}
variable "qux" {}
variable "foo" { foo = "bar" }
variable "foo" {}
# lead comment
variable "bar" {}
# Purposeful newline check below:
variable "foo" {}
variable "purposeful-newline" {}