Improve indentation for multi-line expressions

When someone used a multiline string:

    cmd: "..." +
         "...",

bpfmt used to print this out as:

    cmd: "..." +
    "...",

This change doesn't do the quote alignment like I see in some of our
user-created instances of this, but it does indent a single level:

    cmd: "..." +
        "...",

Test: unit tests
Test: Compared bpfmt results before and after across AOSP
Change-Id: I61bf790be9d08a187857b2725facf71e8b38e372
This commit is contained in:
Dan Willemsen 2018-05-07 16:11:42 -07:00
parent 1e428e0c05
commit d2c8162ca9
2 changed files with 48 additions and 1 deletions

View file

@ -173,17 +173,36 @@ func (p *printer) printMap(m *Map) {
}
func (p *printer) printOperator(operator *Operator) {
p.printOperatorInternal(operator, true)
}
func (p *printer) printOperatorInternal(operator *Operator, allowIndent bool) {
p.printExpression(operator.Args[0])
p.requestSpace()
p.printToken(string(operator.Operator), operator.OperatorPos)
indented := false
if operator.Args[0].End().Line == operator.Args[1].Pos().Line {
p.requestSpace()
} else {
if allowIndent {
indented = true
p.indent(p.curIndent() + 4)
}
p.requestNewline()
}
if op, isOp := operator.Args[1].(*Operator); isOp {
p.printOperatorInternal(op, false)
} else {
p.printExpression(operator.Args[1])
}
if indented {
p.unindent(p.pos)
}
}
func (p *printer) printProperty(property *Property) {
p.printToken(property.Name, property.NamePos)
p.printToken(":", property.ColonPos)

View file

@ -162,6 +162,22 @@ bar {
name: "def",
num: 5,
}
`,
},
{
input: `
foo {
bar: "b" +
"a" +
"z",
}
`,
output: `
foo {
bar: "b" +
"a" +
"z",
}
`,
},
{
@ -190,6 +206,18 @@ foo = 100
bar = foo
baz = foo + bar
baz += foo
`,
},
{
input: `
foo = "bar " +
"" +
"baz"
`,
output: `
foo = "bar " +
"" +
"baz"
`,
},
{