Fast and extensible multi-platform HTTP/1-2-3 web server with automatic HTTPS
Mirrored from https://github.com/Slazee/caddy.git
Synced Nov 14, 05:15 UTC
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>
Vaibhav committed
Mar 17, 2020 at 21:25 UTC
f192ae5ea5c1e20e0c6aefc0772f0a92ee142f1e
2 files changed
+47
-6
caddyconfig/caddyfile/formatter.go
+13
-5
@@ -43,6 +43,12 @@ func Format(body []byte) []byte {
43
err error
44
)
45
46
+ insertTabs := func(num int) {
47
+ for tabs := num; tabs > 0; tabs-- {
48
+ result.WriteRune('\t')
49
+ }
50
+ }
51
+
52
for {
53
prev = curr
54
curr = next
@@ -102,7 +108,7 @@ func Format(body []byte) []byte {
108
if unicode.IsSpace(next) {
109
indentation++
110
105
- if !unicode.IsSpace(prev) {
111
+ if !unicode.IsSpace(prev) && !lineBegin {
112
result.WriteRune(' ')
113
}
114
} else {
@@ -114,10 +120,12 @@ func Format(body []byte) []byte {
120
continue
121
} else {
122
lineBegin = false
117
- if indentation > 0 {
118
- for tabs := indentation; tabs > 0; tabs-- {
119
- result.WriteRune('\t')
120
- }
123
+ if curr == '{' && unicode.IsSpace(next) {
124
+ // If the block is global, i.e., starts with '{'
125
+ // One less indentation for these blocks.
126
+ insertTabs(indentation - 1)
127
+ } else {
128
+ insertTabs(indentation)
129
}
130
}
131
} else {
caddyconfig/caddyfile/formatter_test.go
+34
-1
@@ -45,6 +45,18 @@ m {
45
n { o
46
}
47
}
48
+
49
+{
50
+ p
51
+}
52
+
53
+ { q
54
+}
55
+
56
+ {
57
+{ r
58
+}
59
+}
60
`)
61
expected := []byte(`
62
a
@@ -75,6 +87,20 @@ m {
87
o
88
}
89
}
90
+
91
+{
92
+ p
93
+}
94
+
95
+{
96
+ q
97
+}
98
+
99
+{
100
+ {
101
+ r
102
+ }
103
+}
104
`)
105
testFormat(t, input, expected)
106
}
@@ -110,6 +136,9 @@ b {
136
137
d { {$E}
138
}
139
+
140
+{ {$F}
141
+}
142
`)
143
expected := []byte(`
144
{$A}
@@ -121,6 +150,10 @@ b {
150
d {
151
{$E}
152
}
153
+
154
+{
155
+ {$F}
156
+}
157
`)
158
testFormat(t, input, expected)
159
}
@@ -190,6 +223,6 @@ g {
223
func testFormat(t *testing.T, input, expected []byte) {
224
output := Format(input)
225
if string(output) != string(expected) {
193
- t.Errorf("Expected:\n%s\ngot:\n%s", string(output), string(expected))
226
+ t.Errorf("Expected:\n%s\ngot:\n%s", string(expected), string(output))
227
}
228
}