Skip to content

Commit

Permalink
Merge pull request hashicorp#190 from hashicorp/b-singleline
Browse files Browse the repository at this point in the history
hcl/printer: singleline objects followed by non-singleline need blank
  • Loading branch information
mitchellh committed Feb 17, 2017
2 parents b943d49 + b979c7a commit 630949a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
19 changes: 16 additions & 3 deletions hcl/printer/nodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,23 @@ func (p *printer) output(n interface{}) []byte {
// 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.
// Need to determine if we're going to separate the next item
// with a blank line. The logic here is simple, though there
// are a few conditions:
//
// 1. The next object is more than one line away anyways,
// so we need an empty line.
//
// 2. The next object is not a "single line" object, so
// we need an empty line.
//
// 3. This current object is not a single line object,
// so we need an empty line.
current := t.Items[index]
next := t.Items[index+1]
if next.Pos().Line != t.Items[index].Pos().Line+1 || !p.isSingleLineObject(next) {
if next.Pos().Line != t.Items[index].Pos().Line+1 ||
!p.isSingleLineObject(next) ||
!p.isSingleLineObject(current) {
buf.WriteByte(newline)
}
}
Expand Down
6 changes: 6 additions & 0 deletions hcl/printer/testdata/object_singleline.golden
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ variable "foo" {}
# lead comment
variable "bar" {}

variable "foo" {
default = "bar"
}

variable "bar" {}

# Purposeful newline check below:

variable "foo" {}
Expand Down
3 changes: 3 additions & 0 deletions hcl/printer/testdata/object_singleline.input
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ variable "foo" {}
# lead comment
variable "bar" {}

variable "foo" { default = "bar" }
variable "bar" {}

# Purposeful newline check below:

variable "foo" {}
Expand Down

0 comments on commit 630949a

Please sign in to comment.