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

Router: add rules for new given syntax #4656

Merged
merged 2 commits into from
Dec 20, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,84 @@ class Router(formatOps: FormatOps) {
) withSlbSplit
else getFolded(beforeMultiline eq Newlines.keep)
}

// Given decl
case FT(_: T.KwGiven, _, FT.LeftOwner(gvn: Stat.GivenLike)) =>
if (style.newlines.unfold && gvn.paramClauseGroups.nonEmpty) {
val nonSlbPolicy = gvn.paramClauseGroups.flatMap { pcg =>
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))
policy ==> pcPolicy
}
if (nonSlbPolicy.isEmpty) Seq(Split(Space, 0))
else Seq(
Split(Space, 0)
.withSingleLine(getSlbEndOnLeft(getLast(gvn.paramClauseGroups.last))),
Split(Space, 1, policy = nonSlbPolicy),
)
} else Seq(Split(Space, 0))

// Given conditional arrow
case FT(
left: T.RightArrow,
_,
FT.LeftOwnerParent(
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)
.orElse(gvn.paramClauseGroups.dropWhile(_ ne pcg) match {
case `pcg` :: pcgNext :: _ =>
val tpc = pcgNext.tparamClause
if (tpc.nonEmpty) Some(tpc) else pcgNext.paramClauses.headOption
case _ => None
})
nextParamClause.fold {
gvn match {
case gvn: Defn.Given => binPackParentConstructorSplits(
isFirstCtor = true,
owners = Set(gvn.templ),
rhs = gvn.templ.inits.headOption,
lastFt = templateDerivesOrCurlyOrLastNonTrivial(gvn.templ),
indentLen = style.indent.extendSite,
extendsThenWith = gvn.templ.inits.lengthCompare(1) > 0,
)
case _ =>
val end = gvn match {
case gvn: Tree.WithBody => tokenBefore(gvn.body)
case _ => getLast(gvn)
}
val noSlb = gvn match {
case gvn: Tree.WithDeclTpe => gvn.decltpe match {
case t: Type.Tuple => t.args.lengthCompare(1) > 0
case _ => false
}
case _ => false
}
Seq(
spaceSplit.withSingleLine(getSlbEndOnLeft(end), ignore = noSlb),
Split(Newline, 1)
.withIndent(style.indent.main, end, ExpiresOn.After),
)
}
} { npc =>
val nextArrow =
getSlbEndOnLeft(nextAfterNonCommentSameLine(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),
)
}

// New statement
case FT(_: T.Semicolon, _, StartsStatementRight(stmt))
if !stmt.is[Term.EndMarker] =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7965,3 +7965,28 @@ lazy val onlyImplicitOrTypeParams = paramss.forall(
sym.isType || sym.is(Implicit) || sym.is(Given)
}
)
<<< scala-3.6 given 1
maxColumn = 40
===
given [A] =>
Seq[A] = foo
>>>
given [A] =>
Seq[A] = foo
<<< scala-3.6 given 2
maxColumn = 40
===
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
===
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
Original file line number Diff line number Diff line change
Expand Up @@ -7645,3 +7645,26 @@ lazy val onlyImplicitOrTypeParams = paramss.forall(
lazy val onlyImplicitOrTypeParams = paramss.forall(_.exists { sym =>
sym.isType || sym.is(Implicit) || sym.is(Given)
})
<<< scala-3.6 given 1
maxColumn = 40
===
given [A] =>
Seq[A] = foo
>>>
given [A] => Seq[A] = foo
<<< scala-3.6 given 2
maxColumn = 40
===
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
===
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
Original file line number Diff line number Diff line change
Expand Up @@ -7988,3 +7988,28 @@ lazy val onlyImplicitOrTypeParams = paramss.forall(
sym.isType || sym.is(Implicit) || sym.is(Given)
}
)
<<< scala-3.6 given 1
maxColumn = 40
===
given [A] =>
Seq[A] = foo
>>>
given [A] =>
Seq[A] = foo
<<< scala-3.6 given 2
maxColumn = 40
===
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
===
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
Original file line number Diff line number Diff line change
Expand Up @@ -8286,3 +8286,26 @@ lazy val onlyImplicitOrTypeParams = paramss.forall(
sym.isType || sym.is(Implicit) || sym.is(Given)
}
)
<<< scala-3.6 given 1
maxColumn = 40
===
given [A] =>
Seq[A] = foo
>>>
given [A] => Seq[A] = foo
<<< scala-3.6 given 2
maxColumn = 40
===
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
===
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
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, 1208582, "total explored")
assertEquals(explored, 1209508, "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
Loading