diff --git a/hclwrite/format.go b/hclwrite/format.go index 64f161d..178cb2a 100644 --- a/hclwrite/format.go +++ b/hclwrite/format.go @@ -243,6 +243,9 @@ func spaceAfterToken(subject, before, after *Token) bool { // No extra spaces within templates 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: // 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 diff --git a/hclwrite/format_test.go b/hclwrite/format_test.go index e89f451..38b1089 100644 --- a/hclwrite/format_test.go +++ b/hclwrite/format_test.go @@ -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=["hello"][0]`, + `a = ["hello"][0]`, + }, { `( a+2 )`, `(a + 2)`,