mirror of
https://github.com/caddyserver/caddy.git
synced 2025-04-26 14:54:03 +08:00
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>
This commit is contained in:
parent
5fe69ac4ab
commit
71e81d262b
@ -29,12 +29,13 @@ func Format(body []byte) []byte {
|
|||||||
commented,
|
commented,
|
||||||
quoted,
|
quoted,
|
||||||
escaped,
|
escaped,
|
||||||
block,
|
|
||||||
environ,
|
environ,
|
||||||
lineBegin bool
|
lineBegin bool
|
||||||
|
|
||||||
firstIteration = true
|
firstIteration = true
|
||||||
|
|
||||||
|
indentation = 0
|
||||||
|
|
||||||
prev,
|
prev,
|
||||||
curr,
|
curr,
|
||||||
next rune
|
next rune
|
||||||
@ -93,13 +94,13 @@ func Format(body []byte) []byte {
|
|||||||
if curr == '}' {
|
if curr == '}' {
|
||||||
if environ {
|
if environ {
|
||||||
environ = false
|
environ = false
|
||||||
} else if block {
|
} else if indentation > 0 {
|
||||||
block = false
|
indentation--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if curr == '{' {
|
if curr == '{' {
|
||||||
if unicode.IsSpace(next) {
|
if unicode.IsSpace(next) {
|
||||||
block = true
|
indentation++
|
||||||
|
|
||||||
if !unicode.IsSpace(prev) {
|
if !unicode.IsSpace(prev) {
|
||||||
result.WriteRune(' ')
|
result.WriteRune(' ')
|
||||||
@ -113,10 +114,12 @@ func Format(body []byte) []byte {
|
|||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
lineBegin = false
|
lineBegin = false
|
||||||
if block {
|
if indentation > 0 {
|
||||||
|
for tabs := indentation; tabs > 0; tabs-- {
|
||||||
result.WriteRune('\t')
|
result.WriteRune('\t')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
if prev == '{' &&
|
if prev == '{' &&
|
||||||
(curr == ' ' || curr == '\t') &&
|
(curr == ' ' || curr == '\t') &&
|
||||||
|
@ -29,6 +29,22 @@ b
|
|||||||
|
|
||||||
e { f
|
e { f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g {
|
||||||
|
h {
|
||||||
|
i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
j { k {
|
||||||
|
l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m {
|
||||||
|
n { o
|
||||||
|
}
|
||||||
|
}
|
||||||
`)
|
`)
|
||||||
expected := []byte(`
|
expected := []byte(`
|
||||||
a
|
a
|
||||||
@ -41,6 +57,24 @@ c {
|
|||||||
e {
|
e {
|
||||||
f
|
f
|
||||||
}
|
}
|
||||||
|
|
||||||
|
g {
|
||||||
|
h {
|
||||||
|
i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
j {
|
||||||
|
k {
|
||||||
|
l
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
m {
|
||||||
|
n {
|
||||||
|
o
|
||||||
|
}
|
||||||
|
}
|
||||||
`)
|
`)
|
||||||
testFormat(t, input, expected)
|
testFormat(t, input, expected)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user