From ed19ffd732e270604f8e0f4c90d36037dc873e63 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Thu, 11 Feb 2021 11:13:57 +0300 Subject: [PATCH] bugfix/indentation-inside-string-templates(#758) ### What's done: * Fixed bugs * Added tests --- .../rules/chapter3/files/IndentationRule.kt | 8 +++++--- .../ruleset/utils/indentation/Checkers.kt | 1 + .../spaces/IndentationRuleWarnTest.kt | 20 ++++++++++++++----- .../indentation/IndentationFull1Expected.kt | 8 ++++++++ .../indentation/IndentationFull1Test.kt | 8 ++++++++ 5 files changed, 37 insertions(+), 8 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationRule.kt index 79a3aac6cc..68dcde3a4f 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationRule.kt @@ -33,6 +33,8 @@ import com.pinterest.ktlint.core.ast.ElementType.FILE import com.pinterest.ktlint.core.ast.ElementType.LBRACE import com.pinterest.ktlint.core.ast.ElementType.LBRACKET import com.pinterest.ktlint.core.ast.ElementType.LITERAL_STRING_TEMPLATE_ENTRY +import com.pinterest.ktlint.core.ast.ElementType.LONG_TEMPLATE_ENTRY_END +import com.pinterest.ktlint.core.ast.ElementType.LONG_TEMPLATE_ENTRY_START import com.pinterest.ktlint.core.ast.ElementType.LPAR import com.pinterest.ktlint.core.ast.ElementType.RBRACE import com.pinterest.ktlint.core.ast.ElementType.RBRACKET @@ -325,15 +327,15 @@ class IndentationRule(configRules: List) : DiktatRule("indentation" companion object { const val INDENT_SIZE = 4 - private val increasingTokens = listOf(LPAR, LBRACE, LBRACKET) - private val decreasingTokens = listOf(RPAR, RBRACE, RBRACKET) + private val increasingTokens = listOf(LPAR, LBRACE, LBRACKET, LONG_TEMPLATE_ENTRY_START) + private val decreasingTokens = listOf(RPAR, RBRACE, RBRACKET, LONG_TEMPLATE_ENTRY_END) private val matchingTokens = increasingTokens.zip(decreasingTokens) } } /** * @property expected expected indentation as a number of spaces - * @property actual actial indentation as a number of spaces + * @property actual actual indentation as a number of spaces */ internal data class IndentationError(val expected: Int, val actual: Int) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/Checkers.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/Checkers.kt index 875df6755c..a578f34962 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/Checkers.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/Checkers.kt @@ -24,6 +24,7 @@ import com.pinterest.ktlint.core.ast.ElementType.IS_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.KDOC_END import com.pinterest.ktlint.core.ast.ElementType.KDOC_LEADING_ASTERISK import com.pinterest.ktlint.core.ast.ElementType.KDOC_SECTION +import com.pinterest.ktlint.core.ast.ElementType.LONG_STRING_TEMPLATE_ENTRY import com.pinterest.ktlint.core.ast.ElementType.LPAR import com.pinterest.ktlint.core.ast.ElementType.OPERATION_REFERENCE import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleWarnTest.kt index 8b19ae3c57..7a873c92a4 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleWarnTest.kt @@ -608,15 +608,25 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { @Test @Tag(WarningNames.WRONG_INDENTATION) - fun `test`() { + fun `should trigger on string templates starting with new line`() { lintMethod( """ |fun foo(some: String) { - | val a = "${'$'}{ - | expression - | }" + | fun bar() { + | val a = "${'$'}{ + | expression + | .foo() + | .bar() + | }" + | } + | + | val b = "${'$'}{ foo().bar() }" |} - """.trimMargin() + | + """.trimMargin(), + LintError(4, 1, ruleId, warnText(12, 8), true), + LintError(5, 1, ruleId, warnText(16, 12), true), + LintError(6, 1, ruleId, warnText(16, 12), true) ) } diff --git a/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Expected.kt b/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Expected.kt index 53f92028ba..73b6bfd51d 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Expected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Expected.kt @@ -32,5 +32,13 @@ data class Example(val field1: Type1, else foobaz() } + + fun some() { + val a = "${ + foo().bar() + }" + + val b = "${baz().foo()}" + } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Test.kt b/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Test.kt index 0174a58413..e21fcca323 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Test.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Test.kt @@ -32,4 +32,12 @@ fun foo( else foobaz() } + + fun some() { + val a = "${ + foo().bar() + }" + + val b = "${baz().foo()}" + } }