Skip to content

Commit

Permalink
Router: use indent.{binPackXxx,afterInfix}Site
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jun 17, 2024
1 parent 1683f8d commit a54770c
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ case class Indents(
case _: meta.Ctor => ctorSite
case _ => None
}).getOrElse(defnSite)

def getAfterInfixSite: Int = afterInfixSite.getOrElse(main)
def getBinPackCallSites: (Int, Int) =
(callSite, binPackCallSite.getOrElse(callSite))
def getBinPackDefnSites(tree: meta.Tree): (Int, Int) = {
val len = getDefnSite(tree)
(len, binPackDefnSite.getOrElse(len))
}
}

object Indents {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,12 +600,12 @@ class FormatOps(
else false
}

private val fullIndent: Indent = {
val expire = assignBodyExpire match {
case Some(x) if beforeLhs => x
case _ => fullExpire
}
Indent(Num(style.indent.main), expire, ExpiresOn.After)
private val fullIndent: Indent = assignBodyExpire match {
case Some(x) if beforeLhs =>
Indent(Num(style.indent.main), x, ExpiresOn.After)
case _ =>
val len = style.indent.getAfterInfixSite
Indent(Num(len), fullExpire, ExpiresOn.After)
}

val (nlIndent, nlPolicy) = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1070,11 +1070,6 @@ class Router(formatOps: FormatOps) {
implicit val clauseSiteFlags = ClauseSiteFlags.atDefnSite(leftOwner)

val isBracket = open.is[T.LeftBracket]
val indent = Indent(style.indent.getDefnSite(leftOwner), close, Before)
def noSplitIndents =
if (clauseSiteFlags.alignOpenDelim) getOpenParenAlignIndents(close)
else Seq(indent)

val bracketPenalty =
if (isBracket) Some(Constants.BracketPenalty) else None
val penalizeBrackets = bracketPenalty
Expand Down Expand Up @@ -1132,9 +1127,19 @@ class Router(formatOps: FormatOps) {
val nlOnelinePolicy = nextCommaOneline
.map(x => splitOneArgPerLineAfterCommaOnBreak(x.right))

val (indentLen, bpIndentLen) = style.indent
.getBinPackDefnSites(leftOwner)
val nlIndentLen =
if (nlCloseOnOpen eq NlClosedOnOpen.Cfg) indentLen else bpIndentLen

def noSplitIndents =
if (clauseSiteFlags.alignOpenDelim) getOpenParenAlignIndents(close)
else Seq(Indent(bpIndentLen, close, Before))

Seq(
noSplit.withIndents(noSplitIndents),
Split(nlMod, if (slbOrNL) 0 else nlCost).withIndent(indent)
Split(nlMod, if (slbOrNL) 0 else nlCost)
.withIndent(nlIndentLen, close, Before)
.withPolicy(nlPolicy & penalizeBrackets & nlOnelinePolicy),
)
}
Expand Down Expand Up @@ -1168,8 +1173,7 @@ class Router(formatOps: FormatOps) {
val afterFirstArgOneline =
if (oneline) firstArg.map(tokenAfter) else None

val indentLen = style.indent.callSite
val indent = Indent(Num(indentLen), close, Before)
val (indentLen, bpIndentLen) = style.indent.getBinPackCallSites

val exclude =
if (!isBracket) insideBracesBlock(ft, close)
Expand Down Expand Up @@ -1199,6 +1203,7 @@ class Router(formatOps: FormatOps) {
def noSingleArgIndents = oneline || singleArgAsInfix.isDefined ||
!style.binPack.indentCallSiteSingleArg ||
!isBracket && getAssignAtSingleArgCallSite(args).isDefined
val indent = Indent(bpIndentLen, close, Before)
val noSplitIndents =
if (isSingleArg && noSingleArgIndents) Nil
else if (style.binPack.indentCallSiteOnce) {
Expand Down Expand Up @@ -1228,7 +1233,7 @@ class Router(formatOps: FormatOps) {
penalizeNewlinesPolicy
val indentPolicy = Policy ? noSplitIndents.isEmpty || {
def unindentPolicy = Policy ? (isSingleArg || sjsOneline) &&
unindentAtExclude(exclude, Num(-indentLen))
unindentAtExclude(exclude, Num(-bpIndentLen))
def indentOncePolicy =
Policy ? style.binPack.indentCallSiteOnce && {
val trigger = getIndentTrigger(leftOwner)
Expand Down Expand Up @@ -1271,11 +1276,14 @@ class Router(formatOps: FormatOps) {
}
}

val nlIndentLen =
if (nlClosedOnOpenEffective eq NlClosedOnOpen.Cfg) indentLen
else bpIndentLen
val nlMod =
if (nlOnly && noBreak() && right.is[T.Comment]) Space
else NewlineT(alt = if (singleLineOnly) Some(NoSplit) else None)
val nlSplit = Split(nlMod, bracketPenalty * (if (oneline) 4 else 2))
.withIndent(indent)
.withIndent(nlIndentLen, close, Before)
.withSingleLineNoOptimal(close, ignore = !singleLineOnly).andPolicy(
Policy ? singleLineOnly || nlPolicy & penalizeNewlinesPolicy,
).andPolicy(singleArgAsInfix.map(InfixSplits(_, ft).nlPolicy))
Expand Down
4 changes: 2 additions & 2 deletions scalafmt-tests/src/test/resources/default/ApplyInfix.stat
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ object a {
foo =
bar + baz
foo +=
bar + baz
bar + baz
foo +
bar + baz
bar + baz
}
31 changes: 16 additions & 15 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -8225,14 +8225,14 @@ object Main {
10002 + 0
)
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar1 = foo1(
10000,
10001,
10002 + 0
)
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar2 = foo2(
0,
1,
Expand Down Expand Up @@ -8290,9 +8290,9 @@ object Main {
xs: X*
): Set[Int]
def foo1(x1: X, x2: X,
xs: X*): Set[Int]
xs: X*): Set[Int]
def foo1(x1: X, x2: X,
xs: X*): Set[Int]
xs: X*): Set[Int]
def foo2(
x1: X,
x2: X,
Expand Down Expand Up @@ -8338,15 +8338,16 @@ object Main {
>>>
object Main {
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar2 = foo2(0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10 + 0)
4, 5, 6, 7, 8, 9,
10 + 0)
}
<<< binPack.defnSite with !configStyle, !danglingParentheses, ind=4/6
newlines.configStyleDefnSite.prefer = false
Expand Down Expand Up @@ -8381,18 +8382,18 @@ object Main {
>>>
object Main {
def foo1(x1: X, x2: X,
xs: X*): Set[Int]
xs: X*): Set[Int]
def foo1(x1: X, x2: X,
xs: X*): Set[Int]
xs: X*): Set[Int]
def foo1(x1: X, x2: X,
xs: X*): Set[Int]
xs: X*): Set[Int]
def foo1(x1: X, x2: X,
xs: X*): Set[Int]
xs: X*): Set[Int]
def foo2(x1: X, x2: X,
x3: X, x4: X, xs: X*)
x3: X, x4: X, xs: X*)
: Set[Int]
def foo3(x1: X, x2: X,
x3: X, x4: X, xs: X*)
x3: X, x4: X, xs: X*)
: Set[Int]
}
<<< two statements separated by semicolon
Expand Down
39 changes: 20 additions & 19 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -7735,13 +7735,13 @@ object Main {
>>>
object Main {
val bar1 = foo1(
10000, 10001, 10002 + 0)
10000, 10001, 10002 + 0)
val bar1 = foo1(
10000, 10001, 10002 + 0)
10000, 10001, 10002 + 0)
val bar1 = foo1(
10000, 10001, 10002 + 0)
10000, 10001, 10002 + 0)
val bar1 = foo1(
10000, 10001, 10002 + 0)
10000, 10001, 10002 + 0)
val bar2 = foo2(
0,
1,
Expand Down Expand Up @@ -7789,16 +7789,16 @@ object Main {
>>>
object Main {
def foo1(
x1: X, x2: X, xs: X*)
x1: X, x2: X, xs: X*)
: Set[Int]
def foo1(
x1: X, x2: X, xs: X*)
x1: X, x2: X, xs: X*)
: Set[Int]
def foo1(
x1: X, x2: X, xs: X*)
x1: X, x2: X, xs: X*)
: Set[Int]
def foo1(
x1: X, x2: X, xs: X*)
x1: X, x2: X, xs: X*)
: Set[Int]
def foo2(
x1: X,
Expand Down Expand Up @@ -7845,15 +7845,16 @@ object Main {
>>>
object Main {
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar2 = foo2(0, 1, 2, 3,
4, 5, 6, 7, 8, 9, 10 + 0)
4, 5, 6, 7, 8, 9,
10 + 0)
}
<<< binPack.defnSite with !configStyle, !danglingParentheses, ind=4/6
newlines.configStyleDefnSite.prefer = false
Expand Down Expand Up @@ -7888,18 +7889,18 @@ object Main {
>>>
object Main {
def foo1(x1: X, x2: X,
xs: X*): Set[Int]
xs: X*): Set[Int]
def foo1(x1: X, x2: X,
xs: X*): Set[Int]
xs: X*): Set[Int]
def foo1(x1: X, x2: X,
xs: X*): Set[Int]
xs: X*): Set[Int]
def foo1(x1: X, x2: X,
xs: X*): Set[Int]
xs: X*): Set[Int]
def foo2(x1: X, x2: X,
x3: X, x4: X, xs: X*)
x3: X, x4: X, xs: X*)
: Set[Int]
def foo3(x1: X, x2: X,
x3: X, x4: X, xs: X*)
x3: X, x4: X, xs: X*)
: Set[Int]
}
<<< two statements separated by semicolon
Expand Down
52 changes: 26 additions & 26 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -8140,15 +8140,15 @@ object Main {
10002 + 0
)
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar1 = foo1(
10000,
10001,
10002 + 0
)
val bar1 = foo1(
10000,
10001, 10002 + 0)
10000,
10001, 10002 + 0)
val bar2 = foo2(
0,
1,
Expand Down Expand Up @@ -8206,10 +8206,10 @@ object Main {
xs: X*
): Set[Int]
def foo1(
x1: X,
x2: X, xs: X*): Set[Int]
x1: X,
x2: X, xs: X*): Set[Int]
def foo1(x1: X,
x2: X, xs: X*): Set[Int]
x2: X, xs: X*): Set[Int]
def foo2(
x1: X,
x2: X,
Expand All @@ -8231,7 +8231,7 @@ danglingParentheses.callSite = false
binPack.callSite = always
indent.callSite = 2
indent.binPackCallSite = 4
maxColumn = 30
maxColumn = 32
runner.optimizer.callSite { minSpan = 20, minCount = 5 }
===
object Main {
Expand All @@ -8255,21 +8255,21 @@ object Main {
>>>
object Main {
val bar1 = foo1(
10000,
10001, 10002 + 0
10000,
10001, 10002 + 0
)
val bar1 = foo1(10000,
10001, 10002 + 0)
10001, 10002 + 0)
val bar1 = foo1(10000,
10001, 10002 + 0
10001, 10002 + 0
)
val bar1 = foo1(
10000,
10001, 10002 + 0)
10000,
10001, 10002 + 0)
val bar2 = foo2(
0,
1, 2, 3,
4, 5, 6, 7, 8, 9, 10 + 0)
0,
1, 2, 3,
4, 5, 6, 7, 8, 9, 10 + 0)
}
<<< binPack.defnSite with !configStyle, !danglingParentheses, ind=4/6
newlines.configStyleDefnSite.prefer = false
Expand Down Expand Up @@ -8304,24 +8304,24 @@ object Main {
>>>
object Main {
def foo1(
x1: X,
x2: X, xs: X*
x1: X,
x2: X, xs: X*
): Set[Int]
def foo1(x1: X,
x2: X, xs: X*
x2: X, xs: X*
): Set[Int]
def foo1(
x1: X,
x2: X, xs: X*): Set[Int]
x1: X,
x2: X, xs: X*): Set[Int]
def foo1(x1: X,
x2: X, xs: X*): Set[Int]
x2: X, xs: X*): Set[Int]
def foo2(
x1: X, x2: X, x3: X,
x4: X, xs: X*
x1: X, x2: X, x3: X,
x4: X, xs: X*
): Set[Int]
def foo3(
x1: X, x2: X, x3: X,
x4: X, xs: X*): Set[Int]
x1: X, x2: X, x3: X,
x4: X, xs: X*): Set[Int]
}
<<< two statements separated by semicolon
foo match {
Expand Down
Loading

0 comments on commit a54770c

Please sign in to comment.