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

Commit

Permalink
Admit comments at the top of a Kotlin file and before the import list.
Browse files Browse the repository at this point in the history
  • Loading branch information
hovinen committed Feb 18, 2021
1 parent 4113647 commit 2d32f82
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import org.kotlin.formatter.scanning.nodepattern.nodePattern
internal class KotlinFileScanner(private val kotlinScanner: KotlinScanner) : NodeScanner {
private val nodePattern =
nodePattern {
possibleWhitespaceWithComment()
either {
nonEmptyPackageDirective() thenMapToTokens { nodes ->
kotlinScanner.scanNodes(nodes, ScannerState.BLOCK)
}
possibleWhitespace()
exactlyOne {
possibleWhitespace()
possibleWhitespaceWithComment()
nodeOfType(KtNodeTypes.IMPORT_LIST) thenMapToTokens { nodes ->
kotlinScanner.scanNodes(nodes, ScannerState.BLOCK)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3659,6 +3659,56 @@ class KotlinFormatterTest {
)
}

@Test
fun `admits comments at the top of the file`() {
val subject = KotlinFormatter()

val result = subject.format(
"""
/* A comment */
package org.kotlin.formatter
class MyClass
""".trimIndent()
)

assertThat(result).isEqualTo(
"""
/* A comment */
package org.kotlin.formatter
class MyClass
""".trimIndent()
)
}

@Test
fun `admits comments before the import list`() {
val subject = KotlinFormatter()

val result = subject.format(
"""
package org.kotlin.formatter
/* A comment */
import apackage.AClass
class MyClass : AClass
""".trimIndent()
)

assertThat(result).isEqualTo(
"""
package org.kotlin.formatter
/* A comment */
import apackage.AClass
class MyClass : AClass
""".trimIndent()
)
}

@Test
fun `breaks before inheritance spec when necessary`() {
val subject = KotlinFormatter(maxLineLength = 46)
Expand Down

0 comments on commit 2d32f82

Please sign in to comment.