You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by rx...@apache.org on 2015/07/28 09:52:30 UTC
spark git commit: [SPARK-9394][SQL] Handle parentheses in
CodeFormatter.
Repository: spark
Updated Branches:
refs/heads/master fc3bd96bc -> 15724fac5
[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 <rx...@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.
Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/15724fac
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/15724fac
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/15724fac
Branch: refs/heads/master
Commit: 15724fac569258d2a149507d8c767d0de0ae8306
Parents: fc3bd96
Author: Reynold Xin <rx...@databricks.com>
Authored: Tue Jul 28 00:52:26 2015 -0700
Committer: Reynold Xin <rx...@databricks.com>
Committed: Tue Jul 28 00:52:26 2015 -0700
----------------------------------------------------------------------
.../expressions/codegen/CodeFormatter.scala | 8 +++---
.../codegen/CodeFormatterSuite.scala | 30 ++++++++++++++++++++
2 files changed, 34 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/spark/blob/15724fac/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatter.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatter.scala b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatter.scala
index 2087cc7..c98182c 100644
--- a/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatter.scala
+++ b/sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatter.scala
@@ -18,8 +18,7 @@
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).
*
* Written by Matei Zaharia.
@@ -35,11 +34,12 @@ private class CodeFormatter {
private var indentString = ""
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)
// Lines starting with '}' should be de-indented even if they contain '{' after;
// 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))
} else {
indentString
http://git-wip-us.apache.org/repos/asf/spark/blob/15724fac/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatterSuite.scala
----------------------------------------------------------------------
diff --git a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatterSuite.scala b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatterSuite.scala
index 478702f..46daa3e 100644
--- a/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatterSuite.scala
+++ b/sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatterSuite.scala
@@ -73,4 +73,34 @@ class CodeFormatterSuite extends SparkFunSuite {
|}
""".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
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org