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: no inInterpolation=allow if scala3 if #3115

Merged
merged 2 commits into from
Feb 8, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,16 @@ class Router(formatOps: FormatOps) {
.withIndents(alignIndents.getOrElse(mainIndents))
.withPolicy(decideNewlinesOnlyBeforeClose(close))
}
def findArg = findInterpolateArgAfter(open.end, leftOwner)
style.newlines.inInterpolation match {
case Newlines.InInterpolation.avoid => Seq(spaceSplit)
case _ if style.newlines.keepBreak(newlines) =>
Seq(newlineSplit(0))
case Newlines.InInterpolation.oneline =>
case Newlines.InInterpolation.allow
if !dialect.allowSignificantIndentation ||
!findArg.exists(_.is[Term.If]) =>
Seq(spaceSplit)
case _ =>
/* sequence of tokens:
* - 0 RBrace
* - 1 Interpolation.SpliceEnd (empty)
Expand All @@ -235,7 +240,6 @@ class Router(formatOps: FormatOps) {
spaceSplit.withSingleLine(slbEnd),
newlineSplit(1)
)
case _ => Seq(spaceSplit)
}

case FormatToken(_, _: T.RightBrace, _)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,16 @@ 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)

def getStripMarginCharForInterpolate(tree: Tree): Option[Char] =
findInterpolate(tree).flatMap(getStripMarginChar)

Expand Down
47 changes: 47 additions & 0 deletions scalafmt-tests/src/test/resources/default/String.stat
Original file line number Diff line number Diff line change
Expand Up @@ -533,3 +533,50 @@ object a {
} fo4
|""".stripMargin
}
<<< #3113 allow with scala3 if-else: sig indent must align
maxColumn = 50
runner.dialect = scala3
newlines.inInterpolation = allow
===
val s = raw"""
${
if true then
println()
"1"
else
"2"
}
"""
>>>
val s = raw"""
${
if true then
println()
"1"
else "2"
}
"""
<<< #3113 allow, align with scala3 if-else: sig indent must align
maxColumn = 50
runner.dialect = scala3
align.inInterpolation = true
newlines.inInterpolation = allow
===
val s = raw"""
${
if true then
println()
"1"
else
"2"
}
"""
>>>
val s = raw"""
${
if true then
println()
"1"
else "2"
}
"""