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: handle try/catch/finally without braces #2947

Merged
merged 2 commits into from
Dec 9, 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 @@ -2076,6 +2076,30 @@ class Router(formatOps: FormatOps) {
val enumerator = leftOwner.asInstanceOf[Enumerator.Val]
getSplitsEnumerator(tok, enumerator.rhs)

case FormatToken(_: T.KwTry | _: T.KwCatch | _: T.KwFinally, _, _) =>
val body = formatToken.meta.leftOwner match {
case t: Term.Try =>
formatToken.left match {
case _: T.KwTry => t.expr
case _: T.KwCatch => t
case _: T.KwFinally => t.finallyp.getOrElse(t)
case _ => t
}
case t: Term.TryWithHandler =>
formatToken.left match {
case _: T.KwTry => t.expr
case _: T.KwCatch => t.catchp
case _: T.KwFinally => t.finallyp.getOrElse(t)
case _ => t
}
case t => t
}
val end = getLastToken(body)
val indent = Indent(style.indent.main, end, ExpiresOn.After)
CtrlBodySplits.get(formatToken, body, Seq(indent)) {
Split(Space, 0).withSingleLineNoOptimal(end)
}(Split(Newline, _).withIndent(indent))

// Union/Intersection types
case FormatToken(_: T.Ident, _, ExtractAndOrTypeRhsIdentLeft(rhs)) =>
val rhsEnd = getLastNonTrivialToken(rhs)
Expand Down
113 changes: 113 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4662,3 +4662,116 @@ assembly / assemblyMergeStrategy := {
case A("B", c @ _*) => D.e
case _ => E.f
}
<<< TryWithHandler: newline after catch
object a {
def foo = try
a
catch
bar
finally
a
}
>>>
object a {
def foo = try
a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, short
maxColumn = 12
===
object a {
def foo = try
a
catch
bar
finally
a
}
>>>
object a {
def foo =
try
a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, space comment
object a {
def foo = try
a
catch /* c1 */
bar
finally
a
}
>>>
object a {
def foo = try
a
catch /* c1 */
bar
finally
a
}
<<< TryWithHandler: newline after catch, break comment
object a {
def foo = try
a
catch
/* c2 */
bar
finally
a
}
>>>
object a {
def foo = try
a
catch
/* c2 */
bar
finally
a
}
<<< TryWithHandler: newline after catch, comments
object a {
def foo = try
a
catch /* c1 */
/* c2 */
bar
finally
a
}
>>>
object a {
def foo = try
a
catch /* c1 */
/* c2 */
bar
finally
a
}
<<< TryWithHandler: newline after catch, comments
object a {
def foo = try
a
catch /* c1 */ bar
finally
a
}
>>>
object a {
def foo = try
a
catch /* c1 */ bar
finally
a
}
106 changes: 106 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4460,3 +4460,109 @@ assembly / assemblyMergeStrategy := {
case A("B", c @ _*) => D.e
case _ => E.f
}
<<< TryWithHandler: newline after catch
object a {
def foo = try
a
catch
bar
finally
a
}
>>>
object a {
def foo =
try a
catch bar
finally a
}
<<< TryWithHandler: newline after catch, short
maxColumn = 12
===
object a {
def foo = try
a
catch
bar
finally
a
}
>>>
object a {
def foo =
try a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, space comment
object a {
def foo = try
a
catch /* c1 */
bar
finally
a
}
>>>
object a {
def foo =
try a
catch /* c1 */
bar
finally a
}
<<< TryWithHandler: newline after catch, break comment
object a {
def foo = try
a
catch
/* c2 */
bar
finally
a
}
>>>
object a {
def foo =
try a
catch
/* c2 */
bar
finally a
}
<<< TryWithHandler: newline after catch, comments
object a {
def foo = try
a
catch /* c1 */
/* c2 */
bar
finally
a
}
>>>
object a {
def foo =
try a
catch /* c1 */
/* c2 */
bar
finally a
}
<<< TryWithHandler: newline after catch, comments
object a {
def foo = try
a
catch /* c1 */ bar
finally
a
}
>>>
object a {
def foo =
try a
catch /* c1 */ bar
finally a
}
118 changes: 118 additions & 0 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4688,3 +4688,121 @@ assembly / assemblyMergeStrategy := {
case A("B", c @ _*) => D.e
case _ => E.f
}
<<< TryWithHandler: newline after catch
object a {
def foo = try
a
catch
bar
finally
a
}
>>>
object a {
def foo =
try
a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, short
maxColumn = 12
===
object a {
def foo = try
a
catch
bar
finally
a
}
>>>
object a {
def foo =
try
a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, space comment
object a {
def foo = try
a
catch /* c1 */
bar
finally
a
}
>>>
object a {
def foo =
try
a
catch /* c1 */
bar
finally
a
}
<<< TryWithHandler: newline after catch, break comment
object a {
def foo = try
a
catch
/* c2 */
bar
finally
a
}
>>>
object a {
def foo =
try
a
catch
/* c2 */
bar
finally
a
}
<<< TryWithHandler: newline after catch, comments
object a {
def foo = try
a
catch /* c1 */
/* c2 */
bar
finally
a
}
>>>
object a {
def foo =
try
a
catch /* c1 */
/* c2 */
bar
finally
a
}
<<< TryWithHandler: newline after catch, comments
object a {
def foo = try
a
catch /* c1 */ bar
finally
a
}
>>>
object a {
def foo =
try
a
catch /* c1 */ bar
finally
a
}
Loading