From 21f09aa527eca6ecdfcd38fc7c4e8caaf6fe1388 Mon Sep 17 00:00:00 2001 From: Albert Meltzer <7529386+kitbellew@users.noreply.github.com> Date: Sun, 26 Dec 2021 01:40:33 -0800 Subject: [PATCH] Router: attach/detach comment to comma as needed --- .../scala/org/scalafmt/internal/Router.scala | 20 ++++++++++--------- .../src/test/resources/unit/Comment.stat | 6 ++---- 2 files changed, 13 insertions(+), 13 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 33f47eacf8..e391699847 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 @@ -1385,18 +1385,20 @@ class Router(formatOps: FormatOps) { Split(NoSplit, 0) ) // These are mostly filtered out/modified by policies. - case ft @ FormatToken(left: T.Comma, c: T.Comment, _) => + case ft @ FormatToken(lc: T.Comma, c: T.Comment, _) => if (ft.hasBlankLine) Seq(Split(NewlineT(isDouble = true), 0)) else if (isSingleLineComment(c) || ft.meta.right.hasNL) Seq(Split(Space.orNL(newlines), 0)) - else { - val trailingComma = - rightIsCloseDelimToAddTrailingComma(left, nextNonComment(next(ft))) - Seq( - Split(newlines != 0 && !trailingComma, 0)(Space), - Split(newlines == 0 && trailingComma, 1)(Newline) - ) - } + else if (newlines == 0) { + val endFt = nextNonCommentSameLine(next(ft)) + val useSpaceOnly = endFt.hasBreak || + rightIsCloseDelimToAddTrailingComma(lc, endFt) + Seq(Split(Space, 0), Split(useSpaceOnly, 1)(Newline)) + } else if ( + !style.comments.willWrap && + rightIsCloseDelimToAddTrailingComma(lc, nextNonComment(next(ft))) + ) Seq(Split(Space, 0), Split(Newline, 1)) + else Seq(Split(Newline, 0)) case FormatToken(_: T.Comma, right, _) if leftOwner.isNot[Template] => val splitsOpt = argumentStarts.get(hash(right)).flatMap { nextArg => val callSite = isCallSite(leftOwner) diff --git a/scalafmt-tests/src/test/resources/unit/Comment.stat b/scalafmt-tests/src/test/resources/unit/Comment.stat index 5a349a781f..00c617da92 100644 --- a/scalafmt-tests/src/test/resources/unit/Comment.stat +++ b/scalafmt-tests/src/test/resources/unit/Comment.stat @@ -742,8 +742,7 @@ foo( ) >>> foo( - barBarBar, - /* comment */ + barBarBar, /* comment */ qux ) <<< overflow, comma, attached mlc then another arg after space @@ -755,8 +754,7 @@ foo( ) >>> foo( - barBarBar, - /* comment */ + barBarBar, /* comment */ bazBazBaz, /* comment */ qux )