diff options
author | Vaibhav <vrongmeal@gmail.com> | 2020-03-17 21:25:36 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-17 09:55:36 -0600 |
commit | f192ae5ea5c1e20e0c6aefc0772f0a92ee142f1e (patch) | |
tree | e10b529187e5135cef34c1ccb06fa18cc5ba9de5 /caddyconfig/caddyfile/formatter.go | |
parent | b62f8e058270aff37621b69a9768235a01e8bd34 (diff) |
cmd: fmt: Fix brace opening block indentation (#3153)
This fixes indentation for blocks starting with
a brace as:
```Caddyfile
{
...
}
```
Fixes #3144
Signed-off-by: Vaibhav <vrongmeal@gmail.com>
Diffstat (limited to 'caddyconfig/caddyfile/formatter.go')
-rw-r--r-- | caddyconfig/caddyfile/formatter.go | 18 |
1 files changed, 13 insertions, 5 deletions
diff --git a/caddyconfig/caddyfile/formatter.go b/caddyconfig/caddyfile/formatter.go index e937208..82b8b3c 100644 --- a/caddyconfig/caddyfile/formatter.go +++ b/caddyconfig/caddyfile/formatter.go @@ -43,6 +43,12 @@ func Format(body []byte) []byte { err error ) + insertTabs := func(num int) { + for tabs := num; tabs > 0; tabs-- { + result.WriteRune('\t') + } + } + for { prev = curr curr = next @@ -102,7 +108,7 @@ func Format(body []byte) []byte { if unicode.IsSpace(next) { indentation++ - if !unicode.IsSpace(prev) { + if !unicode.IsSpace(prev) && !lineBegin { result.WriteRune(' ') } } else { @@ -114,10 +120,12 @@ func Format(body []byte) []byte { continue } else { lineBegin = false - if indentation > 0 { - for tabs := indentation; tabs > 0; tabs-- { - result.WriteRune('\t') - } + if curr == '{' && unicode.IsSpace(next) { + // If the block is global, i.e., starts with '{' + // One less indentation for these blocks. + insertTabs(indentation - 1) + } else { + insertTabs(indentation) } } } else { |