Skip to content

Commit

Permalink
Router: simplify forcing newline for source=keep
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 27, 2022
1 parent 7911a86 commit 7068db8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import scala.meta.Template
import scala.meta.Tree

import org.scalafmt.config.Newlines._
import org.scalafmt.internal.FormatToken
import org.scalafmt.util.TreeOps

import metaconfig._
Expand Down Expand Up @@ -216,6 +217,14 @@ case class Newlines(
@inline
def sourceIgnored: Boolean = source.ignoreSourceSplit

@inline
def keepBreak(newlines: => Int): Boolean =
source.eq(Newlines.keep) && newlines != 0

@inline
def keepBreak(ft: FormatToken): Boolean =
keepBreak(ft.newlinesBetween)

val breakAfterInfix: AfterInfix =
afterInfix.getOrElse {
source match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class Router(formatOps: FormatOps) {
}
style.newlines.inInterpolation match {
case Newlines.InInterpolation.avoid => Seq(spaceSplit)
case _ if newlines != 0 && style.newlines.source.eq(Newlines.keep) =>
case _ if style.newlines.keepBreak(newlines) =>
Seq(newlineSplit(0))
case Newlines.InInterpolation.oneline =>
/* sequence of tokens:
Expand Down Expand Up @@ -396,7 +396,7 @@ class Router(formatOps: FormatOps) {
.withIndent(style.indent.main, close, Before),
Split(Space, 0)
.onlyIf(lambdaNLOnly.contains(false) && lambdaPolicy != null)
.notIf(style.newlines.source.eq(Newlines.keep) && newlines != 0)
.notIf(style.newlines.keepBreak(newlines))
.withOptimalTokenOpt(lambdaArrow)
.withIndent(lambdaIndent, close, Before)
.withPolicy(lambdaPolicy)
Expand Down Expand Up @@ -962,7 +962,7 @@ class Router(formatOps: FormatOps) {

val noSplitMod =
if (
style.newlines.source.eq(Newlines.keep) && tok.hasBreak || {
style.newlines.keepBreak(tok) || {
if (!handleImplicit) onlyConfigStyle
else style.newlines.forceBeforeImplicitParamListModifier
}
Expand Down Expand Up @@ -1204,7 +1204,7 @@ class Router(formatOps: FormatOps) {
}
val rightIsComment = right.is[T.Comment]
val mustUseNL = onlyConfigStyle ||
newlines != 0 && style.newlines.source.eq(Newlines.keep) ||
style.newlines.keepBreak(newlines) ||
rightIsComment &&
(newlines != 0 || nextNonCommentSameLine(next(ft)).hasBreak)
val noSplitModification =
Expand Down Expand Up @@ -1247,7 +1247,7 @@ class Router(formatOps: FormatOps) {
mustUseConfigStyle(formatToken, !opensLiteralArgumentList)
val rightIsComment = right.is[T.Comment]
val nlOnly = mustDangleForTrailingCommas || onlyConfigStyle ||
style.newlines.source.eq(Newlines.keep) && newlines != 0 ||
style.newlines.keepBreak(newlines) ||
rightIsComment &&
(newlines != 0 || nextNonCommentSameLine(next(ft)).hasBreak)

Expand Down Expand Up @@ -1435,7 +1435,7 @@ class Router(formatOps: FormatOps) {
isCallSite(leftOwner) =>
val close = matching(open)
val binPackIsEnabled = !style.binPack.unsafeCallSite.isNever
val useSpace = newlines == 0 || style.newlines.source.ne(Newlines.keep)
val useSpace = !style.newlines.keepBreak(newlines)
val singleSplit =
if (!binPackIsEnabled) Split(Space.orNL(useSpace), 0)
else Split(Space, 0).onlyIf(useSpace).withSingleLine(close)
Expand Down Expand Up @@ -1532,8 +1532,7 @@ class Router(formatOps: FormatOps) {
s.map { x => if (x.isNL) x else x.switch(trigger, true) }
}
} else NoPolicy
val noSpace =
style.newlines.source.eq(Newlines.keep) && newlines != 0
val noSpace = style.newlines.keepBreak(newlines)
Seq(
Split(noSpace, 0)(Space)
.withSingleLine(endOfSingleLineBlock(optFT), noSyntaxNL = true),
Expand Down Expand Up @@ -1962,7 +1961,7 @@ class Router(formatOps: FormatOps) {
case FormatToken(_: T.KwWhile | _: T.KwFor, right, _) =>
def spaceMod = Space(style.spaces.isSpaceAfterKeyword(right))
def splitBase = {
val onlyNL = style.newlines.source.eq(Newlines.keep) && newlines != 0
val onlyNL = style.newlines.keepBreak(newlines)
Split(if (onlyNL) Newline else spaceMod, 0)
}
val split = (formatToken.meta.leftOwner match {
Expand Down Expand Up @@ -2375,7 +2374,7 @@ class Router(formatOps: FormatOps) {
else
Seq(Split(Space, 0), Split(Newline, 1))
case FormatToken(_, T.Ident("|"), _) if rightOwner.is[Pat.Alternative] =>
val noNL = style.newlines.source.ne(Newlines.keep) || newlines == 0
val noNL = !style.newlines.keepBreak(newlines)
Seq(Split(Space.orNL(noNL), 0))
case FormatToken(_, T.Ident("*"), _)
if rightOwner.is[Pat.SeqWildcard] ||
Expand Down Expand Up @@ -2517,7 +2516,7 @@ class Router(formatOps: FormatOps) {
Space(style.spaces.inParentheses && allowSpace)
}
val isNL = rightOwner.is[Pat.Alternative] &&
style.newlines.source.eq(Newlines.keep) && newlines != 0
style.newlines.keepBreak(newlines)
Seq(Split(if (isNL) Newline else modNoNL, 0))

case FormatToken(left, _: T.KwCatch | _: T.KwFinally, _)
Expand Down

0 comments on commit 7068db8

Please sign in to comment.