Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Report unexpected indentation characters in margin of multiline string #1260

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,18 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
) {
val psi = node.psi as KtStringTemplateExpression
if (psi.isMultiLine() && psi.isFollowedByTrimIndent()) {
if (node.containsMixedIndentationCharacters()) {
// It can not be determined with certainty how mixed indentation characters should be interpreted.
// The trimIndent function handles tabs and spaces equally (one tabs equals one space) while the user
// might expect that the tab size in the indentation is more than one space.
emit(
node.startOffset,
"Indentation of multiline string should not contain both tab(s) and space(s)",
false
)
return
}

val prefixLength = node.children()
.filterNot { it.elementType == OPEN_QUOTE }
.filterNot { it.elementType == CLOSING_QUOTE }
Expand Down Expand Up @@ -1092,6 +1104,27 @@ class IndentationRule : Rule("indent"), Rule.Modifier.RestrictToRootLast {
children().any { c -> c.textContains('\n') && c.elementType !in ignoreElementTypes }
}
}

private fun ASTNode.containsMixedIndentationCharacters(): Boolean {
assert((this.psi as KtStringTemplateExpression).isMultiLine())
val nonBlankLines = this
.text
.split("\n")
.filterNot { it.startsWith("\"\"\"") }
.filterNot { it.endsWith("\"\"\"") }
.filterNot { it.isBlank() }
val prefixLength = nonBlankLines
.map { it.indentLength() }
.min() ?: 0
val distinctIndentCharacters = nonBlankLines
.joinToString(separator = "") {
it.splitIndentAt(prefixLength).first
}
.toCharArray()
.distinct()
.count()
return distinctIndentCharacters > 1
}
}

private fun ASTNode.isIndentBeforeClosingQuote() =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,31 @@ internal class IndentationRuleTest {
).isEmpty()
}

@Test
fun `multiline string with mixed indentation characters, can not be autocorrected`() {
val code =
"""
val foo = $MULTILINE_STRING_QUOTE
line1
${TAB}line2
$MULTILINE_STRING_QUOTE.trimIndent()
"""
.trimIndent()
assertThat(
IndentationRule().lint(code)
).isEqualTo(
listOf(
LintError(
line = 1,
col = 11,
ruleId = "indent",
detail = "Indentation of multiline string should not contain both tab(s) and space(s)"
),
)
)
assertThat(IndentationRule().format(code)).isEqualTo(code)
}

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