Skip to content

Commit

Permalink
Router: handle try/catch/finally without braces
Browse files Browse the repository at this point in the history
  • Loading branch information
kitbellew committed Dec 9, 2021
1 parent 48dd870 commit 44c3f47
Show file tree
Hide file tree
Showing 9 changed files with 192 additions and 104 deletions.
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
52 changes: 33 additions & 19 deletions scalafmt-tests/src/test/resources/newlines/source_classic.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4673,9 +4673,12 @@ object a {
}
>>>
object a {
def foo = try a
catch bar
finally a
def foo = try
a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, short
maxColumn = 12
Expand All @@ -4691,9 +4694,12 @@ object a {
>>>
object a {
def foo =
try a
catch bar
finally a
try
a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, space comment
object a {
Expand All @@ -4706,10 +4712,12 @@ object a {
}
>>>
object a {
def foo = try a
def foo = try
a
catch /* c1 */
bar
finally a
bar
finally
a
}
<<< TryWithHandler: newline after catch, break comment
object a {
Expand All @@ -4723,11 +4731,13 @@ object a {
}
>>>
object a {
def foo = try a
def foo = try
a
catch
/* c2 */
bar
finally a
/* c2 */
bar
finally
a
}
<<< TryWithHandler: newline after catch, comments
object a {
Expand All @@ -4741,11 +4751,13 @@ object a {
}
>>>
object a {
def foo = try a
def foo = try
a
catch /* c1 */
/* c2 */
bar
finally a
/* c2 */
bar
finally
a
}
<<< TryWithHandler: newline after catch, comments
object a {
Expand All @@ -4757,7 +4769,9 @@ object a {
}
>>>
object a {
def foo = try a
def foo = try
a
catch /* c1 */ bar
finally a
finally
a
}
16 changes: 9 additions & 7 deletions scalafmt-tests/src/test/resources/newlines/source_fold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4491,8 +4491,10 @@ object a {
object a {
def foo =
try a
catch bar
finally a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, space comment
object a {
Expand All @@ -4508,7 +4510,7 @@ object a {
def foo =
try a
catch /* c1 */
bar
bar
finally a
}
<<< TryWithHandler: newline after catch, break comment
Expand All @@ -4526,8 +4528,8 @@ object a {
def foo =
try a
catch
/* c2 */
bar
/* c2 */
bar
finally a
}
<<< TryWithHandler: newline after catch, comments
Expand All @@ -4545,8 +4547,8 @@ object a {
def foo =
try a
catch /* c1 */
/* c2 */
bar
/* c2 */
bar
finally a
}
<<< TryWithHandler: newline after catch, comments
Expand Down
52 changes: 33 additions & 19 deletions scalafmt-tests/src/test/resources/newlines/source_keep.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4700,9 +4700,12 @@ object a {
>>>
object a {
def foo =
try a
catch bar
finally a
try
a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, short
maxColumn = 12
Expand All @@ -4718,9 +4721,12 @@ object a {
>>>
object a {
def foo =
try a
catch bar
finally a
try
a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, space comment
object a {
Expand All @@ -4734,10 +4740,12 @@ object a {
>>>
object a {
def foo =
try a
try
a
catch /* c1 */
bar
finally a
bar
finally
a
}
<<< TryWithHandler: newline after catch, break comment
object a {
Expand All @@ -4752,11 +4760,13 @@ object a {
>>>
object a {
def foo =
try a
try
a
catch
/* c2 */
bar
finally a
/* c2 */
bar
finally
a
}
<<< TryWithHandler: newline after catch, comments
object a {
Expand All @@ -4771,11 +4781,13 @@ object a {
>>>
object a {
def foo =
try a
try
a
catch /* c1 */
/* c2 */
bar
finally a
/* c2 */
bar
finally
a
}
<<< TryWithHandler: newline after catch, comments
object a {
Expand All @@ -4788,7 +4800,9 @@ object a {
>>>
object a {
def foo =
try a
try
a
catch /* c1 */ bar
finally a
finally
a
}
16 changes: 9 additions & 7 deletions scalafmt-tests/src/test/resources/newlines/source_unfold.stat
Original file line number Diff line number Diff line change
Expand Up @@ -4941,8 +4941,10 @@ object a {
object a {
def foo =
try a
catch bar
finally a
catch
bar
finally
a
}
<<< TryWithHandler: newline after catch, space comment
object a {
Expand All @@ -4958,7 +4960,7 @@ object a {
def foo =
try a
catch /* c1 */
bar
bar
finally a
}
<<< TryWithHandler: newline after catch, break comment
Expand All @@ -4976,8 +4978,8 @@ object a {
def foo =
try a
catch
/* c2 */
bar
/* c2 */
bar
finally a
}
<<< TryWithHandler: newline after catch, comments
Expand All @@ -4995,8 +4997,8 @@ object a {
def foo =
try a
catch /* c1 */
/* c2 */
bar
/* c2 */
bar
finally a
}
<<< TryWithHandler: newline after catch, comments
Expand Down
Loading

0 comments on commit 44c3f47

Please sign in to comment.