Skip to content

Commit

Permalink
hcl/printer: format single line objects to not have newline between
Browse files Browse the repository at this point in the history
Fixes hashicorp#161

See hashicorp#161 and test cases for examples.
  • Loading branch information
mitchellh committed Jan 25, 2017
1 parent 72ac8ca commit e2dbf84
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 2 deletions.
40 changes: 39 additions & 1 deletion hcl/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}
Expand Down Expand Up @@ -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++ {
Expand Down
1 change: 1 addition & 0 deletions hcl/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
}

Expand Down
1 change: 0 additions & 1 deletion hcl/printer/testdata/empty_block.golden
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
variable "foo" {}

variable "foo" {}

variable "foo" {
Expand Down
20 changes: 20 additions & 0 deletions hcl/printer/testdata/object_singleline.golden
Original file line number Diff line number Diff line change
@@ -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 changes: 16 additions & 0 deletions hcl/printer/testdata/object_singleline.input
Original file line number Diff line number Diff line change
@@ -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" {}

0 comments on commit e2dbf84

Please sign in to comment.