Browse Source
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>
master
Vaibhav
5 years ago
committed by
GitHub
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with
43 additions and
6 deletions
caddyconfig/caddyfile/formatter.go
caddyconfig/caddyfile/formatter_test.go
@ -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,8 +114,10 @@ func Format(body []byte) []byte {
continue
continue
} else {
} else {
lineBegin = false
lineBegin = false
if block {
if indentation > 0 {
result . WriteRune ( '\t' )
for tabs := indentation ; tabs > 0 ; tabs -- {
result . WriteRune ( '\t' )
}
}
}
}
}
} else {
} else {
@ -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 )
}
}