From e5913da48beb02294e01f96b1dbe0eaaa18dc7f6 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Fri, 24 Sep 2021 18:08:53 -0700 Subject: [PATCH] Router: align binpacked apply as well if needed --- .../main/scala/org/scalafmt/internal/Router.scala | 9 ++++++++- .../src/test/resources/align/DefaultWithAlign.stat | 12 +++++++----- .../src/test/resources/default/Apply.stat | 14 ++++++++------ .../src/test/resources/default/TypeArguments.stat | 8 ++++---- 4 files changed, 27 insertions(+), 16 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 cc482c565f..46c2ff304a 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 @@ -1073,6 +1073,9 @@ class Router(formatOps: FormatOps) { val isBracket = open.is[T.LeftBracket] val indent = Indent(style.indent.getDefnSite(leftOwner), close, Before) + val align = style.align.getOpenDelimSite(isBracket, true) + val noSplitIndents = + if (align) getOpenParenAlignIndents(close) else Seq(indent) val bracketPenalty = if (isBracket) Some(Constants.BracketPenalty) else None @@ -1123,7 +1126,7 @@ class Router(formatOps: FormatOps) { Split(noSplitModification, 0) .notIf(mustUseNL) .withPolicy(noSplitPolicy) - .withIndent(indent), + .withIndents(noSplitIndents), Split(Newline, if (mustUseNL) 0 else nlCost) .withPolicy(nlDanglePolicy & onelinePolicy & penalizeBrackets) .withIndent(indent) @@ -1137,6 +1140,10 @@ class Router(formatOps: FormatOps) { val indent = Indent(Num(indentLen), close, Before) val noSplitIndents = if (style.binPack.indentCallSiteOnce) Seq.empty + else if ( + if (isTuple(leftOwner)) style.align.getOpenParenTupleSite + else style.align.getOpenDelimSite(false, false) + ) getOpenParenAlignIndents(close) else Seq(indent) def baseNoSplit(implicit fileLine: FileLine) = Split(Space(style.spaces.inParentheses), 0) diff --git a/scalafmt-tests/src/test/resources/align/DefaultWithAlign.stat b/scalafmt-tests/src/test/resources/align/DefaultWithAlign.stat index d22c2a3cbc..b1a6957823 100644 --- a/scalafmt-tests/src/test/resources/align/DefaultWithAlign.stat +++ b/scalafmt-tests/src/test/resources/align/DefaultWithAlign.stat @@ -1317,7 +1317,7 @@ object a { >>> object a { foo(bar, baz, - qux) + qux) } <<< #2562 tuple, binpack maxColumn = 13 @@ -1330,7 +1330,7 @@ object a { >>> object a { (bar, baz, - qux) + qux) } <<< #2562 def, binpack maxColumn = 22 @@ -1343,7 +1343,8 @@ object a { >>> object a { def a(b: B, c: C, - ddd: DDD) = ??? + ddd: DDD) = + ??? } <<< #2562 call, with spaces maxColumn = 16 @@ -1402,7 +1403,7 @@ object a { >>> object a { foo( bar, baz, - qux ) + qux ) } <<< #2562 tuple, with spaces, binpack maxColumn = 14 @@ -1430,7 +1431,8 @@ object a { >>> object a { def a( b: B, c: C, - ddd: DDD ) = ??? + ddd: DDD ) = + ??? } <<< #2562 call, !tuple maxColumn = 10 diff --git a/scalafmt-tests/src/test/resources/default/Apply.stat b/scalafmt-tests/src/test/resources/default/Apply.stat index a1689851f2..0d3a905b6e 100644 --- a/scalafmt-tests/src/test/resources/default/Apply.stat +++ b/scalafmt-tests/src/test/resources/default/Apply.stat @@ -1674,7 +1674,7 @@ binPack.preset = true cache.getOrElseUpdate(name, { x; y; z }, foo, bar) >>> cache.getOrElseUpdate(name, { x; y; z }, - foo, bar) + foo, bar) <<< #2633 binPack one arg per line with braces 3 maxColumn = 60 binPack.preset = true @@ -1682,7 +1682,7 @@ binPack.preset = true cache.getOrElseUpdate(name, { x; y; z }, { x => y; z }, foo, bar) >>> cache.getOrElseUpdate(name, { x; y; z }, { x => y; z }, foo, - bar) + bar) <<< #2633 binPack with multiline arg, always maxColumn = 80 indent.callSite = 2 @@ -1697,8 +1697,8 @@ object a { object a { val cls = Select(Select(Select(Select(Select(Select(Ident(nme.ROOTPKG), nme.scala_), - scalajs), js), nme.annotation, x, y, z), internal_, a, b, c), - wasPublicBeforeTyperXxx) + scalajs), js), nme.annotation, x, y, z), + internal_, a, b, c), wasPublicBeforeTyperXxx) } <<< #2633 binPack with multiline arg, oneline maxColumn = 80 @@ -1743,7 +1743,8 @@ object a { symForName .getAnnotation(JSNameAnnotation).fold { sym.addAnnotation(JSNameAnnotation, - Literal(Constant(jsInterop.defaultJSNameOf(symForName)))) + Literal(Constant(jsInterop.defaultJSNameOf( + symForName)))) } { annot => sym.addAnnotation(annot) } @@ -1772,7 +1773,8 @@ object a { symForName .getAnnotation(JSNameAnnotation).fold { sym.addAnnotation(JSNameAnnotation, - Literal(Constant(jsInterop.defaultJSNameOf(symForName)))) + Literal(Constant(jsInterop.defaultJSNameOf( + symForName)))) } { annot => sym.addAnnotation(annot) } diff --git a/scalafmt-tests/src/test/resources/default/TypeArguments.stat b/scalafmt-tests/src/test/resources/default/TypeArguments.stat index 1e3f4a4ee5..e170c3cdae 100644 --- a/scalafmt-tests/src/test/resources/default/TypeArguments.stat +++ b/scalafmt-tests/src/test/resources/default/TypeArguments.stat @@ -435,7 +435,7 @@ def props[M[_[_]], F[_]: Async, I: KeyDecoder, State, Event: PersistentEncoder: >>> object a { def deploy[M[_[_]]: FunctorK, F[_], State, Event: PersistentEncoder: PersistentDecoder, - K: KeyEncoder: KeyDecoder] = ??? + K: KeyEncoder: KeyDecoder] = ??? def props[ M[_[_]], F[_]: Async, I: KeyDecoder, State, Event: PersistentEncoder: PersistentDecoder]() = ??? @@ -453,7 +453,7 @@ def props[M[_[_]], F[_]: Async, I: KeyDecoder, State, Event: PersistentEncoder: >>> object a { def deploy[M[_[_]]: FunctorK, F[_], State, Event: PersistentEncoder: PersistentDecoder, - K: KeyEncoder: KeyDecoder] = ??? + K: KeyEncoder: KeyDecoder] = ??? def props[ M[_[_]], F[_]: Async, I: KeyDecoder, State, Event: PersistentEncoder: PersistentDecoder]() = ??? @@ -471,7 +471,7 @@ def props[M[_[_]], F[_]: Async, I: KeyDecoder, State, Event: PersistentEncoder: >>> object a { def deploy[M[_[_]]: FunctorK, F[_], State, Event: PersistentEncoder: PersistentDecoder, - K: KeyEncoder: KeyDecoder] = ??? + K: KeyEncoder: KeyDecoder] = ??? def props[ M[_[_]], F[_]: Async, I: KeyDecoder, State, Event: PersistentEncoder: PersistentDecoder]() = ??? @@ -489,7 +489,7 @@ def props[M[_[_]], F[_]: Async, I: KeyDecoder, State, Event: PersistentEncoder: >>> object a { def deploy[M[_[_]]: FunctorK, F[_], State, Event: PersistentEncoder: PersistentDecoder, - K: KeyEncoder: KeyDecoder] = ??? + K: KeyEncoder: KeyDecoder] = ??? def props[ M[_[_]], F[_]: Async, I: KeyDecoder, State, Event: PersistentEncoder: PersistentDecoder]() = ???