From 496c9a37b635f15c7ef271fc4af2213026f5eb4f Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 9 Feb 2021 10:54:08 +0300 Subject: [PATCH 1/6] bugfix/indentation-inside-string-templates(#758) ### What's done: * Added a simple test --- .../chapter3/spaces/IndentationRuleWarnTest.kt | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 5d803c7965..8b19ae3c57 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 @@ -606,5 +606,19 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { ) } + @Test + @Tag(WarningNames.WRONG_INDENTATION) + fun `test`() { + lintMethod( + """ + |fun foo(some: String) { + | val a = "${'$'}{ + | expression + | }" + |} + """.trimMargin() + ) + } + private fun warnText(expected: Int, actual: Int) = "${WRONG_INDENTATION.warnText()} expected $expected but was $actual" } From ed19ffd732e270604f8e0f4c90d36037dc873e63 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Thu, 11 Feb 2021 11:13:57 +0300 Subject: [PATCH 2/6] 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()}" + } } From e120aa4f2c735f211b3ab6e18fc578ba02d5f4ef Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Thu, 11 Feb 2021 18:06:20 +0300 Subject: [PATCH 3/6] bugfix/indentation-inside-string-templates(#758) ### What's done: * New bug with warn (fix work fine) --- .../ruleset/rules/chapter3/files/IndentationRule.kt | 10 +++++++++- .../diktat/ruleset/utils/indentation/Checkers.kt | 12 ++++++++++++ .../chapter3/spaces/IndentationRuleWarnTest.kt | 8 ++++---- .../indentation/IndentationFull1Expected.kt | 6 ++++++ .../paragraph3/indentation/IndentationFull1Test.kt | 6 ++++++ 5 files changed, 37 insertions(+), 5 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 68dcde3a4f..024dfceebb 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,7 @@ 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_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 @@ -44,6 +45,7 @@ import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE import com.pinterest.ktlint.core.ast.ElementType.THEN import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.visit +import org.cqfn.diktat.ruleset.utils.hasParent import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement @@ -58,6 +60,7 @@ import org.jetbrains.kotlin.psi.psiUtil.parents import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf import org.jetbrains.kotlin.psi.psiUtil.startOffset import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult +import kotlin.math.abs /** * Rule that checks indentation. The following general rules are checked: @@ -179,10 +182,15 @@ class IndentationRule(configRules: List) : DiktatRule("indentation" } val expectedIndent = checkResult?.expectedIndent ?: indentError.expected - if (checkResult?.adjustNext == true) { + if (checkResult?.adjustNext == true && !astNode.hasParent(LONG_STRING_TEMPLATE_ENTRY)) { val exceptionInitiatorNode = astNode.getExceptionalIndentInitiator() context.addException(exceptionInitiatorNode, expectedIndent - indentError.expected, checkResult.includeLastChild) } + + if (astNode.treeParent.elementType == LONG_STRING_TEMPLATE_ENTRY && indentError.expected != indentError.actual) { + context.addException(astNode.treeParent, abs(indentError.expected - indentError.actual), false) + } + if (checkResult?.isCorrect != true && expectedIndent != indentError.actual) { WRONG_INDENTATION.warnAndFix(configRules, emitWarn, isFixMode, "expected $expectedIndent but was ${indentError.actual}", whiteSpace.startOffset + whiteSpace.text.lastIndexOf('\n') + 1, whiteSpace.node) { 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 a578f34962..b97d5f0088 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 @@ -29,6 +29,7 @@ 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 import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS +import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE import com.pinterest.ktlint.core.ast.ElementType.SUPER_TYPE_LIST import com.pinterest.ktlint.core.ast.ElementType.THEN import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT @@ -38,6 +39,8 @@ import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.nextCodeSibling import com.pinterest.ktlint.core.ast.prevSibling +import org.cqfn.diktat.ruleset.utils.hasParameters +import org.cqfn.diktat.ruleset.utils.hasParent import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiElement import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace @@ -195,6 +198,9 @@ internal class DotCallChecker(config: IndentationConfig) : CustomIndentationChec return false } + private fun ASTNode.isFromStringTemplate(): Boolean = + hasParent(LONG_STRING_TEMPLATE_ENTRY) + @Suppress("ComplexMethod") override fun checkNode(whiteSpace: PsiWhiteSpace, indentError: IndentationError): CheckResult? { whiteSpace.nextSibling.node @@ -205,6 +211,12 @@ internal class DotCallChecker(config: IndentationConfig) : CustomIndentationChec } || nextNode.isCommentBeforeDot() } ?.let { + if (it.isFromStringTemplate()) { + val template = it.parents().takeWhile { it.elementType != STRING_TEMPLATE }.last() + return CheckResult.from(indentError.actual, indentError.expected + + (if (configuration.extendedIndentBeforeDot) 2 else 1) * configuration.indentationSize, true) + } + // we need to get indent before the first expression in calls chain return CheckResult.from(indentError.actual, (whiteSpace.run { parents.takeWhile { it is KtDotQualifiedExpression || it is KtSafeQualifiedExpression }.lastOrNull() ?: this 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 7a873c92a4..b7b21c4e32 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 @@ -610,7 +610,7 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { @Tag(WarningNames.WRONG_INDENTATION) fun `should trigger on string templates starting with new line`() { lintMethod( - """ + """ |fun foo(some: String) { | fun bar() { | val a = "${'$'}{ @@ -624,9 +624,9 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { |} | """.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) + 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 73b6bfd51d..d1bca4541b 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Expected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Expected.kt @@ -39,6 +39,12 @@ data class Example(val field1: Type1, }" val b = "${baz().foo()}" + + val c = "${ + expression + .foo() + .bar() + }" } } 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 e21fcca323..2eb1ab279e 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Test.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/indentation/IndentationFull1Test.kt @@ -39,5 +39,11 @@ fun foo( }" val b = "${baz().foo()}" + + val c = "${ + expression + .foo() + .bar() + }" } } From c92c850016627b3920ff1dce4194b1f6cd672b63 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 16 Feb 2021 10:20:24 +0300 Subject: [PATCH 4/6] bugfix/indentation-inside-string-templates(#758) ### What's done: * Fixed bug --- .../diktat/ruleset/rules/chapter3/files/IndentationRule.kt | 2 +- .../org/cqfn/diktat/ruleset/utils/indentation/Checkers.kt | 5 ++--- .../ruleset/chapter3/spaces/IndentationRuleWarnTest.kt | 3 ++- 3 files changed, 5 insertions(+), 5 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 024dfceebb..7f2118dc88 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 @@ -182,7 +182,7 @@ class IndentationRule(configRules: List) : DiktatRule("indentation" } val expectedIndent = checkResult?.expectedIndent ?: indentError.expected - if (checkResult?.adjustNext == true && !astNode.hasParent(LONG_STRING_TEMPLATE_ENTRY)) { + if (checkResult?.adjustNext == true && astNode.parents().none { it.elementType == LONG_STRING_TEMPLATE_ENTRY }) { val exceptionInitiatorNode = astNode.getExceptionalIndentInitiator() context.addException(exceptionInitiatorNode, expectedIndent - indentError.expected, checkResult.includeLastChild) } 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 b97d5f0088..0e3f2b550d 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 @@ -39,7 +39,6 @@ import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.nextCodeSibling import com.pinterest.ktlint.core.ast.prevSibling -import org.cqfn.diktat.ruleset.utils.hasParameters import org.cqfn.diktat.ruleset.utils.hasParent import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiElement @@ -205,10 +204,10 @@ internal class DotCallChecker(config: IndentationConfig) : CustomIndentationChec override fun checkNode(whiteSpace: PsiWhiteSpace, indentError: IndentationError): CheckResult? { whiteSpace.nextSibling.node .takeIf { nextNode -> - nextNode.isDotBeforeCallOrReference() || + (nextNode.isDotBeforeCallOrReference() || nextNode.elementType == OPERATION_REFERENCE && nextNode.firstChildNode.elementType.let { it == ELVIS || it == IS_EXPRESSION || it == AS_KEYWORD || it == AS_SAFE - } || nextNode.isCommentBeforeDot() + } || nextNode.isCommentBeforeDot()) && whiteSpace.parents.none { it.node.elementType == LONG_STRING_TEMPLATE_ENTRY } } ?.let { if (it.isFromStringTemplate()) { 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 b7b21c4e32..836be9aafa 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 @@ -626,7 +626,8 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { """.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) + LintError(6, 1, ruleId, warnText(16, 12), true), + rulesConfigList = rulesConfigList ) } From 8a5ecfc2ac41aaf80255a47014c8faaa3246df10 Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 16 Feb 2021 10:25:59 +0300 Subject: [PATCH 5/6] bugfix/indentation-inside-string-templates(#758) ### What's done: * Fixed bug --- .../ruleset/rules/chapter3/files/IndentationRule.kt | 1 - .../cqfn/diktat/ruleset/utils/indentation/Checkers.kt | 6 +++--- .../ruleset/chapter3/spaces/IndentationRuleWarnTest.kt | 10 +++++----- 3 files changed, 8 insertions(+), 9 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 e016143a83..bad5240b9d 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 @@ -45,7 +45,6 @@ import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE import com.pinterest.ktlint.core.ast.ElementType.THEN import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.visit -import org.cqfn.diktat.ruleset.utils.hasParent import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement 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 0e3f2b550d..8ecd8494a7 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 @@ -6,6 +6,7 @@ package org.cqfn.diktat.ruleset.utils.indentation import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationError import org.cqfn.diktat.ruleset.rules.chapter3.files.lastIndent +import org.cqfn.diktat.ruleset.utils.hasParent import com.pinterest.ktlint.core.ast.ElementType.ARROW import com.pinterest.ktlint.core.ast.ElementType.AS_KEYWORD @@ -39,7 +40,6 @@ import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.nextCodeSibling import com.pinterest.ktlint.core.ast.prevSibling -import org.cqfn.diktat.ruleset.utils.hasParent import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.PsiElement import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace @@ -205,8 +205,8 @@ internal class DotCallChecker(config: IndentationConfig) : CustomIndentationChec whiteSpace.nextSibling.node .takeIf { nextNode -> (nextNode.isDotBeforeCallOrReference() || - nextNode.elementType == OPERATION_REFERENCE && nextNode.firstChildNode.elementType.let { - it == ELVIS || it == IS_EXPRESSION || it == AS_KEYWORD || it == AS_SAFE + nextNode.elementType == OPERATION_REFERENCE && nextNode.firstChildNode.elementType.let { type -> + type == ELVIS || type == IS_EXPRESSION || type == AS_KEYWORD || type == AS_SAFE } || nextNode.isCommentBeforeDot()) && whiteSpace.parents.none { it.node.elementType == LONG_STRING_TEMPLATE_ENTRY } } ?.let { 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 836be9aafa..d73159b3ca 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 @@ -610,7 +610,7 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { @Tag(WarningNames.WRONG_INDENTATION) fun `should trigger on string templates starting with new line`() { lintMethod( - """ + """ |fun foo(some: String) { | fun bar() { | val a = "${'$'}{ @@ -624,10 +624,10 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { |} | """.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), - rulesConfigList = rulesConfigList + LintError(4, 1, ruleId, warnText(12, 8), true), + LintError(5, 1, ruleId, warnText(16, 12), true), + LintError(6, 1, ruleId, warnText(16, 12), true), + rulesConfigList = rulesConfigList ) } From b0531b405ecf9cdf38f58199bfa658953e84b20f Mon Sep 17 00:00:00 2001 From: aktsay6 Date: Tue, 16 Feb 2021 10:28:53 +0300 Subject: [PATCH 6/6] bugfix/indentation-inside-string-templates(#758) ### What's done: * Fixed bug --- .../org/cqfn/diktat/ruleset/utils/indentation/Checkers.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 8ecd8494a7..35b62e78aa 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 @@ -209,9 +209,9 @@ internal class DotCallChecker(config: IndentationConfig) : CustomIndentationChec type == ELVIS || type == IS_EXPRESSION || type == AS_KEYWORD || type == AS_SAFE } || nextNode.isCommentBeforeDot()) && whiteSpace.parents.none { it.node.elementType == LONG_STRING_TEMPLATE_ENTRY } } - ?.let { - if (it.isFromStringTemplate()) { - val template = it.parents().takeWhile { it.elementType != STRING_TEMPLATE }.last() + ?.let { node -> + if (node.isFromStringTemplate()) { + val template = node.parents().takeWhile { it.elementType != STRING_TEMPLATE }.last() return CheckResult.from(indentError.actual, indentError.expected + (if (configuration.extendedIndentBeforeDot) 2 else 1) * configuration.indentationSize, true) }