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

Add docstrings.forceBlankLineBefore, remove parameters deprecated before v3.0.0 #3081

Merged
merged 4 commits into from
Jan 26, 2022
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
73 changes: 32 additions & 41 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -1691,7 +1691,7 @@ else {

This parameter controls whether a newline is forced between the opening curly
brace and the parameters of a lambda or partial function. Added in 2.7.0,
replacing boolean `alwaysBeforeCurlyBraceLambdaParams`.
replacing boolean `alwaysBeforeCurlyBraceLambdaParams` (removed in 3.4.0).

```scala mdoc:defaults
newlines.beforeCurlyLambdaParams
Expand Down Expand Up @@ -2320,9 +2320,9 @@ When the appropriate `danglingParentheses` flag (e.g., `defnSite`) has been set,
this parameter can be used to limit contexts where dangling is applied
(currently, `class`, `trait`, `enum`, `extension` and `def` are supported).

```scala mdoc:defaults
danglingParentheses.exclude
```
For backwards compatibility, the default depends on whether
[Vertical Multiline](#vertical-multiline) mode is used. If it is, the default is
`[class, trait]`; otherwise, it's empty.

```scala mdoc:scalafmt
indent.defnSite = 2
Expand Down Expand Up @@ -3188,19 +3188,12 @@ def other(a: String, b: String)(c: String, d: String) = a + b + c

### `verticalMultiline.excludeDanglingParens`

> This parameter has been deprecated, please use
> [danglingParentheses.exclude](#danglingparenthesesexclude). Keep in mind,
> though, that the new parameter is empty by default while the old one isn't, so
> to use empty exclude list, one must set the old
> `verticalMultiline.excludeDanglingParens=[]`.

```scala mdoc:defaults
verticalMultiline.excludeDanglingParens
```
This parameter has been removed in 3.4.0, please use
[danglingParentheses.exclude](#danglingparenthesesexclude).

```scala mdoc:scalafmt
indent.defnSite = 2
verticalMultiline.excludeDanglingParens = [def]
danglingParentheses.exclude = [def]
verticalMultiline.atDefnSite = true
verticalMultiline.arityThreshold = 2
verticalMultiline.newlineAfterOpenParen = true
Expand Down Expand Up @@ -3557,6 +3550,31 @@ maxColumn = 30
val a = 1
```

### `docstrings.forceBlankLineBefore`

If true (default), always insert a blank line before docstrings.
If false, preserves blank line only if one exists before.

> Since v3.4.0. Replaced deprecated `optIn.forceBlankLineBeforeDocstring`.

```scala mdoc:scalafmt
docstrings.forceBlankLineBefore = true
---
object Stuff {
/** Some function */
def hello = ()
}
```

```scala mdoc:scalafmt
docstrings.forceBlankLineBefore = false
---
object Stuff {
/** Some function */
def hello = ()
}
```

## Disabling or customizing formatting

### For code block
Expand Down Expand Up @@ -4294,33 +4312,6 @@ maxColumn = 30

## Miscellaneous

### `optIn.forceBlankLineBeforeDocstring`

If true, always insert a blank line before docstrings;
If false, preserves blank line only if one exists before.

```scala mdoc:defaults
optIn.forceBlankLineBeforeDocstring
```

```scala mdoc:scalafmt
optIn.forceBlankLineBeforeDocstring = true
---
object Stuff {
/** Some function */
def hello = ()
}
```

```scala mdoc:scalafmt
optIn.forceBlankLineBeforeDocstring = false
---
object Stuff {
/** Some function */
def hello = ()
}
```

### `rewriteTokens`

Map of tokens to rewrite. For example, Map("⇒" -> "=>") will rewrite unicode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@ case class DanglingParentheses(
defnSite: Boolean,
ctrlSite: Boolean = true,
private[config] val tupleSite: Option[Boolean] = None,
exclude: List[DanglingParentheses.Exclude] = Nil
private val exclude: Option[List[DanglingParentheses.Exclude]] = None
) {
@inline def tupleOrCallSite(isTuple: Boolean) =
if (isTuple) tupleSite.getOrElse(callSite) else callSite

def getExclude(
isVerticalMultiline: Boolean
): Seq[DanglingParentheses.Exclude] =
exclude.getOrElse {
if (!isVerticalMultiline) Nil
else DanglingParentheses.Exclude.defaultVerticalMultiline
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

 that the new parameter is empty by default while the old one
contained `[class, trait]`.

Is this indeed the case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you mean, remove that phrase since we take care of the default internally? yeah...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to the other section, mentioned that the default depends on whether vertical multiline is used.

}

}

object DanglingParentheses {
Expand Down Expand Up @@ -44,9 +53,10 @@ object DanglingParentheses {
case object `def` extends Exclude
case object `given` extends Exclude

implicit val reader: ConfCodecEx[Exclude] =
ReaderUtil
.oneOf[Exclude](`class`, `trait`, `enum`, `extension`, `def`, `given`)
implicit val reader: ConfCodecEx[Exclude] = ReaderUtil
.oneOf[Exclude](`class`, `trait`, `enum`, `extension`, `def`, `given`)

val defaultVerticalMultiline = List(`class`, `trait`)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ case class Docstrings(
removeEmpty: Boolean = false,
wrap: Docstrings.Wrap = Docstrings.Wrap.yes,
private[config] val wrapMaxColumn: Option[Int] = None,
forceBlankLineBefore: Option[Boolean] = None,
blankFirstLine: Docstrings.BlankFirstLine = Docstrings.BlankFirstLine.no,
style: Docstrings.Style = Docstrings.SpaceAsterisk
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,6 @@ case class Newlines(
private[config] val beforeOpenParenDefnSite: Option[BeforeOpenParen] = None,
private[config] val beforeOpenParenCallSite: Option[BeforeOpenParen] = None,
penalizeSingleSelectMultiArgList: Boolean = true,
@annotation.DeprecatedName(
"alwaysBeforeCurlyBraceLambdaParams",
"Use newlines.beforeCurlyLambdaParams instead",
"2.7.0"
)
private val alwaysBeforeCurlyBraceLambdaParams: Boolean = false,
beforeCurlyLambdaParams: BeforeCurlyLambdaParams =
BeforeCurlyLambdaParams.never,
private val topLevelStatementBlankLines: Seq[TopStatBlanks] = Seq.empty,
Expand Down Expand Up @@ -267,8 +261,9 @@ case class Newlines(
lazy val avoidForSimpleOverflowSLC: Boolean =
avoidForSimpleOverflow.contains(AvoidForSimpleOverflow.slc)

lazy val alwaysBeforeCurlyLambdaParams = alwaysBeforeCurlyBraceLambdaParams ||
(beforeCurlyLambdaParams eq BeforeCurlyLambdaParams.always)
@inline
def alwaysBeforeCurlyLambdaParams =
beforeCurlyLambdaParams eq BeforeCurlyLambdaParams.always

lazy val getBeforeMultiline = beforeMultiline.getOrElse(source)
lazy val shouldForceBeforeMultilineAssign = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,28 +106,13 @@ case class OptIn(
selfAnnotationNewline: Boolean = true,
annotationNewlines: Boolean = true,
// Candidate to become default false at some point.
forceBlankLineBeforeDocstring: Boolean = true,
@annotation.DeprecatedName(
"blankLineBeforeDocstring",
"Use optIn.forceBlankLineBeforeDocstring instead",
"2.5.0"
"forceBlankLineBeforeDocstring",
"Use docstrings.forceBlankLineBefore instead",
"3.4.0"
)
blankLineBeforeDocstring: Boolean = false
) {

/** See https://github.com/scalameta/scalafmt/issues/1712
*
* Setting behavior and name were mirrored. After deprecation and right
* naming we need to:
* - if `forceBlankLineBeforeDocstring` (new name) has default value
* (true), fallback to `blankLineBeforeDocstring` (old config) which may
* be configured in .scalafmt.conf
* - if `forceBlankLineBeforeDocstring` configured to non-default value,
* don't look at the old name
*/
lazy val forceNewlineBeforeDocstringSummary: Boolean =
forceBlankLineBeforeDocstring && !blankLineBeforeDocstring
}
forceBlankLineBeforeDocstring: Boolean = true
)

object OptIn {
implicit lazy val surface: Surface[OptIn] = generic.deriveSurface
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ case class ScalafmtConfig(
indentYieldKeyword: Boolean = true,
@annotation.ExtraName("binPackImportSelectors")
importSelectors: ImportSelectors = ImportSelectors.noBinPack,
@annotation.DeprecatedName(
"unindentTopLevelOperators",
"Use indentOperator.topLevelOnly instead",
"2.7.0"
)
private val unindentTopLevelOperators: Boolean = false,
includeCurlyBraceInSelectChains: Boolean = true,
includeNoParensInSelectChains: Boolean = false,
assumeStandardLibraryStripMargin: Boolean = false,
Expand Down Expand Up @@ -238,9 +232,6 @@ case class ScalafmtConfig(
private[scalafmt] lazy val encloseSelectChains =
optIn.encloseClassicChains || newlines.source.ne(Newlines.classic)

private[scalafmt] def indentOperatorTopLevelOnly =
indentOperator.topLevelOnly && !unindentTopLevelOperators

private[scalafmt] lazy val docstringsWrapMaxColumn: Int =
docstrings.wrapMaxColumn.getOrElse(maxColumn)

Expand All @@ -260,6 +251,9 @@ case class ScalafmtConfig(
rewrite = RewriteSettings.default
)

lazy val forceNewlineBeforeDocstring: Boolean =
docstrings.forceBlankLineBefore
.getOrElse(optIn.forceBlankLineBeforeDocstring)
}

object ScalafmtConfig {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,7 @@ import metaconfig._
case class VerticalMultiline(
atDefnSite: Boolean = false,
arityThreshold: Int = 100,
@annotation.DeprecatedName(
"newlineBeforeImplicitKW",
"Use newlines.implicitParamListModifierForce=[before] instead",
"2.5.0"
)
newlineBeforeImplicitKW: Boolean = false,
@annotation.DeprecatedName(
"newlineAfterImplicitKW",
"Use newlines.implicitParamListModifierForce=[after] instead",
"2.5.0"
)
newlineAfterImplicitKW: Boolean = false,
newlineAfterOpenParen: Boolean = false,
@annotation.DeprecatedName(
"excludeDanglingParens",
"Use danglingParentheses.exclude instead",
"2.5.0"
)
excludeDanglingParens: List[DanglingParentheses.Exclude] = List(
DanglingParentheses.Exclude.`class`,
DanglingParentheses.Exclude.`trait`
)
newlineAfterOpenParen: Boolean = false
)

object VerticalMultiline {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ class FormatOps(
}
def isInfixTopLevelMatch(op: String, noindent: Boolean): Boolean = {
noindent == style.indentOperator.noindent(op) &&
noindent == isTopLevel.getOrElse(!style.indentOperatorTopLevelOnly)
noindent == isTopLevel.getOrElse(!style.indentOperator.topLevelOnly)
}
if (style.verticalAlignMultilineOperators)
!InfixApp.isAssignment(ft.meta.left.text)
Expand Down Expand Up @@ -1201,8 +1201,7 @@ class FormatOps(

val shouldAddNewline = {
if (right.is[soft.ImplicitOrUsing])
style.newlines.forceBeforeImplicitParamListModifier ||
style.verticalMultiline.newlineBeforeImplicitKW
style.newlines.forceBeforeImplicitParamListModifier
else
style.verticalMultiline.newlineAfterOpenParen && isDefinition
} || (mixedParams && prev(t).meta.leftOwner.is[CtorModifier])
Expand All @@ -1212,8 +1211,7 @@ class FormatOps(
.withIndent(indentParam, close2, ExpiresOn.Before)
)
case Decision(ftd @ FormatToken(soft.ImplicitOrUsing(), _, _), _)
if (style.newlines.forceAfterImplicitParamListModifier ||
style.verticalMultiline.newlineAfterImplicitKW) &&
if style.newlines.forceAfterImplicitParamListModifier &&
!tokens.isRightCommentThenBreak(ftd) =>
Seq(Split(Newline, 0))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object TokenOps {
def blankLineBeforeDocstring(
ft: FormatToken
)(implicit style: ScalafmtConfig): Boolean =
style.optIn.forceNewlineBeforeDocstringSummary &&
style.forceNewlineBeforeDocstring &&
isDocstring(ft.meta.right.text) &&
TreeOps
.findTreeOrParent(ft.meta.leftOwner) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -708,10 +708,7 @@ object TreeOps {
)(implicit style: ScalafmtConfig): Boolean =
!style.danglingParentheses.defnSite || {
val excludeList =
if (isVerticalMultiline && style.danglingParentheses.exclude.isEmpty)
style.verticalMultiline.excludeDanglingParens
else
style.danglingParentheses.exclude
style.danglingParentheses.getExclude(isVerticalMultiline)
excludeList.nonEmpty && {
val exclude = tree match {
case _: Ctor.Primary | _: Defn.Class =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
newlines.alwaysBeforeCurlyBraceLambdaParams = true
newlines.beforeCurlyLambdaParams = always
<<< newline before lambda params
lst.map { x =>
println(x)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
newlines.alwaysBeforeCurlyBraceLambdaParams = false
newlines.beforeCurlyLambdaParams = never
danglingParentheses.callSite = false
<<< 1: multi-arg multi-stmt lambda call
def f = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
newlines.alwaysBeforeCurlyBraceLambdaParams = false
newlines.beforeCurlyLambdaParams = never
danglingParentheses.callSite = true
<<< 1: multi-arg multi-stmt lambda call
def f = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
preset = default
<<< docstring does not force line break
optIn.forceBlankLineBeforeDocstring = false
docstrings.forceBlankLineBefore = false
===
object Stuff {
/** Some function */
Expand All @@ -12,7 +12,7 @@ object Stuff {
def hello = ()
}
<<< force blank line
optIn.forceBlankLineBeforeDocstring = true
docstrings.forceBlankLineBefore = true
===
object Stuff {
/** Some function */
Expand All @@ -24,8 +24,8 @@ object Stuff {
/** Some function */
def hello = ()
}
<<< support deprecated name with true
optIn.blankLineBeforeDocstring = true
<<< support deprecated name with false
optIn.forceBlankLineBeforeDocstring = false
===
object Stuff {
/** Some function */
Expand All @@ -36,8 +36,8 @@ object Stuff {
/** Some function */
def hello = ()
}
<<< support deprecated name with false
optIn.blankLineBeforeDocstring = false
<<< support deprecated name with true
optIn.forceBlankLineBeforeDocstring = true
===
object Stuff {
/** Some function */
Expand Down
2 changes: 1 addition & 1 deletion scalafmt-tests/src/test/resources/test/DynamicStyle.stat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
maxColumn = 100 # comment
preset = defaultWithAlign
unindentTopLevelOperators = true
indentOperator.topLevelOnly = false
indentOperator.exclude = ":\\+:"
align.tokens."+" = [foo]
danglingParentheses.preset = false
Expand Down
2 changes: 1 addition & 1 deletion scalafmt-tests/src/test/resources/test/OperatorSpray.stat
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// This is copied from UnindentTopLevelOperatorsOperatorSpray,
// but uses the default unindentTopLevelOperators = false
// Tests have been updated to match expected output
unindentTopLevelOperators = false
indentOperator.topLevelOnly = true
indentOperator.preset = spray
<<< Indent operators
object TestRoutes {
Expand Down
Loading