Skip to content

Commit

Permalink
Modification: add {Space,NoSplit}.orNL helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Feb 24, 2020
1 parent a5263e0 commit 9143248
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -946,9 +946,7 @@ class FormatOps(val tree: Tree, val initStyle: ScalafmtConfig) {
(style.verticalMultiline.newlineAfterOpenParen && !isImplicitArgList && isDefinition) ||
mixedParamsWithCtorModifier

val mod =
if (shouldAddNewline) Newline
else NoSplit
val mod = NoSplit.orNL(!shouldAddNewline)

Seq(
Split(mod, 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ case class Provided(code: String) extends Modification {

case object NoSplit extends Modification {
override val newlines: Int = 0
def orNL(flag: Boolean): Modification = if (flag) this else Newline
}

/**
Expand Down Expand Up @@ -51,5 +52,6 @@ object Space extends Modification {
override val newlines: Int = 0
override def toString = "Space"

def apply(flag: Boolean): Modification = if (flag) Space else NoSplit
def apply(flag: Boolean): Modification = if (flag) this else NoSplit
def orNL(flag: Boolean): Modification = if (flag) this else Newline
}
Original file line number Diff line number Diff line change
Expand Up @@ -762,10 +762,7 @@ class Router(formatOps: FormatOps) {

val oneArgOneLine = OneArgOneLineSplit(formatToken)

val newlineMod: Modification =
if (newlines == 0 && isSingleLineComment(right)) Space
else if (right.is[T.LeftBrace]) NoSplit
else Newline
val newlineMod: Modification = NoSplit.orNL(right.is[T.LeftBrace])

val defnSite = isDefnSite(leftOwner)
val closeFormatToken = tokens(close)
Expand Down Expand Up @@ -957,11 +954,7 @@ class Router(formatOps: FormatOps) {
case _ if isInfix =>
Seq(
// Do whatever the user did if infix.
Split(
if (newlines > 0) Newline
else Space,
0
)
Split(Space.orNL(newlines == 0), 0)
)
case _ =>
val indent = leftOwner match {
Expand Down Expand Up @@ -1065,8 +1058,7 @@ class Router(formatOps: FormatOps) {
}

val mod: Modification =
if (isAttachedSingleLineComment(formatToken)) Space
else Newline
Space.orNL(isAttachedSingleLineComment(formatToken))

val exclude =
insideBlock(formatToken, expire, _.isInstanceOf[T.LeftBrace])
Expand Down Expand Up @@ -1122,10 +1114,9 @@ class Router(formatOps: FormatOps) {
val breakOnEveryDot = Policy(expire) {
case Decision(t @ FormatToken(_, _: T.Dot, _), _)
if chain.contains(t.meta.rightOwner) =>
val mod =
if (style.optIn.breaksInsideChains && t.newlinesBetween == 0)
NoSplit
else Newline
val mod = NoSplit.orNL(
style.optIn.breaksInsideChains && t.newlinesBetween == 0
)
Seq(Split(mod, 1))
}
val exclude = getExcludeIf(expire)
Expand Down Expand Up @@ -1310,10 +1301,8 @@ class Router(formatOps: FormatOps) {
}
// Inline comment attached to closing RightParen
val attachedComment = isAttachedSingleLineComment(formatToken)
val newlineModification: Modification =
if (attachedComment)
Space // Inline comment will force newline later.
else Newline
// Inline comment will force newline later
val newlineModification: Modification = Space.orNL(attachedComment)
val exclude =
insideBlock(formatToken, expire, _.is[T.LeftBrace]).map(parensRange)
Seq(
Expand All @@ -1326,7 +1315,7 @@ class Router(formatOps: FormatOps) {
val nlOnly = style.newlines.alwaysBeforeElseAfterCurlyIf ||
!leftOwner.is[Term.Block] || !leftOwner.parent.forall(_ == rightOwner)
Seq(
Split(if (nlOnly) Newline else Space, 0)
Split(Space.orNL(!nlOnly), 0)
)

case FormatToken(T.RightBrace(), T.KwYield(), _) =>
Expand Down

0 comments on commit 9143248

Please sign in to comment.