Merge pull request #11396 from hashicorp/b-update-hcl

vendor update HCL
This commit is contained in:
Mitchell Hashimoto 2017-01-24 17:18:29 -08:00 committed by GitHub
commit a74dab836c
3 changed files with 138 additions and 41 deletions

View File

@ -346,7 +346,7 @@ func (p *Parser) listType() (*ast.ListType, error) {
} }
} }
switch tok.Type { switch tok.Type {
case token.NUMBER, token.FLOAT, token.STRING, token.HEREDOC: case token.BOOL, token.NUMBER, token.FLOAT, token.STRING, token.HEREDOC:
node, err := p.literalType() node, err := p.literalType()
if err != nil { if err != nil {
return nil, err return nil, err
@ -388,8 +388,6 @@ func (p *Parser) listType() (*ast.ListType, error) {
} }
l.Add(node) l.Add(node)
needComma = true needComma = true
case token.BOOL:
// TODO(arslan) should we support? not supported by HCL yet
case token.LBRACK: case token.LBRACK:
// TODO(arslan) should we support nested lists? Even though it's // TODO(arslan) should we support nested lists? Even though it's
// written in README of HCL, it's not a part of the grammar // written in README of HCL, it's not a part of the grammar

View File

@ -171,7 +171,15 @@ func (p *printer) output(n interface{}) []byte {
buf.Write(p.output(t.Items[index])) buf.Write(p.output(t.Items[index]))
if index != len(t.Items)-1 { 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++ index++
} }
@ -263,17 +271,24 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
var nextItem token.Pos var nextItem token.Pos
var commented, newlinePrinted bool var commented, newlinePrinted bool
for { for {
// Determine the location of the next actual non-comment
// item. If we're at the end, the next item is the closing brace
if index != len(o.List.Items) {
nextItem = o.List.Items[index].Pos()
} else {
nextItem = o.Rbrace
}
// Print stand alone comments // Go through the standalone comments in the file and print out
// the comments that we should be for this object item.
for _, c := range p.standaloneComments { for _, c := range p.standaloneComments {
printed := false
var lastCommentPos token.Pos
for _, comment := range c.List { for _, comment := range c.List {
// if we hit the end, last item should be the brace // We only care about comments after the previous item
if index != len(o.List.Items) { // we've printed so that comments are printed in the
nextItem = o.List.Items[index].Pos() // correct locations (between two objects for example).
} else { // And before the next item.
nextItem = o.Rbrace
}
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 // If there are standalone comments and the initial newline has not
// been printed yet, do it now. // been printed yet, do it now.
@ -288,11 +303,33 @@ func (p *printer) objectType(o *ast.ObjectType) []byte {
buf.WriteByte(newline) buf.WriteByte(newline)
} }
// Store this position
lastCommentPos = comment.Pos()
// output the comment itself
buf.Write(p.indent(p.heredocIndent([]byte(comment.Text)))) buf.Write(p.indent(p.heredocIndent([]byte(comment.Text))))
// Set printed to true to note that we printed something
printed = true
/*
if index != len(o.List.Items) {
buf.WriteByte(newline) // do not print on the end
}
*/
}
}
// Stuff to do if we had comments
if printed {
// Always write a newline
buf.WriteByte(newline)
// If there is another item in the object and our comment
// didn't hug it directly, then make sure there is a blank
// line separating them.
if nextItem != o.Rbrace && nextItem.Line != lastCommentPos.Line+1 {
buf.WriteByte(newline) buf.WriteByte(newline)
if index != len(o.List.Items) {
buf.WriteByte(newline) // do not print on the end
}
} }
} }
} }
@ -474,6 +511,13 @@ func (p *printer) list(l *ast.ListType) []byte {
insertSpaceBeforeItem := false insertSpaceBeforeItem := false
lastHadLeadComment := false lastHadLeadComment := false
for i, item := range l.List { for i, item := range l.List {
// Keep track of whether this item is a heredoc since that has
// unique behavior.
heredoc := false
if lit, ok := item.(*ast.LiteralType); ok && lit.Token.Type == token.HEREDOC {
heredoc = true
}
if item.Pos().Line != l.Lbrack.Line { if item.Pos().Line != l.Lbrack.Line {
// multiline list, add newline before we add each item // multiline list, add newline before we add each item
buf.WriteByte(newline) buf.WriteByte(newline)
@ -507,7 +551,7 @@ func (p *printer) list(l *ast.ListType) []byte {
// if this item is a heredoc, then we output the comma on // if this item is a heredoc, then we output the comma on
// the next line. This is the only case this happens. // the next line. This is the only case this happens.
comma := []byte{','} comma := []byte{','}
if lit, ok := item.(*ast.LiteralType); ok && lit.Token.Type == token.HEREDOC { if heredoc {
buf.WriteByte(newline) buf.WriteByte(newline)
comma = p.indent(comma) comma = p.indent(comma)
} }
@ -541,11 +585,36 @@ func (p *printer) list(l *ast.ListType) []byte {
buf.WriteByte(blank) buf.WriteByte(blank)
insertSpaceBeforeItem = false insertSpaceBeforeItem = false
} }
buf.Write(p.output(item))
// Output the item itself
// also indent each line
val := p.output(item)
curLen := len(val)
buf.Write(val)
// If this is a heredoc item we always have to output a newline
// so that it parses properly.
if heredoc {
buf.WriteByte(newline)
}
// If this isn't the last element, write a comma.
if i != len(l.List)-1 { if i != len(l.List)-1 {
buf.WriteString(",") buf.WriteString(",")
insertSpaceBeforeItem = true insertSpaceBeforeItem = true
} }
if lit, ok := item.(*ast.LiteralType); ok && lit.LineComment != nil {
// if the next item doesn't have any comments, do not align
buf.WriteByte(blank) // align one space
for i := 0; i < longestLine-curLen; i++ {
buf.WriteByte(blank)
}
for _, comment := range lit.LineComment.List {
buf.WriteString(comment.Text)
}
}
} }
} }
@ -622,6 +691,36 @@ func (p *printer) heredocIndent(buf []byte) []byte {
return res 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 { func lines(txt string) int {
endline := 1 endline := 1
for i := 0; i < len(txt); i++ { for i := 0; i < len(txt); i++ {

48
vendor/vendor.json vendored
View File

@ -1711,68 +1711,68 @@
{ {
"checksumSHA1": "Ok3Csn6Voou7pQT6Dv2mkwpqFtw=", "checksumSHA1": "Ok3Csn6Voou7pQT6Dv2mkwpqFtw=",
"path": "github.com/hashicorp/hcl", "path": "github.com/hashicorp/hcl",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "XQmjDva9JCGGkIecOgwtBEMCJhU=", "checksumSHA1": "XQmjDva9JCGGkIecOgwtBEMCJhU=",
"path": "github.com/hashicorp/hcl/hcl/ast", "path": "github.com/hashicorp/hcl/hcl/ast",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "DaQmLi48oUAwctWcX6A6DNN61UY=", "checksumSHA1": "DaQmLi48oUAwctWcX6A6DNN61UY=",
"path": "github.com/hashicorp/hcl/hcl/fmtcmd", "path": "github.com/hashicorp/hcl/hcl/fmtcmd",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "vF6LLywGDoAaccTcAGrcY7mYvZc=", "checksumSHA1": "MPz4qnNmoYHHUXDhHj0TpJk4LHk=",
"path": "github.com/hashicorp/hcl/hcl/parser", "path": "github.com/hashicorp/hcl/hcl/parser",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "K1gXmpTINkcQEh4tBjzq9UeKy/U=", "checksumSHA1": "J5zAZ+tMBDPRJ8A07742Pl/c++w=",
"path": "github.com/hashicorp/hcl/hcl/printer", "path": "github.com/hashicorp/hcl/hcl/printer",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "z6wdP4mRw4GVjShkNHDaOWkbxS0=", "checksumSHA1": "z6wdP4mRw4GVjShkNHDaOWkbxS0=",
"path": "github.com/hashicorp/hcl/hcl/scanner", "path": "github.com/hashicorp/hcl/hcl/scanner",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "oS3SCN9Wd6D8/LG0Yx1fu84a7gI=", "checksumSHA1": "oS3SCN9Wd6D8/LG0Yx1fu84a7gI=",
"path": "github.com/hashicorp/hcl/hcl/strconv", "path": "github.com/hashicorp/hcl/hcl/strconv",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "c6yprzj06ASwCo18TtbbNNBHljA=", "checksumSHA1": "c6yprzj06ASwCo18TtbbNNBHljA=",
"path": "github.com/hashicorp/hcl/hcl/token", "path": "github.com/hashicorp/hcl/hcl/token",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "138aCV5n8n7tkGYMsMVQQnnLq+0=", "checksumSHA1": "138aCV5n8n7tkGYMsMVQQnnLq+0=",
"path": "github.com/hashicorp/hcl/json/parser", "path": "github.com/hashicorp/hcl/json/parser",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "YdvFsNOMSWMLnY6fcliWQa0O5Fw=", "checksumSHA1": "YdvFsNOMSWMLnY6fcliWQa0O5Fw=",
"path": "github.com/hashicorp/hcl/json/scanner", "path": "github.com/hashicorp/hcl/json/scanner",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "fNlXQCQEnb+B3k5UDL/r15xtSJY=", "checksumSHA1": "fNlXQCQEnb+B3k5UDL/r15xtSJY=",
"path": "github.com/hashicorp/hcl/json/token", "path": "github.com/hashicorp/hcl/json/token",
"revision": "eb6f65b2d77ed5078887f960ff570fbddbbeb49d", "revision": "db4f0768927a665a06c32855186e1bc762bcc8f5",
"revisionTime": "2017-01-09T00:25:15Z" "revisionTime": "2017-01-25T01:06:35Z"
}, },
{ {
"checksumSHA1": "2Nrl/YKrmowkRgCDLhA6UTFgYEY=", "checksumSHA1": "2Nrl/YKrmowkRgCDLhA6UTFgYEY=",