From d2e9455559e85bb9c31b761c9f13044fdfca3726 Mon Sep 17 00:00:00 2001 From: Andrey Shcheglov Date: Mon, 20 Jun 2022 18:12:41 +0300 Subject: [PATCH] [#1347] Clean up the code ### What's done: * `IndentationError` moved to a separate source file. * Top-level extensions moved from `IndentationRule.kt` to `StringUtils.kt`. --- .../rules/chapter3/files/IndentationError.kt | 7 ++++++ .../rules/chapter3/files/IndentationRule.kt | 24 ++++--------------- .../cqfn/diktat/ruleset/utils/StringUtils.kt | 18 ++++++++++++++ .../ruleset/utils/indentation/Checkers.kt | 2 +- 4 files changed, 31 insertions(+), 20 deletions(-) create mode 100644 diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationError.kt diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationError.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationError.kt new file mode 100644 index 0000000000..ff00e2b5cd --- /dev/null +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationError.kt @@ -0,0 +1,7 @@ +package org.cqfn.diktat.ruleset.rules.chapter3.files + +/** + * @property expected expected 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/rules/chapter3/files/IndentationRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationRule.kt index 5c638b0234..1571166f63 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 @@ -8,8 +8,9 @@ import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_INDENTATION import org.cqfn.diktat.ruleset.rules.DiktatRule -import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationRule.Companion.NEWLINE -import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationRule.Companion.SPACE +import org.cqfn.diktat.ruleset.utils.NEWLINE +import org.cqfn.diktat.ruleset.utils.SPACE +import org.cqfn.diktat.ruleset.utils.TAB import org.cqfn.diktat.ruleset.utils.calculateLineColByOffset import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType import org.cqfn.diktat.ruleset.utils.getAllLeafsWithSpecificType @@ -27,6 +28,8 @@ import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig import org.cqfn.diktat.ruleset.utils.indentation.KdocIndentationChecker import org.cqfn.diktat.ruleset.utils.indentation.SuperTypeListChecker import org.cqfn.diktat.ruleset.utils.indentation.ValueParameterListChecker +import org.cqfn.diktat.ruleset.utils.isSpaceCharacter +import org.cqfn.diktat.ruleset.utils.lastIndent import org.cqfn.diktat.ruleset.utils.leaveOnlyOneNewLine import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION @@ -506,9 +509,6 @@ class IndentationRule(configRules: List) : DiktatRule( */ const val DEFAULT_INDENT_SIZE = 4 const val NAME_ID = "zct-indentation" - internal const val NEWLINE = '\n' - internal const val SPACE = ' ' - internal const val TAB = '\t' 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) @@ -558,17 +558,3 @@ class IndentationRule(configRules: List) : DiktatRule( } } } - -/** - * @property expected expected 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) - -/** - * @return indentation of the last line of this string - */ -internal fun String.lastIndent() = substringAfterLast(NEWLINE).count(::isSpaceCharacter) - -private fun isSpaceCharacter(ch: Char): Boolean = - ch == SPACE diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/StringUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/StringUtils.kt index 5550ae8e43..cf1eb0416c 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/StringUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/StringUtils.kt @@ -6,6 +6,12 @@ package org.cqfn.diktat.ruleset.utils import org.jetbrains.kotlin.lexer.KtTokens +internal const val NEWLINE = '\n' + +internal const val SPACE = ' ' + +internal const val TAB = '\t' + @Suppress("VARIABLE_NAME_INCORRECT_FORMAT") val JAVA = arrayOf("abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "const", @@ -97,3 +103,15 @@ fun String.removePrefix(): String { } return this } + +/** + * @return the indentation of the last line of this string. + */ +internal fun String.lastIndent() = substringAfterLast(NEWLINE).count(::isSpaceCharacter) + +/** + * @param ch the character to examine. + * @return `true` if [ch] is a [SPACE], `false` otherwise. + */ +internal fun isSpaceCharacter(ch: Char): Boolean = + ch == SPACE 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 019c0abb06..5ab0f78d31 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 @@ -5,8 +5,8 @@ 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 org.cqfn.diktat.ruleset.utils.lastIndent import com.pinterest.ktlint.core.ast.ElementType.ARROW import com.pinterest.ktlint.core.ast.ElementType.AS_KEYWORD