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: handle parameter indent.fewerBraces #3557

Merged
merged 2 commits into from
Jun 16, 2023
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
76 changes: 76 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,82 @@ foo // c1
}
```

#### `indent.fewerBraces`

This parameter controls whether extra `indent.main` is added
to the sole argument of a method call using the "fewer braces"
syntax. The following values are supported:

- `always`: always applies extra indent
- `never`: doesn't apply any extra indent
- `beforeSelect`: applies extra indent only to fewer-braces
expressions followed by a `.select`

```scala mdoc:defaults
indent.fewerBraces
```

> In Scala 3.3.0, only `never` provides compiler-compatible code.
> Other options will work in 3.3.1-RC1 and later
> (see [Parser section](https://github.com/lampepfl/dotty/releases/tag/3.3.1-RC1)).

```scala mdoc:scalafmt
runner.dialect = Scala3
indent.significant = 3
indent.fewerBraces = always
---
bar:
2 + 2

foo.bar:
2 + 2

foo:
2 + 2
.bar:
3 + 3
.baz // c
.qux
```

```scala mdoc:scalafmt
runner.dialect = Scala3
indent.significant = 3
indent.fewerBraces = never
---
bar:
2 + 2

foo.bar:
2 + 2

foo:
2 + 2
.bar:
3 + 3
.baz // c
.qux
```

```scala mdoc:scalafmt
runner.dialect = Scala3
indent.significant = 3
indent.fewerBraces = beforeSelect
---
bar:
2 + 2

foo.bar:
2 + 2

foo:
2 + 2
.bar:
3 + 3
.baz // c
.qux
```

### Indent for `binPack.unsafeCallSite`

Normally, even when binpacking, there's a new level of indentation added for
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ case class Indents(
private[config] val ctorSite: Option[Int] = None,
extraBeforeOpenParenDefnSite: Int = 0,
relativeToLhsLastLine: Seq[Indents.RelativeToLhs] = Nil,
fewerBraces: Indents.FewerBraces = Indents.FewerBraces.never,
@annotation.ExtraName("deriveSite")
extendSite: Int = 4,
withSiteRelativeToExtends: Int = 0,
Expand Down Expand Up @@ -62,4 +63,15 @@ object Indents {
implicit val reader: ConfCodecEx[RelativeToLhs] = ReaderUtil
.oneOf[RelativeToLhs](`match`, `infix`)
}

sealed abstract class FewerBraces
object FewerBraces {
case object never extends FewerBraces
case object always extends FewerBraces
case object beforeSelect extends FewerBraces

implicit val reader: ConfCodecEx[FewerBraces] = ReaderUtil
.oneOf[FewerBraces](never, always, beforeSelect)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import org.scalafmt.Error.UnexpectedTree
import org.scalafmt.config.{
BinPack,
IndentOperator,
Indents,
Newlines,
ScalafmtConfig,
ScalafmtRunner,
Expand Down Expand Up @@ -2076,17 +2077,34 @@ class FormatOps(
.withSingleLine(opt)
.andPolicy(decideNewlinesOnlyAfterToken(opt))
}
val indent = t.parent match {
case Some(p: Term.Apply) =>
@tailrec
def isSelect(ma: Member.Apply): Boolean = ma.fun match {
case x: Member.Apply => isSelect(x)
case x => x.is[Term.Select]
}
val ok = (style.indent.fewerBraces match {
case Indents.FewerBraces.never => true
case Indents.FewerBraces.always => false
case Indents.FewerBraces.beforeSelect =>
!p.parent.exists(_.is[Term.Select])
}) || isSelect(p)
if (ok) None // select is taken care off elsewhere
else Some(style.indent.main + style.indent.getSignificant)
case _ => None
}
Some(new OptionalBracesRegion {
def owner = t.parent
def splits = Some(t.values match {
case (tf: Term.FunctionTerm) :: Nil
if !style.newlines.alwaysBeforeCurlyLambdaParams &&
t.parent.exists(_.is[Term.Apply]) =>
getSplits(ft, t, forceNL = false) match {
getSplits(ft, t, forceNL = false, indentOpt = indent) match {
case s +: rs if !s.isNL => funcSplit(tf)(s.fileLine) +: rs
case ss => ss
}
case _ => getSplits(ft, t, forceNL = true)
case _ => getSplits(ft, t, forceNL = true, indentOpt = indent)
})
def rightBrace = treeLast(t)
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.scalafmt.internal

import org.scalafmt.Error.UnexpectedTree
import org.scalafmt.config.{Align, BinPack}
import org.scalafmt.config.{Align, BinPack, Indents}
import org.scalafmt.config.{ImportSelectors, Newlines, ScalafmtConfig, Spaces}
import org.scalafmt.internal.ExpiresOn.{After, Before}
import org.scalafmt.internal.Length.{Num, StateColumn}
Expand Down Expand Up @@ -1770,11 +1770,18 @@ class Router(formatOps: FormatOps) {
val baseSplits = style.newlines.getSelectChains match {
case Newlines.classic =>
def getNlMod = {
val endSelect = nextSelect.fold(expire) { x =>
nextDotIfSig.fold(getLastNonTrivialToken(x.qual))(_.left)
val endSelect = nextDotIfSig.fold {
nextSelect.fold {
val ko = style.indent.fewerBraces ==
Indents.FewerBraces.always && checkFewerBraces(expireTree)
if (ko) None else Some(expire)
} { ns => Some(getLastNonTrivialToken(ns.qual)) }
} { nd =>
val ok = style.indent.fewerBraces == Indents.FewerBraces.never
if (ok) Some(nd.left) else None
}
val altIndent = Indent(-indentLen, endSelect, After)
NewlineT(alt = Some(ModExt(NoSplit).withIndent(altIndent)))
val altIndent = endSelect.map(Indent(-indentLen, _, After))
NewlineT(alt = Some(ModExt(NoSplit).withIndentOpt(altIndent)))
}

val prevChain = inSelectChain(prevSelect, thisSelect, expireTree)
Expand Down Expand Up @@ -1902,8 +1909,8 @@ class Router(formatOps: FormatOps) {
}

// trigger indent only on the first newline
val noIndent =
checkFewerBraces(thisSelect.qual)
val fbIndent = style.indent.fewerBraces != Indents.FewerBraces.never
val noIndent = !fbIndent && checkFewerBraces(thisSelect.qual)
val nlIndent =
if (noIndent) Indent.Empty else Indent(indentLen, expire, After)
val spcPolicy = delayedBreakPolicyOpt
Expand All @@ -1913,9 +1920,16 @@ class Router(formatOps: FormatOps) {
// will break
baseSplits.map(_.withIndent(nlIndent).andFirstPolicyOpt(nlPolicy))
else {
val spcIndent = nextDotIfSig.fold {
val ok = style.indent.fewerBraces == Indents.FewerBraces.always &&
nextSelect.isEmpty && checkFewerBraces(expireTree)
if (ok) nlIndent else Indent.empty
} { x =>
if (fbIndent) Indent(indentLen, x.left, Before) else Indent.Empty
}
baseSplits.map { s =>
if (s.isNL) s.withIndent(nlIndent).andFirstPolicyOpt(nlPolicy)
else s.andFirstPolicyOpt(spcPolicy)
else s.withIndent(spcIndent).andFirstPolicyOpt(spcPolicy)
}
}

Expand Down
Loading