From 875b020e86ee41728c15a6daae3964d93af4e166 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 9 Jan 2022 08:31:12 -0800 Subject: [PATCH 1/2] Add side-by-side for-yield and for-do tests --- .../src/test/resources/newlines/source_unfold.stat | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat index 68f5c700cc..bfce959ff3 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat @@ -655,10 +655,18 @@ def attributes = maxColumn = 80 === val attributes = - for (i ← 1 to count) - yield i + for (i ← 1 to count) yield i >>> val attributes = for (i ← 1 to count) yield i +<<< 2.14 val with short for/do +maxColumn = 80 +=== +val attributes = + for (i ← 1 to count) i * i +>>> +val attributes = + for (i ← 1 to count) + i * i <<< 2.15 val with literal apply object a { val a = b(0x1F, // ID1 From 5c86a924e87c86fbc3bad4be667bfaa1d144f074 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 9 Jan 2022 08:18:31 -0800 Subject: [PATCH 2/2] FormatOps: unfold yield correctly and consistently We were handling it differently in some cases, and inconsistent with `while` and other `for`. --- .../main/scala/org/scalafmt/internal/FormatOps.scala | 10 ++-------- .../src/test/resources/newlines/source_unfold.stat | 4 +++- 2 files changed, 5 insertions(+), 9 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 213bfc96ff..db798396ae 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 @@ -1745,14 +1745,8 @@ class FormatOps( def slbSplit(end: T)(implicit fileLine: FileLine) = Split(Space, 0).withSingleLine(end, noSyntaxNL = true) body match { - case _: Term.ForYield => - // unfold policy on yield forces a break - // revert it if we are attempting a single line - val noBreakOnYield = Policy.before(expire) { - case Decision(ft, s) if s.isEmpty && ft.right.is[T.KwYield] => - Seq(Split(Space, 0)) - } - slbSplit(expire).andPolicy(noBreakOnYield) + // we force newlines in for/yield + case _: Term.ForYield => Split.ignored // we force newlines in try/catch/finally case _: Term.Try | _: Term.TryWithHandler => Split.ignored // don't tuck curried apply diff --git a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat index bfce959ff3..5d7ae7ed1d 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat @@ -657,7 +657,9 @@ maxColumn = 80 val attributes = for (i ← 1 to count) yield i >>> -val attributes = for (i ← 1 to count) yield i +val attributes = + for (i ← 1 to count) + yield i <<< 2.14 val with short for/do maxColumn = 80 ===