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 6825a4cb21..935728d7eb 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 @@ -1677,6 +1677,9 @@ class Router(formatOps: FormatOps) { case Decision(t @ FormatToken(_, _: T.Dot, _), s) if t.meta.rightOwner eq tree => SplitTag.SelectChainFirstNL.activateOnly(s) + case Decision(t @ FormatToken(l, _: T.Comment, _), s) + if t.meta.rightOwner.eq(tree) && !l.is[T.Comment] => + SplitTag.SelectChainFirstNL.activateOnly(s) } } @@ -2166,18 +2169,20 @@ class Router(formatOps: FormatOps) { case FormatToken(_: T.Comment, _, _) => Seq(Split(getMod(formatToken), 0)) // Inline comment - case FormatToken(left, _: T.Comment, _) => + case FormatToken(_, _: T.Comment, _) => val forceBlankLine = formatToken.hasBreak && blankLineBeforeDocstring(formatToken) val mod = if (forceBlankLine) Newline2x else getMod(formatToken) - val indent = formatToken.meta.rightOwner match { - case GetSelectLike(ts) - if findPrevSelect(ts, style.encloseSelectChains).isEmpty => + val baseSplit = Split(mod, 0) + formatToken.meta.rightOwner match { + case GetSelectLike(t) => val expire = nextNonComment(next(formatToken)).left - Indent(style.indent.main, expire, ExpiresOn.After) - case _ => Indent.Empty + val indent = Indent(style.indent.main, expire, ExpiresOn.After) + val split = baseSplit.withIndent(indent) + if (findPrevSelect(t, style.encloseSelectChains).isEmpty) Seq(split) + else Seq(baseSplit, split.onlyFor(SplitTag.SelectChainFirstNL)) + case _ => Seq(baseSplit) } - Seq(Split(mod, 0).withIndent(indent)) case FormatToken(soft.ImplicitOrUsing(), _, _) if style.binPack.unsafeDefnSite.isNever && diff --git a/scalafmt-tests/src/test/resources/newlines/source_classic.stat b/scalafmt-tests/src/test/resources/newlines/source_classic.stat index b8d7b570fd..67282aa2c2 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_classic.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_classic.stat @@ -5452,3 +5452,31 @@ object a { } a + b } +<<< comment in select chain +optIn.breaksInsideChains = true +optIn.breakChainOnFirstMethodDot = false +=== +object a { + foo + // c2 + .bar(baz).foo(bar, baz) + foo // c1 + // c2 + .bar(baz).foo(bar, baz) + foo.bar(baz) // c1 + // c2 + .foo(bar, baz) +} +>>> +object a { + foo + // c2 + .bar(baz).foo(bar, baz) + foo // c1 + // c2 + .bar(baz).foo(bar, baz) + foo + .bar(baz) // c1 + // c2 + .foo(bar, baz) +} diff --git a/scalafmt-tests/src/test/resources/newlines/source_fold.stat b/scalafmt-tests/src/test/resources/newlines/source_fold.stat index c25584dd8a..119f457c1a 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_fold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_fold.stat @@ -5226,3 +5226,27 @@ private implicit class ApiExceptionToErrorResponseConverter(ex: ApiException) { def toErrors: Seq[ErrorResponse] = Seq(ErrorResponse(ex.errorType.code, ex.errorType.name, Option(ex.getMessage))) } +<<< comment in select chain +object a { + foo + // c2 + .bar(baz).foo(bar, baz) + foo // c1 + // c2 + .bar(baz).foo(bar, baz) + foo.bar(baz) // c1 + // c2 + .foo(bar, baz) +} +>>> +object a { + foo + // c2 + .bar(baz).foo(bar, baz) + foo // c1 + // c2 + .bar(baz).foo(bar, baz) + foo.bar(baz) // c1 + // c2 + .foo(bar, baz) +} diff --git a/scalafmt-tests/src/test/resources/newlines/source_keep.stat b/scalafmt-tests/src/test/resources/newlines/source_keep.stat index 61a163a5c1..dbb8bd6db4 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_keep.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_keep.stat @@ -5476,3 +5476,27 @@ object a { } a + b } +<<< comment in select chain +object a { + foo + // c2 + .bar(baz).foo(bar, baz) + foo // c1 + // c2 + .bar(baz).foo(bar, baz) + foo.bar(baz) // c1 + // c2 + .foo(bar, baz) +} +>>> +object a { + foo + // c2 + .bar(baz).foo(bar, baz) + foo // c1 + // c2 + .bar(baz).foo(bar, baz) + foo.bar(baz) // c1 + // c2 + .foo(bar, baz) +} diff --git a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat index d1ac1b5b5c..4dab5af086 100644 --- a/scalafmt-tests/src/test/resources/newlines/source_unfold.stat +++ b/scalafmt-tests/src/test/resources/newlines/source_unfold.stat @@ -5703,3 +5703,28 @@ object a { b } } +<<< comment in select chain +object a { + foo + // c2 + .bar(baz).foo(bar, baz) + foo // c1 + // c2 + .bar(baz).foo(bar, baz) + foo.bar(baz) // c1 + // c2 + .foo(bar, baz) +} +>>> +object a { + foo + // c2 + .bar(baz).foo(bar, baz) + foo // c1 + // c2 + .bar(baz).foo(bar, baz) + foo + .bar(baz) // c1 + // c2 + .foo(bar, baz) +}