summaryrefslogtreecommitdiff
path: root/caddyconfig/caddyfile
diff options
context:
space:
mode:
authorMatthew Holt <mholt@users.noreply.github.com>2020-03-29 13:53:00 -0600
committerMatthew Holt <mholt@users.noreply.github.com>2020-03-29 13:53:00 -0600
commitdeba26d225c5b321a944439eb6b108117ac3d569 (patch)
treed88fd67ec1b32da4a29a699aca612232b92683b4 /caddyconfig/caddyfile
parent178ba024fea4db2b91fd159da629f0a8588f119a (diff)
caddyfile: Minor fixes to the formatter
Diffstat (limited to 'caddyconfig/caddyfile')
-rw-r--r--caddyconfig/caddyfile/formatter.go13
-rw-r--r--caddyconfig/caddyfile/formatter_test.go16
2 files changed, 23 insertions, 6 deletions
diff --git a/caddyconfig/caddyfile/formatter.go b/caddyconfig/caddyfile/formatter.go
index bf70d11..2c97f3b 100644
--- a/caddyconfig/caddyfile/formatter.go
+++ b/caddyconfig/caddyfile/formatter.go
@@ -39,6 +39,7 @@ func Format(input []byte) []byte {
openBrace bool // whether current word/token is or started with open curly brace
openBraceWritten bool // if openBrace, whether that brace was written or not
+ openBraceSpace bool // whether there was a non-newline space before open brace
newLines int // count of newlines consumed
@@ -145,10 +146,11 @@ func Format(input []byte) []byte {
openBrace = false
if beginningOfLine {
indent()
- } else {
+ } else if !openBraceSpace {
write(' ')
}
write('{')
+ openBraceWritten = true
nextLine()
newLines = 0
nesting++
@@ -158,6 +160,10 @@ func Format(input []byte) []byte {
case ch == '{':
openBrace = true
openBraceWritten = false
+ openBraceSpace = spacePrior && !beginningOfLine
+ if openBraceSpace {
+ write(' ')
+ }
continue
case ch == '}' && (spacePrior || !openBrace):
@@ -183,7 +189,7 @@ func Format(input []byte) []byte {
if beginningOfLine {
indent()
}
- if nesting == 0 && last == '}' {
+ if nesting == 0 && last == '}' && beginningOfLine {
nextLine()
nextLine()
}
@@ -193,9 +199,6 @@ func Format(input []byte) []byte {
}
if openBrace && !openBraceWritten {
- if !beginningOfLine {
- write(' ')
- }
write('{')
openBraceWritten = true
}
diff --git a/caddyconfig/caddyfile/formatter_test.go b/caddyconfig/caddyfile/formatter_test.go
index 8f2a012..25bd7fa 100644
--- a/caddyconfig/caddyfile/formatter_test.go
+++ b/caddyconfig/caddyfile/formatter_test.go
@@ -287,10 +287,24 @@ bar "{\"key\":34}"`,
expect: `foo \"literal\"`,
},
{
- description: "simple placeholders",
+ description: "simple placeholders as standalone tokens",
input: `foo {bar}`,
expect: `foo {bar}`,
},
+ {
+ description: "simple placeholders within tokens",
+ input: `foo{bar} foo{bar}baz`,
+ expect: `foo{bar} foo{bar}baz`,
+ },
+ {
+ description: "placeholders and malformed braces",
+ input: `foo{bar} foo{ bar}baz`,
+ expect: `foo{bar} foo {
+ bar
+}
+
+baz`,
+ },
} {
// the formatter should output a trailing newline,
// even if the tests aren't written to expect that