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

FormatWriter: keep braces if trailingCommas=Always #3856

Merged
merged 1 commit into from Mar 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -5,8 +5,8 @@ import java.util.regex.Pattern

import org.scalafmt.CompatCollections.JavaConverters._
import org.scalafmt.{Formatted, Scalafmt}
import org.scalafmt.config.{Comments, Docstrings, Newlines, ScalafmtConfig}
import org.scalafmt.config.{FormatEvent, RewriteScala3Settings}
import org.scalafmt.config.{Comments, Docstrings, FormatEvent, ScalafmtConfig}
import org.scalafmt.config.{Newlines, RewriteScala3Settings, TrailingCommas}
import org.scalafmt.rewrite.RedundantBraces
import org.scalafmt.util.TokenOps._
import org.scalafmt.util.{LiteralOps, TreeOps}
Expand Down Expand Up @@ -156,7 +156,6 @@ class FormatWriter(formatOps: FormatOps) {
while (0 <= idx) {
val loc = locations(idx)
val tok = loc.formatToken
val state = loc.state
tok.left match {
case rb: T.RightBrace => // look for "foo { bar }"
val ok = tok.meta.leftOwner match {
Expand All @@ -175,9 +174,14 @@ class FormatWriter(formatOps: FormatOps) {
lookup.update(beg.meta.idx, tok.meta.idx -> loc.leftLineId)
}
case _: T.LeftBrace =>
val state = loc.state
val style = loc.style
lookup.remove(idx).foreach {
case (end, endOffset) if endOffset == loc.leftLineId =>
val inParentheses = loc.style.spaces.inParentheses
case (end, endOffset)
if endOffset == loc.leftLineId &&
(style.rewrite.trailingCommas.allowFolding ||
style.getTrailingCommas != TrailingCommas.always) =>
val inParentheses = style.spaces.inParentheses
// remove space before "{"
val prevBegState =
if (0 == idx || (state.prev.split.modExt.mod ne Space))
Expand Down
Expand Up @@ -53,9 +53,4 @@ rewrite.redundantBraces.parensForOneLineApply = true
===
foo { bar }
>>>
Idempotency violated
=> Diff (- obtained, + expected)
-foo(
- bar,
-)
+foo(bar)
foo { bar }