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 c37a580 commit 38df6bd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 41 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,15 @@ class Router(formatOps: FormatOps) {
}
val prevSelect = findPrevSelect(thisSelect, enclosed)
val expire = tokens.getLastExceptParen(expireTree.tokens).left
val indentExpire = expireTree.parent match {
case _ if style.indentOperator.relativeToLhsFirstLine => expire
case _ if enclosed && tokens.isEnclosedInParens(expireTree) => expire
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 +1723,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 +1851,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
16 changes: 8 additions & 8 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -6024,10 +6024,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 @@ -6049,9 +6049,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
}
}
}
16 changes: 8 additions & 8 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5733,10 +5733,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 @@ -5758,9 +5758,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
}
}
}
16 changes: 8 additions & 8 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -6030,10 +6030,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 @@ -6055,9 +6055,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
}
}
}
20 changes: 10 additions & 10 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -6182,11 +6182,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 @@ -6208,10 +6208,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
}
}
}

0 comments on commit 38df6bd

Please sign in to comment.