Skip to content

Commit

Permalink
Modification: add conditional ctor to Newline2x
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Apr 6, 2024
1 parent 76f14db commit 7ab6890
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1283,13 +1283,13 @@ class FormatOps(
(space, Newline)
case Newlines.AfterCurlyLambdaParams.always => (false, Newline2x)
case Newlines.AfterCurlyLambdaParams.preserve =>
val noBlanks = newlines < 2
val blanks = newlines >= 2
val space = style.newlines.source match {
case Newlines.fold => noBlanks
case Newlines.fold => !blanks
case Newlines.unfold => false
case _ => newlines == 0
}
(space, if (noBlanks) Newline else Newline2x)
(space, Newline2x(blanks))
}

def getNoSplit(ft: FormatToken, spaceOk: Boolean)(implicit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ case class NewlineT(
override val length: Int = 0
}

object Newline extends NewlineT {
def apply: NewlineT = NewlineT()
}
object Newline extends NewlineT

object Newline2x extends NewlineT(isDouble = true)
object Newline2x extends NewlineT(isDouble = true) {
def apply(isDouble: Boolean): NewlineT = if (isDouble) this else Newline
def apply(ft: FormatToken): NewlineT = apply(ft.hasBlankLine)
}

object NoIndentNewline extends NewlineT(noIndent = true)

Expand All @@ -65,5 +66,5 @@ object Space extends Modification {
def orNL(flag: Boolean): Modification = if (flag) this else Newline
def orNL(nl: Int): Modification =
if (FormatToken.noBreak(nl)) this
else NewlineT(isDouble = FormatToken.hasBlankLine(nl))
else Newline2x(FormatToken.hasBlankLine(nl))
}
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ class Router(formatOps: FormatOps) {
!isSelfAnnotationNL && rightIsComment && blankLineBeforeDocstring(
tok,
)
NewlineT(double)
Newline2x(double)
}

// lambdaNLOnly: None for single line only
Expand Down Expand Up @@ -501,7 +501,7 @@ class Router(formatOps: FormatOps) {
val bodyIsEmpty = isEmptyTree(body)
def baseSplit = Split(Space, 0)
def nlSplit(ft: FormatToken)(cost: Int)(implicit l: FileLine) =
Split(NewlineT(isDouble = ft.hasBlankLine && bodyIsEmpty), cost)
Split(Newline2x(isDouble = ft.hasBlankLine && bodyIsEmpty), cost)
CtrlBodySplits.checkComment(tok, nlSplit(tok)) { ft =>
def withSlbSplit(implicit l: FileLine) = Seq(
baseSplit.withSingleLine(getLastNonTrivialToken(body)),
Expand All @@ -527,7 +527,7 @@ class Router(formatOps: FormatOps) {
}
}
// New statement
case tok @ FormatToken(_: T.Semicolon, _, StartsStatementRight(stmt))
case FormatToken(_: T.Semicolon, _, StartsStatementRight(stmt))
if newlines == 0 =>
val spaceSplit =
if (style.newlines.source eq Newlines.unfold) Split.ignored
Expand All @@ -538,7 +538,7 @@ class Router(formatOps: FormatOps) {
Seq(
spaceSplit,
// For some reason, this newline cannot cost 1.
Split(NewlineT(isDouble = tok.hasBlankLine), 0),
Split(Newline2x(formatToken), 0),
)

case FormatToken(
Expand Down Expand Up @@ -573,10 +573,7 @@ class Router(formatOps: FormatOps) {
) Seq(Split(getMod(formatToken), 0))
else maybeGetInfixSplitsBeforeLhs(
formatToken,
Some(
if (left.is[T.Comment] && tok.noBreak) Space
else NewlineT(isDouble = tok.hasBlankLine),
),
Some(if (left.is[T.Comment] && tok.noBreak) Space else Newline2x(tok)),
) {
val spaceCouldBeOk = annoLeft &&
(style.newlines.source match {
Expand All @@ -592,14 +589,12 @@ class Router(formatOps: FormatOps) {
// This split needs to have an optimalAt field.
Split(Space, 0).onlyIf(spaceCouldBeOk).withSingleLine(expire),
// For some reason, this newline cannot cost 1.
Split(NewlineT(isDouble = tok.hasBlankLine), 0),
Split(Newline2x(tok), 0),
)
}

case FormatToken(_, T.RightBrace(), _) => Seq(
Split(xmlSpace(rightOwner), 0),
Split(NewlineT(isDouble = formatToken.hasBlankLine), 0),
)
case FormatToken(_, T.RightBrace(), _) =>
Seq(Split(xmlSpace(rightOwner), 0), Split(Newline2x(formatToken), 0))
case FormatToken(_: T.KwPackage, _, _) if leftOwner.is[Pkg] =>
Seq(Split(Space, 0))
// Opening [ with no leading space.
Expand Down Expand Up @@ -1425,7 +1420,7 @@ class Router(formatOps: FormatOps) {
// These are mostly filtered out/modified by policies.
case ft @ FormatToken(lc: T.Comma, _: T.Comment, m) =>
val nextFt = next(ft)
if (ft.hasBlankLine) Seq(Split(NewlineT(isDouble = true), 0))
if (ft.hasBlankLine) Seq(Split(Newline2x, 0))
else if (nextFt.hasBreak || m.right.hasNL)
Seq(Split(Space.orNL(newlines), 0))
else if (newlines == 0) {
Expand Down Expand Up @@ -2386,7 +2381,7 @@ class Router(formatOps: FormatOps) {
!left.is[T.RightBrace] || !(leftOwner.eq(rightOwner) ||
leftOwner.is[Term.Block] &&
leftOwner.parent.contains(rightOwner)) =>
Seq(Split(NewlineT(formatToken.hasBlankLine), 0))
Seq(Split(Newline2x(formatToken), 0))

case FormatToken(_, Reserved(), _) => Seq(Split(Space, 0))

Expand Down

0 comments on commit 7ab6890

Please sign in to comment.