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

Commit

Permalink
Fix calculation of the length of a whitespace token when followed by …
Browse files Browse the repository at this point in the history
…an unbroken sequence of LeafNodeTokens
  • Loading branch information
hovinen committed Feb 19, 2021
1 parent 2d32f82 commit 5aa97be
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,14 @@ private class WhitespaceStackElement(
get() = contentLength + initialTextLength

private val initialTextLength: Int
get() = tokens.firstOrNull { it !is BeginWeakToken }?.textLength ?: 0
get() {
val firstRelevantToken = tokens.firstOrNull { it !is BeginWeakToken }
return if (firstRelevantToken is LeafNodeToken) {
tokens.takeWhile { it is LeafNodeToken }.map { it.textLength }.sum()
} else {
firstRelevantToken?.textLength ?: 0
}
}
}

private class LiteralWhitespaceStackElement(
Expand Down
105 changes: 68 additions & 37 deletions formatter/src/test/kotlin/org/kotlin/formatter/KotlinFormatterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import java.io.PrintStream
import java.nio.file.Path
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.AfterEach
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.io.TempDir
Expand Down Expand Up @@ -1957,7 +1958,7 @@ class KotlinFormatterTest {
@Test
fun `format breaks at logical operator in a while statement`() {
val result =
KotlinFormatter(maxLineLength = 50)
KotlinFormatter(maxLineLength = 52)
.format(
"""
fun myFunction() {
Expand Down Expand Up @@ -2537,6 +2538,7 @@ class KotlinFormatterTest {
}

@Test
@Disabled("Not sure whether we want this rule.")
fun `prefers not to break before a function name after modifier`() {
val result =
KotlinFormatter(maxLineLength = 50)
Expand Down Expand Up @@ -3448,6 +3450,31 @@ class KotlinFormatterTest {
)
}

@Test
fun `breaks indents class expressions correctly`() {
val result =
KotlinFormatter(maxLineLength = 37)
.format(
"""
mapOf(
"something" to AClass::class,
"something else" to BClass::class,
)
""".trimIndent()
)

assertThat(result)
.isEqualTo(
"""
mapOf(
"something" to AClass::class,
"something else" to
BClass::class,
)
""".trimIndent()
)
}

@Test
fun `format breaks the short form of the summary fragment`() {
val result =
Expand Down Expand Up @@ -3663,50 +3690,54 @@ class KotlinFormatterTest {
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()
)
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()
)
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()
)
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()
)
assertThat(result)
.isEqualTo(
"""
package org.kotlin.formatter
/* A comment */
import apackage.AClass
class MyClass : AClass
""".trimIndent()
)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,23 @@ internal class TokenPreprocessorTest {
.isEqualTo(listOf(WhitespaceToken(length = lengthExpected, content = " "), token))
}

@Test
fun `outputs a WhitespaceToken with the length of the following sequence of leaf tokens`() {
val subject = TokenPreprocessor()
val input = listOf(WhitespaceToken(content = " "), LeafNodeToken("a"), LeafNodeToken("b"))

val result = subject.preprocess(input)

assertThat(result)
.isEqualTo(
listOf(
WhitespaceToken(length = 3, content = " "),
LeafNodeToken("a"),
LeafNodeToken("b")
)
)
}

@Test
fun `outputs a WhitespaceToken with the length of the following block`() {
val subject = TokenPreprocessor()
Expand Down

0 comments on commit 5aa97be

Please sign in to comment.