Skip to content

Commit

Permalink
Router: handle implicit in non-vertical-multiline
Browse files Browse the repository at this point in the history
This will format "implicit" on the same line as the opening paren, if
possible. It will also break after the "implicit" unless all parameters
fit on one line.

Fixes scalameta#1362.
  • Loading branch information
kitbellew committed Feb 27, 2020
1 parent 8423bb4 commit daa2552
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -780,7 +780,13 @@ class Router(formatOps: FormatOps) {
Policy.empty(close)
}

val noSplitMod = getNoSplit(formatToken, !isBracket)
val handleImplicit =
style.activeForEdition_2020_03 && right.is[T.KwImplicit]

val noSplitMod =
if (handleImplicit &&
style.newlines.beforeImplicitKW == Some(true)) null
else getNoSplit(formatToken, !isBracket)
val noSplitIndent = if (right.is[T.Comment]) indent else Num(0)

val align =
Expand Down Expand Up @@ -829,6 +835,7 @@ class Router(formatOps: FormatOps) {
Seq(
Split(noSplitMod, 0, policy = noSplitPolicy)
.onlyIf(noSplitMod != null)
.notIf(handleImplicit && style.newlines.afterImplicitKW == Some(true))
.withOptimalToken(expirationToken)
.withIndent(noSplitIndent, close, Right),
Split(newlineMod, (1 + nestedPenalty) * bracketCoef)
Expand All @@ -838,10 +845,10 @@ class Router(formatOps: FormatOps) {
.withIndent(indent, close, Right),
Split(noSplitMod, (2 + lhsPenalty) * bracketCoef)
.withPolicy(oneArgOneLine)
.onlyIf(notTooManyArgs && align)
.onlyIf(handleImplicit || (notTooManyArgs && align))
.onlyIf(noSplitMod != null)
.withOptimalToken(expirationToken)
.withIndent(StateColumn, close, Right),
.withIndent(if (align) StateColumn else indent, close, Right),
Split(Newline, (3 + nestedPenalty) * bracketCoef)
.withPolicy(oneArgOneLine)
.onlyIf(!singleArgument && !alignTuple)
Expand Down Expand Up @@ -1447,6 +1454,28 @@ class Router(formatOps: FormatOps) {
case FormatToken(c: T.Comment, _, _) =>
Seq(Split(newlines2Modification(newlines), 0))

case FormatToken(_: T.KwImplicit, _, _)
if style.activeForEdition_2020_03 &&
!style.verticalMultiline.atDefnSite &&
splitDefnIntoParts.isDefinedAt(leftOwner) =>
val paramsOpt = splitDefnIntoParts.lift(leftOwner).flatMap {
case (_, _, _, paramss) =>
findArgsFor(formatToken, paramss)
}
// make sure there's no other param with implicit
paramsOpt
.filterNot(_.exists(_.mods.exists(TreeOps.isExplicitImplicit)))
.fold {
Seq(Split(Space, 0))
} { params =>
Seq(
Split(Space, 0)
.notIf(style.newlines.afterImplicitKW == Some(true))
.withPolicy(SingleLineBlock(params.last.tokens.last)),
Split(Newline, 1)
)
}

case opt
if style.optIn.annotationNewlines &&
optionalNewlines(hash(opt.right)) =>
Expand Down
4 changes: 2 additions & 2 deletions scalafmt-tests/src/test/resources/default/Apply.stat
Original file line number Diff line number Diff line change
Expand Up @@ -718,6 +718,6 @@ align = none
: SerializedSuspendableExecutionContext =
new SerializedSuspendableExecutionContext(throughput)
>>>
def apply(throughput: Int)(implicit context: ExecutionContext)
: SerializedSuspendableExecutionContext =
def apply(throughput: Int)(implicit
context: ExecutionContext): SerializedSuspendableExecutionContext =
new SerializedSuspendableExecutionContext(throughput)
17 changes: 4 additions & 13 deletions scalafmt-tests/src/test/resources/default/Class.stat
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,7 @@ case class Foo(
def run[F[_]](
a: Int,
b: Int = 0
)(
implicit
F: Monad[F]
): F[Resolution] =
)(implicit F: Monad[F]): F[Resolution] =
???
}
<<< #1362 2: single implicit, short line
Expand Down Expand Up @@ -236,8 +233,7 @@ case class Foo(
a: Int,
b: Int = 0
)(
implicit
F: Monad[F]
implicit F: Monad[F]
): F[Resolution] =
???
}
Expand Down Expand Up @@ -278,8 +274,7 @@ case class Foo(
def run[F[_]](
a: Int,
b: Int = 0
)(
implicit
)(implicit
F: Monad[F],
G: Monad[G]
): F[Resolution] =
Expand Down Expand Up @@ -322,10 +317,6 @@ case class Foo(
def run[F[_]](
a: Int,
b: Int = 0
)(
implicit
F: Monad[F],
G: Monad[G]
): F[Resolution] =
)(implicit F: Monad[F], G: Monad[G]): F[Resolution] =
???
}
15 changes: 8 additions & 7 deletions scalafmt-tests/src/test/resources/default/DefDef.stat
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ private def withNewLocalDefs(bindings: List[Binding])(
def joinFull[E1 >: E, E2, U2, D[_], O1, U1, O2](q2: Query[E2, _, D])(implicit ol1: OptionLift[E1, O1], sh1: Shape[FlatShapeLevel, O1, U1, _], ol2: OptionLift[E2, O2], sh2: Shape[FlatShapeLevel, O2, U2, _])
>>>
def joinFull[E1 >: E, E2, U2, D[_], O1, U1, O2](q2: Query[E2, _, D])(
implicit ol1: OptionLift[E1, O1],
implicit
ol1: OptionLift[E1, O1],
sh1: Shape[FlatShapeLevel, O1, U1, _],
ol2: OptionLift[E2, O2],
sh2: Shape[FlatShapeLevel, O2, U2, _])
Expand All @@ -45,16 +46,16 @@ def zipWith[E2, U2, F, G, T, D[_]](q2: Query[E2, U2, D], f: (E, E2) => F)(
<<< slick groupBy
def groupBy[K, T, G, P](f: E => K)(implicit kshape: Shape[_ <: FlatShapeLevel, K, T, G], vshape: Shape[_ <: FlatShapeLevel, E, _, P]): Query[(G, Query[P, U, Seq]), (T, Query[P, U, Seq]), C]
>>>
def groupBy[K, T, G, P](f: E => K)(
implicit kshape: Shape[_ <: FlatShapeLevel, K, T, G],
vshape: Shape[_ <: FlatShapeLevel, E, _, P])
def groupBy[K, T, G, P](f: E => K)(implicit
kshape: Shape[_ <: FlatShapeLevel, K, T, G],
vshape: Shape[_ <: FlatShapeLevel, E, _, P])
: Query[(G, Query[P, U, Seq]), (T, Query[P, U, Seq]), C]
<<< slick groupBy 2
def groupBy[K, T, G, P](f: E => K)(implicit kshape: Shape[_ <: FlatShapeLevel, K, T, G], vshape: Shape[_ <: FlatShapeLevel, E, _, P]): Query[(G, Query[P, U, Seq]), (T, Query[P, U, Seq]), CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC, (T, Query[P, U, Seq]), (T, Query[P, U, Seq])]
>>>
def groupBy[K, T, G, P](f: E => K)(
implicit kshape: Shape[_ <: FlatShapeLevel, K, T, G],
vshape: Shape[_ <: FlatShapeLevel, E, _, P])
def groupBy[K, T, G, P](f: E => K)(implicit
kshape: Shape[_ <: FlatShapeLevel, K, T, G],
vshape: Shape[_ <: FlatShapeLevel, E, _, P])
: Query[(G, Query[P, U, Seq]),
(T, Query[P, U, Seq]),
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,8 @@ implicit def v_s_Op[
OpPow
) Op <: OpType
](
implicit @expand.sequence[Op](
implicit
@expand.sequence[Op](
{ _ + _ },
{ _ - _ },
{ _ * _ },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,8 @@ implicit def v_s_Op[
OpPow
) Op <: OpType
](
implicit @expand.sequence[Op](
implicit
@expand.sequence[Op](
{ _ + _ },
{ _ - _ },
{ _ * _ },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class X(private final val x: Int)(
)
>>>
class X(private final val x: Int)(
implicit
val i1: Int
implicit val i1: Int
)
<<< 1148 — implicit val with only private
class X(private final val x: Int)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ class X(final private val x: Int)(
)
>>>
class X(final private val x: Int)(
implicit
val i1: Int
implicit val i1: Int
)
<<< 1148 — implicit val with only private
class X(private final val x: Int)(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,7 @@ object a {
)(
b1: String,
b2: String, // comment
)(
implicit
)(implicit
c1: SomeType1,
c2: SomeType2, // comment
) {
Expand All @@ -88,8 +87,7 @@ object a {
)(
b1: String,
b2: String, // comment
)(
implicit
)(implicit
c1: SomeType1,
c2: SomeType2, // comment
) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,7 @@ object a {
)(
b1: String,
b2: String, // comment
)(
implicit
)(implicit
c1: SomeType1,
c2: SomeType2, // comment
) {
Expand All @@ -312,8 +311,7 @@ object a {
)(
b1: String,
b2: String, // comment
)(
implicit
)(implicit
c1: SomeType1,
c2: SomeType2, // comment
) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ object a {
)(
b1: String,
b2: String // comment
)(
implicit
)(implicit
c1: SomeType1,
c2: SomeType2 // comment
) {
Expand All @@ -87,8 +86,7 @@ object a {
)(
b1: String,
b2: String // comment
)(
implicit
)(implicit
c1: SomeType1,
c2: SomeType2 // comment
) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ verticalMultiline.atDefnSite = false
===
def format_![T <: Tree](code: String, foo: Int)(f: A => B, k: D)(implicit ev: Parse[T], ev2: EC): String
>>>
def format_![T <: Tree](code: String, foo: Int)(f: A => B, k: D)(
implicit ev: Parse[T],
def format_![T <: Tree](code: String, foo: Int)(f: A => B, k: D)(implicit
ev: Parse[T],
ev2: EC
): String
<<< should add a newline after implicit keyword in class definitions
Expand Down Expand Up @@ -87,7 +87,8 @@ implicit def pairEncoder[A, B](
): CsvEncoder[(A, B)]
>>>
implicit def pairEncoder[A, B](
implicit aEncoder: CsvEncoder[A],
implicit
aEncoder: CsvEncoder[A],
bEncoder: CsvEncoder[B]
): CsvEncoder[(A, B)]
<<< should work without explicit parameter groups (non-vm, explicit before)
Expand All @@ -99,7 +100,7 @@ implicit def pairEncoder[A, B](
bEncoder: CsvEncoder[B]
): CsvEncoder[(A, B)]
>>>
implicit def pairEncoder[A, B](
implicit aEncoder: CsvEncoder[A],
implicit def pairEncoder[A, B](implicit
aEncoder: CsvEncoder[A],
bEncoder: CsvEncoder[B]
): CsvEncoder[(A, B)]
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ verticalMultiline.atDefnSite = false
def format_![T <: Tree](code: String, foo: Int)(f: A => B, k: D)(implicit ev: Parse[T], ev2: EC): String
>>>
def format_![T <: Tree](code: String, foo: Int)(f: A => B, k: D)(
implicit ev: Parse[T],
implicit
ev: Parse[T],
ev2: EC
): String
<<< should add a newline after implicit keyword in class definitions
Expand Down Expand Up @@ -91,6 +92,7 @@ implicit def pairEncoder[A, B](implicit aEncoder: CsvEncoder[A],
): CsvEncoder[(A, B)]
>>>
implicit def pairEncoder[A, B](
implicit aEncoder: CsvEncoder[A],
implicit
aEncoder: CsvEncoder[A],
bEncoder: CsvEncoder[B]
): CsvEncoder[(A, B)]

0 comments on commit daa2552

Please sign in to comment.