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 111504377b..093f7c98bd 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 @@ -1,8 +1,7 @@ package org.scalafmt.rewrite -import org.scalafmt.config.ScalafmtConfig -import org.scalafmt.internal.FormatToken -import org.scalafmt.internal.FormatTokens +import org.scalafmt.config._ +import org.scalafmt.internal._ import org.scalafmt.util.TreeOps._ import scala.meta._ @@ -78,7 +77,7 @@ private class RemoveScala3OptionalBraces(implicit val ftoks: FormatTokens) session: Session, style: ScalafmtConfig, ): Option[(Replacement, Replacement)] = { - val nextFt = ftoks.nextNonComment(ftoks.next(ft)) + val nextFt = ftoks.nextNonCommentAfter(ft) val notOkToRewrite = hasFormatOff || // can't force significant indentation (nextFt.meta.rightOwner match { case t: Term.Name => t.parent.exists { @@ -88,6 +87,8 @@ private class RemoveScala3OptionalBraces(implicit val ftoks: FormatTokens) !t.tokens.head.isSymbolicInfixOperator case _ => false } + case _: Term.Select => nextFt.noBreak && + (style.newlines.getSelectChains eq Newlines.keep) case _ => false }) || (left.ft.right match { diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat index 484c3b2d32..7aec1ac460 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces.stat @@ -7234,3 +7234,48 @@ object a: ) /*<|<*/ { // foo } +<<< #4133 remove opt-braces to get fewer braces, with AvoidInfix +rewrite { + rules = [AvoidInfix] + scala3.removeOptionalBraces = yes +} +=== +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars { def inputStream() = is } slurp codec +} +>>> +object Streamable: + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +<<< #4133 remove opt-braces to get fewer braces, without AvoidInfix +rewrite { + scala3.removeOptionalBraces = yes +} +=== +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars { def inputStream() = is }.slurp(codec) +} +>>> +object Streamable: + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +<<< #4133 no need to remove opt-braces to get fewer braces +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +} +>>> +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +} diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat index 271114cbf4..fcd38de97d 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_fold.stat @@ -6960,3 +6960,45 @@ object a: ) /*<|<*/ { // foo } +<<< #4133 remove opt-braces to get fewer braces, with AvoidInfix +rewrite { + rules = [AvoidInfix] + scala3.removeOptionalBraces = yes +} +=== +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars { def inputStream() = is } slurp codec +} +>>> +object Streamable: + def slurp(is: => InputStream)(implicit codec: Codec): String = new Chars: + def inputStream() = is + .slurp(codec) +<<< #4133 remove opt-braces to get fewer braces, without AvoidInfix +rewrite { + scala3.removeOptionalBraces = yes +} +=== +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars { def inputStream() = is }.slurp(codec) +} +>>> +object Streamable: + def slurp(is: => InputStream)(implicit codec: Codec): String = new Chars: + def inputStream() = is + .slurp(codec) +<<< #4133 no need to remove opt-braces to get fewer braces +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +} +>>> +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = new Chars: + def inputStream() = is + .slurp(codec) +} diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat index eebf56ae87..22317dc148 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_keep.stat @@ -5871,9 +5871,9 @@ object A: }.bar() >>> object A: - new Foo("xyz"): - print("msg") - .bar() + new Foo("xyz") { + print("msg") + }.bar() <<< #3537 2 object A: new Foo("xyz"): @@ -5937,10 +5937,10 @@ def foo(url: String) = { } >>> def foo(url: String) = - url.match - case "foo" => Some("") - case _ => None - .map { img => + url.match { + case "foo" => Some("") + case _ => None + }.map { img => Html(s"""$url""") } <<< new anon as infix lhs, with rewrite @@ -5991,12 +5991,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 <<< #3623 beforeCurlyLambdaParams newlines.beforeCurlyLambdaParams = true === @@ -7276,3 +7276,44 @@ object a: ) /*<|<*/ { // foo } +<<< #4133 remove opt-braces to get fewer braces, with AvoidInfix +rewrite { + rules = [AvoidInfix] + scala3.removeOptionalBraces = yes +} +=== +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars { def inputStream() = is } slurp codec +} +>>> +object Streamable: + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars { def inputStream() = is }.slurp(codec) +<<< #4133 remove opt-braces to get fewer braces, without AvoidInfix +rewrite { + scala3.removeOptionalBraces = yes +} +=== +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars { def inputStream() = is }.slurp(codec) +} +>>> +object Streamable: + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars { def inputStream() = is }.slurp(codec) +<<< #4133 no need to remove opt-braces to get fewer braces +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +} +>>> +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +} diff --git a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat index bed8105605..43b738d189 100644 --- a/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat +++ b/scalafmt-tests/shared/src/test/resources/scala3/OptionalBraces_unfold.stat @@ -7525,3 +7525,48 @@ object a: ) /*<|<*/ { // foo } +<<< #4133 remove opt-braces to get fewer braces, with AvoidInfix +rewrite { + rules = [AvoidInfix] + scala3.removeOptionalBraces = yes +} +=== +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars { def inputStream() = is } slurp codec +} +>>> +object Streamable: + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +<<< #4133 remove opt-braces to get fewer braces, without AvoidInfix +rewrite { + scala3.removeOptionalBraces = yes +} +=== +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars { def inputStream() = is }.slurp(codec) +} +>>> +object Streamable: + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +<<< #4133 no need to remove opt-braces to get fewer braces +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +} +>>> +object Streamable { + def slurp(is: => InputStream)(implicit codec: Codec): String = + new Chars: + def inputStream() = is + .slurp(codec) +}