Skip to content

Commit

Permalink
FormatOps: implement indentOperator.exemptScope
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 27, 2022
1 parent 7044913 commit 115f2cb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ case class IndentOperator(
"Use indentOperator.exemptScope instead (true->topLevelOnly, false->all)",
"3.4.0"
)
topLevelOnly: Boolean = true,
private val topLevelOnly: Boolean = true,
@annotation.ExtraName("include")
includeRegex: String = ".*",
@annotation.ExtraName("exclude")
Expand All @@ -65,6 +65,12 @@ case class IndentOperator(

def noindent(op: String): Boolean =
excludeRegexp.matcher(op).find() || !includeRegexp.matcher(op).find()

lazy val getExemptScope: IndentOperator.Exempt =
exemptScope.getOrElse(
if (topLevelOnly) IndentOperator.Exempt.oldTopLevel
else IndentOperator.Exempt.all
)
}

object IndentOperator {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package org.scalafmt.internal
import org.scalafmt.Error.UnexpectedTree
import org.scalafmt.config.{
BinPack,
IndentOperator,
Newlines,
ScalafmtConfig,
ScalafmtRunner,
Expand Down Expand Up @@ -583,8 +584,31 @@ class FormatOps(
case t: Case => t.pat.eq(child) || t.body.eq(child)
case _ => false
}
val allowNoIndent =
!style.indentOperator.topLevelOnly || isOldTopLevel(getChild)
def isAloneEnclosed(child: Tree) = child.parent.exists {
case Case(`child`, _, _) => true
case Term.If(`child`, _, _) => true
case Term.While(`child`, _) => true
case Term.Do(_, `child`) => true
case Term.Block(List(`child`)) => true
case fun: Term.FunctionTerm => isBlockFunction(fun)
case SplitCallIntoParts(_, Left(Seq(`child`))) => true
case _ => false
}
def isAloneArgOrBody(child: Tree) = child.parent.exists {
case t: Case => t.pat.eq(child) || t.body.eq(child)
case _: Term.If | _: Term.While | _: Term.Do => true
case Term.Block(List(`child`)) => true
case Term.ForYield(_, `child`) => true
case SplitAssignIntoParts(`child`, _) => true
case SplitCallIntoParts(_) => true
case _ => false
}
val allowNoIndent = style.indentOperator.getExemptScope match {
case IndentOperator.Exempt.all => true
case IndentOperator.Exempt.oldTopLevel => isOldTopLevel(getChild)
case IndentOperator.Exempt.aloneEnclosed => isAloneEnclosed(getChild)
case IndentOperator.Exempt.aloneArgOrBody => isAloneArgOrBody(getChild)
}
def isInfixTopLevelMatch(op: String, noindent: Boolean): Boolean = {
noindent == style.indentOperator.noindent(op) &&
noindent == allowNoIndent
Expand Down
50 changes: 25 additions & 25 deletions scalafmt-tests/src/test/resources/test/IndentOperator.stat
Original file line number Diff line number Diff line change
Expand Up @@ -2956,7 +2956,7 @@ object a {
b
} else
a &&
b
b
if (
a &&
b
Expand All @@ -2965,12 +2965,12 @@ object a {
b
} else
a &&
b
b
if a &&
b
then
a &&
b
b
else {
a &&
b
Expand All @@ -2980,7 +2980,7 @@ object a {
b
then
a &&
b
b
else {
a &&
b
Expand Down Expand Up @@ -3193,24 +3193,24 @@ object a {
b
)
a &&
b
b
while (
a &&
b
)
a &&
b
b
while a &&
b
do
a &&
b
b
while
a &&
b
do
a &&
b
b
}
<<< aloneArgOrBody, while
runner.dialect = scala3
Expand Down Expand Up @@ -3375,7 +3375,7 @@ object a {
object a {
foo(
a &&
b
b
)
foo {
a &&
Expand All @@ -3389,10 +3389,10 @@ object a {
)
foo(
a &&
b
b
)(
a &&
b
b
)
}
<<< aloneArgOrBody, apply
Expand Down Expand Up @@ -3426,24 +3426,24 @@ object a {
object a {
foo(
a &&
b
b
)
foo {
a &&
b
}
foo(
a &&
b,
b,
a &&
b
b
)
foo(
a &&
b
b
)(
a &&
b
b
)
}
<<< oldTopLevel, apply
Expand Down Expand Up @@ -3524,21 +3524,21 @@ a match {
a match {
case foo =>
a &&
b
b
case foo =>
val baz = qux
a &&
b
b
case bar => {
val baz = qux
a &&
b
b
}
case bar if
a &&
b =>
a &&
b
b
}
<<< aloneArgOrBody, case
newlines.source = keep
Expand Down Expand Up @@ -3571,11 +3571,11 @@ a match {
case foo =>
val baz = qux
a &&
b
b
case bar => {
val baz = qux
a &&
b
b
}
case bar if
a &&
Expand Down Expand Up @@ -3654,7 +3654,7 @@ object a {
foo { x =>
val bar = qux
a &&
b
b
}
foo(x =>
a &&
Expand Down Expand Up @@ -3689,11 +3689,11 @@ object a {
foo { x =>
val bar = qux
a &&
b
b
}
foo(x =>
a &&
b
b
)
}
<<< oldTopLevel, lambda
Expand Down

0 comments on commit 115f2cb

Please sign in to comment.