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

State: don't delay penalty with pending spaces #4467

Merged
merged 2 commits into from
Oct 21, 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 @@ -402,17 +402,7 @@ class Router(formatOps: FormatOps) {
case _ => endOfSingleLineBlock(closeFT)
}
// copy logic from `( ...`, binpack=never, defining `slbSplit`
val excludeBlocks =
if (slbParensPolicy.isEmpty || style.newlines.unfold)
TokenRanges.empty
else leftOwner match {
case Tree.Block(t :: Nil)
if style.newlines.fold && isTreeEndingInArgumentClause(t) =>
parensTuple(t)
case _ => insideBracesBlock(ft, close)
}
Split(slbMod, 0)
.withSingleLine(expire, noSyntaxNL = true, exclude = excludeBlocks)
Split(slbMod, 0).withSingleLine(expire, noSyntaxNL = true)
.andPolicy(sldPolicy & slbParensPolicy)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,19 @@ final class State(

def noOverflowPenalties = (math.max(0, delayedPenalty), 0) // fits inside column

def overflowPenalties(column: Int) = {
def overflowPenalties(column: Int, noDelay: Boolean = false) = {
val defaultOverflowPenalty = Constants.ExceedColumnPenalty + column
if (style.newlines.avoidForSimpleOverflow.isEmpty)
(defaultOverflowPenalty, 0)
else getOverflowPenalty(nextSplit, defaultOverflowPenalty)
else getOverflowPenalty(nextSplit, defaultOverflowPenalty, noDelay)
}

val (penalty, nextDelayedPenalty) =
if (columnOnCurrentLine <= style.maxColumn) noOverflowPenalties
else if (nextPendingSpaces.nonEmpty) overflowPenalties(
columnOnCurrentLine + nextPendingSpaces.length,
noDelay = true,
)
else if (right.is[Token.Comment]) {
def trailing = nextTok.hasBreak // newline after comment
if (nextSplit.isNL) { // newline before comment
Expand Down Expand Up @@ -165,6 +169,7 @@ final class State(
private def getOverflowPenalty(
nextSplit: Split,
defaultOverflowPenalty: Int,
noDelay: Boolean = false,
)(implicit style: ScalafmtConfig, tokens: FormatTokens): (Int, Int) = {
val prevActive = delayedPenalty > 0
val fullPenalty = defaultOverflowPenalty +
Expand All @@ -177,7 +182,7 @@ final class State(
(penalty, if (nextActive) nextDelayedPenalty else -nextDelayedPenalty)
}
val ft = tokens(depth)
if (nextSplit.isNL || ft.right.is[Token.EOF])
if (noDelay || nextSplit.isNL || ft.right.is[Token.EOF])
result(if (prevActive) fullPenalty else defaultOverflowPenalty, false)
else {
val tokLength = ft.meta.right.text.length
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10642,6 +10642,6 @@ val newExprs = exprs.map { _.transform {
case a: AttributeReference => attrMap.getOrElse(a, a)
}}
>>>
val newExprs = exprs.map { _.transform { case a: AttributeReference =>
attrMap.getOrElse(a, a)
}}
val newExprs = exprs.map {
_.transform { case a: AttributeReference => attrMap.getOrElse(a, a) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -9944,6 +9944,6 @@ val newExprs = exprs.map { _.transform {
case a: AttributeReference => attrMap.getOrElse(a, a)
}}
>>>
val newExprs = exprs.map { _.transform { case a: AttributeReference =>
attrMap.getOrElse(a, a)
}}
val newExprs = exprs.map {
_.transform { case a: AttributeReference => attrMap.getOrElse(a, a) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10391,6 +10391,6 @@ val newExprs = exprs.map { _.transform {
case a: AttributeReference => attrMap.getOrElse(a, a)
}}
>>>
val newExprs = exprs.map { _.transform { case a: AttributeReference =>
attrMap.getOrElse(a, a)
}}
val newExprs = exprs.map {
_.transform { case a: AttributeReference => attrMap.getOrElse(a, a) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10772,6 +10772,6 @@ val newExprs = exprs.map { _.transform {
case a: AttributeReference => attrMap.getOrElse(a, a)
}}
>>>
val newExprs = exprs.map { _.transform { case a: AttributeReference =>
attrMap.getOrElse(a, a)
}}
val newExprs = exprs.map {
_.transform { case a: AttributeReference => attrMap.getOrElse(a, a) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -7378,3 +7378,35 @@ object a:
z <-
loremipsumdolorsitametconsecteturadipiscingelitseddoeiusmodtemporincididu
) yield x + y
<<< #4133 avoid braces-to-parens restrictions on break
runner.maxStateVisits = 1000
rewrite.rules = [RedundantBraces]
===
processTreeOpt(s) { s match
case td: TypeDef if !td.symbol.flags.is(Flags.Synthetic) && (!td.symbol.flags.is(Flags.Case) || !td.symbol.flags.is(Flags.Enum)) =>
Some(parseTypeDef(td, c))

case vd: ValDef if !isSyntheticField(vd.symbol) && (!vd.symbol.flags.is(Flags.Case) || !vd.symbol.flags.is(Flags.Enum)) =>
Some(parseValDef(c, vd))

case c: ClassDef if c.symbol.shouldDocumentClasslike =>
Some(parseClasslike(c))
}
>>>
processTreeOpt(s) {
s match
case td: TypeDef
if !td.symbol.flags.is(Flags.Synthetic) && (!td.symbol.flags.is(
Flags.Case
) || !td.symbol.flags.is(Flags.Enum)) =>
Some(parseTypeDef(td, c))

case vd: ValDef
if !isSyntheticField(vd.symbol) && (!vd.symbol.flags.is(
Flags.Case
) || !vd.symbol.flags.is(Flags.Enum)) =>
Some(parseValDef(c, vd))

case c: ClassDef if c.symbol.shouldDocumentClasslike =>
Some(parseClasslike(c))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7095,3 +7095,34 @@ object a:
y <- loremipsumdolorsitametconsecteturadipiscingelitseddoeiusmodtemporincididu;
z <- loremipsumdolorsitametconsecteturadipiscingelitseddoeiusmodtemporincididu
) yield x + y
<<< #4133 avoid braces-to-parens restrictions on break
runner.maxStateVisits = 1000
rewrite.rules = [RedundantBraces]
===
processTreeOpt(s) { s match
case td: TypeDef if !td.symbol.flags.is(Flags.Synthetic) && (!td.symbol.flags.is(Flags.Case) || !td.symbol.flags.is(Flags.Enum)) =>
Some(parseTypeDef(td, c))

case vd: ValDef if !isSyntheticField(vd.symbol) && (!vd.symbol.flags.is(Flags.Case) || !vd.symbol.flags.is(Flags.Enum)) =>
Some(parseValDef(c, vd))

case c: ClassDef if c.symbol.shouldDocumentClasslike =>
Some(parseClasslike(c))
}
>>>
processTreeOpt(s) {
s match
case td: TypeDef
if !td.symbol.flags.is(Flags.Synthetic) &&
(!td.symbol.flags.is(Flags.Case) ||
!td.symbol.flags.is(Flags.Enum)) => Some(parseTypeDef(td, c))

case vd: ValDef
if !isSyntheticField(vd.symbol) &&
(!vd.symbol.flags.is(Flags.Case) ||
!vd.symbol.flags.is(Flags.Enum)) => Some(parseValDef(c, vd))

case c: ClassDef if c.symbol.shouldDocumentClasslike =>
Some(parseClasslike(c))
}

Original file line number Diff line number Diff line change
Expand Up @@ -7407,3 +7407,35 @@ object a:
z <-
loremipsumdolorsitametconsecteturadipiscingelitseddoeiusmodtemporincididu
) yield x + y
<<< #4133 avoid braces-to-parens restrictions on break
runner.maxStateVisits = 1000
rewrite.rules = [RedundantBraces]
===
processTreeOpt(s) { s match
case td: TypeDef if !td.symbol.flags.is(Flags.Synthetic) && (!td.symbol.flags.is(Flags.Case) || !td.symbol.flags.is(Flags.Enum)) =>
Some(parseTypeDef(td, c))

case vd: ValDef if !isSyntheticField(vd.symbol) && (!vd.symbol.flags.is(Flags.Case) || !vd.symbol.flags.is(Flags.Enum)) =>
Some(parseValDef(c, vd))

case c: ClassDef if c.symbol.shouldDocumentClasslike =>
Some(parseClasslike(c))
}
>>>
processTreeOpt(s) {
s match
case td: TypeDef
if !td.symbol.flags.is(Flags.Synthetic) && (!td.symbol.flags.is(
Flags.Case
) || !td.symbol.flags.is(Flags.Enum)) =>
Some(parseTypeDef(td, c))

case vd: ValDef
if !isSyntheticField(vd.symbol) && (!vd.symbol.flags.is(
Flags.Case
) || !vd.symbol.flags.is(Flags.Enum)) =>
Some(parseValDef(c, vd))

case c: ClassDef if c.symbol.shouldDocumentClasslike =>
Some(parseClasslike(c))
}
Original file line number Diff line number Diff line change
Expand Up @@ -7670,3 +7670,35 @@ object a:
z <- loremipsumdolorsitametconsecteturadipiscingelitseddoeiusmodtemporincididu
)
yield x + y
<<< #4133 avoid braces-to-parens restrictions on break
runner.maxStateVisits = 1000
rewrite.rules = [RedundantBraces]
===
processTreeOpt(s) { s match
case td: TypeDef if !td.symbol.flags.is(Flags.Synthetic) && (!td.symbol.flags.is(Flags.Case) || !td.symbol.flags.is(Flags.Enum)) =>
Some(parseTypeDef(td, c))

case vd: ValDef if !isSyntheticField(vd.symbol) && (!vd.symbol.flags.is(Flags.Case) || !vd.symbol.flags.is(Flags.Enum)) =>
Some(parseValDef(c, vd))

case c: ClassDef if c.symbol.shouldDocumentClasslike =>
Some(parseClasslike(c))
}
>>>
processTreeOpt(s) {
s match
case td: TypeDef
if !td.symbol.flags.is(Flags.Synthetic) &&
(!td.symbol.flags.is(Flags.Case) ||
!td.symbol.flags.is(Flags.Enum)) =>
Some(parseTypeDef(td, c))

case vd: ValDef
if !isSyntheticField(vd.symbol) &&
(!vd.symbol.flags.is(Flags.Case) ||
!vd.symbol.flags.is(Flags.Enum)) =>
Some(parseValDef(c, vd))

case c: ClassDef if c.symbol.shouldDocumentClasslike =>
Some(parseClasslike(c))
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,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, 1499425, "total explored")
assertEquals(explored, 1501658, "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