Skip to content

Commit

Permalink
add check closed quoter
Browse files Browse the repository at this point in the history
### Whats added:
 * corrected the logic of checking MultilionString
 * added fix test in IndentationRuleFixTest.kt - MultilionStringExpected.kt, MultilionStringTest.kt
 * run diktat:fix@diktat

  ### Issue (#811)
  • Loading branch information
Arrgentum committed Jun 15, 2022
1 parent 1ae4cff commit 86b838b
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -79,6 +81,7 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
}
private lateinit var filePath: String
private lateinit var customIndentationCheckers: List<CustomIndentationChecker>
private lateinit var positionByOffset: (Int) -> Pair<Int, Int>

override fun logic(node: ASTNode) {
if (node.elementType == FILE) {
Expand Down Expand Up @@ -175,9 +178,20 @@ class IndentationRule(configRules: List<RulesConfig>) : 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) {
Expand All @@ -201,7 +215,9 @@ class IndentationRule(configRules: List<RulesConfig>) : 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
)
}

Original file line number Diff line number Diff line change
@@ -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"
)
}

0 comments on commit 86b838b

Please sign in to comment.