From 771789a705a2cea21d7c5e77d9f5ad811b55d76d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 22 Sep 2020 18:35:40 +0300 Subject: [PATCH 1/4] Merge branch 'master' into 'feature/configurableindentation', --- .../ruleset/rules/files/IndentationRule.kt | 11 +++++------ .../ruleset/utils/indentation/Checkers.kt | 17 ++++++++--------- .../utils/indentation/IndentationConfig.kt | 5 +++++ 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/IndentationRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/IndentationRule.kt index 43b1749071..6600825290 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/IndentationRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/IndentationRule.kt @@ -46,7 +46,6 @@ import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult */ class IndentationRule(private val configRules: List) : Rule("indentation") { companion object { - const val INDENT_SIZE = 4 private val increasingTokens = listOf(LPAR, LBRACE, LBRACKET) private val decreasingTokens = listOf(RPAR, RBRACE, RBRACKET) } @@ -100,7 +99,7 @@ class IndentationRule(private val configRules: List) : Rule("indent .apply { if (isEmpty()) return true } .forEach { WRONG_INDENTATION.warnAndFix(configRules, emitWarn, isFixMode, "tabs are not allowed for indentation", it.startOffset + it.text.indexOf('\t'), it) { - (it as LeafPsiElement).replaceWithText(it.text.replace("\t", " ".repeat(INDENT_SIZE))) + (it as LeafPsiElement).replaceWithText(it.text.replace("\t", " ".repeat(configuration.indentationSize))) } } return isFixMode // true if we changed all tabs to spaces @@ -128,7 +127,7 @@ class IndentationRule(private val configRules: List) : Rule("indent * Traverses the tree, keeping track of regular and exceptional indentations */ private fun checkIndentation(node: ASTNode) { - val context = IndentContext() + val context = IndentContext(configuration) node.visit { astNode -> context.checkAndReset(astNode) if (astNode.elementType in increasingTokens) { @@ -176,16 +175,16 @@ class IndentationRule(private val configRules: List) : Rule("indent /** * Class that contains state needed to calculate indent and keep track of exceptional indents */ - private class IndentContext { + private class IndentContext(private val config: IndentationConfig) { private var regularIndent = 0 private val exceptionalIndents = mutableListOf() fun inc() { - regularIndent += INDENT_SIZE + regularIndent += config.indentationSize } fun dec() { - regularIndent -= INDENT_SIZE + regularIndent -= config.indentationSize } fun indent() = regularIndent + exceptionalIndents.sumBy { it.indent } 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 4bc89e659c..2ab4d3fc4d 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 @@ -23,7 +23,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.prevSibling import org.cqfn.diktat.ruleset.rules.files.IndentationError -import org.cqfn.diktat.ruleset.rules.files.IndentationRule.Companion.INDENT_SIZE import org.cqfn.diktat.ruleset.rules.files.lastIndent import org.jetbrains.kotlin.com.intellij.psi.PsiElement import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace @@ -44,7 +43,7 @@ internal class AssignmentOperatorChecker(configuration: IndentationConfig) : Cus val prevNode = whiteSpace.prevSibling.node if (prevNode.elementType == EQ && prevNode.treeNext.let { it.elementType == WHITE_SPACE && it.textContains('\n') }) { return CheckResult.from(indentError.actual, (whiteSpace.parentIndent() - ?: indentError.expected) + INDENT_SIZE, true) + ?: indentError.expected) + configuration.indentationSize, true) } return null } @@ -69,7 +68,7 @@ internal class ValueParameterListChecker(configuration: IndentationConfig) : Cus // fixme: probably there is a better way to find column number parameterList.parents().last().text.substringBefore(parameterAfterLpar.text).lines().last().count() } else if (parameterAfterLpar == null && configuration.extendedIndentOfParameters) { - indentError.expected + INDENT_SIZE + indentError.expected + configuration.indentationSize } else { indentError.expected } @@ -87,7 +86,7 @@ internal class ExpressionIndentationChecker(configuration: IndentationConfig) : override fun checkNode(whiteSpace: PsiWhiteSpace, indentError: IndentationError): CheckResult? { if (whiteSpace.parent.node.elementType == BINARY_EXPRESSION && whiteSpace.prevSibling.node.elementType == OPERATION_REFERENCE) { val expectedIndent = (whiteSpace.parentIndent() ?: indentError.expected) + - (if (configuration.extendedIndentAfterOperators) 2 else 1) * INDENT_SIZE + (if (configuration.extendedIndentAfterOperators) 2 else 1) * configuration.indentationSize return CheckResult.from(indentError.actual, expectedIndent) } return null @@ -117,10 +116,10 @@ internal class SuperTypeListChecker(config: IndentationConfig) : CustomIndentati if (whiteSpace.nextSibling.node.elementType == SUPER_TYPE_LIST) { val hasNewlineBeforeColon = whiteSpace.node.prevSibling { it.elementType == COLON }!! .treePrev.takeIf { it.elementType == WHITE_SPACE }?.textContains('\n') ?: false - val expectedIndent = indentError.expected + (if (hasNewlineBeforeColon) 2 else 1) * INDENT_SIZE + val expectedIndent = indentError.expected + (if (hasNewlineBeforeColon) 2 else 1) * configuration.indentationSize return CheckResult.from(indentError.actual, expectedIndent) } else if (whiteSpace.parent.node.elementType == SUPER_TYPE_LIST) { - val expectedIndent = whiteSpace.parentIndent() ?: (indentError.expected + INDENT_SIZE) + val expectedIndent = whiteSpace.parentIndent() ?: (indentError.expected + configuration.indentationSize) return CheckResult.from(indentError.actual, expectedIndent) } return null @@ -128,7 +127,7 @@ internal class SuperTypeListChecker(config: IndentationConfig) : CustomIndentati } /** - * This checker performs the following check: When dot call start on a new line, it should be indented by [INDENT_SIZE] + * This checker performs the following check: When dot call start on a new line, it should be indented by [IndentationConfig.indentationSize] */ internal class DotCallChecker(config: IndentationConfig) : CustomIndentationChecker(config) { override fun checkNode(whiteSpace: PsiWhiteSpace, indentError: IndentationError): CheckResult? { @@ -136,7 +135,7 @@ internal class DotCallChecker(config: IndentationConfig) : CustomIndentationChec it.elementType in listOf(DOT, SAFE_ACCESS) && it.treeNext.elementType in listOf(CALL_EXPRESSION, REFERENCE_EXPRESSION) }?.let { return CheckResult.from(indentError.actual, (whiteSpace.parentIndent() - ?: indentError.expected) + INDENT_SIZE, true) + ?: indentError.expected) + configuration.indentationSize, true) } return null } @@ -157,7 +156,7 @@ internal class ConditionalsAndLoopsWithoutBracesChecker(config: IndentationConfi .takeIf { it } ?.let { CheckResult.from(indentError.actual, (whiteSpace.parentIndent() - ?: indentError.expected) + INDENT_SIZE, false) + ?: indentError.expected) + configuration.indentationSize, false) } } } diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt index eeb8fbea00..2e6ce78b9a 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt @@ -20,4 +20,9 @@ internal class IndentationConfig(config: Map) : RuleConfiguratio * If true, if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one */ val extendedIndentAfterOperators = config["extendedIndentAfterOperators"]?.toBoolean() ?: true + + /** + * The indentation size for each file + */ + val indentationSize = config["indentationSize"]?.toInt() ?: 4 } From a4920df50462c29345cedd629434145d1fb540c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 22 Sep 2020 18:49:37 +0300 Subject: [PATCH 2/4] Merge branch master into feature/configurableindentation --- diktat-analysis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/diktat-analysis.yml b/diktat-analysis.yml index c92be347a0..e920c0f202 100644 --- a/diktat-analysis.yml +++ b/diktat-analysis.yml @@ -171,6 +171,7 @@ extendedIndentOfParameters: true alignedParameters: true extendedIndentAfterOperators: true + indentationSize: 4 - name: EMPTY_BLOCK_STRUCTURE_ERROR enabled: true configuration: From cd4b0face5c3c081d05897cef284efd28d96fdb5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 22 Sep 2020 18:53:30 +0300 Subject: [PATCH 3/4] Merge branch master into feature/configurableindentation --- diktat-common/src/test/resources/test-rules-config.yml | 1 + diktat-rules/src/main/resources/diktat-analysis-huawei.yml | 1 + diktat-rules/src/main/resources/diktat-analysis.yml | 1 + 3 files changed, 3 insertions(+) diff --git a/diktat-common/src/test/resources/test-rules-config.yml b/diktat-common/src/test/resources/test-rules-config.yml index ce151307ab..10c371af45 100644 --- a/diktat-common/src/test/resources/test-rules-config.yml +++ b/diktat-common/src/test/resources/test-rules-config.yml @@ -171,6 +171,7 @@ extendedIndentOfParameters: true alignedParameters: true extendedIndentAfterOperators: true + indentationSize: 4 - name: EMPTY_BLOCK_STRUCTURE_ERROR enabled: true configuration: diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml index befbf9d4bf..1ed937db30 100644 --- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml +++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml @@ -171,6 +171,7 @@ extendedIndentOfParameters: true alignedParameters: true extendedIndentAfterOperators: true + indentationSize: 4 - name: EMPTY_BLOCK_STRUCTURE_ERROR enabled: true configuration: diff --git a/diktat-rules/src/main/resources/diktat-analysis.yml b/diktat-rules/src/main/resources/diktat-analysis.yml index 026bcf13b5..a168d247f7 100644 --- a/diktat-rules/src/main/resources/diktat-analysis.yml +++ b/diktat-rules/src/main/resources/diktat-analysis.yml @@ -173,6 +173,7 @@ extendedIndentOfParameters: true alignedParameters: true extendedIndentAfterOperators: true + indentationSize: 4 - name: EMPTY_BLOCK_STRUCTURE_ERROR enabled: true configuration: From 798db58b456903ecad2caccb5e73ea21ec7be84f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hasan=20Demirta=C5=9F?= Date: Tue, 22 Sep 2020 19:39:08 +0300 Subject: [PATCH 4/4] Remove magic number. --- .../org/cqfn/diktat/ruleset/rules/files/IndentationRule.kt | 1 + .../cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/IndentationRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/IndentationRule.kt index 6600825290..d80ca86d56 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/IndentationRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/files/IndentationRule.kt @@ -46,6 +46,7 @@ import org.jetbrains.kotlin.utils.addToStdlib.firstNotNullResult */ class IndentationRule(private val configRules: List) : Rule("indentation") { companion object { + const val INDENT_SIZE = 4 private val increasingTokens = listOf(LPAR, LBRACE, LBRACKET) private val decreasingTokens = listOf(RPAR, RBRACE, RBRACKET) } diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt index 2e6ce78b9a..91f7d56e67 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt @@ -1,6 +1,7 @@ package org.cqfn.diktat.ruleset.utils.indentation import org.cqfn.diktat.common.config.rules.RuleConfiguration +import org.cqfn.diktat.ruleset.rules.files.IndentationRule internal class IndentationConfig(config: Map) : RuleConfiguration(config) { val newlineAtEnd = config["newlineAtEnd"]?.toBoolean() ?: true @@ -24,5 +25,5 @@ internal class IndentationConfig(config: Map) : RuleConfiguratio /** * The indentation size for each file */ - val indentationSize = config["indentationSize"]?.toInt() ?: 4 + val indentationSize = config["indentationSize"]?.toInt() ?: IndentationRule.INDENT_SIZE }