Skip to content

Commit

Permalink
FormatOps: handle .match as select + apply
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 11, 2023
1 parent abb7516 commit 54fa563
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1518,14 +1518,23 @@ class FormatOps(
val thisTree = thisSelectLike.tree
val ok = thisTree.ne(lastApply) &&
!cannotStartSelectChainOnExpr(thisSelectLike.qual)
ok && (thisTree.parent match {
def checkParent = thisTree.parent match {
case `nextSelect` => style.includeNoParensInSelectChains
case Some(p: Term.Apply)
if tokens.getHead(p.argClause).left.is[T.LeftBrace] =>
style.includeCurlyBraceInSelectChains &&
!nextSelect.contains(lastApply) // exclude short curly
case Some(p: Member.Apply) => p.fun eq thisTree
case _ => false
}
ok && (thisTree match {
case _: Term.Match => // like select and apply in one
val hasBrace =
nextNonComment(tokens(thisSelectLike.nameToken)).right.is[T.LeftBrace]
!hasBrace ||
style.includeCurlyBraceInSelectChains &&
nextSelect.isDefined && !nextSelect.contains(lastApply)
case _ => checkParent
})
}

Expand Down
22 changes: 13 additions & 9 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -3713,7 +3713,8 @@ object a:
>>>
object a:
def f(): Unit =
List(1, 2, 3).match
List(1, 2, 3)
.match
case _ => a + 2
.foo: a =>
2 + a
Expand All @@ -3734,7 +3735,8 @@ object a:
>>>
object a:
def f(): Unit =
List(1, 2, 3).match
List(1, 2, 3)
.match
case _ => a + 2
.foo: a =>
2 + a
Expand Down Expand Up @@ -3945,18 +3947,19 @@ class test:
foo.match
case bar => ""
case baz => ""
<<< SKIP #3489 10
<<< #3489 10
class test:
foo.match
case bar => ""
case baz => ""
.qux
>>>
BestFirstSearch:326 Failed to format
UNABLE TO FORMAT,
tok=""∙.[63:67]
toks.length=17
deepestYet.length=14
class test:
foo
.match
case bar => ""
case baz => ""
.qux
<<< #3489 11
class test:
foo.match
Expand All @@ -3966,7 +3969,8 @@ class test:
3 + 3
>>>
class test:
foo.match
foo
.match
case bar => ""
case baz => ""
.qux:
Expand Down

0 comments on commit 54fa563

Please sign in to comment.