Skip to content

Commit

Permalink
RemoveScala3OptionalBraces: handle infix on rbrace
Browse files Browse the repository at this point in the history
Instead of handling a specific owner of the left brace (previously, it
was a Term.Match only), let's instead look for infix when we encounter
the right brace instead, and ensure that the next token is not the op
of an infix.

This also handles the case when Term.Match was not an LHS but in the
middle of an infix chain.
  • Loading branch information
kitbellew committed Jul 7, 2023
1 parent 87d16e4 commit f8539f2
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ private class RemoveScala3OptionalBraces(ftoks: FormatTokens)
} =>
removeToken
case _: Term.ForYield => removeToken
case t: Term.Match if t.parent.exists(_.isNot[Term.ApplyInfix]) =>
removeToken
case _: Term.Match => removeToken
case _: Type.Match => removeToken
case _: Term.Try => removeToken
case _: Ctor.Secondary
Expand All @@ -70,21 +69,30 @@ private class RemoveScala3OptionalBraces(ftoks: FormatTokens)
ft: FormatToken,
style: ScalafmtConfig
): Option[(Replacement, Replacement)] =
if (hasFormatOff) None // can't guarantee significant indentation
else
ft.right match {
case x: Token.RightBrace =>
ft.right match {
case _ if hasFormatOff => None // can't guarantee significant indentation
case x: Token.RightBrace =>
val nextFt = ftoks.nextNonComment(ftoks.next(ft))
val ok = nextFt.meta.rightOwner match {
case t: Term.Name =>
t.parent.exists {
case p: Term.Select => p.name ne t
case p: Term.ApplyInfix => p.op ne t
case _ => true
}
case _ => true
}
if (ok) {
val replacement = ft.meta.rightOwner match {
case _: Term.For
if allowOldSyntax &&
!ftoks.nextNonComment(ftoks.next(ft)).right.is[Token.KwDo] =>
if allowOldSyntax && !nextFt.right.is[Token.KwDo] =>
replaceToken("do")(new Token.KwDo(x.input, x.dialect, x.start))
case _ => removeToken
}

Some((left, replacement))
case _ => None
}
} else None
case _ => None
}

private def onLeftForBlock(
tree: Term.Block
Expand Down
34 changes: 16 additions & 18 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5021,10 +5021,10 @@ object a:
withEval.so(evalComment so { c =>
s"($c) "
}) +
this.match
case MateAdvice(seq, _, _, _) => seq.desc
case CpAdvice(judgment, _, _) => judgment.toString
+ "."
this.match {
case MateAdvice(seq, _, _, _) => seq.desc
case CpAdvice(judgment, _, _) => judgment.toString
} + "."
<<< match as infix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down Expand Up @@ -5075,15 +5075,13 @@ object a {
} mapFutureList pager.withChaptersAndLiking(me)
}
>>>
test does not parse
object a:
new AdapterLike[Study]:
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) =
api.search(query, From(offset), Size(length))
mapFutureList pager.withChaptersAndLiking(me)
^
new AdapterLike[Study] {
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) =
api.search(query, From(offset), Size(length))
} mapFutureList pager.withChaptersAndLiking(me)
<<< new anon as postfix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand All @@ -5096,12 +5094,12 @@ object a {
}
>>>
object a:
new AdapterLike[Study]:
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) =
api.search(query, From(offset), Size(length))
mapFutureList
new AdapterLike[Study] {
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) =
api.search(query, From(offset), Size(length))
} mapFutureList
<<< new anon as select lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down
34 changes: 16 additions & 18 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4803,10 +4803,10 @@ object a {
}
>>>
object a:
withEval.so(evalComment so { c => s"($c) " }) + this.match
case MateAdvice(seq, _, _, _) => seq.desc
case CpAdvice(judgment, _, _) => judgment.toString
+ "."
withEval.so(evalComment so { c => s"($c) " }) + this.match {
case MateAdvice(seq, _, _, _) => seq.desc
case CpAdvice(judgment, _, _) => judgment.toString
} + "."
<<< match as infix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down Expand Up @@ -4851,15 +4851,13 @@ object a {
} mapFutureList pager.withChaptersAndLiking(me)
}
>>>
test does not parse
object a:
new AdapterLike[Study]:
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
mapFutureList pager.withChaptersAndLiking(me)
^
new AdapterLike[Study] {
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
} mapFutureList pager.withChaptersAndLiking(me)
<<< new anon as postfix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand All @@ -4872,12 +4870,12 @@ object a {
}
>>>
object a:
new AdapterLike[Study]:
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
mapFutureList
new AdapterLike[Study] {
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
} mapFutureList
<<< new anon as select lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down
34 changes: 16 additions & 18 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5053,10 +5053,10 @@ object a:
withEval.so(evalComment so { c =>
s"($c) "
}) +
this.match
case MateAdvice(seq, _, _, _) => seq.desc
case CpAdvice(judgment, _, _) => judgment.toString
+ "."
this.match {
case MateAdvice(seq, _, _, _) => seq.desc
case CpAdvice(judgment, _, _) => judgment.toString
} + "."
<<< match as infix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down Expand Up @@ -5106,15 +5106,13 @@ object a {
} mapFutureList pager.withChaptersAndLiking(me)
}
>>>
test does not parse
object a:
new AdapterLike[Study]:
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) =
api.search(query, From(offset), Size(length))
mapFutureList pager.withChaptersAndLiking(me)
^
new AdapterLike[Study] {
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) =
api.search(query, From(offset), Size(length))
} mapFutureList pager.withChaptersAndLiking(me)
<<< new anon as postfix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand All @@ -5127,12 +5125,12 @@ object a {
}
>>>
object a:
new AdapterLike[Study]:
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) =
api.search(query, From(offset), Size(length))
mapFutureList
new AdapterLike[Study] {
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) =
api.search(query, From(offset), Size(length))
} mapFutureList
<<< new anon as select lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down
38 changes: 18 additions & 20 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5176,12 +5176,12 @@ object a:
s"($c) "
}
) +
this.match
case MateAdvice(seq, _, _, _) =>
seq.desc
case CpAdvice(judgment, _, _) =>
judgment.toString
+ "."
this.match {
case MateAdvice(seq, _, _, _) =>
seq.desc
case CpAdvice(judgment, _, _) =>
judgment.toString
} + "."
<<< match as infix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down Expand Up @@ -5236,15 +5236,13 @@ object a {
} mapFutureList pager.withChaptersAndLiking(me)
}
>>>
test does not parse
object a:
new AdapterLike[Study]:
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
mapFutureList pager.withChaptersAndLiking(me)
^
new AdapterLike[Study] {
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
} mapFutureList pager.withChaptersAndLiking(me)
<<< new anon as postfix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand All @@ -5257,12 +5255,12 @@ object a {
}
>>>
object a:
new AdapterLike[Study]:
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
mapFutureList
new AdapterLike[Study] {
def query = Query(text take 100, me.map(_.id))
def nbResults = api count query
def slice(offset: Int, length: Int) = api
.search(query, From(offset), Size(length))
} mapFutureList
<<< new anon as select lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down

0 comments on commit f8539f2

Please sign in to comment.