Skip to content

Commit

Permalink
DanglingParentheses: add bracketSite versions
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed May 10, 2024
1 parent f415a54 commit 55e479c
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 8 deletions.
37 changes: 37 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2700,6 +2700,43 @@ object a {
}
```

### `danglingParentheses.bracketXxxSite`

These control the bracketed type clauses in definitions and method calls and
by default are equal to their non-bracket counterparts.

```scala mdoc:scalafmt
danglingParentheses.defnSite = true
danglingParentheses.callSite = true
danglingParentheses.bracketDefnSite = false
danglingParentheses.bracketCallSite = false
maxColumn=25
---
object a {
// defnSite
def method[A <: Any, B]: Boolean

// callSite
method[String, SomeOtherType]
}
```

```scala mdoc:scalafmt
danglingParentheses.defnSite = false
danglingParentheses.callSite = false
danglingParentheses.bracketDefnSite = true
danglingParentheses.bracketCallSite = true
maxColumn=25
---
object a {
// defnSite
def method[A <: Any, B]: Boolean

// callSite
method[String, SomeOtherType]
}
```

### `danglingParentheses.ctrlSite`

> Since v2.5.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,29 @@ case class DanglingParentheses(
private[config] val defnSite: Boolean,
ctrlSite: Boolean = true,
private[config] val tupleSite: Option[Boolean] = None,
private[config] val bracketCallSite: Option[Boolean] = None,
private[config] val bracketDefnSite: Option[Boolean] = None,
private val exclude: Option[List[Exclude]] = None,
) {
@inline
def tupleOrCallSite(isTuple: Boolean) =
if (isTuple) tupleSite.getOrElse(callSite) else callSite
def atTupleSite: Boolean = tupleSite.getOrElse(callSite)

@inline
def atDefnSite(lpOwner: Tree): Boolean = defnSite && isExcluded(lpOwner)
def atBracketCallSite: Boolean = bracketCallSite.getOrElse(callSite)

def atCallSite(lpOwner: Tree): Boolean = lpOwner match {
case _: Member.Tuple => atTupleSite
case _: Type.ArgClause => atBracketCallSite
case _ => callSite
}

@inline
def atBracketDefnSite: Boolean = bracketDefnSite.getOrElse(defnSite)

@inline
def atDefnSite(lpOwner: Tree): Boolean =
(if (lpOwner.is[Type.ParamClause]) atBracketDefnSite else defnSite) &&
isExcluded(lpOwner)

@inline
def atVerticalMultilineSite(lpOwner: Tree): Boolean = defnSite &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2650,7 +2650,7 @@ class FormatOps(
if (dangleForTrailingCommas) ConfigStyle.None
else mustUseConfigStyle(ftAfterOpen, ftBeforeClose, !literalArgList)
val shouldDangle = style.danglingParentheses
.tupleOrCallSite(isTuple(ftAfterOpen.meta.leftOwner))
.atCallSite(ftAfterOpen.meta.leftOwner)
val scalaJsStyle = style.newlines.source == Newlines.classic &&
configStyle == ConfigStyle.None && !configStyleFlags.prefer &&
!literalArgList && shouldDangle
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -909,7 +909,7 @@ class Router(formatOps: FormatOps) {
mustDangleForTrailingCommas
val shouldDangle =
if (defnSite) style.danglingParentheses.atDefnSite(leftOwner)
else style.danglingParentheses.tupleOrCallSite(tupleSite)
else style.danglingParentheses.atCallSite(leftOwner)
val wouldDangle = shouldDangle ||
beforeClose.hasBreak && beforeClose.left.is[T.Comment]

Expand Down Expand Up @@ -2498,9 +2498,9 @@ class Router(formatOps: FormatOps) {
): Seq[Split] = {
def wouldDangle = ft.meta.leftOwner.parent.exists {
case p: Member.ParamClause => style.danglingParentheses.atDefnSite(p)
case _: Member.Tuple => style.danglingParentheses.tupleOrCallSite(true)
case p: Member.ArgClause => style.danglingParentheses
.tupleOrCallSite(false) &&
case _: Member.Tuple => style.danglingParentheses.atTupleSite
case _: Type.ArgClause => style.danglingParentheses.atBracketCallSite
case p: Member.ArgClause => style.danglingParentheses.callSite &&
(p.parent match {
case Some(_: Term.ApplyInfix) => style.newlines.formatInfix &&
p.values.lengthCompare(1) > 0
Expand Down

0 comments on commit 55e479c

Please sign in to comment.