From 614fd5c90661ee2a42da3fbaf71d8eae645e5db4 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 30 Oct 2022 09:02:13 -0700 Subject: [PATCH 1/2] Add tests for if-while-for body starting with `(` --- .../src/test/resources/default/For.stat | 18 ++++++++++++++++++ .../src/test/resources/default/If.stat | 18 ++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/scalafmt-tests/src/test/resources/default/For.stat b/scalafmt-tests/src/test/resources/default/For.stat index a502a31bb3..f7841daaf3 100644 --- a/scalafmt-tests/src/test/resources/default/For.stat +++ b/scalafmt-tests/src/test/resources/default/For.stat @@ -213,3 +213,21 @@ object a { } a + b } +<<< body starts with lparen +object a { + for (b <- c) (d(e)) + for (b <- c) (d.e(f)) + for (b <- c) (d(e)) + f + for {b <- c} (d(e)) + for {b <- c} (d.e(f)) + for {b <- c} (d(e)) + f +} +>>> +object a { + for (b <- c)(d(e)) + for (b <- c)(d.e(f)) + for (b <- c)(d(e)) + f + for { b <- c }(d(e)) + for { b <- c }(d.e(f)) + for { b <- c }(d(e)) + f +} diff --git a/scalafmt-tests/src/test/resources/default/If.stat b/scalafmt-tests/src/test/resources/default/If.stat index d77383597a..71fcdb3da4 100644 --- a/scalafmt-tests/src/test/resources/default/If.stat +++ b/scalafmt-tests/src/test/resources/default/If.stat @@ -363,3 +363,21 @@ if (host.indexOf(':') != -1 && host.indexOf(']') == -1 && host.indexOf('[') == -1) { hostVar = "[" + host + "]" } +<<< body starts with lparen +object a { + if (true) (d(e)) + if (true) (d.e(f)) + if (true) (d(e)) + f + while (true) (d(e)) + while (true) (d.e(f)) + while (true) (d(e)) + f +} +>>> +object a { + if (true)(d(e)) + if (true)(d.e(f)) + if (true)(d(e)) + f + while (true)(d(e)) + while (true)(d.e(f)) + while (true)(d(e)) + f +} From b3e9fbc96937f0f4f145d1fe812ae93f7c8ef49d Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 30 Oct 2022 09:05:29 -0700 Subject: [PATCH 2/2] Router: space between if-while-for cond and body --- .../scala/org/scalafmt/internal/Router.scala | 21 ++++++++++++------- .../src/test/resources/default/Apply.stat | 4 ++-- .../src/test/resources/default/For.stat | 12 +++++------ .../src/test/resources/default/If.stat | 12 +++++------ 4 files changed, 28 insertions(+), 21 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 25d9d96a86..1c021b8518 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 @@ -662,13 +662,20 @@ class Router(formatOps: FormatOps) { // infix applications have no space. case _: Type.ApplyInfix | _: Term.ApplyInfix => false case _ => true - } && (prevNonComment(formatToken).left match { - case RightParenOrBracket() | T.KwSuper() | T.KwThis() | - T.Ident(_) | T.RightBrace() | T.Underscore() | - T.Constant.Symbol(_) => - true - case _ => false - }) => + } && { + val prevFt = prevNonComment(formatToken) + prevFt.left match { + case _: T.RightParen | _: T.RightBrace => + prevFt.meta.leftOwner match { + case _: Term.For | _: Term.If | _: Term.While => false + case _ => true + } + case _: T.RightBracket | _: T.KwSuper | _: T.KwThis | + _: T.Ident | _: T.Underscore | _: T.Constant.Symbol => + true + case _ => false + } + } => def modification: Modification = leftOwner match { case _: Mod => Space // Add a space between constructor annotations and their parameter lists diff --git a/scalafmt-tests/src/test/resources/default/Apply.stat b/scalafmt-tests/src/test/resources/default/Apply.stat index 1c83a8aac8..5c865b994d 100644 --- a/scalafmt-tests/src/test/resources/default/Apply.stat +++ b/scalafmt-tests/src/test/resources/default/Apply.stat @@ -1359,8 +1359,8 @@ object a { val boss = system.actorOf(Props(new Actor { def receive = { case "run" ⇒ - for (_ ← 1 to num)(context.watch( - context.actorOf(props))) ! cachedMessage + for (_ ← 1 to num) + (context.watch(context.actorOf(props))) ! cachedMessage case Terminated(child) ⇒ stopLatch.countDown() } }).withDispatcher("boss")) diff --git a/scalafmt-tests/src/test/resources/default/For.stat b/scalafmt-tests/src/test/resources/default/For.stat index f7841daaf3..5355c740f3 100644 --- a/scalafmt-tests/src/test/resources/default/For.stat +++ b/scalafmt-tests/src/test/resources/default/For.stat @@ -224,10 +224,10 @@ object a { } >>> object a { - for (b <- c)(d(e)) - for (b <- c)(d.e(f)) - for (b <- c)(d(e)) + f - for { b <- c }(d(e)) - for { b <- c }(d.e(f)) - for { b <- c }(d(e)) + f + for (b <- c) (d(e)) + for (b <- c) (d.e(f)) + for (b <- c) (d(e)) + f + for { b <- c } (d(e)) + for { b <- c } (d.e(f)) + for { b <- c } (d(e)) + f } diff --git a/scalafmt-tests/src/test/resources/default/If.stat b/scalafmt-tests/src/test/resources/default/If.stat index 71fcdb3da4..c9dbb92c2b 100644 --- a/scalafmt-tests/src/test/resources/default/If.stat +++ b/scalafmt-tests/src/test/resources/default/If.stat @@ -374,10 +374,10 @@ object a { } >>> object a { - if (true)(d(e)) - if (true)(d.e(f)) - if (true)(d(e)) + f - while (true)(d(e)) - while (true)(d.e(f)) - while (true)(d(e)) + f + if (true) (d(e)) + if (true) (d.e(f)) + if (true) (d(e)) + f + while (true) (d(e)) + while (true) (d.e(f)) + while (true) (d(e)) + f }