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: extend single line for bp.defnsite=oneline #4000

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 @@ -1072,7 +1072,6 @@ class Router(formatOps: FormatOps) {
if style.binPack.defnSiteFor(open) != BinPack.Site.Never &&
isParamClauseSite(leftOwner) =>
val close = matching(open)
def slbPolicy = SingleLineBlock(close, okSLC = true, noSyntaxNL = true)
val noSplitMod = Space(style.spaces.inParentheses)
if (close eq right) Seq(Split(noSplitMod, 0))
else {
Expand All @@ -1090,8 +1089,7 @@ class Router(formatOps: FormatOps) {
.map(p => PenalizeAllNewlines(close, p + 3))
val afterClose = after(close)

val argsHeadOpt = argumentStarts.get(ft.meta.idx)
val nextCommaOneline = argsHeadOpt.flatMap { x =>
val nextCommaOneline = argumentStarts.get(ft.meta.idx).flatMap { x =>
val noNeed = isSeqSingle(getArgs(leftOwner)) ||
style.binPack.defnSiteFor(isBracket) != BinPack.Site.Oneline
if (noNeed) None else findFirstOnRight[T.Comma](getLast(x), close)
Expand All @@ -1104,13 +1102,17 @@ class Router(formatOps: FormatOps) {
val slbOrNL = nlOnly || noNLPolicy == null

val rightIsComment = right.is[T.Comment]
def baseNoSplit(implicit fileLine: FileLine) =
Split(if (rightIsComment) Space.orNL(noBreak()) else noSplitMod, 0)
def getNoSplit(slbEnd: Option[T])(implicit fileLine: FileLine) = {
val mod = if (rightIsComment) Space.orNL(noBreak()) else noSplitMod
slbEnd.fold(Split(mod, 0)) { x =>
Split(mod, 0).withOptimalToken(x, killOnFail = true)
.withPolicy(SingleLineBlock(x, okSLC = true, noSyntaxNL = true))
}
}

val noSplit =
if (nlOnly) Split.ignored
else if (slbOrNL) baseNoSplit.withPolicy(slbPolicy)
.withOptimalToken(close, killOnFail = true)
else if (slbOrNL) getNoSplit(Some(close))
else {
val opensPolicy = bracketPenalty.fold(Policy.noPolicy) { p =>
Policy.before(close) {
Expand All @@ -1121,13 +1123,8 @@ class Router(formatOps: FormatOps) {
else s.map(x => if (x.isNL) x.withPenalty(p) else x)
}
}
val argPolicy = argsHeadOpt.fold(Policy.noPolicy) { x =>
if (nextCommaOneline.isEmpty) NoPolicy
else SingleLineBlock(x.tokens.last, noSyntaxNL = true)
}
baseNoSplit
.withPolicy((opensPolicy | penalizeBrackets) & noNLPolicy())
.andPolicy(argPolicy)
getNoSplit(nextCommaOneline.map(endOfSingleLineBlock))
.andPolicy((opensPolicy | penalizeBrackets) & noNLPolicy())
}

def nlCost = bracketPenalty.getOrElse(1)
Expand Down
5 changes: 2 additions & 3 deletions scalafmt-tests/src/test/resources/default/TypeArguments.stat
Original file line number Diff line number Diff line change
Expand Up @@ -453,9 +453,8 @@ def props[M[_[_]], F[_]: Async, I: KeyDecoder, State, Event: PersistentEncoder:
object a {
def deploy[M[_[_]]: FunctorK, F[_], State, Event: PersistentEncoder: PersistentDecoder,
K: KeyEncoder: KeyDecoder] = ???
def props[
M[_[_]], F[_]: Async, I: KeyDecoder, State, Event: PersistentEncoder: PersistentDecoder]() =
???
def props[M[_[_]], F[_]: Async, I: KeyDecoder, State,
Event: PersistentEncoder: PersistentDecoder]() = ???
}
<<< #2739 bracketDefnSite = always, danglingParentheses
maxColumn = 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5050,11 +5050,9 @@ object a {
}
>>>
object a {
def foo(
bb: BB,
cc: CC,
dd: DD = DDD.ddd
): Bar[Baz] = {
def foo(bb: BB, cc: CC, dd: DD = DDD.ddd): Bar[
Baz
] = {
// c
qux
}
Expand Down
7 changes: 3 additions & 4 deletions scalafmt-tests/src/test/resources/scalajs/DefDef.stat
Original file line number Diff line number Diff line change
Expand Up @@ -509,8 +509,8 @@ object a {
}
>>>
object a {
implicit def toFunction12[
VeryVeryVeryVeryVeryVeryVeryVeryVeryLongTypeParam, R](
implicit def toFunction12[VeryVeryVeryVeryVeryVeryVeryVeryVeryLongTypeParam,
R](
f: js.Function12[VeryVeryVeryVeryVeryVeryVeryVeryVeryLongTypeParam, R])
: scala.Function12[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12,
R] =
Expand All @@ -530,8 +530,7 @@ object a {
}
>>>
object a {
def setTimeout(
handler: js.Function0[Any], interval: Double): SetTimeoutHandle =
def setTimeout(handler: js.Function0[Any], interval: Double): SetTimeoutHandle =
js.native
}
<<< break at maxColumn, with overflow enabled 2
Expand Down
Loading