Skip to content

Commit

Permalink
Router: implement newlines.inInterpolation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jan 25, 2022
1 parent 360c85e commit 7252b03
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ class Router(formatOps: FormatOps) {
)
case FormatToken(start: T.Interpolation.Start, _, _) =>
val end = matching(start)
val policy =
if (isTripleQuote(formatToken.meta.left.text)) NoPolicy
else PenalizeAllNewlines(end, BreakSingleLineInterpolatedString)
val split = Split(NoSplit, 0).withPolicy(policy)
val okNewlines =
style.newlines.inInterpolation.ne(Newlines.InInterpolation.avoid) &&
isTripleQuote(formatToken.meta.left.text)
def policy = PenalizeAllNewlines(end, BreakSingleLineInterpolatedString)
val split = Split(NoSplit, 0).withPolicy(policy, okNewlines)
Seq(
if (getStripMarginChar(formatToken).isEmpty) split
else if (style.align.stripMargin)
Expand Down Expand Up @@ -186,7 +187,49 @@ class Router(formatOps: FormatOps) {
// Interpolated string left brace
case FormatToken(open @ T.LeftBrace(), _, _)
if leftOwner.is[SomeInterpolate] =>
Seq(Split(Space(style.spaces.inInterpolatedStringCurlyBraces), 0))
val close = matching(open)
val alignIndents =
if (style.align.inInterpolation) Some {
Seq(
Indent(Length.StateColumn, close, ExpiresOn.After),
Indent(Length.Num(style.indent.main), close, ExpiresOn.Before),
Indent(Length.Num(-1), close, ExpiresOn.After)
)
}
else None
def spaceSplit(implicit fileLine: FileLine) =
Split(Space(style.spaces.inInterpolatedStringCurlyBraces), 0)
.withIndents(alignIndents.getOrElse(Nil))
def newlineSplit(cost: Int)(implicit fileLine: FileLine) = {
def mainIndents = Seq(
Indent(style.indent.main, close, ExpiresOn.Before),
Indent(style.indent.main, close, ExpiresOn.After)
)
Split(Newline, cost)
.withIndents(alignIndents.getOrElse(mainIndents))
.withPolicy(decideNewlinesOnlyBeforeClose(close))
}
style.newlines.inInterpolation match {
case Newlines.InInterpolation.avoid => Seq(spaceSplit)
case _ if newlines != 0 && style.newlines.source.eq(Newlines.keep) =>
Seq(newlineSplit(0))
case Newlines.InInterpolation.oneline =>
/* sequence of tokens:
* - 0 RBrace
* - 1 Interpolation.SpliceEnd (empty)
* - 2 Interpolation.Part (next string)
* - 3 Interpolation.End (quotes) or Interpolation.SliceStart/LBrace (${)
*/
val afterClose = tokens(close, 3)
val lastPart = afterClose.left.is[T.Interpolation.End]
val slbEnd = if (lastPart) afterClose.left else afterClose.right
Seq(
spaceSplit.withSingleLine(slbEnd),
newlineSplit(1)
)
case _ => Seq(spaceSplit)
}

case FormatToken(_, close @ T.RightBrace(), _)
if rightOwner.is[SomeInterpolate] =>
Seq(Split(Space(style.spaces.inInterpolatedStringCurlyBraces), 0))
Expand Down
45 changes: 25 additions & 20 deletions scalafmt-tests/src/test/resources/default/String.stat
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,10 @@ object a {
object a {
s"foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(baz, bar)} foo4"
s"""
|foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(
baz,
bar)} foo4
|foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(baz, bar)} foo4
|""".stripMargin
s"""
|fo1 ${br} fo2 ${bz(br)} fo3 ${qux(bz,
br)} fo4
|fo1 ${br} fo2 ${bz(br)} fo3 ${qux(bz, br)} fo4
|""".stripMargin
}
<<< #3079 allow
Expand Down Expand Up @@ -430,7 +427,9 @@ object a {
}
>>>
object a {
s"foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(baz, bar)} foo4"
s"foo1 ${
bar
} foo2 ${baz(bar)} foo3 ${qux(baz, bar)} foo4"
s"""
|foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(
baz,
Expand Down Expand Up @@ -461,11 +460,14 @@ object a {
}
>>>
object a {
s"foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(baz, bar)} foo4"
s"foo1 ${
bar
} foo2 ${baz(bar)} foo3 ${
qux(baz, bar)
} foo4"
s"""
|foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(
baz,
bar)} foo4
|foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(baz,
bar)} foo4
|""".stripMargin
s"""
|fo1 ${br} fo2 ${bz(br)} fo3 ${qux(bz,
Expand All @@ -490,13 +492,14 @@ object a {
object a {
s"foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(baz, bar)} foo4"
s"""
|foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(
baz,
bar)} foo4
|foo1 ${bar} foo2 ${baz(bar)} foo3 ${
qux(baz, bar)
} foo4
|""".stripMargin
s"""
|fo1 ${br} fo2 ${bz(br)} fo3 ${qux(bz,
br)} fo4
|fo1 ${br} fo2 ${bz(br)} fo3 ${
qux(bz, br)
} fo4
|""".stripMargin
}
<<< #3079 oneline, align
Expand All @@ -519,12 +522,14 @@ object a {
object a {
s"foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(baz, bar)} foo4"
s"""
|foo1 ${bar} foo2 ${baz(bar)} foo3 ${qux(
baz,
bar)} foo4
|foo1 ${bar} foo2 ${baz(bar)} foo3 ${
qux(baz,
bar)
} foo4
|""".stripMargin
s"""
|fo1 ${br} fo2 ${bz(br)} fo3 ${qux(bz,
br)} fo4
|fo1 ${br} fo2 ${bz(br)} fo3 ${
qux(bz, br)
} fo4
|""".stripMargin
}

0 comments on commit 7252b03

Please sign in to comment.