Skip to content

Commit 1ce4606

Browse files
committed
FormatWriter: add align shift to multiline strings
1 parent 4e9de61 commit 1ce4606

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

‎scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class FormatWriter(formatOps: FormatOps) {
3737
styleMap.init.runner.event(FormatEvent.Written(locations))
3838

3939
var delayedAlign = 0
40+
var totalAlignShift = 0
4041
locations.foreach { entry =>
4142
val location = entry.curr
4243
implicit val style: ScalafmtConfig = location.style
@@ -47,8 +48,8 @@ class FormatWriter(formatOps: FormatOps) {
4748
ft.left match {
4849
case _ if entry.previous.formatToken.meta.formatOff => sb.append(ltext) // checked the state for left
4950
case _: T.Comment => entry.formatComment
50-
case _: T.Interpolation.Part | _: T.Constant.String =>
51-
entry.formatMarginized
51+
case _: T.Interpolation.Part | _: T.Constant.String => entry
52+
.formatMarginized(totalAlignShift)
5253
case _: T.Constant.Int => LiteralOps.prettyPrintInteger(ltext)
5354
case _: T.Constant.Long => LiteralOps.prettyPrintInteger(ltext)
5455
case _: T.Constant.Float => LiteralOps.prettyPrintFloat(ltext)
@@ -85,7 +86,16 @@ class FormatWriter(formatOps: FormatOps) {
8586
} else if (ft.right.is[T.RightParen]) skipWs = true
8687
} else if (location.missingBracesOpenOrTuck) sb.append(" {")
8788

88-
if (!skipWs) delayedAlign = entry.formatWhitespace(delayedAlign)
89+
if (!skipWs) {
90+
val alignShift = entry.formatWhitespace(delayedAlign)
91+
if (location.state.mod.isNL) {
92+
delayedAlign = 0
93+
totalAlignShift = 0
94+
} else if (alignShift > 0) {
95+
delayedAlign = 0
96+
totalAlignShift += alignShift
97+
} else delayedAlign = -alignShift
98+
}
8999
}
90100

91101
sb.toString()
@@ -453,11 +463,12 @@ class FormatWriter(formatOps: FormatOps) {
453463
sb.append(p.betweenText)
454464
0
455465

456-
case NoSplit if style.align.delayUntilSpace => align // delay
466+
case NoSplit if style.align.delayUntilSpace => -align // delay
457467

458468
case _ =>
459-
sb.append(getIndentation(mod.length + align))
460-
0
469+
val alignShift = align
470+
sb.append(getIndentation(mod.length + alignShift))
471+
alignShift
461472
}
462473
}
463474

@@ -533,7 +544,9 @@ class FormatWriter(formatOps: FormatOps) {
533544
}
534545
}
535546

536-
def formatMarginized(implicit sb: StringBuilder): Unit = {
547+
def formatMarginized(
548+
alignShift: Int,
549+
)(implicit sb: StringBuilder): Unit = {
537550
val text = tok.meta.left.text
538551
val tupleOpt = tok.left match {
539552
case _ if !style.assumeStandardLibraryStripMargin => None
@@ -559,7 +572,7 @@ class FormatWriter(formatOps: FormatOps) {
559572
}
560573
tupleOpt match {
561574
case Some((pipe, indent)) =>
562-
val spaces = getIndentation(indent)
575+
val spaces = getIndentation(indent + alignShift)
563576
val matcher = RegexCompat.getStripMarginPattern(pipe).matcher(text)
564577
var pos = 0
565578
while (matcher.find()) {

‎scalafmt-tests/shared/src/test/resources/align/DefaultWithAlign.stat‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2325,7 +2325,7 @@ val y = y && y && s"""|y
23252325
object a {
23262326
val xy = xy && xy && xy
23272327
val x = x && x && """|x
2328-
|x""".stripMargin
2328+
|x""".stripMargin
23292329
val y = y && y && s"""|y
2330-
|y""".stripMargin
2330+
|y""".stripMargin
23312331
}

0 commit comments

Comments
 (0)