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

Indents: ignore fewerBraces if indentation is 1 #3592

Merged
merged 1 commit into from
Jul 21, 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
1 change: 1 addition & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,7 @@ 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)).
> Also, `never` is implicitly forced if indentation width is less than 2.

```scala mdoc:scalafmt
runner.dialect = Scala3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ case class Indents(
private[config] val ctorSite: Option[Int] = None,
extraBeforeOpenParenDefnSite: Int = 0,
relativeToLhsLastLine: Seq[Indents.RelativeToLhs] = Nil,
fewerBraces: Indents.FewerBraces = Indents.FewerBraces.never,
private[config] val fewerBraces: Indents.FewerBraces =
Indents.FewerBraces.never,
@annotation.ExtraName("deriveSite")
extendSite: Int = 4,
withSiteRelativeToExtends: Int = 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ case class ScalafmtConfig(

def formatInfix(tree: => Tree): Boolean =
breakAfterInfix(tree) ne Newlines.AfterInfix.keep

def getFewerBraces(): Indents.FewerBraces =
if (indent.getSignificant < 2) Indents.FewerBraces.never
else indent.fewerBraces
}

object ScalafmtConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2103,7 +2103,7 @@ class FormatOps(
case x: Member.Apply => isSelect(x)
case x => x.is[Term.Select]
}
val ok = (style.indent.fewerBraces match {
val ok = (style.getFewerBraces() match {
case Indents.FewerBraces.never => true
case Indents.FewerBraces.always => false
case Indents.FewerBraces.beforeSelect =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1772,12 +1772,12 @@ class Router(formatOps: FormatOps) {
def getNlMod = {
val endSelect = nextDotIfSig.fold {
nextSelect.fold {
val ko = style.indent.fewerBraces ==
val ko = style.getFewerBraces() ==
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
val ok = style.getFewerBraces() == Indents.FewerBraces.never
if (ok) Some(nd.left) else None
}
val altIndent = endSelect.map(Indent(-indentLen, _, After))
Expand Down Expand Up @@ -1909,7 +1909,7 @@ class Router(formatOps: FormatOps) {
}

// trigger indent only on the first newline
val fbIndent = style.indent.fewerBraces != Indents.FewerBraces.never
val fbIndent = style.getFewerBraces() != Indents.FewerBraces.never
val noIndent = !fbIndent && checkFewerBraces(thisSelect.qual)
val nlIndent =
if (noIndent) Indent.Empty else Indent(indentLen, expire, After)
Expand All @@ -1921,7 +1921,7 @@ class Router(formatOps: FormatOps) {
baseSplits.map(_.withIndent(nlIndent).andFirstPolicyOpt(nlPolicy))
else {
val spcIndent = nextDotIfSig.fold {
val ok = style.indent.fewerBraces == Indents.FewerBraces.always &&
val ok = style.getFewerBraces() == Indents.FewerBraces.always &&
nextSelect.isEmpty && checkFewerBraces(expireTree)
if (ok) nlIndent else Indent.empty
} { x =>
Expand Down