diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index f60a4bdaf4..56856bd032 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -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 diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Modification.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Modification.scala index c5953bdac9..3bc5b92dfd 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Modification.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Modification.scala @@ -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) @@ -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)) } diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index cd72897cc2..32d1498f67 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -241,7 +241,7 @@ class Router(formatOps: FormatOps) { !isSelfAnnotationNL && rightIsComment && blankLineBeforeDocstring( tok, ) - NewlineT(double) + Newline2x(double) } // lambdaNLOnly: None for single line only @@ -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)), @@ -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 @@ -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( @@ -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 { @@ -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. @@ -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) { @@ -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))