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 a502a31bb3..5355c740f3 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..c9dbb92c2b 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 +}