Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
Allow comments immediately preceding catch clauses in try-catch expre…
Browse files Browse the repository at this point in the history
…ssions
  • Loading branch information
hovinen committed Nov 17, 2020
1 parent 13ab75c commit 5757959
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ internal class TryScanner(private val kotlinScanner: KotlinScanner) : NodeScanne
blockPattern.matchSequence(nodes.first().children().asIterable())
}
oneOrMore {
possibleWhitespace()
either {
possibleWhitespace() thenMapToTokens { listOf(ClosingForcedBreakToken) }
comment()
possibleWhitespace() thenMapToTokens { listOf(ClosingForcedBreakToken) }
} or { possibleWhitespace() thenMapToTokens { listOf(nonBreakingSpaceToken()) } }
nodeOfType(KtNodeTypes.CATCH) thenMapToTokens { nodes ->
listOf(nonBreakingSpaceToken())
.plus(catchPattern.matchSequence(nodes.first().children().asIterable()))
catchPattern.matchSequence(nodes.first().children().asIterable())
}
}
end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3038,6 +3038,36 @@ class KotlinFormatterTest {
)
}

@Test
fun `handles comments before a catch clause`() {
val result =
KotlinFormatter(maxLineLength = 40)
.format(
"""
try {
aFunction()
}
// A comment
catch (e: Exception) {
anotherFunction()
}
""".trimIndent()
)

assertThat(result)
.isEqualTo(
"""
try {
aFunction()
}
// A comment
catch (e: Exception) {
anotherFunction()
}
""".trimIndent()
)
}

@Test
fun `breaks before try expression inside function literal`() {
val result =
Expand Down

0 comments on commit 5757959

Please sign in to comment.