Skip to content
Snippets Groups Projects
Commit 15724fac authored by Reynold Xin's avatar Reynold Xin
Browse files

[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 <rxin@databricks.com>

Closes #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.
parent fc3bd96b
No related branches found
No related tags found
No related merge requests found
...@@ -18,8 +18,7 @@ ...@@ -18,8 +18,7 @@
package org.apache.spark.sql.catalyst.expressions.codegen package org.apache.spark.sql.catalyst.expressions.codegen
/** /**
* An utility class that indents a block of code based on the curly braces. * An utility class that indents a block of code based on the curly braces and parentheses.
*
* This is used to prettify generated code when in debug mode (or exceptions). * This is used to prettify generated code when in debug mode (or exceptions).
* *
* Written by Matei Zaharia. * Written by Matei Zaharia.
...@@ -35,11 +34,12 @@ private class CodeFormatter { ...@@ -35,11 +34,12 @@ private class CodeFormatter {
private var indentString = "" private var indentString = ""
private def addLine(line: String): Unit = { private def addLine(line: String): Unit = {
val indentChange = line.count(_ == '{') - line.count(_ == '}') val indentChange =
line.count(c => "({".indexOf(c) >= 0) - line.count(c => ")}".indexOf(c) >= 0)
val newIndentLevel = math.max(0, indentLevel + indentChange) val newIndentLevel = math.max(0, indentLevel + indentChange)
// Lines starting with '}' should be de-indented even if they contain '{' after; // Lines starting with '}' should be de-indented even if they contain '{' after;
// in addition, lines ending with ':' are typically labels // in addition, lines ending with ':' are typically labels
val thisLineIndent = if (line.startsWith("}") || line.endsWith(":")) { val thisLineIndent = if (line.startsWith("}") || line.startsWith(")") || line.endsWith(":")) {
" " * (indentSize * (indentLevel - 1)) " " * (indentSize * (indentLevel - 1))
} else { } else {
indentString indentString
......
...@@ -73,4 +73,34 @@ class CodeFormatterSuite extends SparkFunSuite { ...@@ -73,4 +73,34 @@ class CodeFormatterSuite extends SparkFunSuite {
|} |}
""".stripMargin """.stripMargin
} }
testCase("if else on the same line") {
"""
|class A {
| if (c) {duh;} else {boo;}
|}
""".stripMargin
}{
"""
|class A {
| if (c) {duh;} else {boo;}
|}
""".stripMargin
}
testCase("function calls") {
"""
|foo(
|a,
|b,
|c)
""".stripMargin
}{
"""
|foo(
| a,
| b,
| c)
""".stripMargin
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment