From 60a74398e6107f2b0116dde45ca5b36283f304fc Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 28 Sep 2024 08:43:47 -0700 Subject: [PATCH 1/2] Test partial function within apply --- .../resources/newlines/source_classic.stat | 14 +++++++++++ .../test/resources/newlines/source_fold.stat | 14 +++++++++++ .../test/resources/newlines/source_keep.stat | 23 +++++++++++++++++++ .../resources/newlines/source_unfold.stat | 16 +++++++++++++ 4 files changed, 67 insertions(+) diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat index 855af52694..e6e1efbe63 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_classic.stat @@ -9662,3 +9662,17 @@ object a { } } } +<<< #4133 partial function within apply, short +rdd.map( + {case (id, count) => (count, id)}) +>>> +rdd.map({ case (id, count) => + (count, id) +}) +<<< #4133 partial function within apply, long +maxColumn = 80 +=== +rdd.map( + {case (id, count) => (count, id)}) +>>> +rdd.map({ case (id, count) => (count, id) }) diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat index 7ffd2bd256..731b841de5 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_fold.stat @@ -9062,3 +9062,17 @@ object a { } } } +<<< #4133 partial function within apply, short +rdd.map( + {case (id, count) => (count, id)}) +>>> +rdd.map({ case (id, count) => + (count, id) +}) +<<< #4133 partial function within apply, long +maxColumn = 80 +=== +rdd.map( + {case (id, count) => (count, id)}) +>>> +rdd.map({ case (id, count) => (count, id) }) diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat index b9a84b2ca5..f849f2c014 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat @@ -9459,3 +9459,26 @@ object a { } } } +<<< #4133 partial function within apply, short +rdd.map( + {case (id, count) => (count, id)}) +>>> +Idempotency violated +=> Diff (- obtained, + expected) + rdd.map({ case (id, count) => +- (count, id) +-}) ++ (count, id) ++ } ++) +<<< #4133 partial function within apply, long +maxColumn = 80 +=== +rdd.map( + {case (id, count) => (count, id)}) +>>> +Idempotency violated +=> Diff (- obtained, + expected) +-rdd.map({ case (id, count) => (count, id) }) ++rdd.map({ case (id, count) => (count, id) } ++) diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat index 92f0435190..9d30b5cf52 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_unfold.stat @@ -9739,3 +9739,19 @@ object a { } } } +<<< #4133 partial function within apply, short +rdd.map( + {case (id, count) => (count, id)}) +>>> +rdd.map({ case (id, count) => + (count, id) +}) +<<< #4133 partial function within apply, long +maxColumn = 80 +=== +rdd.map( + {case (id, count) => (count, id)}) +>>> +rdd.map({ case (id, count) => + (count, id) +}) From a85a37b3ea63b785f7db51aa664661c9920f30d8 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sat, 28 Sep 2024 09:00:14 -0700 Subject: [PATCH 2/2] Router: check if "NL" for `(` is actually not NL That will require that the regular no-NL split is produced, as well. --- .../scala/org/scalafmt/internal/Router.scala | 18 +++++++++--------- .../test/resources/newlines/source_keep.stat | 17 ++++------------- 2 files changed, 13 insertions(+), 22 deletions(-) diff --git a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala index 06763c9536..ba973954a5 100644 --- a/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala +++ b/scalafmt-core/shared/src/main/scala/org/scalafmt/internal/Router.scala @@ -923,13 +923,14 @@ class Router(formatOps: FormatOps) { (if (onlyConfigStyle) opensConfigStyleImplicitParamList(ft) else hasImplicitParamList(rightOwner)) + val noSplitForNL = !onlyConfigStyle && right.is[T.LeftBrace] + val skipNoSplit = !noSplitForNL && + (style.newlines.keepBreak(newlines) || { + if (!handleImplicit) onlyConfigStyle + else style.newlines.forceBeforeImplicitParamListModifier + }) val noSplitMod = - if ( - style.newlines.keepBreak(newlines) || { - if (!handleImplicit) onlyConfigStyle - else style.newlines.forceBeforeImplicitParamListModifier - } - ) null + if (skipNoSplit) null else getNoSplitAfterOpening(ft, commentNL = null, spaceOk = !isBracket) val rightIsComment = right.is[T.Comment] @@ -1086,10 +1087,9 @@ class Router(formatOps: FormatOps) { if (multipleArgs) Split(Newline, cost, policy = oneArgOneLine) .withIndent(extraOneArgPerLineIndent) else { - val noSplit = !onlyConfigStyle && right.is[T.LeftBrace] - val noConfigStyle = noSplit || newlinePolicy.isEmpty || + val noConfigStyle = noSplitForNL || newlinePolicy.isEmpty || !configStyleFlag - Split(NoSplit.orNL(noSplit), cost, policy = newlinePolicy) + Split(NoSplit.orNL(noSplitForNL), cost, policy = newlinePolicy) .andPolicy(Policy ? noConfigStyle && singleLine(4)).andPolicy( Policy ? singleArgument && asInfixApp(args.head) .map(InfixSplits(_, ft).nlPolicy), diff --git a/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat b/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat index f849f2c014..eaabfbff81 100644 --- a/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat +++ b/scalafmt-tests/shared/src/test/resources/newlines/source_keep.stat @@ -9463,22 +9463,13 @@ object a { rdd.map( {case (id, count) => (count, id)}) >>> -Idempotency violated -=> Diff (- obtained, + expected) - rdd.map({ case (id, count) => -- (count, id) --}) -+ (count, id) -+ } -+) +rdd.map({ case (id, count) => + (count, id) +}) <<< #4133 partial function within apply, long maxColumn = 80 === rdd.map( {case (id, count) => (count, id)}) >>> -Idempotency violated -=> Diff (- obtained, + expected) --rdd.map({ case (id, count) => (count, id) }) -+rdd.map({ case (id, count) => (count, id) } -+) +rdd.map({ case (id, count) => (count, id) })