Skip to content

Commit

Permalink
FormatOps: outindent after fewer braces in infix
Browse files Browse the repository at this point in the history
When a fewer braces call is used before an infix operator, that operator
may not be indented as it will otherwise be interpreted as part of the
fewer-braces argument.

This fix leaves a few cases (notably, many for newlines.source=unfold)
still broken, but that requires a change to the parser which will be
released in the new version of scalameta.
  • Loading branch information
kitbellew committed Dec 30, 2023
1 parent db6f118 commit 11dd3a8
Show file tree
Hide file tree
Showing 4 changed files with 238 additions and 259 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,8 @@ class FormatOps(
case x => !isEnclosedInParens(x)
})
val afterInfix = style.breakAfterInfix(t)
if (afterInfix ne Newlines.AfterInfix.keep) {
if (isBeforeOp && endsWithFewerBraces(app.lhs)) Seq(Split(Newline, 0))
else if (afterInfix ne Newlines.AfterInfix.keep) {
if (isBeforeOp) Seq(Split(Space, 0))
else {
val spaceMod = Space(useSpace)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1019,4 +1019,16 @@ object TreeOps {
def isFewerBraces(tree: Term.Apply)(implicit dialect: Dialect): Boolean =
dialect.allowFewerBraces && tree.argClause.tokens.head.is[Colon]

@tailrec
def endsWithFewerBraces(tree: Tree)(implicit dialect: Dialect): Boolean =
tree match {
case t: Term.Apply => isFewerBraces(t)
case t: Term.ApplyInfix =>
t.argClause.values match {
case arg :: Nil => endsWithFewerBraces(arg)
case _ => false
}
case _ => false
}

}
Loading

0 comments on commit 11dd3a8

Please sign in to comment.