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

#1627 [25]: FormatOps bugfix: afterCurlyLambda=preserve for [un]fold #2100

Merged
merged 4 commits into from
Jul 20, 2020
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
21 changes: 18 additions & 3 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -886,13 +886,20 @@ else {

### `newlines.afterCurlyLambda`

This parameter controls handling of newlines after the arrow following the
parameters of a curly brace lambda or partial function, and whether a space
can be used for one-line formatting of the entire function body (if allowed
but the body doesn't fit, a break is always forced).

```scala mdoc:defaults
newlines.afterCurlyLambda
```

```scala mdoc:scalafmt
newlines.afterCurlyLambda = never
newlines.afterCurlyLambda = squash
---
// remove all blank lines if any
// one-line formatting is allowed
something.map { x =>


Expand All @@ -904,8 +911,11 @@ something.map { x => f(x) }
```

```scala mdoc:scalafmt
newlines.afterCurlyLambda = always
newlines.afterCurlyLambda = never
---
// remove all blank lines if any
// one-line formatting depends on newlines.source:
// yes for fold; no for unfold; otherwise, only if there was no break
something.map { x =>


Expand All @@ -919,6 +929,9 @@ something.map { x => f(x) }
```scala mdoc:scalafmt
newlines.afterCurlyLambda = preserve
---
// if blank lines are present, keep only one
// one-line formatting depends on newlines.source:
// yes for fold; no for unfold; otherwise, only if there was no break
something.map { x =>


Expand All @@ -930,8 +943,10 @@ something.map { x => f(x) }
```

```scala mdoc:scalafmt
newlines.afterCurlyLambda = squash
newlines.afterCurlyLambda = always
---
// ensure a single blank line
// one-line formatting is not allowed
something.map { x =>


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package org.scalafmt.config

import org.scalafmt.config.Newlines.AfterInfix
import org.scalafmt.config.Newlines._

import metaconfig._
import metaconfig.generic.Surface
Expand Down Expand Up @@ -140,30 +140,30 @@ import metaconfig.generic.Surface
* and would likely overflow even after a break
*/
case class Newlines(
source: Newlines.SourceHints = Newlines.classic,
source: SourceHints = Newlines.classic,
neverInResultType: Boolean = false,
neverBeforeJsNative: Boolean = false,
sometimesBeforeColonInMethodReturnType: Boolean = true,
penalizeSingleSelectMultiArgList: Boolean = true,
alwaysBeforeCurlyBraceLambdaParams: Boolean = false,
topLevelStatementsMinBreaks: Int = 1,
topLevelStatements: Seq[Newlines.BeforeAfter] = Seq.empty,
topLevelStatements: Seq[BeforeAfter] = Seq.empty,
@annotation.DeprecatedName(
"alwaysBeforeTopLevelStatements",
"Use newlines.topLevelStatements instead",
"2.5.0"
)
alwaysBeforeTopLevelStatements: Boolean = false,
afterCurlyLambda: NewlineCurlyLambda = NewlineCurlyLambda.never,
implicitParamListModifierForce: Seq[Newlines.BeforeAfter] = Seq.empty,
implicitParamListModifierPrefer: Option[Newlines.BeforeAfter] = None,
afterCurlyLambda: AfterCurlyLambdaParams = AfterCurlyLambdaParams.never,
implicitParamListModifierForce: Seq[BeforeAfter] = Seq.empty,
implicitParamListModifierPrefer: Option[BeforeAfter] = None,
alwaysBeforeElseAfterCurlyIf: Boolean = false,
alwaysBeforeMultilineDef: Boolean = true,
afterInfix: Option[AfterInfix] = None,
afterInfixBreakOnNested: Boolean = false,
afterInfixMaxCountPerExprForSome: Int = 10,
afterInfixMaxCountPerFile: Int = 500,
avoidForSimpleOverflow: Seq[Newlines.AvoidForSimpleOverflow] = Seq.empty,
avoidForSimpleOverflow: Seq[AvoidForSimpleOverflow] = Seq.empty,
avoidAfterYield: Boolean = true
) {
if (
Expand All @@ -177,14 +177,14 @@ case class Newlines(
}

val reader: ConfDecoder[Newlines] = generic.deriveDecoder(this).noTypos
if (source != Newlines.classic) Newlines.warnSourceIsExperimental
if (source != Newlines.classic) warnSourceIsExperimental

@inline
def sourceIs(hint: Newlines.SourceHints): Boolean =
def sourceIs(hint: SourceHints): Boolean =
hint eq source

@inline
def sourceIn(hints: Newlines.SourceHints*): Boolean =
def sourceIn(hints: SourceHints*): Boolean =
hints.contains(source)

val sourceIgnored: Boolean =
Expand All @@ -210,12 +210,12 @@ case class Newlines(
}

lazy val forceBeforeImplicitParamListModifier: Boolean =
implicitParamListModifierForce.contains(Newlines.before)
implicitParamListModifierForce.contains(before)
lazy val forceAfterImplicitParamListModifier: Boolean =
implicitParamListModifierForce.contains(Newlines.after)
implicitParamListModifierForce.contains(after)

private def preferBeforeImplicitParamListModifier: Boolean =
implicitParamListModifierPrefer.contains(Newlines.before)
implicitParamListModifierPrefer.contains(before)
lazy val notPreferAfterImplicitParamListModifier: Boolean =
implicitParamListModifierForce.nonEmpty ||
preferBeforeImplicitParamListModifier
Expand All @@ -226,15 +226,15 @@ case class Newlines(
!forceBeforeImplicitParamListModifier

lazy val forceBlankBeforeMultilineTopLevelStmt: Boolean =
topLevelStatements.contains(Newlines.before) ||
topLevelStatements.contains(before) ||
alwaysBeforeTopLevelStatements
lazy val forceBlankAfterMultilineTopLevelStmt: Boolean =
topLevelStatements.contains(Newlines.after)
topLevelStatements.contains(after)

lazy val avoidForSimpleOverflowPunct: Boolean =
avoidForSimpleOverflow.contains(Newlines.AvoidForSimpleOverflow.punct)
avoidForSimpleOverflow.contains(AvoidForSimpleOverflow.punct)
lazy val avoidForSimpleOverflowTooLong: Boolean =
avoidForSimpleOverflow.contains(Newlines.AvoidForSimpleOverflow.tooLong)
avoidForSimpleOverflow.contains(AvoidForSimpleOverflow.tooLong)
}

object Newlines {
Expand Down Expand Up @@ -284,17 +284,14 @@ object Newlines {
ReaderUtil.oneOf[AvoidForSimpleOverflow](punct, tooLong)
}

}

sealed abstract class NewlineCurlyLambda

object NewlineCurlyLambda {

case object preserve extends NewlineCurlyLambda
case object always extends NewlineCurlyLambda
case object never extends NewlineCurlyLambda
case object squash extends NewlineCurlyLambda
sealed abstract class AfterCurlyLambdaParams
object AfterCurlyLambdaParams {
case object preserve extends AfterCurlyLambdaParams
case object always extends AfterCurlyLambdaParams
case object never extends AfterCurlyLambdaParams
case object squash extends AfterCurlyLambdaParams
implicit val codec: ConfCodec[AfterCurlyLambdaParams] =
ReaderUtil.oneOf[AfterCurlyLambdaParams](preserve, always, never, squash)
}

implicit val newlineCurlyLambdaReader: ConfCodec[NewlineCurlyLambda] =
ReaderUtil.oneOf[NewlineCurlyLambda](preserve, always, never, squash)
}
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ case class ScalafmtConfig(
xmlLiterals: XmlLiterals = XmlLiterals(),
edition: Edition = Edition.Latest
) {
import Newlines._
val allErrors = new mutable.ArrayBuffer[String]
locally {
import ValidationOps._
implicit val errors = new mutable.ArrayBuffer[String]
if (newlines.sourceIgnored) {
addIf(newlines.afterCurlyLambda == NewlineCurlyLambda.preserve)
addIf(newlines.afterCurlyLambda == AfterCurlyLambdaParams.preserve)
addIf(optIn.configStyleArguments && align.openParenCallSite)
addIf(optIn.configStyleArguments && align.openParenDefnSite)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import java.{util => ju}

import org.scalafmt.CompatCollections.JavaConverters._
import org.scalafmt.Error.UnexpectedTree
import org.scalafmt.config.{
BinPack,
Comments,
NewlineCurlyLambda,
Newlines,
ScalafmtConfig
}
import org.scalafmt.config.{BinPack, Comments, Newlines, ScalafmtConfig}
import org.scalafmt.internal.Length.Num
import org.scalafmt.internal.Policy.NoPolicy
import org.scalafmt.util._
Expand Down Expand Up @@ -1221,17 +1215,22 @@ class FormatOps(
newlines: Int
)(implicit style: ScalafmtConfig): (Boolean, NewlineT) =
style.newlines.afterCurlyLambda match {
case NewlineCurlyLambda.squash => (true, Newline)
case NewlineCurlyLambda.never =>
case Newlines.AfterCurlyLambdaParams.squash => (true, Newline)
case Newlines.AfterCurlyLambdaParams.never =>
val space = style.newlines.source match {
case Newlines.fold => true
case Newlines.unfold => false
case _ => newlines == 0
}
(space, Newline)
case NewlineCurlyLambda.always => (false, Newline2x)
case NewlineCurlyLambda.preserve =>
(newlines == 0, if (newlines >= 2) Newline2x else Newline)
case Newlines.AfterCurlyLambdaParams.always => (false, Newline2x)
case Newlines.AfterCurlyLambdaParams.preserve =>
val space = style.newlines.source match {
case Newlines.fold => true
case Newlines.unfold => false
case _ => newlines == 0
}
(space, if (newlines >= 2) Newline2x else Newline)
}

def getNoSplit(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
package org.scalafmt.internal

import org.scalafmt.Error.UnexpectedTree
import org.scalafmt.config.{
ImportSelectors,
NewlineCurlyLambda,
Newlines,
ScalafmtConfig
}
import org.scalafmt.config.{ImportSelectors, Newlines, ScalafmtConfig}
import org.scalafmt.internal.ExpiresOn.{After, Before}
import org.scalafmt.internal.Length.{Num, StateColumn}
import org.scalafmt.internal.Policy.NoPolicy
Expand Down Expand Up @@ -373,7 +368,8 @@ class Router(formatOps: FormatOps) {
leftOwner.is[Template] || leftOwner.parent.exists(_.is[Template])

def noSquash =
style.newlines.afterCurlyLambda != NewlineCurlyLambda.squash
style.newlines.afterCurlyLambda ne
Newlines.AfterCurlyLambdaParams.squash

isCurlyLambda && (style.newlines.source match {
case Newlines.fold => false
Expand Down