Skip to content

Commit

Permalink
Router: extend single line for bp.defnsite=oneline
Browse files Browse the repository at this point in the history
Include comma and any attached comments after it.
  • Loading branch information
kitbellew committed May 16, 2024
1 parent 713e9c4 commit abeb397
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 26 deletions.
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

0 comments on commit abeb397

Please sign in to comment.