diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index 99f575c15d..366004eb5b 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -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) @@ -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)) diff --git a/scalafmt-tests/src/test/resources/default/String.stat b/scalafmt-tests/src/test/resources/default/String.stat index 7db565f49e..a0f706c34d 100644 --- a/scalafmt-tests/src/test/resources/default/String.stat +++ b/scalafmt-tests/src/test/resources/default/String.stat @@ -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 @@ -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, @@ -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, @@ -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 @@ -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 }