From f8539f243743ace2e9ed6afcd521ed1e6436b10a Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Thu, 6 Jul 2023 07:31:21 -0700 Subject: [PATCH] RemoveScala3OptionalBraces: handle infix on rbrace 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. --- .../rewrite/RemoveScala3OptionalBraces.scala | 30 +++++++++------ .../test/resources/scala3/OptionalBraces.stat | 34 ++++++++--------- .../resources/scala3/OptionalBraces_fold.stat | 34 ++++++++--------- .../resources/scala3/OptionalBraces_keep.stat | 34 ++++++++--------- .../scala3/OptionalBraces_unfold.stat | 38 +++++++++---------- 5 files changed, 85 insertions(+), 85 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala index 46823a3888..32389590f5 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/rewrite/RemoveScala3OptionalBraces.scala @@ -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 @@ -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 diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat index f745fb654d..401f415f75 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat @@ -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 === @@ -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 === @@ -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 === diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat index 3958819ae7..7a96077eeb 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat @@ -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 === @@ -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 === @@ -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 === diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat index ac507f8baa..0406f95d7d 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat @@ -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 === @@ -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 === @@ -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 === diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat index 112199687a..7496a30b34 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat @@ -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 === @@ -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 === @@ -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 ===