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

RewriteScala3Settings: expand RemoveOptionalBraces #3813

Merged
merged 1 commit into from
Mar 5, 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
31 changes: 24 additions & 7 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -3100,7 +3100,7 @@ def f() = {
> formatter might need to be run twice.
>
> This rule cannot be used with `rewrite.scala3.insertEndMarkerMinLines` or
> `rewrite.scala3.removeOptionalBraces == oldSyntaxToo`.
> `rewrite.scala3.removeOptionalBraces.oldSyntaxToo == true`.

This rewrite in essence provides the opposite of what `RedundantBraces` achieves,
and somewhat similar to Scala3's end marker rewrite rules.
Expand Down Expand Up @@ -3678,16 +3678,33 @@ NB: You could control these rules individually by

### `rewrite.scala3.removeOptionalBraces`

If this flag is enabled,
If this section is enabled,
[optional braces](https://dotty.epfl.ch/docs/reference/other-new-features/indentation.html)
will be removed and significant indentation applied.

The flag takes the following values:
```scala mdoc:defaults
rewrite.scala3.removeOptionalBraces
```

The section contains the following settings (available since v3.8.1):

- `enabled`:
- if `false`, disables any rewriting, regardless of other
flags in this section
- if `true`, enables rewriting
- applies to expressions using the new control syntax
(or those which would rewritten to new syntax if
`rewrite.scala3.convertToNewSyntax` is set)
- other flags below might extend rewrites to other cases
- `oldSyntaxToo`
- if `true`, applies also to expressions using deprecated syntax

Prior to v3.8.1, `rewrite.scala3.removeOptionalBraces` was a flag which
took three possible values (with their equivalent current settings shown):

- `no`: disabled
- `yes`: applies to expressions using the new control syntax (or
`rewrite.scala3.convertToNewSyntax` is set)
- `oldSyntaxToo`: applies also to expressions using deprecated syntax
- `no`: `enabled = false`
- `yes`: `enabled = true`
- `oldSyntaxToo`: `enabled = true` and `oldSyntaxToo = true`

### `rewrite.scala3.insertEndMarkerMinLines`

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,33 @@ object RewriteScala3Settings {
case Conf.Bool(false) => default
}

sealed abstract class RemoveOptionalBraces
case class RemoveOptionalBraces(
enabled: Boolean = true,
oldSyntaxToo: Boolean = false
)

object RemoveOptionalBraces {

implicit val codec: ConfCodecEx[RemoveOptionalBraces] = ReaderUtil
.oneOfCustom[RemoveOptionalBraces](yes, no, oldSyntaxToo) {
case Conf.Bool(true) => Configured.Ok(yes)
case Conf.Bool(false) => Configured.Ok(no)
}
val yes = RemoveOptionalBraces()
val no = RemoveOptionalBraces(enabled = false)

case object no extends RemoveOptionalBraces
case object yes extends RemoveOptionalBraces
case object oldSyntaxToo extends RemoveOptionalBraces
implicit val surface: generic.Surface[RemoveOptionalBraces] =
generic.deriveSurface

implicit val encoder: ConfEncoder[RemoveOptionalBraces] =
generic.deriveEncoder[RemoveOptionalBraces]

implicit final val decoder: ConfDecoderEx[RemoveOptionalBraces] = {
val baseDecoder = generic.deriveDecoderEx[RemoveOptionalBraces](no)
(stateOpt, conf) =>
conf match {
case Conf.Bool(true) | Conf.Str("yes") => Configured.Ok(yes)
case Conf.Bool(false) | Conf.Str("no") => Configured.Ok(no)
case Conf.Str("oldSyntaxToo") =>
Configured.Ok(RemoveOptionalBraces(oldSyntaxToo = true))
case _ => baseDecoder.read(stateOpt, conf)
}
}
}

sealed abstract class EndMarkerLines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import scala.meta.Type
import scala.util.Try

import metaconfig._
import org.scalafmt.config.RewriteScala3Settings._
import org.scalafmt.rewrite.FormatTokensRewrite
import org.scalafmt.rewrite.RedundantBraces
import org.scalafmt.sysops.AbsoluteFile
Expand Down Expand Up @@ -453,7 +452,7 @@ object ScalafmtConfig {
if (rewrite.scala3.insertEndMarkerMinLines != 0)
addIf(rewrite.scala3.removeEndMarkerMaxLines >= rewrite.scala3.insertEndMarkerMinLines)
addIf(rewrite.insertBraces.minLines != 0 && rewrite.scala3.insertEndMarkerMinLines != 0)
addIf(rewrite.insertBraces.minLines != 0 && rewrite.scala3.removeOptionalBraces == RemoveOptionalBraces.oldSyntaxToo)
addIf(rewrite.insertBraces.minLines != 0 && rewrite.scala3.removeOptionalBraces.oldSyntaxToo)
if (rewrite.insertBraces.minLines != 0 && rewrite.rules.contains(RedundantBraces))
addIf(rewrite.insertBraces.minLines < rewrite.redundantBraces.maxBreaks)
addIf(align.beforeOpenParenDefnSite && !align.closeParenSite)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.scalafmt.rewrite
import scala.meta._
import scala.meta.tokens.Token

import org.scalafmt.config.RewriteScala3Settings
import org.scalafmt.config.ScalafmtConfig
import org.scalafmt.internal.FormatToken
import org.scalafmt.internal.FormatTokens
Expand All @@ -13,8 +12,7 @@ object RemoveScala3OptionalBraces extends FormatTokensRewrite.RuleFactory {

override def enabled(implicit style: ScalafmtConfig): Boolean =
style.dialect.allowSignificantIndentation &&
style.rewrite.scala3.removeOptionalBraces
.ne(RewriteScala3Settings.RemoveOptionalBraces.no)
style.rewrite.scala3.removeOptionalBraces.enabled

override def create(implicit ftoks: FormatTokens): FormatTokensRewrite.Rule =
new RemoveScala3OptionalBraces
Expand All @@ -28,8 +26,7 @@ private class RemoveScala3OptionalBraces(implicit val ftoks: FormatTokens)

private def allowOldSyntax(implicit style: ScalafmtConfig): Boolean =
ConvertToNewScala3Syntax.enabled ||
style.rewrite.scala3.removeOptionalBraces
.eq(RewriteScala3Settings.RemoveOptionalBraces.oldSyntaxToo)
style.rewrite.scala3.removeOptionalBraces.oldSyntaxToo

override def enabled(implicit style: ScalafmtConfig): Boolean =
RemoveScala3OptionalBraces.enabled
Expand Down
Loading