Skip to content

Commit

Permalink
Fix indentation of secondary constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Dingemans committed Dec 11, 2021
1 parent c9fbab9 commit 222dd50
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/).
### Fixed
- Fix false positive in rule spacing-between-declarations-with-annotations ([#1281](https://github.com/pinterest/ktlint/issues/1281))
- Fix NoSuchElementException for property accessor (`trailing-comma`) ([#1280](https://github.com/pinterest/ktlint/issues/1280))
- Fix indentation of secondary constructor (`indent`) ([#1222](https://github.com/pinterest/ktlint/issues/1222))

### Changed
- Update Kotlin version to `1.6.0` release
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ import com.pinterest.ktlint.core.ast.ElementType.RBRACKET
import com.pinterest.ktlint.core.ast.ElementType.REGULAR_STRING_PART
import com.pinterest.ktlint.core.ast.ElementType.RPAR
import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.SECONDARY_CONSTRUCTOR
import com.pinterest.ktlint.core.ast.ElementType.SHORT_STRING_TEMPLATE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE
import com.pinterest.ktlint.core.ast.ElementType.SUPER_TYPE_CALL_ENTRY
Expand Down Expand Up @@ -765,13 +766,24 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
}

private fun adjustExpectedIndentAfterColon(n: ASTNode, ctx: IndentContext) {
expectedIndent++
debug { "++after(COLON) -> $expectedIndent" }
if (n.isPartOf(FUN)) {
val returnType = n.nextCodeSibling()
ctx.exitAdjBy(returnType!!, -1)
} else {
ctx.exitAdjBy(n.treeParent, -1)
when {
n.isPartOf(FUN) -> {
expectedIndent++
debug { "++after(COLON IN FUN) -> $expectedIndent" }
val returnType = n.nextCodeSibling()
ctx.exitAdjBy(returnType!!, -1)
}
n.treeParent.isPartOf(SECONDARY_CONSTRUCTOR) -> {
expectedIndent += 2
debug { "++after(COLON IN CONSTRUCTOR) -> $expectedIndent" }
val returnType = n.nextCodeSibling()
ctx.exitAdjBy(returnType!!, -2)
}
else -> {
expectedIndent++
debug { "++after(COLON) -> $expectedIndent" }
ctx.exitAdjBy(n.treeParent, -1)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1343,6 +1343,66 @@ internal class IndentationRuleTest {
assertThat(IndentationRule().format(codeTabs, INDENT_STYLE_TABS)).isEqualTo(codeTabs)
}

@Test
fun `Issue 1222 - format secondary constructor`() {
val code =
"""
class Issue1222 {
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) :
super(context, attrs, defStyleAttr, defStyleRes) {
init(attrs, defStyleAttr, defStyleRes)
}
}
""".trimIndent()
val formattedCode =
"""
class Issue1222 {
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) :
super(context, attrs, defStyleAttr, defStyleRes) {
init(attrs, defStyleAttr, defStyleRes)
}
}
""".trimIndent()
assertThat(IndentationRule().lint(code))
.containsExactly(
LintError(3, 1, "indent", "Unexpected indentation (8) (should be 12)"),
)
assertThat(IndentationRule().format(code)).isEqualTo(formattedCode)
}

@Test
fun `Issue 1222 - format class constructor, parameter of super invocations are indented`() {
val code =
"""
class Issue1222 {
constructor(string1: String, string2: String2) :
super(
string1, string2
) {
// do something
}
}
""".trimIndent()
val formattedCode =
"""
class Issue1222 {
constructor(string1: String, string2: String2) :
super(
string1, string2
) {
// do something
}
}
""".trimIndent()
assertThat(IndentationRule().lint(code))
.containsExactly(
LintError(3, 1, "indent", "Unexpected indentation (8) (should be 12)"),
LintError(4, 1, "indent", "Unexpected indentation (8) (should be 16)"),
LintError(5, 1, "indent", "Unexpected indentation (8) (should be 12)"),
)
assertThat(IndentationRule().format(code)).isEqualTo(formattedCode)
}

private companion object {
const val MULTILINE_STRING_QUOTE = "${'"'}${'"'}${'"'}"
const val TAB = "${'\t'}"
Expand Down

0 comments on commit 222dd50

Please # to comment.