Skip to content

Commit

Permalink
BestFirstSearch: recurse within parens as well
Browse files Browse the repository at this point in the history
Also, remove check for statements within braces.
  • Loading branch information
kitbellew committed Sep 25, 2024
1 parent 3112829 commit 817af95
Show file tree
Hide file tree
Showing 6 changed files with 346 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ private class BestFirstSearch private (range: Set[Range])(implicit

private def getBlockCloseToRecurse(ft: FormatToken, stop: Token)(implicit
style: ScalafmtConfig,
): Option[Token] = getEndOfBlockOrOptionalBraces(ft).flatMap {
case Left(x) => Some(x)
case Right(x) =>
if (extractStatementsIfAny(ft.meta.leftOwner).isEmpty) None else Some(x)
}.filter { close =>
): Option[Token] = getEndOfBlock(ft, parensToo = true).filter { close =>
// Block must span at least 3 lines to be worth recursing.
close != stop && distance(ft.left, close) > style.maxColumn * 3
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2675,18 +2675,13 @@ class FormatOps(
def getLastNonTrivialTokenOpt(tree: Tree): Option[T] = tokens
.getLastNonTrivialOpt(tree).map(_.left)

def getEndOfBlock(ft: FormatToken, parensToo: Boolean)(implicit
def getEndOfBlock(ft: FormatToken, parensToo: => Boolean)(implicit
style: ScalafmtConfig,
): Option[T] = getEndOfBlockOrOptionalBraces(ft, parensToo).map(_.merge)

def getEndOfBlockOrOptionalBraces(ft: FormatToken, parensToo: Boolean = false)(
implicit style: ScalafmtConfig,
): Option[Either[T, T]] = ft.left match {
case x: T.LeftBrace => matchingOpt(x).map(Right.apply)
case x: T.LeftParen =>
if (parensToo) matchingOpt(x).map(Right.apply) else None
): Option[T] = ft.left match {
case x: T.LeftBrace => matchingOpt(x)
case x: T.LeftParen => if (parensToo) matchingOpt(x) else None
case _ => OptionalBraces.get(ft)
.flatMap(_.rightBrace.map(x => Left(nextNonCommentSameLine(x).left)))
.flatMap(_.rightBrace.map(x => nextNonCommentSameLine(x).left))
}

def isCloseDelimForTrailingCommasMultiple(ft: FormatToken): Boolean =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ val createAsStat = if (semantics.asInstanceOfs == Unchecked) {
val createAsStat = if (semantics.asInstanceOfs == Unchecked) {
js.Skip()
} else {
envFieldDef(
"as",
className,
js.Function(List(objParam),
envFieldDef("as",
className,
js.Function(
List(objParam),
js.Return(className match {
case Definitions.ObjectClass => obj
case _ =>
Expand Down
183 changes: 91 additions & 92 deletions scalafmt-tests/shared/src/test/resources/default/SearchState.stat
Original file line number Diff line number Diff line change
Expand Up @@ -354,105 +354,104 @@
)
))
>>>
EventsResponse.fromJson(json, run).success.value.events should be(
Seq(
Stored(
timestamp = Instant.parse("2016-12-02T16:23:47.412278Z"),
id = "OKO0c_F1RiWU44-qLwYYuA",
storage = Storage(
"https://se.api.mailgun.net/v3/domains/sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org/messages/eyJwIjpmYWxzZSwiayI6ImUzZWQ3YjYyLTI4YmMtNDZiYy04MGQ3LTg0NTUyZDc4YzU4MSIsInMiOiJlZDY3MzYyNWRhIiwiYyI6InRhbmtiIn0=",
"eyJwIjpmYWxzZSwiayI6ImUzZWQ3YjYyLTI4YmMtNDZiYy04MGQ3LTg0NTUyZDc4YzU4MSIsInMiOiJlZDY3MzYyNWRhIiwiYyI6InRhbmtiIn0="
),
`user-variables` = Map(
"custom" -> """{"createdAt":"2016-12-02T16:23:46.637Z","customerId":"canary","traceToken":"22629a34-e5ef-4d2a-bbd4-5193af11e90d","canary":true}"""
),
flags = Map(
"is-test-mode" -> false
),
tags = Nil,
`log-level` = "info",
message = MessageSummary(
Map(
"to" -> "[email protected]",
"message-id" -> "20161202162347.92318.58763.6224A6CC@sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org",
"from" -> "Ovo Energy <[email protected]>",
"subject" -> "Canary test email (22629a34-e5ef-4d2a-bbd4-5193af11e90d)"
),
Nil,
Seq("[email protected]"),
20430
)
EventsResponse.fromJson(json, run).success.value.events should be(Seq(
Stored(
timestamp = Instant.parse("2016-12-02T16:23:47.412278Z"),
id = "OKO0c_F1RiWU44-qLwYYuA",
storage = Storage(
"https://se.api.mailgun.net/v3/domains/sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org/messages/eyJwIjpmYWxzZSwiayI6ImUzZWQ3YjYyLTI4YmMtNDZiYy04MGQ3LTg0NTUyZDc4YzU4MSIsInMiOiJlZDY3MzYyNWRhIiwiYyI6InRhbmtiIn0=",
"eyJwIjpmYWxzZSwiayI6ImUzZWQ3YjYyLTI4YmMtNDZiYy04MGQ3LTg0NTUyZDc4YzU4MSIsInMiOiJlZDY3MzYyNWRhIiwiYyI6InRhbmtiIn0="
),
Accepted(
timestamp = Instant.parse("2016-12-02T16:23:47.124505Z"),
id = "N97VBM3jRIGdpHExzz47mg",
tags = Nil,
envelope = Map(
"sender" -> "[email protected]",
"transport" -> "smtp",
"targets" -> "[email protected]"
),
`user-variables` = Map(
"custom" -> """{"createdAt":"2016-12-02T16:23:46.637Z","customerId":"canary","traceToken":"22629a34-e5ef-4d2a-bbd4-5193af11e90d","canary":true}"""
),
storage = Some(Storage(
"https://se.api.mailgun.net/v3/domains/sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org/messages/eyJwIjpmYWxzZSwiayI6ImUzZWQ3YjYyLTI4YmMtNDZiYy04MGQ3LTg0NTUyZDc4YzU4MSIsInMiOiJlZDY3MzYyNWRhIiwiYyI6InRhbmtiIn0=",
"eyJwIjpmYWxzZSwiayI6ImUzZWQ3YjYyLTI4YmMtNDZiYy04MGQ3LTg0NTUyZDc4YzU4MSIsInMiOiJlZDY3MzYyNWRhIiwiYyI6InRhbmtiIn0="
)),
recipient =
"[email protected]",
`recipient-domain` =
"sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org",
method = "http",
flags = Map(
"is-routed" -> false,
"is-authenticated" -> true,
"is-system-test" -> false,
"is-test-mode" -> false
`user-variables` = Map(
"custom" -> """{"createdAt":"2016-12-02T16:23:46.637Z","customerId":"canary","traceToken":"22629a34-e5ef-4d2a-bbd4-5193af11e90d","canary":true}"""
),
flags = Map(
"is-test-mode" -> false
),
tags = Nil,
`log-level` = "info",
message = MessageSummary(
Map(
"to" -> "[email protected]",
"message-id" -> "20161202162347.92318.58763.6224A6CC@sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org",
"from" -> "Ovo Energy <[email protected]>",
"subject" -> "Canary test email (22629a34-e5ef-4d2a-bbd4-5193af11e90d)"
),
`log-level` = "info",
message = MessageSummary(
Map(
"to" -> "[email protected]",
"message-id" -> "20161202162347.92318.58763.6224A6CC@sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org",
"from" -> "Ovo Energy <[email protected]>",
"subject" -> "Canary test email (22629a34-e5ef-4d2a-bbd4-5193af11e90d)"
),
Nil,
Seq("[email protected]"),
20430
)
Nil,
Seq("[email protected]"),
20430
)
),
Accepted(
timestamp = Instant.parse("2016-12-02T16:23:47.124505Z"),
id = "N97VBM3jRIGdpHExzz47mg",
tags = Nil,
envelope = Map(
"sender" -> "[email protected]",
"transport" -> "smtp",
"targets" -> "[email protected]"
),
`user-variables` = Map(
"custom" -> """{"createdAt":"2016-12-02T16:23:46.637Z","customerId":"canary","traceToken":"22629a34-e5ef-4d2a-bbd4-5193af11e90d","canary":true}"""
),
Stored(
timestamp = Instant.parse("2016-12-02T15:53:47.045888Z"),
id = "-KayH5ZASKm2AaQQ1wizDw",
storage = Storage(
"https://si.api.mailgun.net/v3/domains/sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org/messages/eyJwIjpmYWxzZSwiayI6IjViNjhkN2QwLWEwZTYtNDU4Yy1hOGJiLTk2ZTY0MWI3MTNhNSIsInMiOiI1ZDY4YWE5M2FmIiwiYyI6InNiaWFkIn0=",
"eyJwIjpmYWxzZSwiayI6IjViNjhkN2QwLWEwZTYtNDU4Yy1hOGJiLTk2ZTY0MWI3MTNhNSIsInMiOiI1ZDY4YWE5M2FmIiwiYyI6InNiaWFkIn0="
storage = Some(Storage(
"https://se.api.mailgun.net/v3/domains/sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org/messages/eyJwIjpmYWxzZSwiayI6ImUzZWQ3YjYyLTI4YmMtNDZiYy04MGQ3LTg0NTUyZDc4YzU4MSIsInMiOiJlZDY3MzYyNWRhIiwiYyI6InRhbmtiIn0=",
"eyJwIjpmYWxzZSwiayI6ImUzZWQ3YjYyLTI4YmMtNDZiYy04MGQ3LTg0NTUyZDc4YzU4MSIsInMiOiJlZDY3MzYyNWRhIiwiYyI6InRhbmtiIn0="
)),
recipient =
"[email protected]",
`recipient-domain` =
"sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org",
method = "http",
flags = Map(
"is-routed" -> false,
"is-authenticated" -> true,
"is-system-test" -> false,
"is-test-mode" -> false
),
`log-level` = "info",
message = MessageSummary(
Map(
"to" -> "[email protected]",
"message-id" -> "20161202162347.92318.58763.6224A6CC@sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org",
"from" -> "Ovo Energy <[email protected]>",
"subject" -> "Canary test email (22629a34-e5ef-4d2a-bbd4-5193af11e90d)"
),
`user-variables` = Map(
"custom" -> """{"createdAt":"2016-12-02T15:53:45.732Z","customerId":"canary","traceToken":"769b0d0e-0f89-48b6-8a07-5a459bbdc2ab","canary":true}"""
Nil,
Seq("[email protected]"),
20430
)
),
Stored(
timestamp = Instant.parse("2016-12-02T15:53:47.045888Z"),
id = "-KayH5ZASKm2AaQQ1wizDw",
storage = Storage(
"https://si.api.mailgun.net/v3/domains/sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org/messages/eyJwIjpmYWxzZSwiayI6IjViNjhkN2QwLWEwZTYtNDU4Yy1hOGJiLTk2ZTY0MWI3MTNhNSIsInMiOiI1ZDY4YWE5M2FmIiwiYyI6InNiaWFkIn0=",
"eyJwIjpmYWxzZSwiayI6IjViNjhkN2QwLWEwZTYtNDU4Yy1hOGJiLTk2ZTY0MWI3MTNhNSIsInMiOiI1ZDY4YWE5M2FmIiwiYyI6InNiaWFkIn0="
),
`user-variables` = Map(
"custom" -> """{"createdAt":"2016-12-02T15:53:45.732Z","customerId":"canary","traceToken":"769b0d0e-0f89-48b6-8a07-5a459bbdc2ab","canary":true}"""
),
flags = Map(
"is-test-mode" -> false
),
tags = Nil,
`log-level` = "info",
message = MessageSummary(
Map(
"to" -> "[email protected]",
"message-id" -> "20161202155346.30852.16325.1915382A@sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org",
"from" -> "Ovo Energy <[email protected]>",
"subject" -> "Canary test email (769b0d0e-0f89-48b6-8a07-5a459bbdc2ab)"
),
flags = Map(
"is-test-mode" -> false
Nil,
Seq(
"[email protected]"
),
tags = Nil,
`log-level` = "info",
message = MessageSummary(
Map(
"to" -> "[email protected]",
"message-id" -> "20161202155346.30852.16325.1915382A@sandbox98d59d0a8d0a4af588f2bb683a4a57cc.mailgun.org",
"from" -> "Ovo Energy <[email protected]>",
"subject" -> "Canary test email (769b0d0e-0f89-48b6-8a07-5a459bbdc2ab)"
),
Nil,
Seq(
"[email protected]"
),
20430
)
20430
)
))
)
))
<<< react #458
val TodoApp = ReactComponentB[Unit]("TodoApp")
.initialState(State(Nil, "")).renderS(
Expand Down
88 changes: 65 additions & 23 deletions scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -6898,27 +6898,24 @@ object a {
}
>>>
object a {
buffer.append((if (!fetchContent) {
DiffInfo(
changeType = ChangeType.ADD,
newContent = None,
newObjectId = Option(treeWalk.getObjectId(0))
.map(_.name),
tooLarge = false
)
} else {
DiffInfo(
changeType = ChangeType.ADD,
newContent = JGitUtil.getContentFromId(
git,
treeWalk.getObjectId(0),
false
).filter(FileUtil.isText).map(convertFromByteArray),
newObjectId = Option(treeWalk.getObjectId(0))
.map(_.name),
tooLarge = false
)
}))
buffer
.append((if (!fetchContent) {
DiffInfo(
changeType = ChangeType.ADD,
newContent = None,
newObjectId = Option(treeWalk.getObjectId(0)).map(_.name),
tooLarge = false
)
} else {
DiffInfo(
changeType = ChangeType.ADD,
newContent = JGitUtil
.getContentFromId(git, treeWalk.getObjectId(0), false)
.filter(FileUtil.isText).map(convertFromByteArray),
newObjectId = Option(treeWalk.getObjectId(0)).map(_.name),
tooLarge = false
)
}))
}
<<< SM 4.7.0: 29.3
maxColumn = 75
Expand Down Expand Up @@ -8322,7 +8319,7 @@ object a {
val foo = barBarBarBarBarBarBar;
val baz = foo
}
<<< SKIP #4133 binpack with complex nested applies and not forcing config style, !dangling
<<< #4133 binpack with complex nested applies and not forcing config style, !dangling
binPack.preset = always
danglingParentheses.preset = false
runner.optimizer {
Expand Down Expand Up @@ -8361,7 +8358,52 @@ object a {
)
}
>>>
FormatTests:54 [e]: org.scalafmt.Error$SearchStateExploded: Search state exploded on '[227] )∙(: RightParen [1072..1073) [] LeftParen [1073..1074)', line 27: exceeded `runner.maxStateVisits` [see https://scalameta.org/scalafmt/docs/configuration.html#search-state-exploded]
object a {
div(cls := "cover")(
div(cls := "doc")(bodyContents),
Option.when(full)(
section(id := "attributes")(
Option.when(
memberTypeParams.nonEmpty)(
Seq(
h2(cls := "h500")(
"Type parameters"),
dl(cls := "attributes")(
memberTypeParams *)))
.toSeq.flatten,
Option.when(
memberValueParams.nonEmpty)(
Seq(
h2(cls := "h500")(
"Value parameters"),
dl(cls := "attributes")(
memberValueParams *)))
.toSeq.flatten,
h2(cls := "h500")("Attributes"),
dl(cls := "attributes")(
attributes *))).getOrElse(
Option.when(
memberTypeParams.nonEmpty)(
Seq(
h2(cls := "h200")(
"Type parameters"),
dl(
cls := "attributes attributes-small")(
memberTypeParams *))).toSeq
.flatten ++ Option.when(
memberValueParams.nonEmpty)(
Seq(
h2(cls := "h200")(
"Value parameters"),
dl(
cls := "attributes attributes-small")(
memberValueParams *)))
.toSeq.flatten :+ h2(
cls := "h200")("Attributes") :+ dl(
cls :=
"attributes attributes-small")(
attributes *)))
}
<<< #4133 binpack with complex nested applies and not forcing config style, dangling
binPack.preset = always
danglingParentheses.preset = true
Expand Down
Loading

0 comments on commit 817af95

Please sign in to comment.