Skip to content

Commit

Permalink
do not add newline for each function in => => => scenarios
Browse files Browse the repository at this point in the history
resolves #45
  • Loading branch information
ktoso committed Feb 11, 2015
1 parent 2296faa commit 34a7bbd
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.danieltrinh.scalariform.formatter

import com.danieltrinh.scalariform.formatter.Alignment._
import com.danieltrinh.scalariform.formatter.AnnotationFormatter
import com.danieltrinh.scalariform.lexer.Chars
import com.danieltrinh.scalariform.lexer.Token
import com.danieltrinh.scalariform.lexer.Tokens._
import com.danieltrinh.scalariform.parser._
import com.danieltrinh.scalariform.utils.{ TextEditProcessor, Utils }
import com.danieltrinh.scalariform.formatter.preferences._
import scala.PartialFunction._
import Alignment._

trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter with HasHiddenTokenInfo with TypeFormatter with TemplateFormatter with ScalaFormatter with XmlFormatter with CaseClauseFormatter

Expand Down Expand Up @@ -756,16 +757,26 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
if (statSeq.firstTokenOption.isDefined) {
statSeq.firstStatOpt match {
case Some(Expr(List(anonFn @ AnonymousFunction(params, arrowToken, subStatSeq))))
def hasNestedAnonymousFunction(subStatSeq: StatSeq): Boolean =
(for {
firstStat <- subStatSeq.firstStatOpt
head <- firstStat.immediateChildren.headOption
} yield head.isInstanceOf[AnonymousFunction]).getOrElse(false)

val (instruction, subStatState) =
if (hiddenPredecessors(params(0).firstToken).containsNewline)
if (hasNestedAnonymousFunction(subStatSeq))
(CompactEnsuringGap, indentedState.indent(-1))
else if (hiddenPredecessors(params(0).firstToken).containsNewline)
(indentedInstruction, indentedState.indent)
else
(CompactEnsuringGap, indentedState)
formatResult = formatResult.before(statSeq.firstToken, instruction)
formatResult ++= format(params)
for (firstToken subStatSeq.firstTokenOption) {
val instruction =
if (hiddenPredecessors(firstToken).containsNewline || containsNewline(subStatSeq))
if (hasNestedAnonymousFunction(subStatSeq))
CompactEnsuringGap
else if (hiddenPredecessors(firstToken).containsNewline || containsNewline(subStatSeq))
statFormatterState(subStatSeq.firstStatOpt)(subStatState).currentIndentLevelInstruction
else
CompactEnsuringGap
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.danieltrinh.scalariform.formatter

import com.danieltrinh.scalariform.parser.{CompilationUnit, ScalaParser}

// format: OFF
class FunctionFormatterTest extends AbstractFormatterTest {

"val f = x => x" ==> "val f = x => x"
"val f: Int => Int => Unit = a => b => ()" ==> "val f: Int => Int => Unit = a => b => ()"

"{ ctx => j => () }" ==> "{ ctx => j => () }"
"fun { ctx => j => () }" ==> "fun { ctx => j => () }"
"Thing() { ctx => j => () }" ==> "Thing() { ctx => j => () }"

"""{ ctx =>
| ???
|}""".stripMargin ==>
"""{ ctx =>
| ???
|}""".stripMargin

"""{ ctx => j =>
| ???
|}""".stripMargin ==>
"""{ ctx => j =>
| ???
|}""".stripMargin

"""val x = { ctx => j =>
| one()
| two()
|}""".stripMargin ==>
"""val x = { ctx => j =>
| one()
| two()
|}""".stripMargin

override val debug = false

type Result = CompilationUnit

def parse(parser: ScalaParser) = parser.compilationUnitOrScript()

def format(formatter: ScalaFormatter, result: Result) = formatter.format(result)(FormatterState())

}

0 comments on commit 34a7bbd

Please sign in to comment.