hclwrite: Format shouldn't introduce spaces before index brackets
This is another heuristic because the "[" syntax is also the tuple constructor start marker, but this takes care of the common cases of indexing keywords and bracketed expressions. This fixes #29.
This commit is contained in:
parent
524cf10f48
commit
81d2277300
@ -243,6 +243,9 @@ func spaceAfterToken(subject, before, after *Token) bool {
|
|||||||
// No extra spaces within templates
|
// No extra spaces within templates
|
||||||
return false
|
return false
|
||||||
|
|
||||||
|
case after.Type == hclsyntax.TokenOBrack && (subject.Type == hclsyntax.TokenIdent || subject.Type == hclsyntax.TokenNumberLit || tokenBracketChange(subject) < 0):
|
||||||
|
return false
|
||||||
|
|
||||||
case subject.Type == hclsyntax.TokenMinus:
|
case subject.Type == hclsyntax.TokenMinus:
|
||||||
// Since a minus can either be subtraction or negation, and the latter
|
// Since a minus can either be subtraction or negation, and the latter
|
||||||
// should _not_ have a space after it, we need to use some heuristics
|
// should _not_ have a space after it, we need to use some heuristics
|
||||||
|
@ -27,6 +27,18 @@ func TestFormat(t *testing.T) {
|
|||||||
`a=b.c`,
|
`a=b.c`,
|
||||||
`a = b.c`,
|
`a = b.c`,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
`a=b[c]`,
|
||||||
|
`a = b[c]`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`a=b()[c]`,
|
||||||
|
`a = b()[c]`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`a=["hello"][0]`,
|
||||||
|
`a = ["hello"][0]`,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
`( a+2 )`,
|
`( a+2 )`,
|
||||||
`(a + 2)`,
|
`(a + 2)`,
|
||||||
|
Loading…
Reference in New Issue
Block a user