Skip to content

Commit

Permalink
} could be from a block arg or splice
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Mar 1, 2024
1 parent 22930a3 commit 07c61a2
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 41 deletions.
Expand Up @@ -1382,12 +1382,6 @@ class FormatOps(
if (!ft.right.is[T.Comment] || isDone(ft)) ft else iter(ft)
}

def xmlSpace(owner: Tree): Modification =
owner match {
case _: Term.Xml | _: Pat.Xml => NoSplit
case _ => Space
}

def getSpaceAndNewlineAfterCurlyLambda(
newlines: Int
)(implicit style: ScalafmtConfig): (Boolean, NewlineT) =
Expand Down
Expand Up @@ -191,14 +191,19 @@ class Router(formatOps: FormatOps) {
.withIndents(alignIndents.getOrElse(mainIndents))
.withPolicy(decideNewlinesOnlyBeforeClose(close))
}
def findArg = findInterpolateArgAfter(open.end, leftOwner)
def isSimpleInterpolate = (leftOwner match {
case t: Pat.Interpolate => findArgAfter(open.end, t.args)
case t: Term.Interpolate => findArgAfter(open.end, t.args)
case t: Term.Block => getBlockSingleStat(t)
case _ => None
}).exists(!_.is[Term.If])

style.newlines.inInterpolation match {
case Newlines.InInterpolation.avoid => Seq(spaceSplit)
case _ if style.newlines.keepBreak(newlines) =>
Seq(newlineSplit(0))
case Newlines.InInterpolation.allow
if !dialect.allowSignificantIndentation ||
!findArg.exists(_.is[Term.If]) =>
if !dialect.allowSignificantIndentation || isSimpleInterpolate =>
Seq(spaceSplit)
case _ =>
/* sequence of tokens:
Expand All @@ -216,8 +221,7 @@ class Router(formatOps: FormatOps) {
)
}

case FormatToken(_, _: T.RightBrace, _)
if rightOwner.is[SomeInterpolate] =>
case FormatToken(_, _: T.RightBrace, _) if isInterpolate(rightOwner) =>
Seq(Split(Space(style.spaces.inInterpolatedStringCurlyBraces), 0))

// optional braces: block follows
Expand Down
Expand Up @@ -152,6 +152,13 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
session: Session,
style: ScalafmtConfig
): Replacement = {
def handleInterpolation =
if (
style.rewrite.redundantBraces.stringInterpolation &&
processInterpolation
) removeToken
else null

owner match {
case t: Term.FunctionTerm if t.tokens.last.is[Token.RightBrace] =>
if (!okToRemoveFunctionInApplyOrInit(t)) null
Expand All @@ -167,13 +174,11 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {
case Some(f: Term.FunctionTerm)
if okToReplaceFunctionInSingleArgApply(f) =>
replaceWithLeftParen
case Some(_: Term.Interpolate) => handleInterpolation
case _ =>
if (processBlock(t)) removeToken else null
}
case _: Term.Interpolate
if style.rewrite.redundantBraces.stringInterpolation &&
processInterpolation =>
removeToken
case _: Term.Interpolate => handleInterpolation
case Importer(_, List(x))
if !(x.is[Importee.Rename] || x.is[Importee.Unimport]) ||
style.dialect.allowAsForImportRename &&
Expand Down Expand Up @@ -219,7 +224,7 @@ class RedundantBraces(ftoks: FormatTokens) extends FormatTokensRewrite.Rule {

def isLiteralIdentifier(arg: Term.Name): Boolean = {
val syntax = arg.toString()
syntax.headOption.contains('`') && syntax.lastOption.contains('`')
syntax.nonEmpty && syntax.head == '`' && syntax.last == '`'
}

/** we need to keep braces
Expand Down

This file was deleted.

Expand Up @@ -12,6 +12,7 @@ import scala.reflect.ClassTag

import org.scalafmt.Error
import org.scalafmt.config.{DanglingParentheses, ScalafmtConfig}
import org.scalafmt.internal.{Modification, Space}
import org.scalafmt.internal.{FormatToken, FormatTokens}
import org.scalafmt.util.InfixApp._

Expand Down Expand Up @@ -756,13 +757,6 @@ object TreeOps {
}
}

def findInterpolateArgAfter(end: Int, tree: Tree): Option[Tree] =
tree match {
case t: Pat.Interpolate => findArgAfter(end, t.args)
case t: Term.Interpolate => findArgAfter(end, t.args)
case _ => None
}

def findArgAfter(end: Int, trees: Seq[Tree]): Option[Tree] =
trees.find(_.pos.start >= end)

Expand Down Expand Up @@ -1067,4 +1061,13 @@ object TreeOps {
def isParentAnApply(t: Tree): Boolean =
t.parent.exists(_.is[Term.Apply])

def isTreeOrBlockParent(owner: Tree)(pred: Tree => Boolean): Boolean =
if (owner.is[Term.Block]) owner.parent.exists(pred) else pred(owner)

def xmlSpace(owner: Tree): Modification =
Space(!isTreeOrBlockParent(owner)(_.isAny[Term.Xml, Pat.Xml]))

def isInterpolate(tree: Tree): Boolean =
isTreeOrBlockParent(tree)(_.isAny[Term.Interpolate, Pat.Interpolate])

}

0 comments on commit 07c61a2

Please sign in to comment.