Skip to content

Commit

Permalink
Polish newline penalties in applications, fixes #160.
Browse files Browse the repository at this point in the history
  • Loading branch information
olafurpg committed Apr 16, 2016
1 parent 6865233 commit 0694841
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 21 deletions.
28 changes: 15 additions & 13 deletions core/src/main/scala/org/scalafmt/internal/Router.scala
Expand Up @@ -374,20 +374,22 @@ class Router(formatOps: FormatOps) {
}
val singleArgument = args.length == 1

val baseSingleLinePolicy =
if (isBracket) {
if (singleArgument)
SingleLineBlock(
def singleLine(newlinePenalty: Int): Policy = {
val baseSingleLinePolicy =
if (isBracket) {
if (singleArgument)
SingleLineBlock(
close, excludeRanges, disallowInlineComments = false)
else SingleLineBlock(close)
} else {
if (singleArgument) {
penalizeAllNewlines(close, 5) // TODO(olafur) magic number
} else SingleLineBlock(close, excludeRanges)
}
val singleLine =
else SingleLineBlock(close)
} else {
if (singleArgument) {
penalizeAllNewlines(close, newlinePenalty)
} else SingleLineBlock(close, excludeRanges)
}

if (exclude.isEmpty || isBracket) baseSingleLinePolicy
else baseSingleLinePolicy.andThen(unindentAtExclude)
}

val oneArgOneLine = OneArgOneLineSplit(open)

Expand Down Expand Up @@ -426,13 +428,13 @@ class Router(formatOps: FormatOps) {
Seq(
Split(modification,
0,
policy = singleLine,
policy = singleLine(6),
ignoreIf = !fitsOnOneLine || isConfigStyle)
.withOptimalToken(expirationToken)
.withIndent(indent, close, Left),
Split(newlineModification,
(1 + nestedPenalty + lhsPenalty) * bracketMultiplier,
policy = singleLine,
policy = singleLine(5),
ignoreIf = !fitsOnOneLine || isConfigStyle)
.withOptimalToken(expirationToken)
.withIndent(indent, close, Left),
Expand Down
24 changes: 20 additions & 4 deletions core/src/test/resources/default/Apply.stat
Expand Up @@ -253,10 +253,11 @@ val configsWithAlternatives = Map[String, Seq[AlternateConfig]](
translation = s => s"${s.toLong * 10}s")),
"spark.reducer.maxSizeInFlight" -> Seq(
AlternateConfig("spark.reducer.maxMbInFlight", "1.4")),
"spark.kryoserializer.buffer" -> Seq(AlternateConfig(
"spark.kryoserializer.buffer.mb",
"1.4",
translation = s => s"${(s.toDouble * 1000).toInt}k")),
"spark.kryoserializer.buffer" -> Seq(
AlternateConfig("spark.kryoserializer.buffer.mb",
"1.4",
translation = s =>
s"${(s.toDouble * 1000).toInt}k")),
"spark.yarn.jars" -> Seq(AlternateConfig("spark.yarn.jar", "2.0"))
)
}
Expand Down Expand Up @@ -359,3 +360,18 @@ class ResolutionCopier(x: Int) {
_isEqualTokens(node.rightBracket, toNode.rightBracket));
}
}
<<< single arg breaking #160
implicit class IterableW[+A](it: JSIterable[A]) {
def iterator(): Iterator[A] =
toIterator(it
.asInstanceOf[IteratorMethodAccess]
.bracketCall[JSIterator[A]](iteratorSymbol)())
}
>>>
implicit class IterableW[+A](it: JSIterable[A]) {
def iterator(): Iterator[A] =
toIterator(
it
.asInstanceOf[IteratorMethodAccess]
.bracketCall[JSIterator[A]](iteratorSymbol)())
}
3 changes: 2 additions & 1 deletion core/src/test/resources/default/If.stat
Expand Up @@ -56,7 +56,8 @@ def identModification(ident: Ident): Modification =
val thisLocalDef =
if (thisType == NoType) None
else {
Some("this" -> LocalDef(
Some(
"this" -> LocalDef(
RefinedType(thisType, isExact = false, isNullable = false),
false,
ReplaceWithThis()))
Expand Down
5 changes: 3 additions & 2 deletions core/src/test/resources/unit/Argument.source
Expand Up @@ -61,6 +61,7 @@ object a {
}
>>>
object a {
function(function(function(a, b),
function(c, d)))
function(
function(function(a, b),
function(c, d)))
}
3 changes: 2 additions & 1 deletion core/src/test/resources/unit/Lambda.stat
Expand Up @@ -69,7 +69,8 @@ Thing(implicit ctx => j => ???)
<<< long curried with ()
Thing(implicit ctx => j => k => aaaaaaaaaaaaa(bbbbbbbbbbbbbbbbbbb, cccccccccc, ddddddddddddddddddddj)) // coooooooooooooooomment
>>>
Thing(implicit ctx =>
Thing(
implicit ctx =>
j =>
k =>
aaaaaaaaaaaaa(
Expand Down

0 comments on commit 0694841

Please sign in to comment.