Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bump penalty for select chains of length 1. #611

Merged
merged 1 commit into from
Dec 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions core/src/main/scala/org/scalafmt/config/Newlines.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,34 @@ import metaconfig.ConfigReader
* def foo = 2
* }
* )
*
* @param penalizeSingleSelectMultiArgList
* If true, adds a penalty to newlines before a dot starting a select
* chain of length one and argument list. The penalty matches the number
* of arguments to the select chain application.
* {{{
* // If true, favor
* logger.elem(a,
* b,
* c)
* // instead of
* logger
* .elem(a, b, c)
*
* // penalty is proportional to argument count, example:
* logger.elem(a, b, c) // penalty 2
* logger.elem(a, b, c, d) // penalty 3, etc.
* }}}
*
* If false, matches pre-v0.5 behavior. Note. this option may be
* removed in a future release.
*/
@ConfigReader
case class Newlines(
neverInDanglingParenthesesSingleLineArgList: Boolean = false,
neverInResultType: Boolean = false,
neverBeforeJsNative: Boolean = false,
sometimesBeforeColonInMethodReturnType: Boolean = true,
penalizeSingleSelectMultiArgList: Boolean = true,
alwaysBeforeCurlyBraceLambdaParams: Boolean = false
)
24 changes: 19 additions & 5 deletions core/src/main/scala/org/scalafmt/internal/Router.scala
Original file line number Diff line number Diff line change
Expand Up @@ -863,12 +863,26 @@ class Router(formatOps: FormatOps) {
val newlinePolicy = breakOnEveryDot
.andThen(penalizeNewlinesInApply.f)
.copy(expire = lastToken.end)
Seq(
Split(NoSplit,
0,
ignoreIf = style.breakChainOnFirstMethodDot && newlines > 0)
val ignoreNoSplit = style.breakChainOnFirstMethodDot && newlines > 0
val chainLengthPenalty =
if (!style.bestEffortInDeeplyNestedCode && // breaks tests.
style.newlines.penalizeSingleSelectMultiArgList &&
chain.length < 2) {
// penalize by the number of arguments in the rhs open apply.
// I know, it's a bit arbitrary, but my manual experiments seem
// to show that it produces OK output. The key insight is that
// many arguments on the same line can be hard to read. By not
// putting a newline before the dot, we force the argument list
// to break into multiple lines.
splitApplyIntoLhsAndArgsLifted(owners(next(next(tok)).right)).map {
case (_, lst) => Math.max(0, lst.length - 1)
}.getOrElse(0)
} else 0
Seq(
Split(NoSplit, 0, ignoreIf = ignoreNoSplit)
.withPolicy(noSplitPolicy),
Split(Newline.copy(acceptNoSplit = true), 2 + nestedPenalty)
Split(Newline.copy(acceptNoSplit = true),
2 + nestedPenalty + chainLengthPenalty)
.withPolicy(newlinePolicy)
.withIndent(2, optimalToken, Left)
)
Expand Down
19 changes: 19 additions & 0 deletions core/src/test/resources/default/Select.stat
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,22 @@ object a {
computeOrReadCheckpoint(partition, context)
})
}
<<< #599
{{
logger
.elem(threp1, threp2, threp3, tax.total.value, tax.usedPersonalDiscount)
}}
>>>
{
{
logger.elem(threp1,
threp2,
threp3,
tax.total.value,
tax.usedPersonalDiscount)
}
}
<<< No args #599
keys.clear() // we need to remove the selected keys from the set, otherwise they remain selected
>>>
keys.clear() // we need to remove the selected keys from the set, otherwise they remain selected