Skip to content

Commit

Permalink
FormatOps: fix leading infix before fewer braces
Browse files Browse the repository at this point in the history
Specifically, just like after fewer braces, we shouldn't indent leading
infix before fewer braces either.
  • Loading branch information
kitbellew committed Mar 27, 2024
1 parent bb5b96a commit c5400b6
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 27 deletions.
Expand Up @@ -508,7 +508,9 @@ class FormatOps(
val mod = getMod(ft)
val modOrNoSplit =
if (mod != Space || isBeforeOp || useSpace) mod else NoSplit
Seq(InfixSplits.withNLIndent(Split(modOrNoSplit, 0))(app, ft))
val split = Split(modOrNoSplit, 0)
if (isBeforeOp && isFewerBracesRhs(app.arg)) Seq(split)
else Seq(InfixSplits.withNLIndent(split)(app, ft))
}
}

Expand Down
Expand Up @@ -1083,6 +1083,17 @@ object TreeOps {
case _ => false
})

@tailrec
def isFewerBracesRhs(
tree: Tree
)(implicit dialect: Dialect, ftoks: FormatTokens): Boolean =
!ftoks.isEnclosedInMatching(tree) && (tree match {
case t: Term.Apply => isFewerBraces(t)
case t: Term.ApplyInfix => isFewerBracesRhs(t.lhs)
case Term.ArgClause(arg :: Nil, _) => isFewerBracesRhs(arg)
case _ => false
})

def isParentAnApply(t: Tree): Boolean =
t.parent.exists(_.is[Term.Apply])

Expand Down
25 changes: 12 additions & 13 deletions scalafmt-tests/src/test/resources/scala3/FewerBraces.stat
Expand Up @@ -2271,16 +2271,15 @@ object a:
x + 3
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
x + 2
- + mtd3 { x =>
- x + 1
- x + 2
- x + 3
- }
+ + mtd3 { x =>
+ x + 1
+ x + 2
+ x + 3
+ }
object a:
mtd1 { x =>
x + 1
}
+ mtd2: x =>
x + 1
x + 2
+ mtd3 { x =>
x + 1
x + 2
x + 3
}
25 changes: 12 additions & 13 deletions scalafmt-tests/src/test/resources/scala3/FewerBraces_keep.stat
Expand Up @@ -2248,16 +2248,15 @@ object a:
x + 3
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
x + 2
- + mtd3 { x =>
- x + 1
- x + 2
- x + 3
- }
+ + mtd3 { x =>
+ x + 1
+ x + 2
+ x + 3
+ }
object a:
mtd1 { x =>
x + 1
}
+ mtd2: x =>
x + 1
x + 2
+ mtd3 { x =>
x + 1
x + 2
x + 3
}

0 comments on commit c5400b6

Please sign in to comment.