Skip to content

Commit

Permalink
Router: break before => in new given
Browse files Browse the repository at this point in the history
Apparently, break after forces an indented region and fails to compile.
  • Loading branch information
kitbellew committed Jan 16, 2025
1 parent 3224575 commit cb14e47
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@ class FormatOps(
final def getSlbEndOnLeft(start: FT)(implicit style: ScalafmtConfig): FT = {
val nft = start.right match {
case _: T.EOF => start
case _: T.Comma | _: T.Semicolon | _: T.RightArrow | _: T.Equals |
case _: T.Comma | _: T.Semicolon | _: T.Equals |
_: T.Interpolation.Start | _: T.Interpolation.SpliceEnd |
_: T.Interpolation.End | _: T.Interpolation.SpliceStart |
_: T.Interpolation.Part => null
case _: T.RightArrow => // given breaks before `=>`
if (start.rightOwner.is[Member.ParamClauseGroup]) start else null
case _ if start.hasBlankLine => start
case _
if AsInfixOp(start.rightOwner)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,9 +630,9 @@ class Router(formatOps: FormatOps) {
if (pcg.tparamClause.values.isEmpty) pcg.paramClauses
else pcg.tparamClause +: pcg.paramClauses,
).foldLeft(Policy.noPolicy) { case (policy, pc) =>
val pcLast = getLast(pc)
val pcPolicy = Policy ? pcLast.left.is[T.RightArrow] &&
decideNewlinesOnlyAfterToken(nextNonCommentSameLine(pcLast))
val afterpc = tokenAfter(pc)
val pcPolicy = Policy ? afterpc.right.is[T.RightArrow] &&
decideNewlinesOnlyAfterToken(afterpc)
policy ==> pcPolicy
}
if (nonSlbPolicy.isEmpty) Seq(Split(Space, 0))
Expand All @@ -645,16 +645,17 @@ class Router(formatOps: FormatOps) {

// Given conditional arrow
case FT(
left: T.RightArrow,
_,
FT.LeftOwnerParent(
right: T.RightArrow,
FT.RightOwnerParent(
pcg: Member.ParamClauseGroup,
Some(gvn: Stat.GivenLike),
),
) =>
val nlOnly = !style.newlines.sourceIgnored && hasBreak()
def spaceSplit(implicit fl: FileLine) = Split(nlOnly, 0)(Space)
val nextParamClause = pcg.paramClauses.find(_.pos.start > left.start)
val nextParamClause = (pcg.tparamClause +: pcg.paramClauses)
.find(_.pos.end > right.end)
.orElse(gvn.paramClauseGroups.dropWhile(_ ne pcg) match {
case `pcg` :: pcgNext :: _ =>
val tpc = pcgNext.tparamClause
Expand Down Expand Up @@ -690,13 +691,12 @@ class Router(formatOps: FormatOps) {
)
}
} { npc =>
val nextArrow =
getSlbEndOnLeft(nextAfterNonCommentSameLine(getLast(npc)))
val nextArrow = getSlbEndOnLeft(nextNonCommentSameLine(getLast(npc)))
val noSlb = npc.values.lengthCompare(1) != 0
Seq(
spaceSplit.withSingleLine(nextArrow, ignore = noSlb),
Split(Newline, 1)
.withIndent(style.indent.main, nextArrow, ExpiresOn.After),
.withIndent(style.indent.main, nextArrow, ExpiresOn.Before),
)
}

Expand Down
22 changes: 11 additions & 11 deletions scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -7974,9 +7974,9 @@ given [A] =>
given [A]
=> Seq[A] = foo
>>>
given [A] =>
Seq[A] = foo
given [A] => Seq[A] = foo
given [A]
=> Seq[A] = foo
<<< scala-3.6 given 2
maxColumn = 40
runner.parser = source
Expand All @@ -7986,10 +7986,10 @@ given [A] => (Seq[A] =>
given [A] => (Seq[A]
=> List[A]) => List[A] = foo
>>>
given [A] => (Seq[A] => List[A]) =>
List[A] = foo
given [A] => (Seq[A] => List[A]) =>
List[A] = foo
given [A] => (Seq[A] => List[A])
=> List[A] = foo
given [A] => (Seq[A] => List[A])
=> List[A] = foo
<<< scala-3.6 given 3
maxColumn = 40
runner.parser = source
Expand All @@ -7999,11 +7999,11 @@ given foo: (a: A) =>
given foo: (a: A)
=> [B, C <: AnyRef] => (b: B[A], C) => List[A] = foo
>>>
given foo: (a: A) =>
[B, C <: AnyRef] => (b: B[A], C) =>
List[A] = foo
given foo: (a: A) => [B, C <: AnyRef] =>
(b: B[A], C) => List[A] = foo
given foo: (a: A) => [B, C <: AnyRef]
=> (b: B[A], C) => List[A] = foo
given foo: (a: A)
=> [B, C <: AnyRef] => (b: B[A], C)
=> List[A] = foo
<<< don't rewrite complex infix to braces
rewrite.redundantBraces.oneStatApply.bracesMinSpan = 1
rewrite.rules = [RedundantBraces, RedundantParens]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7665,10 +7665,10 @@ given [A] => (Seq[A] =>
given [A] => (Seq[A]
=> List[A]) => List[A] = foo
>>>
given [A] => (Seq[A] => List[A]) =>
List[A] = foo
given [A] => (Seq[A] => List[A]) =>
List[A] = foo
given [A] => (Seq[A] => List[A])
=> List[A] = foo
given [A] => (Seq[A] => List[A])
=> List[A] = foo
<<< scala-3.6 given 3
maxColumn = 40
runner.parser = source
Expand All @@ -7678,10 +7678,10 @@ given foo: (a: A) =>
given foo: (a: A)
=> [B, C <: AnyRef] => (b: B[A], C) => List[A] = foo
>>>
given foo: (a: A) => [B, C <: AnyRef] =>
(b: B[A], C) => List[A] = foo
given foo: (a: A) => [B, C <: AnyRef] =>
(b: B[A], C) => List[A] = foo
given foo: (a: A) => [B, C <: AnyRef]
=> (b: B[A], C) => List[A] = foo
given foo: (a: A) => [B, C <: AnyRef]
=> (b: B[A], C) => List[A] = foo
<<< don't rewrite complex infix to braces
rewrite.redundantBraces.oneStatApply.bracesMinSpan = 1
rewrite.rules = [RedundantBraces, RedundantParens]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7997,9 +7997,9 @@ given [A] =>
given [A]
=> Seq[A] = foo
>>>
given [A] =>
Seq[A] = foo
given [A] => Seq[A] = foo
given [A]
=> Seq[A] = foo
<<< scala-3.6 given 2
maxColumn = 40
runner.parser = source
Expand All @@ -8009,10 +8009,10 @@ given [A] => (Seq[A] =>
given [A] => (Seq[A]
=> List[A]) => List[A] = foo
>>>
given [A] => (Seq[A] => List[A]) =>
List[A] = foo
given [A] => (Seq[A] => List[A]) =>
List[A] = foo
given [A] => (Seq[A] => List[A])
=> List[A] = foo
given [A] => (Seq[A] => List[A])
=> List[A] = foo
<<< scala-3.6 given 3
maxColumn = 40
runner.parser = source
Expand All @@ -8022,11 +8022,11 @@ given foo: (a: A) =>
given foo: (a: A)
=> [B, C <: AnyRef] => (b: B[A], C) => List[A] = foo
>>>
given foo: (a: A) =>
[B, C <: AnyRef] => (b: B[A], C) =>
List[A] = foo
given foo: (a: A) => [B, C <: AnyRef] =>
(b: B[A], C) => List[A] = foo
given foo: (a: A) => [B, C <: AnyRef]
=> (b: B[A], C) => List[A] = foo
given foo: (a: A)
=> [B, C <: AnyRef] => (b: B[A], C)
=> List[A] = foo
<<< don't rewrite complex infix to braces
rewrite.redundantBraces.oneStatApply.bracesMinSpan = 1
rewrite.rules = [RedundantBraces, RedundantParens]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8306,10 +8306,12 @@ given [A] => (Seq[A] =>
given [A] => (Seq[A]
=> List[A]) => List[A] = foo
>>>
given [A] => (Seq[A] => List[A]) =>
List[A] = foo
given [A] => (Seq[A] => List[A]) =>
List[A] = foo
given [A]
=> (Seq[A] => List[A])
=> List[A] = foo
given [A]
=> (Seq[A] => List[A])
=> List[A] = foo
<<< scala-3.6 given 3
maxColumn = 40
runner.parser = source
Expand All @@ -8319,10 +8321,14 @@ given foo: (a: A) =>
given foo: (a: A)
=> [B, C <: AnyRef] => (b: B[A], C) => List[A] = foo
>>>
given foo: (a: A) => [B, C <: AnyRef] =>
(b: B[A], C) => List[A] = foo
given foo: (a: A) => [B, C <: AnyRef] =>
(b: B[A], C) => List[A] = foo
given foo: (a: A)
=> [B, C <: AnyRef]
=> (b: B[A], C)
=> List[A] = foo
given foo: (a: A)
=> [B, C <: AnyRef]
=> (b: B[A], C)
=> List[A] = foo
<<< don't rewrite complex infix to braces
rewrite.redundantBraces.oneStatApply.bracesMinSpan = 1
rewrite.rules = [RedundantBraces, RedundantParens]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class FormatTests extends FunSuite with CanRunTests with FormatAssertions {
val explored = Debug.explored.get()
logger.debug(s"Total explored: $explored")
if (!onlyUnit && !onlyManual)
assertEquals(explored, 1253679, "total explored")
assertEquals(explored, 1253866, "total explored")
val results = debugResults.result()
// TODO(olafur) don't block printing out test results.
// I don't want to deal with scalaz's Tasks :'(
Expand Down

0 comments on commit cb14e47

Please sign in to comment.