spark-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Kousuke Saruta (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (SPARK-3854) Scala style: require spaces before `{`
Date Thu, 09 Oct 2014 10:20:33 GMT

    [ https://issues.apache.org/jira/browse/SPARK-3854?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14164978#comment-14164978
] 

Kousuke Saruta commented on SPARK-3854:
---------------------------------------

[~joshrosen] I tried to write code to check spaces before '{' like as follows.

{code}
package org.apache.spark.scalastyle

import org.scalastyle.{PositionError, ScalariformChecker, ScalastyleError}
import scala.collection.mutable.{ListBuffer, Queue}
import scalariform.lexer.{Token, Tokens}
import scalariform.lexer.Tokens._
import scalariform.parser.CompilationUnit

class SparkSpaceBeforeLeftBraceChecker extends ScalariformChecker {
  val errorKey: String = "insert.a.single.space.before.left.brace"

  val rememberQueue: Queue[Token] = Queue[Token]()

  // The list of disallowed tokens before left brace without single space.
  val disallowedTokensBeforeLBrace = Seq (
    ARROW, ELSE, OP, RPAREN, TRY, MATCH, NEW, DO, FINALLY, PACKAGE, RETURN, THROW, YIELD,
VARID
  )

  override def verify(ast: CompilationUnit): List[ScalastyleError] = {

    var list: ListBuffer[ScalastyleError] = new ListBuffer[ScalastyleError]

    for (token <- ast.tokens) {
      rememberToken(token)
      if (isLBrace(token) &&
          isTokenAfterSpecificTokens(token) &&
          !hasSingleWhiteSpaceBefore(token)) {
        list += new PositionError(token.offset)
      }
    }
    list.toList
  }

  private def rememberToken(x: Token) = {
    rememberQueue.enqueue(x)
    if (rememberQueue.size > 2) {
      rememberQueue.dequeue
    }
    x
  }

  private def isTokenAfterSpecificTokens(x: Token) = {
    val previousToken = rememberQueue.head
    disallowedTokensBeforeLBrace.contains(previousToken.tokenType)
  }

  private def isLBrace(x: Token) =
    x.tokenType == Tokens.LBRACE

  private def hasSingleWhiteSpaceBefore(x: Token) =
    x.associatedWhitespaceAndComments.whitespaces.size == 1
}
{code}

How does this look?

> Scala style: require spaces before `{`
> --------------------------------------
>
>                 Key: SPARK-3854
>                 URL: https://issues.apache.org/jira/browse/SPARK-3854
>             Project: Spark
>          Issue Type: Sub-task
>          Components: Project Infra
>            Reporter: Josh Rosen
>
> We should require spaces before opening curly braces.  This isn't in the style guide,
but it probably should be:
> {code}
> // Correct:
> if (true) {
>   println("Wow!")
> }
> // Incorrect:
> if (true){
>    println("Wow!")
> }
> {code}
> See https://github.com/apache/spark/pull/1658#discussion-diff-18611791 for an example
"in the wild."
> {{git grep "){"}} shows only a few occurrences of this style.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org


Mime
View raw message