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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration to disable newlines between multiline defs #1134

Merged
merged 4 commits into from Apr 13, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -102,6 +102,22 @@ import metaconfig._
* // ...
* }
* else //...
* @param alwaysBeforeMultilineDef
* If true, add a newline before the body of a multiline def without
* curly braces. See #1126 for discussion.
* For example,
* {{{
* // newlines.alwaysBeforeMultilineDef = false
* def foo(bar: Bar): Foo = bar
* .flatMap(f)
* .map(g)
*
* // newlines.alwaysBeforeMultilineDef = true
* def foo(bar: Bar): Foo =
* bar
* .flatMap(f)
* .map(g)
* }}}
*/
@DeriveConfDecoder
case class Newlines(
Expand All @@ -114,7 +130,8 @@ case class Newlines(
afterCurlyLambda: NewlineCurlyLambda = NewlineCurlyLambda.never,
afterImplicitKWInVerticalMultiline: Boolean = false,
beforeImplicitKWInVerticalMultiline: Boolean = false,
alwaysBeforeElseAfterCurlyIf: Boolean = false
alwaysBeforeElseAfterCurlyIf: Boolean = false,
alwaysBeforeMultilineDef: Boolean = true
)

sealed abstract class NewlineCurlyLambda
Expand Down
Expand Up @@ -403,6 +403,8 @@ class Router(formatOps: FormatOps) {
// 1
// )
true
case RightParen() if !style.newlines.alwaysBeforeMultilineDef =>
true
case _ => false
}
)
Expand All @@ -418,7 +420,10 @@ class Router(formatOps: FormatOps) {
Space,
0,
ignoreIf = newlines > 0 && !rhsIsJsNative,
policy = SingleLineBlock(expire, exclude = exclude)),
policy =
if (!style.newlines.alwaysBeforeMultilineDef) NoPolicy
else SingleLineBlock(expire, exclude = exclude)
),
Split(Newline, 1, ignoreIf = rhsIsJsNative)
.withIndent(2, expire, Left)
)
Expand Down
20 changes: 20 additions & 0 deletions scalafmt-tests/src/test/resources/default/DefDef.stat
Expand Up @@ -283,3 +283,23 @@ def zip[C](x: A =>? C): A =>? (B, C) = 2
implicit def adtSyntax[A](target: A): Any =
LiftMacros.materializeADTSyntax[A]
}
<<< #1126 Should format and add newline before multiline def
def foo = bar.flatMap(x => f(x))
.flatMap(y => g(y)).map(z => veryLongFunctionNameFor80Characters(z))
>>>
def foo =
bar
.flatMap(x => f(x))
.flatMap(y => g(y))
.map(z => veryLongFunctionNameFor80Characters(z))
<<< #1126
def foo = Future({
val bar = fetchBar()
bar.map(_ + 10)
})
>>>
def foo =
Future({
val bar = fetchBar()
bar.map(_ + 10)
})
@@ -0,0 +1,20 @@
maxColumn = 40
newlines.alwaysBeforeMultilineDef = false

<<< Wrap long line but don't add newline
def foo = bar
.flatMap(x => f(x)).map(x => g(x))
>>>
def foo = bar
.flatMap(x => f(x))
.map(x => g(x))
<<< Don't touch multiline function call
def foo = Future({
val bar = fetchBar()
bar.map(_ + 10)
})
>>>
def foo = Future({
val bar = fetchBar()
bar.map(_ + 10)
})