Skip to content

Commit

Permalink
Multiline string indent (#1052)
Browse files Browse the repository at this point in the history
* Fix multiline string indentation

The indenting margin is determined analog to the trimIndent function. Spaces and
tabs are both handled as single indent characters. The indentation of the closing
quotes is not relevant unless the opening and closing quotes are on different
lines but does not contain any content which is weird but still valid Kotlin
code. The 'IndentationRuleTrimIndentTest' is added to clarify and validate the
behavior of the trimIndent function.

Content of a multiline string should not be indented with respect to the closing
quotes (note that the opening quotes can be on a previous line, for example
after an assignment).

File 'format-raw-string-trim-indent.kt.spec' is changed for following reasons:
* Example code was not valid Kotlin code
* Changed a comment in an example to explain why the autocorrected code
  actually looks worse than the original. This will be fixed in a next PR.

* Fix code which does not comply to changed indentation rules

* Rename file IndentationRuleTrimIndentTest.kt to IndentationRuleTrimIndentTest.kt.txt

Avoid breaking the build as long as the ktlint-disable directive on the package
statement is not recognized. See pull request #1038

* Fix indentation in unit tests

* Remove changing the indentation margin of a multiline raw string literal except for the line with the closing quotes

* Change message when closing quotes of multiline raw string literal are incorrectly indented

* Remove IndentationRuleTrimIndentTest

The indentation margin is no longer been altered by the indent rule. So there is no need anymore
to verify the behavior of the trimIndent function.

Co-authored-by: Paul Dingemans <pdingemans@bol.com>
  • Loading branch information
paul-dingemans and Paul Dingemans authored Oct 24, 2021
1 parent 2833711 commit 27f90a4
Show file tree
Hide file tree
Showing 12 changed files with 606 additions and 287 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -938,11 +938,12 @@ class AnnotationRuleTest {

@Test
fun `lint file annotations should be separated with a blank line in script 1`() {
val code = """
val code =
"""
@file:Suppress("UnstableApiUsage")
pluginManagement {
}
""".trimIndent()
""".trimIndent()
assertThat(AnnotationRule().lint(code, script = true)).isEqualTo(
listOf(
LintError(1, 34, "annotation", AnnotationRule.fileAnnotationsShouldBeSeparated)
Expand All @@ -952,12 +953,13 @@ class AnnotationRuleTest {

@Test
fun `lint file annotations should be separated with a blank line in script 2`() {
val code = """
val code =
"""
@file:Suppress("UnstableApiUsage")
pluginManagement {
}
""".trimIndent()
""".trimIndent()
assertThat(AnnotationRule().lint(code, script = true)).isEmpty()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,12 +332,12 @@ class AnnotationSpacingRuleTest {
fun `annotations should not be separated by comments from the annotated construct`() {
val code =
"""
@Suppress("DEPRECATION") @Hello
/**
* block comment
*/
class Foo {
}
@Suppress("DEPRECATION") @Hello
/**
* block comment
*/
class Foo {
}
""".trimIndent()
assertThat(
AnnotationSpacingRule().lint(code)
Expand All @@ -352,41 +352,41 @@ class AnnotationSpacingRuleTest {
fun `annotations should be moved after comments`() {
val code =
"""
@Suppress("DEPRECATION") @Hello
/**
* block comment
*/
class Foo {
}
@Suppress("DEPRECATION") @Hello
/**
* block comment
*/
class Foo {
}
""".trimIndent()
assertThat(
AnnotationSpacingRule().format(code)
).isEqualTo(
"""
/**
* block comment
*/
@Suppress("DEPRECATION") @Hello
class Foo {
}
/**
* block comment
*/
@Suppress("DEPRECATION") @Hello
class Foo {
}
""".trimIndent()
)

val codeEOL =
"""
@Suppress("DEPRECATION") @Hello
// hello
class Foo {
}
@Suppress("DEPRECATION") @Hello
// hello
class Foo {
}
""".trimIndent()
assertThat(
AnnotationSpacingRule().format(codeEOL)
).isEqualTo(
"""
// hello
@Suppress("DEPRECATION") @Hello
class Foo {
}
// hello
@Suppress("DEPRECATION") @Hello
class Foo {
}
""".trimIndent()
)
}
Expand All @@ -395,33 +395,33 @@ class AnnotationSpacingRuleTest {
fun `preceding whitespaces are preserved`() {
val code =
"""
package a.b.c
package a.b.c
val hello = 5
val hello = 5
@Suppress("DEPRECATION") @Hello
/**
* block comment
*/
class Foo {
}
@Suppress("DEPRECATION") @Hello
/**
* block comment
*/
class Foo {
}
""".trimIndent()
assertThat(
AnnotationSpacingRule().format(code)
).isEqualTo(
"""
package a.b.c
package a.b.c
val hello = 5
val hello = 5
/**
* block comment
*/
@Suppress("DEPRECATION") @Hello
class Foo {
}
/**
* block comment
*/
@Suppress("DEPRECATION") @Hello
class Foo {
}
""".trimIndent()
)
}
Expand All @@ -443,18 +443,18 @@ class AnnotationSpacingRuleTest {
fun `format eol comment on the same line as the annotation`() {
val code =
"""
@SuppressWarnings // foo
@SuppressWarnings // foo
fun bar() {
}
fun bar() {
}
""".trimIndent()
assertThat(
AnnotationSpacingRule().format(code)
).isEqualTo(
"""
@SuppressWarnings // foo
fun bar() {
}
@SuppressWarnings // foo
fun bar() {
}
""".trimIndent()
)
}
Expand All @@ -463,19 +463,19 @@ class AnnotationSpacingRuleTest {
fun `format eol comment on the same line as the annotation 2`() {
val code =
"""
@SuppressWarnings // foo
// bar
fun bar() {
}
@SuppressWarnings // foo
// bar
fun bar() {
}
""".trimIndent()
assertThat(
AnnotationSpacingRule().format(code)
).isEqualTo(
"""
// bar
@SuppressWarnings // foo
fun bar() {
}
// bar
@SuppressWarnings // foo
fun bar() {
}
""".trimIndent()
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class ArgumentListWrappingRuleTest {
assertThat(
ArgumentListWrappingRule().lint(
"""
val x = f(
a,
b, c
)
val x = f(
a,
b, c
)
""".trimIndent()
)
).isEqualTo(
Expand All @@ -36,19 +36,19 @@ class ArgumentListWrappingRuleTest {
assertThat(
ArgumentListWrappingRule().format(
"""
val x = f(
a,
b, c
)
val x = f(
a,
b, c
)
""".trimIndent()
)
).isEqualTo(
"""
val x = f(
a,
b,
c
)
val x = f(
a,
b,
c
)
""".trimIndent()
)
}
Expand All @@ -58,25 +58,25 @@ class ArgumentListWrappingRuleTest {
assertThat(
ArgumentListWrappingRule().format(
"""
val x = test(
one("a", "b",
"c"),
"Two", "Three", "Four"
)
val x = test(
one("a", "b",
"c"),
"Two", "Three", "Four"
)
""".trimIndent()
)
).isEqualTo(
"""
val x = test(
one(
"a",
"b",
"c"
),
"Two",
"Three",
"Four"
)
val x = test(
one(
"a",
"b",
"c"
),
"Two",
"Three",
"Four"
)
""".trimIndent()
)
}
Expand All @@ -86,7 +86,7 @@ class ArgumentListWrappingRuleTest {
assertThat(
ArgumentListWrappingRule().lint(
"""
val x = f(a, b, c)
val x = f(a, b, c)
""".trimIndent(),
userData = mapOf("max_line_length" to "10")
)
Expand Down
Loading

0 comments on commit 27f90a4

Please sign in to comment.