Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router: indent comment before select chain break #2986

Merged
merged 2 commits into from
Dec 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down Expand Up @@ -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 &&
Expand Down
28 changes: 28 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 24 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
24 changes: 24 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
25 changes: 25 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}