Skip to content

Commit

Permalink
FormatOps: split OneArgOneLineSplit into two
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Feb 18, 2020
1 parent 2f3e215 commit b4f6430
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -344,34 +344,54 @@ class FormatOps(val tree: Tree, val initStyle: ScalafmtConfig) {
}
}.getOrElse(tree.tokens.last)

def OneArgOneLineSplit(tok: FormatToken, noTrailingCommas: Boolean = false)(
implicit line: sourcecode.Line
@inline
def OneArgOneLineSplit(tok: FormatToken)(
implicit line: sourcecode.Line,
style: ScalafmtConfig
): Policy =
if (style.poorMansTrailingCommasInConfigStyle)
splitOneArgPerLineBeforeComma(tok)
else
splitOneArgPerLineAfterComma(tok)

def splitOneArgPerLineBeforeComma(tok: FormatToken)(
implicit line: sourcecode.Line,
style: ScalafmtConfig
): Policy = {
val owner = tok.meta.leftOwner
// TODO(olafur) clear queue between arguments, they are independent.
Policy(matching(tok.left)) {
case Decision(t @ FormatToken(_, _: T.Comma, _), splits)
if noTrailingCommas &&
!next(t).right.is[T.Comment] &&
owner == t.meta.rightOwner =>
splits.map {
case x if x.modification == NoSplit =>
x.copy(modification = Newline)
case x => x
if owner == t.meta.rightOwner && !next(t).right.is[T.Comment] =>
splits.map { x =>
if (x.modification != NoSplit) x else x.copy(modification = Newline)
}

case Decision(t @ FormatToken(_: T.Comma, right, _), splits)
if owner == t.meta.leftOwner &&
!right.is[T.LeftBrace] &&
// If comment is bound to comma, see unit/Comment.
(!right.is[T.Comment] || t.newlinesBetween != 0) =>
val isNewline = right.is[T.Comment]
splits.filter(_.modification.isNewline == isNewline)
}
}

def splitOneArgPerLineAfterComma(tok: FormatToken)(
implicit line: sourcecode.Line,
style: ScalafmtConfig
): Policy = {
val owner = tok.meta.leftOwner
// TODO(olafur) clear queue between arguments, they are independent.
Policy(matching(tok.left)) {
// Newline on every comma.
case Decision(t @ FormatToken(_: T.Comma, right, _), splits)
if owner == t.meta.leftOwner &&
// TODO(olafur) what the right { decides to be single line?
!right.is[T.LeftBrace] &&
// If comment is bound to comma, see unit/Comment.
(!right.is[T.Comment] || t.newlinesBetween != 0) =>
splits.filter { x =>
val isNewline = x.modification.isNewline
if (noTrailingCommas && !right.is[T.Comment]) !isNewline
else isNewline
}
splits.filter(_.modification.isNewline)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,13 +615,7 @@ class Router(formatOps: FormatOps) {
val isForcedBinPack = styleMap.forcedBinPack.contains(leftOwner)
val policy =
if (isForcedBinPack) Policy(close)(newlineBeforeClose)
else {
val oneArgOneLine = OneArgOneLineSplit(
formatToken,
noTrailingCommas = style.poorMansTrailingCommasInConfigStyle
)
oneArgOneLine.copy(f = oneArgOneLine.f.orElse(newlineBeforeClose))
}
else OneArgOneLineSplit(formatToken).orElse(newlineBeforeClose)
Seq(
Split(Newline, 0, policy = policy)
.withIndent(indent, close, Right)
Expand Down

0 comments on commit b4f6430

Please sign in to comment.