Skip to content

Commit c224ef4

Browse files
committed
Merge pull request #46 from ktoso/wip-function-to-function-formatting-fix-ktoso
do not add newline for each function in `=> => =>` scenarios
2 parents 2296faa + 34a7bbd commit c224ef4

File tree

2 files changed

+60
-3
lines changed

2 files changed

+60
-3
lines changed

scalariform/src/main/scala/com/danieltrinh/scalariform/formatter/ExprFormatter.scala

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package com.danieltrinh.scalariform.formatter
22

3+
import com.danieltrinh.scalariform.formatter.Alignment._
4+
import com.danieltrinh.scalariform.formatter.AnnotationFormatter
35
import com.danieltrinh.scalariform.lexer.Chars
46
import com.danieltrinh.scalariform.lexer.Token
57
import com.danieltrinh.scalariform.lexer.Tokens._
68
import com.danieltrinh.scalariform.parser._
79
import com.danieltrinh.scalariform.utils.{ TextEditProcessor, Utils }
810
import com.danieltrinh.scalariform.formatter.preferences._
911
import scala.PartialFunction._
10-
import Alignment._
1112

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

@@ -756,16 +757,26 @@ trait ExprFormatter { self: HasFormattingPreferences with AnnotationFormatter wi
756757
if (statSeq.firstTokenOption.isDefined) {
757758
statSeq.firstStatOpt match {
758759
case Some(Expr(List(anonFn @ AnonymousFunction(params, arrowToken, subStatSeq))))
760+
def hasNestedAnonymousFunction(subStatSeq: StatSeq): Boolean =
761+
(for {
762+
firstStat <- subStatSeq.firstStatOpt
763+
head <- firstStat.immediateChildren.headOption
764+
} yield head.isInstanceOf[AnonymousFunction]).getOrElse(false)
765+
759766
val (instruction, subStatState) =
760-
if (hiddenPredecessors(params(0).firstToken).containsNewline)
767+
if (hasNestedAnonymousFunction(subStatSeq))
768+
(CompactEnsuringGap, indentedState.indent(-1))
769+
else if (hiddenPredecessors(params(0).firstToken).containsNewline)
761770
(indentedInstruction, indentedState.indent)
762771
else
763772
(CompactEnsuringGap, indentedState)
764773
formatResult = formatResult.before(statSeq.firstToken, instruction)
765774
formatResult ++= format(params)
766775
for (firstToken subStatSeq.firstTokenOption) {
767776
val instruction =
768-
if (hiddenPredecessors(firstToken).containsNewline || containsNewline(subStatSeq))
777+
if (hasNestedAnonymousFunction(subStatSeq))
778+
CompactEnsuringGap
779+
else if (hiddenPredecessors(firstToken).containsNewline || containsNewline(subStatSeq))
769780
statFormatterState(subStatSeq.firstStatOpt)(subStatState).currentIndentLevelInstruction
770781
else
771782
CompactEnsuringGap
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.danieltrinh.scalariform.formatter
2+
3+
import com.danieltrinh.scalariform.parser.{CompilationUnit, ScalaParser}
4+
5+
// format: OFF
6+
class FunctionFormatterTest extends AbstractFormatterTest {
7+
8+
"val f = x => x" ==> "val f = x => x"
9+
"val f: Int => Int => Unit = a => b => ()" ==> "val f: Int => Int => Unit = a => b => ()"
10+
11+
"{ ctx => j => () }" ==> "{ ctx => j => () }"
12+
"fun { ctx => j => () }" ==> "fun { ctx => j => () }"
13+
"Thing() { ctx => j => () }" ==> "Thing() { ctx => j => () }"
14+
15+
"""{ ctx =>
16+
| ???
17+
|}""".stripMargin ==>
18+
"""{ ctx =>
19+
| ???
20+
|}""".stripMargin
21+
22+
"""{ ctx => j =>
23+
| ???
24+
|}""".stripMargin ==>
25+
"""{ ctx => j =>
26+
| ???
27+
|}""".stripMargin
28+
29+
"""val x = { ctx => j =>
30+
| one()
31+
| two()
32+
|}""".stripMargin ==>
33+
"""val x = { ctx => j =>
34+
| one()
35+
| two()
36+
|}""".stripMargin
37+
38+
override val debug = false
39+
40+
type Result = CompilationUnit
41+
42+
def parse(parser: ScalaParser) = parser.compilationUnitOrScript()
43+
44+
def format(formatter: ScalaFormatter, result: Result) = formatter.format(result)(FormatterState())
45+
46+
}

0 commit comments

Comments
 (0)