diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala index 3b0ac5c679..b7e228383c 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/FormatOps.scala @@ -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) @@ -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, @@ -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) diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat index b8e0294af9..423e18812c 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat @@ -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: @@ -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 @@ -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 === @@ -6939,10 +6931,9 @@ 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] === @@ -6950,10 +6941,9 @@ 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] @@ -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