Skip to content

Commit

Permalink
Remove lint warning about unexpected indentation outside of indent rule
Browse files Browse the repository at this point in the history
Only the indentation rule should emit warnings about incorrect indentation to avoid
conflicting warnings from different rules about the indentation of the exact same
line. However, those other rules should still fix the indentation as good as they
can for the case that the indent rule is not run at all.

Closes pinterest#1267, pinterest#1119, pinterest#1045
  • Loading branch information
Paul Dingemans committed Nov 10, 2021
1 parent d044ffe commit 5d25e86
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,18 +139,19 @@ class ArgumentListWrappingRule : Rule("argument-list-wrapping") {
if (childIndent == intendedIndent) {
continue@nextChild
}
emit(
child.startOffset,
"Unexpected indentation" +
" (expected ${intendedIndent.length - 1}, actual ${childIndent.length - 1})",
true
)
// Do not emit a warning about incorrect indentation. Final indentation is
// determined by the indent rule. Displaying, possibly conflicting warnings about
// the exact indenting position is really confusing for the user.
} else {
emit(child.startOffset, errorMessage(child), true)
}
if (autoCorrect) {
val finalIndent =
(if (cut > -1) spacing.substring(0, cut) else "") + intendedIndent
// Although no warning is displayed about incorrect indentation, an attempt is
// made to fix it. Note that in autoCorrect mode the indent rule, if enabled, will
// run after this rule and might fix it to the final indentation. But if the rule
// is disabled then the indent is kept.
val originalIndent = if (cut > -1) spacing.substring(0, cut) else ""
val finalIndent = originalIndent + intendedIndent
argumentInnerIndentAdjustment = finalIndent.length - prevLeaf.getTextLength()
(prevLeaf as LeafPsiElement).rawReplaceWithText(finalIndent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,19 @@ class ParameterListWrappingRule : Rule("parameter-list-wrapping") {
if (childIndent == intendedIndent) {
continue@nextChild
}
emit(
child.startOffset,
"Unexpected indentation" +
" (expected ${intendedIndent.length - 1}, actual ${childIndent.length - 1})",
true
)
// Do not emit a warning about incorrect indentation. Final indentation is
// determined by the indent rule. Displaying, possibly conflicting warnings about
// the exact indenting position is really confusing for the user.
} else {
emit(child.startOffset, errorMessage(child), true)
}
if (autoCorrect) {
val adjustedIndent = (if (cut > -1) spacing.substring(0, cut) else "") + intendedIndent
// Although no warning is displayed about incorrect indentation, an attempt is
// made to fix it. Note that in autoCorrect mode the indent rule, if enabled, will
// run after this rule and might fix it to the final indentation. But if the rule
// is disabled then the indent is kept.
val originalIndent = if (cut > -1) spacing.substring(0, cut) else ""
val adjustedIndent = originalIndent + intendedIndent
paramInnerIndentAdjustment = adjustedIndent.length - prevLeaf.getTextLength()
(prevLeaf as LeafPsiElement).rawReplaceWithText(adjustedIndent)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class ParameterListWrappingRuleTest {
listOf(
LintError(1, 14, "parameter-list-wrapping", "Parameter should be on a separate line (unless all parameters can fit a single line)"),
LintError(1, 30, "parameter-list-wrapping", "Parameter should be on a separate line (unless all parameters can fit a single line)"),
LintError(2, 14, "parameter-list-wrapping", "Unexpected indentation (expected 4, actual 13)"),
LintError(2, 28, "parameter-list-wrapping", """Missing newline before ")"""")
)
)
Expand Down Expand Up @@ -156,8 +155,6 @@ class ParameterListWrappingRuleTest {
).isEqualTo(
listOf(
LintError(1, 7, "parameter-list-wrapping", "Parameter should be on a separate line (unless all parameters can fit a single line)"),
LintError(2, 7, "parameter-list-wrapping", "Unexpected indentation (expected 4, actual 6)"),
LintError(3, 7, "parameter-list-wrapping", "Unexpected indentation (expected 4, actual 6)"),
LintError(3, 13, "parameter-list-wrapping", """Missing newline before ")"""")
)
)
Expand Down Expand Up @@ -447,11 +444,7 @@ class ParameterListWrappingRuleTest {
)
""".trimIndent()
)
).isEqualTo(
listOf(
LintError(6, 4, "parameter-list-wrapping", "Unexpected indentation (expected 4, actual 3)")
)
)
).isEmpty()
}

@Test
Expand Down

0 comments on commit 5d25e86

Please sign in to comment.