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:
parent
72ac8ca2e6
commit
e2dbf84eb0
@ -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++ {
|
||||
|
@ -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"},
|
||||
}
|
||||
|
||||
|
1
hcl/printer/testdata/empty_block.golden
vendored
1
hcl/printer/testdata/empty_block.golden
vendored
@ -1,5 +1,4 @@
|
||||
variable "foo" {}
|
||||
|
||||
variable "foo" {}
|
||||
|
||||
variable "foo" {
|
||||
|
20
hcl/printer/testdata/object_singleline.golden
vendored
Normal file
20
hcl/printer/testdata/object_singleline.golden
vendored
Normal 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" {}
|
16
hcl/printer/testdata/object_singleline.input
vendored
Normal file
16
hcl/printer/testdata/object_singleline.input
vendored
Normal 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" {}
|
Loading…
Reference in New Issue
Block a user