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
fmt: Add support for block nesting. (#3105)
Previously the formatter did not include support for blocks inside other blocks. Hence the formatter could not indent some files properly. This fixes it. Fixes #3104 Signed-off-by: Vaibhav <vrongmeal@gmail.com>
Vaibhav committed
Mar 1, 2020 at 01:53 UTC
71e81d262bc34545f73f1380bc5d078d83d1570f
2 files changed
+43
-6
caddyconfig/caddyfile/formatter.go
+9
-6
@@ -29,12 +29,13 @@ func Format(body []byte) []byte {
29
commented,
30
quoted,
31
escaped,
32
- block,
32
environ,
33
lineBegin bool
34
35
firstIteration = true
36
37
+ indentation = 0
38
+
39
prev,
40
curr,
41
next rune
@@ -93,13 +94,13 @@ func Format(body []byte) []byte {
94
if curr == '}' {
95
if environ {
96
environ = false
96
- } else if block {
97
- block = false
97
+ } else if indentation > 0 {
98
+ indentation--
99
}
100
}
101
if curr == '{' {
102
if unicode.IsSpace(next) {
102
- block = true
103
+ indentation++
104
105
if !unicode.IsSpace(prev) {
106
result.WriteRune(' ')
@@ -113,8 +114,10 @@ func Format(body []byte) []byte {
114
continue
115
} else {
116
lineBegin = false
116
- if block {
117
- result.WriteRune('\t')
117
+ if indentation > 0 {
118
+ for tabs := indentation; tabs > 0; tabs-- {
119
+ result.WriteRune('\t')
120
+ }
121
}
122
}
123
} else {
caddyconfig/caddyfile/formatter_test.go
+34
@@ -29,6 +29,22 @@ b
29
30
e { f
31
}
32
+
33
+g {
34
+h {
35
+i
36
+}
37
+}
38
+
39
+j { k {
40
+l
41
+}
42
+}
43
+
44
+m {
45
+ n { o
46
+ }
47
+}
48
`)
49
expected := []byte(`
50
a
@@ -41,6 +57,24 @@ c {
57
e {
58
f
59
}
60
+
61
+g {
62
+ h {
63
+ i
64
+ }
65
+}
66
+
67
+j {
68
+ k {
69
+ l
70
+ }
71
+}
72
+
73
+m {
74
+ n {
75
+ o
76
+ }
77
+}
78
`)
79
testFormat(t, input, expected)
80
}