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: recurse only SLB in ctrl body for keep #4246

Merged
merged 1 commit into from
Sep 12, 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 @@ -1478,7 +1478,7 @@ class FormatOps(
nlSplitFunc: Int => Split,
isKeep: Boolean,
spaceIndents: Seq[Indent] = Seq.empty,
)(implicit style: ScalafmtConfig): Seq[Split] = {
)(implicit style: ScalafmtConfig, ft: FormatToken): Seq[Split] = {
val btokens = body.tokens
def bheadFT = getHead(btokens, body)
val blastFT = getLastNonTrivial(btokens, body)
Expand Down Expand Up @@ -1507,7 +1507,12 @@ class FormatOps(
fileLine: FileLine,
) = {
val spacePolicy = policy | penalize(penalty)
Split(Space, 0).withPolicy(spacePolicy).withOptimalToken(blast)
val miniSlbEnd = rhsOptimalToken(next(ft), blastFT.right.end)
val slbLite = style.newlines.keep
val opt = if (slbLite) miniSlbEnd else blast
Split(Space, 0).withSingleLineNoOptimal(miniSlbEnd, noSyntaxNL = true)
.andPolicy(spacePolicy)
.withOptimalToken(opt, killOnFail = slbLite, recurseOnly = slbLite)
}
def getPolicySplits(penalty: Int, policy: Policy, nlCost: Int = 1)(
implicit fileLine: FileLine,
Expand Down Expand Up @@ -1578,7 +1583,7 @@ class FormatOps(
nlSplitFunc: Int => Split,
isKeep: Boolean,
spaceIndents: Seq[Indent],
)(implicit style: ScalafmtConfig): Seq[Split] =
)(implicit style: ScalafmtConfig, ft: FormatToken): Seq[Split] =
if (body.tokens.isEmpty) Seq(Split(Space, 0))
else foldedNonEmptyNonComment(body, nlSplitFunc, isKeep, spaceIndents)

Expand Down
70 changes: 27 additions & 43 deletions scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -6097,10 +6097,9 @@ object a:
Context
): List[Symbol] =
if (!sym.owner.isClass) Nil
else
sym.allOverriddenSymbols.toList.filter(
_ != NoSymbol
) // TODO: could also be `sym.owner.allOverrid..`
else sym.allOverriddenSymbols.toList.filter(
_ != NoSymbol
) // TODO: could also be `sym.owner.allOverrid..`
// else sym.owner.ancestors map (sym overriddenSymbol _) filter (_ != NoSymbol)
<<< braceless catch, single multi-line case, empty body
object a:
Expand Down Expand Up @@ -6777,18 +6776,13 @@ object a:
for template <- templates do
val current_Map_String__Object = template_templateFile_settings_getOrElse("page", Map.empty).asInstanceOf[Map_String_Object]
>>>
Idempotency violated
=> Diff (- obtained, + expected)
for template <- templates do
- val current_Map_String__Object =
- template_templateFile_settings_getOrElse(
- "page",
- Map.empty
- ).asInstanceOf[Map_String_Object]
+ val current_Map_String__Object = template_templateFile_settings_getOrElse(
+ "page",
+ Map.empty
+ ).asInstanceOf[Map_String_Object]
object a:
def foo =
for template <- templates do
val current_Map_String__Object =
template_templateFile_settings_getOrElse("page", Map.empty).asInstanceOf[
Map_String_Object
]
<<< scala.js overflow within for-yield !dangling
binPack.preset = always
danglingParentheses.preset = false
Expand All @@ -6798,14 +6792,12 @@ object a:
for template <- templates do
val current_Map_String__Object = template_templateFile_settings_getOrElse("page", Map.empty).asInstanceOf[Map_String_Object]
>>>
Idempotency violated
=> Diff (- obtained, + expected)
for template <- templates do
- val current_Map_String__Object =
- template_templateFile_settings_getOrElse(
- "page", Map.empty).asInstanceOf[Map_String_Object]
+ val current_Map_String__Object = template_templateFile_settings_getOrElse(
+ "page", Map.empty).asInstanceOf[Map_String_Object]
object a:
def foo =
for template <- templates do
val current_Map_String__Object =
template_templateFile_settings_getOrElse("page", Map.empty).asInstanceOf[
Map_String_Object]
<<< #4133 for-yield with rewritten body
rewrite.scala3.removeOptionalBraces = yes
===
Expand Down Expand Up @@ -6939,21 +6931,19 @@ object a:
val hashbangClasspathJars = hashbangJars.map { _.name }.sorted.distinct // get jar basenames, remove duplicates
>>>
object a:
val hashbangClasspathJars =
hashbangJars.map {
_.name
}.sorted.distinct // get jar basenames, remove duplicates
val hashbangClasspathJars = hashbangJars.map {
_.name
}.sorted.distinct // get jar basenames, remove duplicates
<<< #4133 avoid braces to parens rewrite when overflow 2
rewrite.rules = [RedundantBraces]
===
object a:
val hashbangClasspathJars = hashbangJars_map { _.name }.sorted_distinct // get jar basenames, remove duplicates
>>>
object a:
val hashbangClasspathJars =
hashbangJars_map {
_.name
}.sorted_distinct // get jar basenames, remove duplicates
val hashbangClasspathJars = hashbangJars_map {
_.name
}.sorted_distinct // get jar basenames, remove duplicates
<<< redundant block within block, outer semicolon-terminated
rewrite {
rules = [RedundantBraces]
Expand Down Expand Up @@ -7055,17 +7045,11 @@ object a {
@threadUnsafe lazy val AnnotationRetentionSourceAttr: TermSymbol = requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue("SOURCE")
}
>>>
Idempotency violated
=> Diff (- obtained, + expected)
object a {
- @threadUnsafe lazy val AnnotationRetentionSourceAttr: TermSymbol =
- requiredClass(
- "java.lang.annotation.RetentionPolicy").linkedClass.requiredValue(
- "SOURCE")
+ @threadUnsafe lazy val AnnotationRetentionSourceAttr: TermSymbol = requiredClass(
+ "java.lang.annotation.RetentionPolicy").linkedClass.requiredValue(
+ "SOURCE")
}
object a {
@threadUnsafe lazy val AnnotationRetentionSourceAttr: TermSymbol =
requiredClass("java.lang.annotation.RetentionPolicy").linkedClass.requiredValue(
"SOURCE")
}
<<< #4133 overflow apply with binpack, dangling
maxColumn = 70
binPack.preset = always
Expand Down
Loading