Skip to content

Commit

Permalink
IndentationRule: fix wrong indentation in delegation (#961)
Browse files Browse the repository at this point in the history
* IndentationRule: fix wrong indentation in delegation

* Add a test for #963
  • Loading branch information
t-kameyama authored Nov 18, 2020
1 parent c3e8602 commit 5e088fe
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.pinterest.ktlint.core.ast.ElementType.CLOSING_QUOTE
import com.pinterest.ktlint.core.ast.ElementType.COLON
import com.pinterest.ktlint.core.ast.ElementType.COMMA
import com.pinterest.ktlint.core.ast.ElementType.CONDITION
import com.pinterest.ktlint.core.ast.ElementType.DELEGATED_SUPER_TYPE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.DOT
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.ELSE
Expand Down Expand Up @@ -488,26 +489,6 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
// class A :
// SUPER_TYPE_LIST
adjustExpectedIndentInsideSuperTypeList(n)
SUPER_TYPE_CALL_ENTRY -> {
// IDEA quirk:
//
// class A : B({
// f() {}
// }),
// C({
// f() {}
// })
//
// instead of expected
//
// class A : B({
// f() {}
// }),
// C({
// f() {}
// })
adjustExpectedIndentInsideSuperTypeCall(n, ctx)
}
STRING_TEMPLATE ->
indentStringTemplate(n, autoCorrect, emit, editorConfig)
DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION, BINARY_EXPRESSION, BINARY_WITH_TYPE -> {
Expand Down Expand Up @@ -682,32 +663,17 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
}

private fun adjustExpectedIndentInsideSuperTypeList(n: ASTNode) {
if (!n.treePrev.isWhiteSpaceWithNewline() && n.children().none { it.isWhiteSpaceWithNewline() }) return
expectedIndent++
debug { "++inside(${n.elementType}) -> $expectedIndent" }
}

private fun adjustExpectedIndentAfterSuperTypeList(n: ASTNode) {
if (!n.treePrev.isWhiteSpaceWithNewline() && n.children().none { it.isWhiteSpaceWithNewline() }) return
expectedIndent--
debug { "--after(${n.elementType}) -> $expectedIndent" }
}

private fun adjustExpectedIndentInsideSuperTypeCall(n: ASTNode, ctx: IndentContext) {
// Don't adjust indents for initializer lists
if (n.treeParent?.elementType != SUPER_TYPE_LIST) {
return
}

if (
// n.treePrev == null &&
// n.treeParent.elementType == SUPER_TYPE_LIST &&
n.prevLeaf()?.textContains('\n') == false
) {
expectedIndent--
debug { "--inside(${n.elementType}) -> $expectedIndent" }
ctx.exitAdjBy(n, 1)
}
}

private fun adjustExpectedIndentAfterEq(n: ASTNode, ctx: IndentContext) {
expectedIndent++
debug { "++after(EQ) -> $expectedIndent" }
Expand Down Expand Up @@ -989,7 +955,7 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
// val i: Int
// by lazy { 1 }
nextLeafElementType == BY_KEYWORD ->
1
if (node.isPartOf(DELEGATED_SUPER_TYPE_ENTRY)) 0 else 1
// IDEA quirk:
// var value: DataClass =
// DataClass("too long line")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -625,4 +625,105 @@ internal class IndentationRuleTest {
)
).isEmpty()
}

@Test
fun `lint delegation 1`() {
assertThat(
IndentationRule().lint(
"""
interface Foo
class Bar(a: Int, b: Int, c: Int) : Foo
class Test1 : Foo by Bar(
a = 1,
b = 2,
c = 3
)
""".trimIndent()
)
).isEmpty()
}

@Test
fun `lint delegation 2`() {
assertThat(
IndentationRule().lint(
"""
interface Foo
class Bar(a: Int, b: Int, c: Int) : Foo
class Test2 : Foo
by Bar(
a = 1,
b = 2,
c = 3
)
""".trimIndent()
)
).isEmpty()
}

@Test
fun `lint delegation 3`() {
assertThat(
IndentationRule().lint(
"""
interface Foo
class Bar(a: Int, b: Int, c: Int) : Foo
class Test3 :
Foo by Bar(
a = 1,
b = 2,
c = 3
)
""".trimIndent()
)
).isEmpty()
}

@Test
fun `lint delegation 4`() {
assertThat(
IndentationRule().lint(
"""
interface Foo
class Bar(a: Int, b: Int, c: Int) : Foo
class Test4 :
Foo
by Bar(
a = 1,
b = 2,
c = 3
)
""".trimIndent()
)
).isEmpty()
}

@Test
fun `lint delegation 5`() {
assertThat(
IndentationRule().lint(
"""
interface Foo
class Bar(a: Int, b: Int, c: Int) : Foo
class Test5 {
companion object : Foo by Bar(
a = 1,
b = 2,
c = 3
)
}
""".trimIndent()
)
).isEmpty()
}
}

0 comments on commit 5e088fe

Please sign in to comment.