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: not with no-NL select #4352

Merged
merged 2 commits into from
Sep 27, 2024
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
@@ -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._
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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"""<img class="embed" src="$img" alt="$url"/>""")
}
<<< new anon as infix lhs, with rewrite
Expand Down Expand Up @@ -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
===
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Loading