Skip to content

Commit

Permalink
Fix indentation for lines at expected indentation level but with wron…
Browse files Browse the repository at this point in the history
…g indentation style (pinterest#425)
  • Loading branch information
Paul Dingemans committed Dec 25, 2020
1 parent 68e65db commit 7e749f0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,9 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
(if (!autoCorrect) "would have " else "") +
"changed indentation to $expectedIndentLength (from ${normalizedNodeIndent.length})"
}
if (autoCorrect) {
}
if (autoCorrect) {
if (nodeIndent != normalizedNodeIndent || normalizedNodeIndent.length != expectedIndentLength) {
val indent = when (editorConfig.indentStyle) {
IndentStyle.SPACE -> " "
IndentStyle.TAB -> "\t"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,32 @@ internal class IndentationRuleTest {
.isEqualTo("fun main() {\n return 0\n}")
}

@Test
fun testUnexpectedTabCharacterForLinesAtCorrectIndentationLevel() {
val ktScript = "" +
"class Foo {\n" +
"\tfun doBar() {\n" +
"\t\t\tprintln(\"test\")\n" +
"\t}\n" +
"}\n"
assertThat(IndentationRule().lint(ktScript)).isEqualTo(
listOf(
LintError(line = 2, col = 1, ruleId = "indent", detail = "Unexpected tab character(s)"),
LintError(line = 3, col = 1, ruleId = "indent", detail = "Unexpected tab character(s)"),
LintError(line = 3, col = 1, ruleId = "indent", detail = "Unexpected indentation (12) (should be 8)"),
LintError(line = 4, col = 1, ruleId = "indent", detail = "Unexpected tab character(s)")
)
)
assertThat(IndentationRule().format(ktScript))
.isEqualTo(
"class Foo {\n" +
" fun doBar() {\n" +
" println(\"test\")\n" +
" }\n" +
"}\n"
)
}

@Test
fun testUnexpectedTabCharacterWithCustomIndentSize() {
val ktScript = "fun main() {\n\t\treturn 0\n\t}"
Expand Down

0 comments on commit 7e749f0

Please sign in to comment.