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

Router: attach/detach comment to comma as needed #3002

Merged
merged 2 commits into from
Dec 26, 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 @@ -1385,18 +1385,20 @@ class Router(formatOps: FormatOps) {
Split(NoSplit, 0)
)
// These are mostly filtered out/modified by policies.
case ft @ FormatToken(left: T.Comma, c: T.Comment, _) =>
case ft @ FormatToken(lc: T.Comma, c: T.Comment, _) =>
if (ft.hasBlankLine) Seq(Split(NewlineT(isDouble = true), 0))
else if (isSingleLineComment(c) || ft.meta.right.hasNL)
Seq(Split(Space.orNL(newlines), 0))
else {
val trailingComma =
rightIsCloseDelimToAddTrailingComma(left, nextNonComment(next(ft)))
Seq(
Split(newlines != 0 && !trailingComma, 0)(Space),
Split(newlines == 0 && trailingComma, 1)(Newline)
)
}
else if (newlines == 0) {
val endFt = nextNonCommentSameLine(next(ft))
val useSpaceOnly = endFt.hasBreak ||
rightIsCloseDelimToAddTrailingComma(lc, endFt)
Seq(Split(Space, 0), Split(useSpaceOnly, 1)(Newline))
} else if (
!style.comments.willWrap &&
rightIsCloseDelimToAddTrailingComma(lc, nextNonComment(next(ft)))
) Seq(Split(Space, 0), Split(Newline, 1))
else Seq(Split(Newline, 0))
case FormatToken(_: T.Comma, right, _) if leftOwner.isNot[Template] =>
val splitsOpt = argumentStarts.get(hash(right)).flatMap { nextArg =>
val callSite = isCallSite(leftOwner)
Expand Down
49 changes: 49 additions & 0 deletions scalafmt-tests/src/test/resources/unit/Comment.stat
Original file line number Diff line number Diff line change
Expand Up @@ -733,3 +733,52 @@ object a {
* IDs is requsted for MD5 and SHA1 and not for LUID */
) = ???
}
<<< overflow, comma, attached mlc then another arg after break
maxColumn = 10
===
foo(
barBarBar, /* comment */
qux
)
>>>
foo(
barBarBar, /* comment */
qux
)
<<< overflow, comma, attached mlc then another arg after space
maxColumn = 10
===
foo(
barBarBar, /* comment */
bazBazBaz, /* comment */ qux
)
>>>
foo(
barBarBar, /* comment */
bazBazBaz,
/* comment */ qux
)
<<< overflow, comma, attached mlc, binpack
maxColumn = 30
indent.callSite = 2
align.preset = none
binPack.preset = true
optIn.configStyleArguments = false
===
foo(barBarBarBar, /* c1 */ baz, /* c2 */ qux)
>>>
foo(barBarBarBar,
/* c1 */ baz, /* c2 */ qux)
<<< overflow, comma, attached mlc, binpack, fold
maxColumn = 30
indent.callSite = 2
align.preset = none
binPack.preset = true
optIn.configStyleArguments = false
newlines.source = fold
includeCurlyBraceInSelectChains = true
===
foo(barBarBarBar, /* c1 */ baz, /* c2 */ qux)
>>>
foo(barBarBarBar,
/* c1 */ baz, /* c2 */ qux)