Skip to content

Commit

Permalink
FormatWriter: check topLevelStatBlanks blank gaps
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Jun 24, 2024
1 parent a4f93c4 commit 751ee40
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ object Newlines {
lazy val pattern = regex.map(_.r.pattern)
def checkParams(v: TopStatBlanksParams, prefix: String): Boolean =
checkRange(v.nest, minNest, maxNest) &&
checkRange(v.blankGaps, minBlankGaps, maxBlankGaps) &&
pattern.forall(_.matcher(prefix).find())
}
object TopStatBlanks {
Expand All @@ -534,6 +535,7 @@ object Newlines {
case class TopStatBlanksParams( // what to match against in checkParams above
numBreaks: Int,
nest: Int,
blankGaps: Int,
)
private def checkRange(v: Int, min: Int, max: Int) = min <= v && v <= max

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,21 @@ class FormatWriter(formatOps: FormatOps) {
val result = new Array[FormatLocation](depth)

@tailrec
def iter(cur: State, lineId: Int): Unit = {
def iter(cur: State, lineId: Int, gapId: Int): Unit = {
val prev = cur.prev
val idx = prev.depth
val ft = toks(idx)
if (idx == 0) // done
result(idx) = FormatLocation(ft, cur, initStyle, lineId)
result(idx) = FormatLocation(ft, cur, initStyle, lineId, gapId)
else {
val nl = cur.split.modExt.mod.newlines
val nLineId = lineId + nl + ft.meta.left.countNL
result(idx) = FormatLocation(ft, cur, styleMap.at(ft), nLineId)
iter(prev, nLineId)
val nGapId = gapId + (if (nl > 1) 1 else 0)
result(idx) = FormatLocation(ft, cur, styleMap.at(ft), nLineId, nGapId)
iter(prev, nLineId, nGapId)
}
}
if (depth != 0) iter(state, 0)
if (depth != 0) iter(state, 0, 0)

if (depth == toks.length) { // format completed
val initStyle = styleMap.init
Expand Down Expand Up @@ -1581,6 +1582,7 @@ class FormatWriter(formatOps: FormatOps) {
val params = Newlines.TopStatBlanksParams( // set search parameters
numBreaks = getLineDiff(bLoc, eLoc),
nest = nest,
blankGaps = getBlankGapsDiff(bLoc, eLoc),
)
bLoc.style.newlines.getTopStatBlankLines(statHead)(params)
.map((_, head, last))
Expand Down Expand Up @@ -1613,6 +1615,7 @@ object FormatWriter {
state: State,
style: ScalafmtConfig,
leftLineId: Int, // counts back from the end of the file
leftBlankGapId: Int, // accumulates number of blank gaps, also from end
shift: Int = 0,
optionalBraces: Map[Int, Tree] = Map.empty,
// if indent is empty, indicates open; otherwise, whether to tuck
Expand Down Expand Up @@ -1996,6 +1999,10 @@ object FormatWriter {
private def getLineDiff(beg: FormatLocation, end: FormatLocation): Int =
beg.leftLineId - end.leftLineId

@inline
private def getBlankGapsDiff(beg: FormatLocation, end: FormatLocation): Int =
beg.leftBlankGapId - end.leftBlankGapId

@inline
private def getLineDiff(
toks: Array[FormatLocation],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,14 +498,11 @@ object aboveRange {
}
>>>
object aboveRange {

object belowRange {

val a = {
b
c
}

}

object inRange {
Expand Down

0 comments on commit 751ee40

Please sign in to comment.