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: implement binPack.indentCallSiteSingleArg #3352

Merged
merged 2 commits into from
Oct 28, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
54 changes: 54 additions & 0 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,60 @@ maxColumn = 20
foo(bar1(baz1(qux1, qux2), baz2), bar2(baz3, baz4))
```

#### `binPack.indentCallSiteSingleArg`

When this parameter is disabled, no indentation is added for same-line single-arg
cases; the assumption is that if the argument expression spans multiple lines,
it will introduce its own indentation.

```scala mdoc:defaults
binPack.indentCallSiteSingleArg
```

With the parameter enabled:

```scala mdoc:scalafmt
binPack.unsafeCallSite = true
binPack.indentCallSiteSingleArg = true
indent.callSite = 2
maxColumn = 20
---
foo(bar(baz.qux(xyz + zyx)))
foo(bar((_, _) =>
baz { qux =>
noop
} baz { qux =>
noop
} baz //
{ qux =>
noop
} baz { qux =>
noop
}))
```

With the parameter disabled:

```scala mdoc:scalafmt
binPack.unsafeCallSite = true
binPack.indentCallSiteSingleArg = false
indent.callSite = 2
maxColumn = 20
---
foo(bar(baz.qux(xyz + zyx)))
foo(bar((_, _) =>
baz { qux =>
noop
} baz { qux =>
noop
} baz //
{ qux =>
noop
} baz { qux =>
noop
}))
```

### `indentOperator`

Normally, the first eligible break _inside_ a chain of infix operators is
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ case class BinPack(
private val bracketCallSite: Option[BinPack.Unsafe] = None,
private val bracketDefnSite: Option[BinPack.Unsafe] = None,
indentCallSiteOnce: Boolean = false,
indentCallSiteSingleArg: Boolean = true,
parentConstructors: BinPack.ParentCtors = BinPack.ParentCtors.source,
literalArgumentLists: Boolean = true,
literalsIncludeSimpleExpr: Boolean = false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,7 @@ class Router(formatOps: FormatOps) {
!isBracket && getAssignAtSingleArgCallSite(leftOwner).isDefined
val noSplitIndents =
if (noNoSplitIndents) Nil
else if (isSingleArg && !style.binPack.indentCallSiteSingleArg) Nil
else if (style.binPack.indentCallSiteOnce) {
@tailrec
def iter(tree: Tree): Option[T] = tree.parent match {
Expand Down Expand Up @@ -1349,7 +1350,7 @@ class Router(formatOps: FormatOps) {
SingleLineBlock(slbEnd, noSyntaxNL = true)
}
} else penalizeNewlinesPolicy
val indentOncePolicy =
def indentOncePolicy =
if (style.binPack.indentCallSiteOnce) {
val trigger = getIndentTrigger(leftOwner)
Policy.on(close) {
Expand All @@ -1362,7 +1363,7 @@ class Router(formatOps: FormatOps) {
.withOptimalTokenOpt(opt)
.withPolicy(noSplitPolicy)
.andPolicy(unindentPolicy, !isSingleArg || noSplitIndents.isEmpty)
.andPolicy(indentOncePolicy)
.andPolicy(indentOncePolicy, noSplitIndents.isEmpty)
}

val nlPolicy = {
Expand Down
40 changes: 38 additions & 2 deletions scalafmt-tests/src/test/resources/default/Apply.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1948,9 +1948,10 @@ class a {
maxIdleDuration = Duration.Inf
)(F)
}
<<< binPack unsafeCallSite, !indentOnce
<<< binPack unsafeCallSite, !indentOnce, indentCallSiteSingleArg
binPack.unsafeCallSite = always
binPack.indentCallSiteOnce = false
binPack.indentCallSiteSingleArg = true
indent.callSite = 2
align.preset = none
===
Expand Down Expand Up @@ -1981,9 +1982,10 @@ object a {
noop4
}))
}
<<< binPack unsafeCallSite, indentOnce
<<< binPack unsafeCallSite, indentOnce, indentCallSiteSingleArg
binPack.unsafeCallSite = always
binPack.indentCallSiteOnce = true
binPack.indentCallSiteSingleArg = true
indent.callSite = 2
align.preset = none
===
Expand Down Expand Up @@ -2014,3 +2016,37 @@ object a {
noop4
}))
}
<<< binPack unsafeCallSite, indentOnce, !indentCallSiteSingleArg
binPack.unsafeCallSite = always
binPack.indentCallSiteOnce = true
binPack.indentCallSiteSingleArg = false
indent.callSite = 2
align.preset = none
===
object a {
val ref = foo(bar((_, _) =>
baz1 { qux1 =>
noop1
} baz2 { qux2 =>
noop2
} baz3 //
{ qux3 =>
noop3
} baz4 { qux4 =>
noop4
}))
}
>>>
object a {
val ref = foo(bar((_, _) =>
baz1 { qux1 =>
noop1
} baz2 { qux2 =>
noop2
} baz3 //
{ qux3 =>
noop3
} baz4 { qux4 =>
noop4
}))
}