From 0fecc3fb2d5d463120927a1537f751a1e1e964c0 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 11 Aug 2023 00:52:38 +0300 Subject: [PATCH] FormatWriter: append to empty align block --- .../scala/org/scalafmt/internal/FormatWriter.scala | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala index 26a3dbe6cc..2ad8eb7679 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatWriter.scala @@ -1198,13 +1198,14 @@ class FormatWriter(formatOps: FormatOps) { if (alignContainer ne null) { val candidates = columnCandidates.result() val block = getOrCreateBlock(alignContainer) - def appendToBlock(matches: Int = 0) = { + def appendToBlock(matches: Int = 0): Unit = { val eolColumn = location.state.prev.column + columnShift val alignLine = new AlignLine(candidates, eolColumn) - if (!block.tryAppendToBlock(alignLine, matches)) { + if (!block.isEmpty) { + if (block.tryAppendToBlock(alignLine, matches)) return flushAlignBlock(block) - block.tryAppendToBlock(alignLine, 0) } + block.appendToEmptyBlock(alignLine) } if (block.isEmpty) { if (!isBlankLine) appendToBlock() @@ -1697,6 +1698,13 @@ object FormatWriter { var refStops: Seq[AlignStop] = Seq.empty, var stopColumns: IndexedSeq[Int] = IndexedSeq.empty ) { + def appendToEmptyBlock(line: AlignLine): Unit = { + val stops = line.stops + refStops = stops + buffer += line + stopColumns = stops.map(_.column) + } + def tryAppendToBlock(line: AlignLine, matches: Int): Boolean = { // truncate if matches are shorter than both lists val truncate = shouldTruncate(line, matches)