From dfe10473fedeb329ea4d7ab5ef234739fe63626f Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Wed, 7 Aug 2024 07:59:40 +0300 Subject: [PATCH] FormatOps: Ctrl body can be tuple in removed block --- .../org/scalafmt/internal/FormatOps.scala | 18 ++++---- .../test/resources/scala3/OptionalBraces.stat | 45 +++++-------------- .../resources/scala3/OptionalBraces_fold.stat | 23 +++++----- .../resources/scala3/OptionalBraces_keep.stat | 23 +++++----- .../scala3/OptionalBraces_unfold.stat | 23 +++++----- 5 files changed, 60 insertions(+), 72 deletions(-) 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 c958992e0a..44f16d33c4 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 @@ -1819,18 +1819,20 @@ class FormatOps( val treeTokens = tree.tokens val end = getLast(treeTokens, tree) val slbExpire = nextNonCommentSameLine(end).left - val closeOpt = - if (isTuple(tree)) None - else { + def head = getHead(treeTokens, tree) + val close = (tree match { + case _: Member.Tuple => None + case Term.Block((_: Member.Tuple) :: Nil) + if !head.left.is[T.LeftBrace] => None + case _ => val maybeClose = prevNonComment(end) - tokens.getClosingIfInParens(maybeClose)(getHead(treeTokens, tree)) + tokens.getClosingIfInParens(maybeClose)(head) .map(prevNonComment(_).left) - } + }).getOrElse(slbExpire) def nlPolicy(implicit fileLine: FileLine) = Policy ? danglingKeyword && - decideNewlinesOnlyAfterClose(closeOpt.getOrElse(slbExpire)) + decideNewlinesOnlyAfterClose(close) val indentLen = indentOpt.getOrElse(style.indent.getSignificant) - val indent = - Indent(Num(indentLen), closeOpt.getOrElse(slbExpire), ExpiresOn.After) + val indent = Indent(Num(indentLen), close, ExpiresOn.After) if (ft.hasBlankLine) Seq(Split(Newline2x, 0).withIndent(indent).withPolicy(nlPolicy)) else if (forceNL) diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat index 19d1f8db53..9065a4fb69 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces.stat @@ -5596,39 +5596,16 @@ object a { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - "only in packlib classpath" -- ) -+ ) - else - "only in hashbang classpath" -- ) -+ ) - } -<<< convert to new if-then syntax, remove redundant braces, longer -maxColumn = 77 -rewrite { - rules = [RedundantBraces] - scala3.convertToNewSyntax = true - redundantBraces.ifElseExpressions = yes -} -=== object a { - val (diff: Set[File], msg: String) = if (packlibJars.size > hashbangClasspathJars.size) { - (packlibJars.toSet -- hashbangJars.toSet , "only in packlib classpath") - } else { - (hashbangJars.toSet -- packlibJars.toSet , "only in hashbang classpath") - } + val (diff: Set[File], msg: String) = + if packlibJars.size > hashbangClasspathJars.size then + ( + packlibJars.toSet -- hashbangJars.toSet, + "only in packlib classpath" + ) + else + ( + hashbangJars.toSet -- packlibJars.toSet, + "only in hashbang classpath" + ) } ->>> -Idempotency violated -=> Diff (- obtained, + expected) - else -- ( -- hashbangJars.toSet -- packlibJars.toSet, -- "only in hashbang classpath" -- ) -+ (hashbangJars.toSet -- packlibJars.toSet, "only in hashbang classpath" -+ ) - } diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat index 1a6806f3b9..f317205444 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_fold.stat @@ -5352,13 +5352,16 @@ object a { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - "only in packlib classpath" -- ) -+ ) - else - "only in hashbang classpath" -- ) -+ ) - } +object a { + val (diff: Set[File], msg: String) = + if packlibJars.size > hashbangClasspathJars.size then + ( + packlibJars.toSet -- hashbangJars.toSet, + "only in packlib classpath" + ) + else + ( + hashbangJars.toSet -- packlibJars.toSet, + "only in hashbang classpath" + ) +} diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat index 7d9b63b7fe..a237cf4ca8 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_keep.stat @@ -5637,13 +5637,16 @@ object a { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - "only in packlib classpath" -- ) -+ ) - else - "only in hashbang classpath" -- ) -+ ) - } +object a { + val (diff: Set[File], msg: String) = + if packlibJars.size > hashbangClasspathJars.size then + ( + packlibJars.toSet -- hashbangJars.toSet, + "only in packlib classpath" + ) + else + ( + hashbangJars.toSet -- packlibJars.toSet, + "only in hashbang classpath" + ) +} diff --git a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat index e185f6246b..3521baf4a8 100644 --- a/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat +++ b/scalafmt-tests/src/test/resources/scala3/OptionalBraces_unfold.stat @@ -5780,13 +5780,16 @@ object a { } } >>> -Idempotency violated -=> Diff (- obtained, + expected) - "only in packlib classpath" -- ) -+ ) - else - "only in hashbang classpath" -- ) -+ ) - } +object a { + val (diff: Set[File], msg: String) = + if packlibJars.size > hashbangClasspathJars.size then + ( + packlibJars.toSet -- hashbangJars.toSet, + "only in packlib classpath" + ) + else + ( + hashbangJars.toSet -- packlibJars.toSet, + "only in hashbang classpath" + ) +}