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

RemoveScala3OptionalBraces: handle infix on rbrace #3576

Merged
merged 2 commits into from
Jul 7, 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 @@ -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
75 changes: 75 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5004,6 +5004,27 @@ object A:
new Foo("xyz"):
print("msg")
.bar()
<<< match inside infix chain, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
object a {
withEval.so(evalComment so { c =>
s"($c) "
}) +
this.match {
case MateAdvice(seq, _, _, _) => seq.desc
case CpAdvice(judgment, _, _) => judgment.toString
} + "."
}
>>>
object a:
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 @@ -5043,3 +5064,57 @@ def foo(url: String) =
.map { img =>
Html(s"""<img class="embed" src="$img" alt="$url"/>""")
}
<<< new anon as infix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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)
}
>>>
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 anon as postfix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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
}
>>>
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 anon as select lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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
}
>>>
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
72 changes: 72 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4789,6 +4789,24 @@ object A:
new Foo("xyz"):
print("msg")
.bar()
<<< match inside infix chain, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
object a {
withEval.so(evalComment so { c =>
s"($c) "
}) +
this.match {
case MateAdvice(seq, _, _, _) => seq.desc
case CpAdvice(judgment, _, _) => judgment.toString
} + "."
}
>>>
object a:
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 @@ -4822,3 +4840,57 @@ def foo(url: String) = url.match
case "foo" => Some("")
case _ => None
.map { img => Html(s"""<img class="embed" src="$img" alt="$url"/>""") }
<<< new anon as infix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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)
}
>>>
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 anon as postfix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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
}
>>>
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 anon as select lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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
}
>>>
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
75 changes: 75 additions & 0 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -5036,6 +5036,27 @@ object A:
new Foo("xyz"):
print("msg")
.bar()
<<< match inside infix chain, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
object a {
withEval.so(evalComment so { c =>
s"($c) "
}) +
this.match {
case MateAdvice(seq, _, _, _) => seq.desc
case CpAdvice(judgment, _, _) => judgment.toString
} + "."
}
>>>
object a:
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 @@ -5074,3 +5095,57 @@ def foo(url: String) =
.map { img =>
Html(s"""<img class="embed" src="$img" alt="$url"/>""")
}
<<< new anon as infix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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)
}
>>>
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 anon as postfix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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
}
>>>
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 anon as select lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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
}
>>>
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
Original file line number Diff line number Diff line change
Expand Up @@ -5157,6 +5157,31 @@ object A:
new Foo("xyz"):
print("msg")
.bar()
<<< match inside infix chain, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
object a {
withEval.so(evalComment so { c =>
s"($c) "
}) +
this.match {
case MateAdvice(seq, _, _, _) => seq.desc
case CpAdvice(judgment, _, _) => judgment.toString
} + "."
}
>>>
object a:
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 @@ -5200,3 +5225,57 @@ def foo(url: String) =
.map { img =>
Html(s"""<img class="embed" src="$img" alt="$url"/>""")
}
<<< new anon as infix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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)
}
>>>
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 anon as postfix lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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
}
>>>
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 anon as select lhs, with rewrite
rewrite.scala3.removeOptionalBraces = yes
===
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
}
>>>
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
Loading