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: keep then NL cost at 1 even if no space #4212

Merged
merged 3 commits into from
Sep 3, 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 @@ -899,9 +899,10 @@ class Router(formatOps: FormatOps) {

val nestedPenalty = 1 + nestedApplies(leftOwner) + lhsPenalty

val indent =
if (anyDefnSite) Num(style.indent.getDefnSite(leftOwner))
else Num(style.indent.callSite)
val indentLen =
if (anyDefnSite) style.indent.getDefnSite(leftOwner)
else style.indent.callSite
val indent = Indent(Num(indentLen), close, ExpiresOn.Before)

val isBeforeOpenParen =
if (defnSite) style.newlines.isBeforeOpenParenDefnSite
Expand Down Expand Up @@ -935,7 +936,6 @@ class Router(formatOps: FormatOps) {
else getNoSplitAfterOpening(ft, commentNL = null, spaceOk = !isBracket)

val rightIsComment = right.is[T.Comment]
val noSplitIndent = if (rightIsComment) indent else Num(0)

val align = !rightIsComment && clauseSiteFlags.alignOpenDelim &&
(!handleImplicit || style.newlines.forceAfterImplicitParamListModifier)
Expand All @@ -955,7 +955,7 @@ class Router(formatOps: FormatOps) {
decideNewlinesOnlyAfterToken(breakToken)
Seq(
Split(Newline, nestedPenalty + Constants.ExceedColumnPenalty)
.withPolicy(newlinePolicy).withIndent(indent, close, Before),
.withPolicy(newlinePolicy).withIndent(indent),
Split(NoSplit, nestedPenalty).withSingleLineNoOptimal(breakToken)
.andPolicy(newlinePolicy & newlineAfterAssignDecision),
)
Expand Down Expand Up @@ -992,7 +992,7 @@ class Router(formatOps: FormatOps) {
else insideBracesBlock(ft, close)

def singleLine(newlinePenalty: Int)(implicit fileLine: FileLine): Policy =
if (multipleArgs && (isBracket || excludeBlocks.ranges.isEmpty))
if (multipleArgs && (isBracket || excludeBlocks.isEmpty))
SingleLineBlock(close, noSyntaxNL = true)
else if (isBracket)
PenalizeAllNewlines(close, newlinePenalty, penalizeLambdas = false)
Expand Down Expand Up @@ -1030,8 +1030,7 @@ class Router(formatOps: FormatOps) {
else if (onlyConfigStyle) Seq(
Split(noSplitMod, 0).withPolicy(oneArgOneLine & implicitPolicy)
.withOptimalToken(right, killOnFail = true)
.withIndent(extraOneArgPerLineIndent)
.withIndent(indent, close, Before),
.withIndents(extraOneArgPerLineIndent, indent),
)
else {
val noSplitPolicy =
Expand All @@ -1046,18 +1045,18 @@ class Router(formatOps: FormatOps) {
)
else if (splitsForAssign.isDefined) singleLine(3)
else singleLine(10)
val okIndent = rightIsComment || handleImplicit
Seq(
Split(noSplitMod, 0, policy = noSplitPolicy)
.notIf(mustDangleForTrailingCommas).withOptimalToken(optimal)
.withIndent(noSplitIndent, close, Before),
.withIndent(indent, ignore = !okIndent),
Split(noSplitMod, (implicitPenalty + lhsPenalty) * bracketCoef)
.withPolicy(oneArgOneLine & implicitPolicy).onlyIf(
(notTooManyArgs && align) ||
(handleImplicit &&
style.newlines.notBeforeImplicitParamListModifier),
).withIndents(
if (align) getOpenParenAlignIndents(close)
else Seq(Indent(indent, close, ExpiresOn.Before)),
if (align) getOpenParenAlignIndents(close) else Seq(indent),
),
)
}
Expand Down Expand Up @@ -1085,7 +1084,7 @@ class Router(formatOps: FormatOps) {
.map(InfixSplits(_, ft).nlPolicy),
)
}
Seq(split.withIndent(indent, close, Before))
Seq(split.withIndent(indent))
}

splitsNoNL ++ splitsNL ++ splitsForAssign.getOrElse(Seq.empty)
Expand Down Expand Up @@ -2009,12 +2008,12 @@ class Router(formatOps: FormatOps) {
Split(Newline, 1),
)
case FormatToken(_, _: T.KwThen | _: T.KwDo, _) =>
if (style.newlines.sourceIgnored || noBreak()) Seq(
Split(Space, 0)
val okSpace = style.newlines.sourceIgnored || noBreak()
Seq(
Split(!okSpace, 0)(Space)
.withOptimalToken(nextNonCommentSameLineAfter(ft).left),
Split(Newline, 1),
)
else Seq(Split(Newline, 0))
// Last else branch
case FormatToken(_: T.KwElse, _, _) if (leftOwner match {
case t: Term.If => t.elsep match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ case class Split(
def withIndentOpt(indent: => Option[Indent]): Split =
withMod(modExt.withIndentOpt(indent))

def withIndents(indents: Indent*): Split = withMod(modExt.withIndents(indents))

def withIndents(indents: Seq[Indent], ignore: Boolean = false): Split =
withMod(modExt.withIndents(indents), ignore)

Expand Down
15 changes: 14 additions & 1 deletion scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,20 @@ extension [A](
def add2(b: A) = a + b

def add3(b: A) = a + b
<<< if(cond) indentation
<<< #4133 if cond overflow before then
object a:
if reducePattern(caseBindingMap, scrutineeSym.termRef, cdef.pat)(using gadtCtx) then {
// c1
}
>>>
object a:
if reducePattern(caseBindingMap, scrutineeSym.termRef, cdef.pat)(using
gadtCtx
)
then {
// c1
}
<<< if(cond) indentation
trait A:
val cond =
if (true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,20 @@ extension [A](
def add2(b: A) = a + b

def add3(b: A) = a + b
<<< if(cond) indentation
<<< #4133 if cond overflow before then
object a:
if reducePattern(caseBindingMap, scrutineeSym.termRef, cdef.pat)(using gadtCtx) then {
// c1
}
>>>
object a:
if reducePattern(caseBindingMap, scrutineeSym.termRef, cdef.pat)(using
gadtCtx
)
then {
// c1
}
<<< if(cond) indentation
trait A:
val cond =
if (true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,19 @@ extension [A](a: Map[
def add2(b: A) = a + b

def add3(b: A) = a + b
<<< if(cond) indentation
<<< #4133 if cond overflow before then
object a:
if reducePattern(caseBindingMap, scrutineeSym.termRef, cdef.pat)(using gadtCtx) then {
// c1
}
>>>
object a:
if reducePattern(caseBindingMap, scrutineeSym.termRef, cdef.pat)(using
gadtCtx)
then {
// c1
}
<<< if(cond) indentation
trait A:
val cond =
if (true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,20 @@ extension [A](
def add2(b: A) = a + b

def add3(b: A) = a + b
<<< if(cond) indentation
<<< #4133 if cond overflow before then
object a:
if reducePattern(caseBindingMap, scrutineeSym.termRef, cdef.pat)(using gadtCtx) then {
// c1
}
>>>
object a:
if reducePattern(caseBindingMap, scrutineeSym.termRef, cdef.pat)(using
gadtCtx
)
then {
// c1
}
<<< if(cond) indentation
trait A:
val cond =
if (true)
Expand Down
Loading