Skip to content

Commit 15724fa

Browse files
committed
[SPARK-9394][SQL] Handle parentheses in CodeFormatter.
Our CodeFormatter currently does not handle parentheses, and as a result in code dump, we see code formatted this way: ``` foo( a, b, c) ``` With this patch, it is formatted this way: ``` foo( a, b, c) ``` Author: Reynold Xin <[email protected]> Closes apache#7712 from rxin/codeformat-parentheses and squashes the following commits: c2b1c5f [Reynold Xin] Took square bracket out 3cfb174 [Reynold Xin] Code review feedback. 91f5bb1 [Reynold Xin] [SPARK-9394][SQL] Handle parentheses in CodeFormatter.
1 parent fc3bd96 commit 15724fa

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatter.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@
1818
package org.apache.spark.sql.catalyst.expressions.codegen
1919

2020
/**
21-
* An utility class that indents a block of code based on the curly braces.
22-
*
21+
* An utility class that indents a block of code based on the curly braces and parentheses.
2322
* This is used to prettify generated code when in debug mode (or exceptions).
2423
*
2524
* Written by Matei Zaharia.
@@ -35,11 +34,12 @@ private class CodeFormatter {
3534
private var indentString = ""
3635

3736
private def addLine(line: String): Unit = {
38-
val indentChange = line.count(_ == '{') - line.count(_ == '}')
37+
val indentChange =
38+
line.count(c => "({".indexOf(c) >= 0) - line.count(c => ")}".indexOf(c) >= 0)
3939
val newIndentLevel = math.max(0, indentLevel + indentChange)
4040
// Lines starting with '}' should be de-indented even if they contain '{' after;
4141
// in addition, lines ending with ':' are typically labels
42-
val thisLineIndent = if (line.startsWith("}") || line.endsWith(":")) {
42+
val thisLineIndent = if (line.startsWith("}") || line.startsWith(")") || line.endsWith(":")) {
4343
" " * (indentSize * (indentLevel - 1))
4444
} else {
4545
indentString

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatterSuite.scala

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,34 @@ class CodeFormatterSuite extends SparkFunSuite {
7373
|}
7474
""".stripMargin
7575
}
76+
77+
testCase("if else on the same line") {
78+
"""
79+
|class A {
80+
| if (c) {duh;} else {boo;}
81+
|}
82+
""".stripMargin
83+
}{
84+
"""
85+
|class A {
86+
| if (c) {duh;} else {boo;}
87+
|}
88+
""".stripMargin
89+
}
90+
91+
testCase("function calls") {
92+
"""
93+
|foo(
94+
|a,
95+
|b,
96+
|c)
97+
""".stripMargin
98+
}{
99+
"""
100+
|foo(
101+
| a,
102+
| b,
103+
| c)
104+
""".stripMargin
105+
}
76106
}

0 commit comments

Comments
 (0)