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: fix alignment for incorrect // pos #2959

Merged
merged 2 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
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 @@ -990,28 +990,34 @@ class FormatWriter(formatOps: FormatOps) {
columnShift = 0
}
val floc = locations(idx)
val ft = floc.formatToken
idx += 1
columnShift += floc.shift
if (floc.hasBreakAfter || floc.formatToken.leftHasNewline) floc
if (floc.hasBreakAfter || ft.leftHasNewline) floc
else {
getAlignContainer(floc).foreach { container =>
def appendCandidate() =
columnCandidates += new AlignStop(
getAlignColumn(floc) + columnShift,
floc,
alignContainer,
getAlignHashKey(floc)
)
if (alignContainer eq null)
alignContainer = container
else if (alignContainer ne container) {
val pos1 = alignContainer.pos
val pos2 = container.pos
if (pos2.start <= pos1.start && pos2.end >= pos1.end) {
if (isSingleLineComment(ft.right)) {
if (pos1.end >= tokens.prevNonCommentSameLine(ft).left.end)
appendCandidate()
} else if (pos2.start <= pos1.start && pos2.end >= pos1.end) {
alignContainer = container
columnCandidates.clear()
}
}
if (alignContainer eq container)
columnCandidates += new AlignStop(
getAlignColumn(floc) + columnShift,
floc,
container,
getAlignHashKey(floc)
)
appendCandidate()
}
if (idx < locations.length) processLine else floc
}
Expand Down
15 changes: 15 additions & 0 deletions scalafmt-tests/src/test/resources/align/DefaultWithAlign.stat
Original file line number Diff line number Diff line change
Expand Up @@ -1822,3 +1822,18 @@ class A(y: YCoord) {
@targetName("addY")
def toInt: Int = y.toInt
}
<<< scala3 align match
maxColumn = 100
runner.dialect = scala3
===
val (xTranslation, yTranslation) = (isHMirror, isVMirror) match
case (true, true) => ((rotationWidth / 2).toInt, (rotationHeight / 2).toInt) /* c */ // TL
case (true, false) => ((rotationHeight / 2).toInt, -(rotationWidth / 2).toInt) /* c */ // BL
case (false, true) => (-(rotationHeight / 2).toInt, (rotationWidth / 2).toInt) /* c */ // TR
case (false, false) => (-(rotationWidth / 2).toInt, -(rotationHeight / 2).toInt) /* c */ // BR
>>>
val (xTranslation, yTranslation) = (isHMirror, isVMirror) match
case (true, true) => ((rotationWidth / 2).toInt, (rotationHeight / 2).toInt) /* c */ // TL
case (true, false) => ((rotationHeight / 2).toInt, -(rotationWidth / 2).toInt) /* c */ // BL
case (false, true) => (-(rotationHeight / 2).toInt, (rotationWidth / 2).toInt) /* c */ // TR
case (false, false) => (-(rotationWidth / 2).toInt, -(rotationHeight / 2).toInt) /* c */ // BR