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

TreeOps: inline extractStatementsIfAny #4397

Merged
merged 1 commit into from
Oct 5, 2024
Merged
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
33 changes: 14 additions & 19 deletions scalafmt-core/shared/src/main/scala/org/scalafmt/util/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,6 @@ object TreeOps {
ftoks: FormatTokens,
): Boolean = SingleArgInBraces.orBlock(parent).exists(_._2 eq expr)

def extractStatementsIfAny(tree: Tree): Seq[Tree] = tree match {
case t: Term.EnumeratorsBlock => getEnumStatements(t.enums)
case t: Term.PartialFunction => t.cases match {
case _ :: Nil => Nil
case x => x
}
case t @ Term.Block(s) =>
if (t.parent.is[CaseTree])
if (getSingleStatExceptEndMarker(s).isEmpty) s else s.drop(1)
else s match {
case (_: Term.FunctionTerm) :: Nil => Nil
case _ => s
}
case Tree.Block(s) => s
case _ => Nil
}

def getStatementStarts(tree: Tree, soft: SoftKeywordClasses)(implicit
ftoks: FormatTokens,
): Map[Int, Tree] = {
Expand Down Expand Up @@ -195,8 +178,20 @@ object TreeOps {
// ignore single-stat block if opening brace was removed
x.is[Token.LeftBrace] && ftoks(x).left.ne(x)
} =>
case t => // Nothing
addAll(extractStatementsIfAny(t))
case t: Term.EnumeratorsBlock => addAll(getEnumStatements(t.enums))
case t: Term.PartialFunction => t.cases match {
case _ :: Nil =>
case x => addAll(x)
}
case t @ Term.Block(s) =>
if (t.parent.is[CaseTree])
addAll(if (getSingleStatExceptEndMarker(s).isEmpty) s else s.drop(1))
else s match {
case (_: Term.FunctionTerm) :: Nil =>
case _ => addAll(s)
}
case Tree.Block(s) => addAll(s)
case _ => // Nothing
}
subtree.children.foreach(loop)
}
Expand Down
Loading