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 26, 2022
1 parent 7a97f45 commit 69b9515
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ case class IndentOperator(
"Use indentOperator.exemptScope instead (true->topLevelOnly, false->all)",
"3.4.0"
)
topLevelOnly: Boolean = true,
private val topLevelOnly: Boolean = true,
private val exemptScope: Option[IndentOperator.Exempt] = None,
@annotation.ExtraName("include")
includeRegex: String = ".*",
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.topLevel
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,27 @@ class FormatOps(
case t: Case => t.pat.eq(child) || t.body.eq(child)
case _ => false
}
val allowNoIndent =
!style.indentOperator.topLevelOnly || isTopLevel(getChild)
def isEnclosedAlone(child: Tree) = child.parent.exists {
case Case(`child`, _, _) => true
case Term.If(`child`, _, _) => true
case Term.While(`child`, _) => true
case Term.Block(List(`child`)) => true
case SplitCallIntoParts(_, Left(Seq(`child`))) => true
case fun: Term.FunctionTerm => isBlockFunction(fun)
case _ =>
tokens.getHeadOpt(child).exists { headFt =>
val last = tokens.getLast(child)
matchingOpt(headFt.left)
.contains(prevNonComment(last).left) ||
matchingOpt(prevNonComment(prev(headFt)).left)
.contains(nextNonComment(last).right)
}
}
val allowNoIndent = style.indentOperator.getExemptScope match {
case IndentOperator.Exempt.all => true
case IndentOperator.Exempt.topLevel => isTopLevel(getChild)
case IndentOperator.Exempt.enclosedAlone => isEnclosedAlone(getChild)
}
def isInfixTopLevelMatch(op: String, noindent: Boolean): Boolean = {
noindent == style.indentOperator.noindent(op) &&
noindent == allowNoIndent
Expand Down
12 changes: 6 additions & 6 deletions scalafmt-tests/src/test/resources/test/IndentOperator.stat
Original file line number Diff line number Diff line change
Expand Up @@ -2930,7 +2930,7 @@ object a {
b
} else
a &&
b
b
}
<<< ONLY enclosedAloneOnly, while
indentOperator.exemptScope = enclosedAlone
Expand Down Expand Up @@ -2964,7 +2964,7 @@ object a {
b
)
a &&
b
b
}
<<< ONLY enclosedAloneOnly, apply
indentOperator.exemptScope = enclosedAlone
Expand Down Expand Up @@ -2996,7 +2996,7 @@ object a {
object a {
foo(
a &&
b
b
)
foo {
a &&
Expand All @@ -3010,10 +3010,10 @@ object a {
)
foo(
a &&
b
b
)(
a &&
b
b
)
}
<<< ONLY enclosedAloneOnly, case
Expand All @@ -3032,7 +3032,7 @@ a match {
a match {
case foo =>
a &&
b
b
case bar => {
a &&
b
Expand Down

0 comments on commit 69b9515

Please sign in to comment.