From 86b838b3ef7e43ada17c826392fdf4287c7eccfe Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 15 Jun 2022 14:41:52 +0300 Subject: [PATCH] add check closed quoter ### Whats added: * corrected the logic of checking MultilionString * added fix test in IndentationRuleFixTest.kt - MultilionStringExpected.kt, MultilionStringTest.kt * run diktat:fix@diktat ### Issue (#811) --- .../rules/chapter3/files/IndentationRule.kt | 18 ++++++- .../indentation/MultilionStringExpected.kt | 47 +++++++++++++++---- .../indentation/MultilionStringTest.kt | 43 +++++++++++++++-- 3 files changed, 94 insertions(+), 14 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 37c70bba7e..7b607bc9ce 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 @@ -22,6 +22,7 @@ import org.cqfn.diktat.ruleset.utils.indentation.SuperTypeListChecker import org.cqfn.diktat.ruleset.utils.indentation.ValueParameterListChecker import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION +import com.pinterest.ktlint.core.ast.ElementType.CLOSING_QUOTE import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.ELSE import com.pinterest.ktlint.core.ast.ElementType.FILE @@ -32,6 +33,7 @@ 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 +import com.pinterest.ktlint.core.ast.ElementType.OPEN_QUOTE import com.pinterest.ktlint.core.ast.ElementType.RBRACE import com.pinterest.ktlint.core.ast.ElementType.RBRACKET import com.pinterest.ktlint.core.ast.ElementType.RPAR @@ -79,6 +81,7 @@ class IndentationRule(configRules: List) : DiktatRule( } private lateinit var filePath: String private lateinit var customIndentationCheckers: List + private lateinit var positionByOffset: (Int) -> Pair override fun logic(node: ASTNode) { if (node.elementType == FILE) { @@ -175,9 +178,20 @@ class IndentationRule(configRules: List) : DiktatRule( } } + private fun closeQuoterOffset(nodeWhiteSpace: ASTNode): Int { + val nextNode = nodeWhiteSpace.treeNext + val nextNodeDot = if (nextNode.elementType == DOT_QUALIFIED_EXPRESSION) { + nextNode + } else { + nextNode.getFirstChildWithType(DOT_QUALIFIED_EXPRESSION) + } + return nextNodeDot?.getFirstChildWithType(STRING_TEMPLATE)?.getFirstChildWithType(CLOSING_QUOTE)?.treePrev?.text?.length ?: -1 + } + @Suppress("ForbiddenComment") private fun visitWhiteSpace(astNode: ASTNode, context: IndentContext) { context.maybeIncrement() + positionByOffset = astNode.treeParent.calculateLineColByOffset() val whiteSpace = astNode.psi as PsiWhiteSpace if (astNode.treeNext.elementType in decreasingTokens) { @@ -201,7 +215,9 @@ class IndentationRule(configRules: List) : DiktatRule( context.addException(astNode.treeParent, abs(indentError.expected - indentError.actual), false) } - if (checkResult?.isCorrect != true && expectedIndent != indentError.actual) { + val closeQuoterShift = closeQuoterOffset(astNode) + + if ((checkResult?.isCorrect != true && expectedIndent != indentError.actual) || (closeQuoterShift != expectedIndent && closeQuoterShift > 0)) { WRONG_INDENTATION.warnAndFix(configRules, emitWarn, isFixMode, "expected $expectedIndent but was ${indentError.actual}", whiteSpace.startOffset + whiteSpace.text.lastIndexOf('\n') + 1, whiteSpace.node) { checkStringLiteral(whiteSpace, expectedIndent, indentError.actual) diff --git a/diktat-rules/src/test/resources/test/paragraph3/indentation/MultilionStringExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/indentation/MultilionStringExpected.kt index 8106fde268..d4007dd98c 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/indentation/MultilionStringExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/indentation/MultilionStringExpected.kt @@ -1,15 +1,46 @@ package test.paragraph3.indentation -fun checkScript() { +//test of correct opening quotation mark and incorrect closing quotation mark +fun multilionString() { lintMethod( - """ - |val q = 1 - | - """.trimMargin(), - fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts" + """ + |val q = 1 + | + """.trimMargin(), + fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts" ) } -fun lintMethod(trimMargin: String, fileName: String) { - TODO("Not yet implemented") +//test of incorrect opening quotation mark and incorrect closing quotation mark1 +fun multilionString() { + lintMethod( + """ + |val q = 1 + | + """.trimMargin(), + fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts" + ) +} + +//test of incorrect opening quotation mark and incorrect closing quotation mark2 +fun multilionString() { + lintMethod( + """ + |val q = 1 + | + """.trimMargin(), + fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts" + ) } + +//test of incorrect opening quotation mark and incorrect closing quotation mark with incorrect shift +fun multilionString() { + lintMethod( + """ + |val q = 1 + | + """.trimMargin(), + fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts" + ) +} + diff --git a/diktat-rules/src/test/resources/test/paragraph3/indentation/MultilionStringTest.kt b/diktat-rules/src/test/resources/test/paragraph3/indentation/MultilionStringTest.kt index 036641b335..6274177be1 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/indentation/MultilionStringTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/indentation/MultilionStringTest.kt @@ -1,13 +1,46 @@ package test.paragraph3.indentation -fun checkScript() { +//test of correct opening quotation mark and incorrect closing quotation mark +fun multilionString() { + lintMethod( + """ + |val q = 1 + | + """.trimMargin(), + fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts" + ) +} + +//test of incorrect opening quotation mark and incorrect closing quotation mark1 +fun multilionString() { + lintMethod( + """ + |val q = 1 + | + """.trimMargin(), + fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts" + ) +} + +//test of incorrect opening quotation mark and incorrect closing quotation mark2 +fun multilionString() { lintMethod( """ - |val A = "ASD" - """.trimMargin(), + |val q = 1 + | + """.trimMargin(), + fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts" ) } -fun lintMethod(trimMargin: String, fileName: String) { - TODO("Not yet implemented") +//test of incorrect opening quotation mark and incorrect closing quotation mark with incorrect shift +fun multilionString() { + lintMethod( + """ + |val q = 1 + | + """.trimMargin(), + fileName = "src/main/kotlin/org/cqfn/diktat/Example.kts" + ) } +