Skip to content

Commit

Permalink
FormatOps: extend SlbEnd to lbrace, colon, comment
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Oct 9, 2024
1 parent 702ece7 commit 7e14708
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ class FormatOps(
val nft = start.right match {
case t if t.end >= end => start
case _: T.EOF => start
case _: T.Comma | _: T.Semicolon | _: T.RightArrow | _: T.Equals => null
case _: T.Comma | _: T.Semicolon | _: T.RightArrow | _: T.Equals |
_: T.Interpolation.Start | _: T.Interpolation.SpliceEnd |
_: T.Interpolation.End | _: T.Interpolation.SpliceStart |
_: T.Interpolation.Part => null
case _ if start.hasBlankLine => start
case _ if !style.newlines.formatInfix && {
isInfixOp(start.rightOwner) ||
Expand All @@ -89,22 +92,6 @@ class FormatOps(
!style.newlines.isBeforeOpenParenCallSite
case t => !isJustBeforeTree(start)(t)
}) => null
case _: T.RightBracket if start.left.is[T.RightBracket] => null
case _: T.LeftBracket => null
case _: T.Dot => start.rightOwner match {
case _: Type.Select => null
case _: Term.Select
if start.noBreak &&
(style.newlines.getSelectChains eq Newlines.keep) => null
case _ => start
}
case _: T.Ident => start.leftOwner match {
case _: Type.Select => null
case _: Term.Select
if start.noBreak &&
(style.newlines.getSelectChains eq Newlines.keep) => null
case _ => start
}
case t: T.RightParen =>
if (start.left.is[T.LeftParen]) null
else {
Expand All @@ -120,12 +107,36 @@ class FormatOps(
) start
else null
}
case _: T.Comment =>
if (start.noBreak) {
val nft = next(start)
if (!start.left.is[T.LeftParen] || hasBreakBeforeNonComment(nft))
return nft // RETURN!!!
case _: T.LeftBrace
if start.left.is[T.RightParen] &&
start.leftOwner.is[Member.SyntaxValuesClause] &&
start.rightOwner.parent.exists {
case p: Tree.WithBody => p.body eq start.rightOwner
case _ => false
} => null
case _: T.RightBracket if start.left.is[T.RightBracket] => null
case _: T.LeftBracket => null
case _: T.Dot => start.rightOwner match {
case _: Type.Select => null
case _: Term.Select
if start.noBreak &&
(style.newlines.getSelectChains eq Newlines.keep) => null
case _ => start
}
case _: T.Ident => start.leftOwner match {
case _: Type.Select => null
case _: Term.Select
if start.noBreak &&
(style.newlines.getSelectChains eq Newlines.keep) => null
case _ => start
}
case _: T.Colon if {
!style.newlines.sometimesBeforeColonInMethodReturnType &&
colonDeclType(start.rightOwner).isDefined
} => tokens(start, 2) // can't break after colon either
case _: T.Comment if start.noBreak =>
val nft = nextNonCommentSameLineAfter(start)
if (!start.left.is[T.LeftParen] || nft.hasBreakOrEOF) return nft // RETURN!!!
start
case _ => start
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,14 @@ class Router(formatOps: FormatOps) {
*/
val afterClose = tokens(closeFt, 3)
val lastPart = afterClose.left.is[T.Interpolation.End]
val slbEnd = if (lastPart) afterClose.left else afterClose.right
val slbEnd = endOfSingleLineBlock(
if (lastPart) afterClose else next(afterClose),
)
Seq(spaceSplit.withSingleLine(slbEnd), newlineSplit(1))
}

case FormatToken(_, _: T.RightBrace, _) if isInterpolate(rightOwner) =>
case FormatToken(_, _: T.RightBrace, _)
if next(ft).right.is[T.Interpolation.SpliceEnd] =>
Seq(Split(Space(style.spaces.inInterpolatedStringCurlyBraces), 0))

// optional braces: block follows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,9 +909,6 @@ object TreeOps {
def xmlSpace(owner: Tree): Modification =
Space(!isTreeOrBlockParent(owner)(_.isAny[Term.Xml, Pat.Xml]))

def isInterpolate(tree: Tree): Boolean =
isTreeOrBlockParent(tree)(_.isAny[Term.Interpolate, Pat.Interpolate])

def isEmptyTree(tree: Tree): Boolean = tree match {
case t: Term.Block => t.stats.isEmpty
case t => t.tokens.isEmpty
Expand Down

0 comments on commit 7e14708

Please sign in to comment.