Skip to content

Commit

Permalink
Router: fix match/infix indent after select chain
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Oct 1, 2022
1 parent de280b2 commit c240a2b
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ class FormatOps(
}

@tailrec
private[FormatOps] def findEnclosingInfix(child: InfixApp): InfixApp = {
private[internal] def findEnclosingInfix(child: InfixApp): InfixApp = {
val childTree = child.all
if (isEnclosedInParens(childTree)) child
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1688,6 +1688,16 @@ class Router(formatOps: FormatOps) {
}
val prevSelect = findPrevSelect(thisSelect, enclosed)
val expire = tokens.getLastExceptParen(expireTree.tokens).left
val indentExpire =
if (enclosed && tokens.isEnclosedInParens(expireTree)) expire
else
expireTree.parent match {
case Some(p: Term.Match) if p.expr eq expireTree =>
getLastToken(p)
case Some(p: Term.ApplyInfix) if p.lhs eq expireTree =>
getLastToken(InfixSplits.findEnclosingInfix(InfixApp(p)).all)
case _ => expire
}
val indentLen = style.indent.main

def breakOnNextDot: Policy =
Expand All @@ -1714,8 +1724,9 @@ class Router(formatOps: FormatOps) {
val baseSplits = style.newlines.getSelectChains match {
case Newlines.classic =>
def getNlMod = {
val endSelect =
nextSelect.fold(expire)(x => getLastNonTrivialToken(x.qual))
val endSelect = nextSelect.fold(indentExpire)(x =>
getLastNonTrivialToken(x.qual)
)
val altIndent = Indent(-indentLen, endSelect, After)
NewlineT(alt = Some(ModExt(NoSplit).withIndent(altIndent)))
}
Expand Down Expand Up @@ -1841,7 +1852,7 @@ class Router(formatOps: FormatOps) {
}

// trigger indent only on the first newline
val indent = Indent(indentLen, expire, After)
val indent = Indent(indentLen, indentExpire, After)
val willBreak = nextNonCommentSameLine(tokens(t, 2)).right.is[T.Comment]
val splits = baseSplits.map { s =>
if (willBreak || s.isNL) s.withIndent(indent)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ case class InfixApp(lhs: Tree, op: Name, rhs: Seq[Tree], all: Tree) {

object InfixApp {

def apply(t: Type.ApplyInfix): InfixApp = InfixApp(t.lhs, t.op, Seq(t.rhs), t)
def apply(t: Term.ApplyInfix): InfixApp = InfixApp(t.lhs, t.op, t.args, t)
def apply(t: Pat.ExtractInfix): InfixApp = InfixApp(t.lhs, t.op, t.rhs, t)

def unapply(tree: Tree): Option[InfixApp] =
tree match {
case t: Type.ApplyInfix => Some(InfixApp(t.lhs, t.op, Seq(t.rhs), t))
case t: Term.ApplyInfix => Some(InfixApp(t.lhs, t.op, t.args, t))
case t: Pat.ExtractInfix => Some(InfixApp(t.lhs, t.op, t.rhs, t))
case t: Type.ApplyInfix => Some(apply(t))
case t: Term.ApplyInfix => Some(apply(t))
case t: Pat.ExtractInfix => Some(apply(t))
case _ => None
}

Expand Down
22 changes: 11 additions & 11 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -2635,9 +2635,9 @@ val a: Vector[Array[Double]] = b.c
val a: Vector[Array[Double]] = b.c
// Only handle first case, others will be fixed on the next pass.
.headOption.a match {
case None =>
case _ =>
}
case None =>
case _ =>
}
<<< #1888 check presence of comment, lack of break
maxColumn = 80
===
Expand Down Expand Up @@ -6022,10 +6022,10 @@ object A {
IO.delay {
ds // c1
.headOption match {
case Some(Row(value: Long)) =>
value
case _ => 0
}
case Some(Row(value: Long)) =>
value
case _ => 0
}
}
}
<<< #3327 infix
Expand All @@ -6045,9 +6045,9 @@ object A {
IO.delay {
ds // c1
.headOption infix {
case Some(Row(value: Long)) =>
value
case _ => 0
}
case Some(Row(value: Long)) =>
value
case _ => 0
}
}
}
26 changes: 13 additions & 13 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -818,8 +818,8 @@ object a {
object a {
(intercept[java.lang.IllegalStateException] { in.readObject })
.getMessage should ===(
"Trying to deserialize a serialized ActorRef without an ActorSystem in scope." +
" Use 'akka.serialization.Serialization.currentSystem.withValue(system) { ... }'")
"Trying to deserialize a serialized ActorRef without an ActorSystem in scope." +
" Use 'akka.serialization.Serialization.currentSystem.withValue(system) { ... }'")
}
<<< 3.14 enclosed, assignment, by itself
maxColumn = 80
Expand Down Expand Up @@ -2487,9 +2487,9 @@ val a: Vector[Array[Double]] = b.c
val a: Vector[Array[Double]] = b.c
// Only handle first case, others will be fixed on the next pass.
.headOption.a match {
case None =>
case _ =>
}
case None =>
case _ =>
}
<<< #1888 check presence of comment, lack of break
maxColumn = 80
===
Expand Down Expand Up @@ -5731,10 +5731,10 @@ object A {
IO.delay {
ds // c1
.headOption match {
case Some(Row(value: Long)) =>
value
case _ => 0
}
case Some(Row(value: Long)) =>
value
case _ => 0
}
}
}
<<< #3327 infix
Expand All @@ -5754,9 +5754,9 @@ object A {
IO.delay {
ds // c1
.headOption infix {
case Some(Row(value: Long)) =>
value
case _ => 0
}
case Some(Row(value: Long)) =>
value
case _ => 0
}
}
}
22 changes: 11 additions & 11 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -2631,9 +2631,9 @@ val a: Vector[Array[Double]] = b.c
// Only handle first case, others will be fixed on the next pass.
.headOption
.a match {
case None =>
case _ =>
}
case None =>
case _ =>
}
<<< #1888 check presence of comment, lack of break
maxColumn = 80
===
Expand Down Expand Up @@ -6028,10 +6028,10 @@ object A {
IO.delay {
ds // c1
.headOption match {
case Some(Row(value: Long)) =>
value
case _ => 0
}
case Some(Row(value: Long)) =>
value
case _ => 0
}
}
}
<<< #3327 infix
Expand All @@ -6051,9 +6051,9 @@ object A {
IO.delay {
ds // c1
.headOption infix {
case Some(Row(value: Long)) =>
value
case _ => 0
}
case Some(Row(value: Long)) =>
value
case _ => 0
}
}
}
26 changes: 13 additions & 13 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -2786,9 +2786,9 @@ val a: Vector[Array[Double]] =
b.c
// Only handle first case, others will be fixed on the next pass.
.headOption.a match {
case None =>
case _ =>
}
case None =>
case _ =>
}
<<< #1888 check presence of comment, lack of break
maxColumn = 80
===
Expand Down Expand Up @@ -6180,11 +6180,11 @@ object A {
IO.delay {
ds // c1
.headOption match {
case Some(Row(value: Long)) =>
value
case _ =>
0
}
case Some(Row(value: Long)) =>
value
case _ =>
0
}
}
}
<<< #3327 infix
Expand All @@ -6204,10 +6204,10 @@ object A {
IO.delay {
ds // c1
.headOption infix {
case Some(Row(value: Long)) =>
value
case _ =>
0
}
case Some(Row(value: Long)) =>
value
case _ =>
0
}
}
}
10 changes: 5 additions & 5 deletions scalafmt-tests/src/test/resources/unit/Apply.stat
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ jv.validated[AddScheduledQueryRequest]
>>>
jv.validated[AddScheduledQueryRequest]
.disjunction leftMap { err =>
badRequest(
"Request body %s is not a valid scheduling query request: %s"
.format(jv.renderCompact,
err.message))
}
badRequest(
"Request body %s is not a valid scheduling query request: %s"
.format(jv.renderCompact,
err.message))
}
<<< Init
new DenseMatrix(
data = from.data map { _.conjugate },
Expand Down

0 comments on commit c240a2b

Please sign in to comment.