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

Router: use spaces.afterColonInMatchPattern #4070

Merged
merged 2 commits into from
Jun 20, 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
44 changes: 44 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -4691,6 +4691,50 @@ foo ===(baz)
foo bar (baz)
```

### `spaces.afterColonInMatchPattern`

> Since v3.8.3.

```scala mdoc:defaults
spaces.afterColonInMatchPattern
```

This parameter controls whether to output a space between a colon and a type in
typed match patterns. It takes the following values:

- `Always`:

```scala mdoc:scalafmt
spaces.afterColonInMatchPattern = always
---
foo match {
case e: Etype | f: Ftype =>
case g: Gtype =>
}
```

- `Never`:

```scala mdoc:scalafmt
spaces.afterColonInMatchPattern = never
---
foo match {
case e: Etype | f: Ftype =>
case g: Gtype =>
}
```

- `NoAlternatives` (when there are no pipe-separated alternatives):

```scala mdoc:scalafmt
spaces.afterColonInMatchPattern = noAlternatives
---
foo match {
case e: Etype | f: Ftype =>
case g: Gtype =>
}
```

## Literals

> Since v2.5.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,22 @@ case class Spaces(
afterKeywordBeforeParen: Boolean = true,
inByNameTypes: Boolean = true,
afterSymbolicDefs: Boolean = false,
private val afterColonInMatchPattern: Spaces.AfterColonInMatchPattern =
Spaces.AfterColonInMatchPattern.Always,
) {
def isSpaceAfterKeyword(tokenAfter: Token): Boolean =
afterKeywordBeforeParen || !tokenAfter.is[Token.LeftParen]

def notAfterColon(owner: meta.Tree): Boolean = owner match {
case x: meta.Pat.Typed => afterColonInMatchPattern match {
case Spaces.AfterColonInMatchPattern.Never => true
case Spaces.AfterColonInMatchPattern.Always => false
case Spaces.AfterColonInMatchPattern.NoAlternatives => x.parent
.exists(_.is[meta.Pat.Alternative])
}
case _ => false
}

}

object Spaces {
Expand All @@ -84,6 +97,15 @@ object Spaces {
case object IfMultipleBounds extends BeforeContextBound
}

sealed abstract class AfterColonInMatchPattern
object AfterColonInMatchPattern {
implicit val codec: ConfCodecEx[AfterColonInMatchPattern] = ReaderUtil
.oneOf[AfterColonInMatchPattern](Always, Never, NoAlternatives)
case object Always extends AfterColonInMatchPattern
case object Never extends AfterColonInMatchPattern
case object NoAlternatives extends AfterColonInMatchPattern
}

sealed abstract class BeforeArgInParens {
def apply(name: => String): Boolean
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2366,6 +2366,8 @@ class Router(formatOps: FormatOps) {
case FormatToken(T.RightArrow(), _, _) if leftOwner.is[Type.ByName] =>
val mod = Space(style.spaces.inByNameTypes)
Seq(Split(mod, 0))
case FormatToken(_: T.Colon, _, _)
if style.spaces.notAfterColon(leftOwner) => Seq(Split(NoSplit, 0))
case FormatToken(_: T.Symbolic, _, _) => Seq(Split(Space, 0))
case tok =>
logger.debug(s"MISSING CASE: $tok")
Expand Down
36 changes: 36 additions & 0 deletions scalafmt-tests/src/test/resources/unit/Case.stat
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,39 @@ object a {
case e: Exception => bar; baz
}
}
<<< pattern alternatives, afterColonInMatchPattern = always
spaces.afterColonInMatchPattern = always
===
foo match {
case e: Etype | f: Ftype =>
case g: Gtype =>
}
>>>
foo match {
case e: Etype | f: Ftype =>
case g: Gtype =>
}
<<< pattern alternatives, afterColonInMatchPattern = never
spaces.afterColonInMatchPattern = never
===
foo match {
case e: Etype | f: Ftype =>
case g: Gtype =>
}
>>>
foo match {
case e:Etype | f:Ftype =>
case g:Gtype =>
}
<<< pattern alternatives, afterColonInMatchPattern = noAlternatives
spaces.afterColonInMatchPattern = noAlternatives
===
foo match {
case e: Etype | f: Ftype =>
case g: Gtype =>
}
>>>
foo match {
case e:Etype | f:Ftype =>
case g: Gtype =>
}
Loading