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: fix bp.defnsite=oneline inconsistency bug #4002

Merged
merged 1 commit into from
May 16, 2024
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 @@ -1091,12 +1091,11 @@ class Router(formatOps: FormatOps) {
val afterClose = after(close)

val argsHeadOpt = argumentStarts.get(ft.meta.idx)
val isSingleArg = isSeqSingle(getArgs(leftOwner))
val oneline = style.binPack.defnSiteFor(isBracket) eq
BinPack.Site.Oneline

val nextComma = (if (isSingleArg) None else argsHeadOpt)
.flatMap(x => findFirstOnRight[T.Comma](getLast(x), close))
val nextCommaOneline = argsHeadOpt.flatMap { x =>
val noNeed = isSeqSingle(getArgs(leftOwner)) ||
style.binPack.defnSiteFor(isBracket) != BinPack.Site.Oneline
if (noNeed) None else findFirstOnRight[T.Comma](getLast(x), close)
}

val flags =
getBinpackSiteFlags(ft, prev(afterClose), literalArgList = false)
Expand All @@ -1123,7 +1122,7 @@ class Router(formatOps: FormatOps) {
}
}
val argPolicy = argsHeadOpt.fold(Policy.noPolicy) { x =>
if (oneline && isSingleArg) NoPolicy
if (nextCommaOneline.isEmpty) NoPolicy
else SingleLineBlock(x.tokens.last, noSyntaxNL = true)
}
baseNoSplit
Expand All @@ -1141,14 +1140,13 @@ class Router(formatOps: FormatOps) {
case NlClosedOnOpen.Yes => getDanglePolicy
case NlClosedOnOpen.No => NoPolicy
}
def nlOnelinePolicy = nextComma
val nlOnelinePolicy = nextCommaOneline
.map(x => splitOneArgPerLineAfterCommaOnBreak(x.right))

Seq(
noSplit.withIndents(noSplitIndents),
Split(nlMod, if (slbOrNL) 0 else nlCost).withIndent(indent)
.withPolicy(nlPolicy & penalizeBrackets)
.andPolicyOpt(nlOnelinePolicy, !oneline),
.withPolicy(nlPolicy & penalizeBrackets & nlOnelinePolicy),
)
}

Expand Down
3 changes: 1 addition & 2 deletions scalafmt-tests/src/test/resources/scalajs/Class.stat
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ class Response(content: BodyInit = null, init: ResponseInit = null)
class Promise[+A](
executor: js.Function2[js.Function1[A | Thenable[A], _], js.Function1[scala.Any, _], _])
>>>
class Promise[+A](
executor: js.Function2[js.Function1[A | Thenable[A], _],
class Promise[+A](executor: js.Function2[js.Function1[A | Thenable[A], _],
js.Function1[scala.Any, _], _])
<<< #270
object a {
Expand Down
4 changes: 2 additions & 2 deletions scalafmt-tests/src/test/resources/scalajs/DefDef.stat
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ object a {
>>>
object a {
object b {
def applyDynamicNamed(name: String)(
fields: (String, Any)*): Object with Dynamic = sys.error("stub")
def applyDynamicNamed(name: String)(fields: (String,
Any)*): Object with Dynamic = sys.error("stub")
}
}
<<< weird breaking #252, oneline
Expand Down
Loading