Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FormatOps: handle .match as select + apply #3500

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1517,14 +1517,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