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

FormatWriter: add style, get overflow in AlignLine #3918

Merged
merged 1 commit into from
Apr 9, 2024
Merged
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 @@ -1185,8 +1185,11 @@ class FormatWriter(formatOps: FormatOps) {
val block = getOrCreateBlock(alignContainer)
val blockWasEmpty = block.isEmpty
if (!blockWasEmpty || !isBlankLine) {
val alignLine =
new AlignLine(candidates, floc.state.prev.column + columnShift)
val alignLine = new AlignLine(
candidates,
floc.state.prev.column + columnShift,
floc.style,
)
val appendToEmptyBlock = blockWasEmpty || {
val matches = columnMatches(
wasSameContainer(alignContainer),
Expand Down Expand Up @@ -1326,11 +1329,13 @@ class FormatWriter(formatOps: FormatOps) {
)(implicit builder: mutable.Builder[(Int, Int), Map[Int, Int]]): Unit = {
val endIndex = locations.length - 1
block.foreach { x =>
val headStop = x.stops.head
if (headStop.floc.style.align.multiline) {
val offset = block.stopColumns.head - headStop.column
if (x.style.align.multiline) {
val headStop = x.stops.head
val ftIndex = headStop.floc.formatToken.meta.idx
if (ftIndex < endIndex) shiftStateColumnIndent(ftIndex + 1, offset)
if (ftIndex < endIndex) shiftStateColumnIndent(
ftIndex + 1,
block.stopColumns.head - headStop.column,
)
}
var previousShift = 0
x.stops.zip(block.stopColumns).foreach { case (stop, blockStop) =>
Expand Down Expand Up @@ -1653,7 +1658,14 @@ object FormatWriter {

class AlignStop(val column: Int, val floc: FormatLocation, val hashKey: Int)

class AlignLine(var stops: IndexedSeq[AlignStop], val eolColumn: Int)
class AlignLine(
var stops: IndexedSeq[AlignStop],
val eolColumn: Int,
val style: ScalafmtConfig,
) {
@inline
def noOverflow(shift: Int) = eolColumn + shift <= style.maxColumn
}

class AlignBlock(
buffer: mutable.ArrayBuffer[AlignLine] =
Expand Down Expand Up @@ -1697,29 +1709,25 @@ object FormatWriter {
oldStops.drop(newStops.length).foreach(newStopCols += oldShift + _)
newStops.drop(oldStops.length).foreach(newStopCols += newShift + _.column)

// check overflow
val style = line.stops.last.floc.style
if (!style.align.allowOverflow) {
val overflow = (line.eolColumn + newShift) > style.maxColumn ||
buffer.exists { x =>
val idx = -1 + {
if (truncate < 0) math.min(matches, x.stops.length)
else x.stops.length
def finalize() =
(line.style.align.allowOverflow || // check overflow
line.noOverflow(newShift) && buffer.forall { bl =>
bl.style.align.allowOverflow || {
val len = bl.stops.length
val idx = -1 + (if (truncate < 0) math.min(matches, len) else len)
bl.noOverflow(newStopCols(idx) - bl.stops(idx).column)
}
val lastStop = x.stops(idx)
val totalShift = newStopCols(idx) - lastStop.column
x.eolColumn + totalShift > lastStop.floc.style.maxColumn
}
if (overflow) return false // RETURNING
}
}) && {
// now we mutate
line.stops = newStops
if (truncate < 0) foreach(x => x.stops = x.stops.take(matches))
if (newStopCols.length == newStops.length) refStops = newStops
buffer += line
stopColumns = newStopCols.toIndexedSeq
true
}

// now we mutate
line.stops = newStops
if (truncate < 0) foreach(x => x.stops = x.stops.take(matches))
if (newStopCols.length == newStops.length) refStops = newStops
buffer += line
stopColumns = newStopCols.toIndexedSeq
true
finalize()
}

// <0 old, 0 neither, >0 new
Expand Down
Loading