From 81cb1b184055037cd6462a92facbdc93394424e8 Mon Sep 17 00:00:00 2001 From: Andrey Shcheglov Date: Wed, 13 Jul 2022 10:16:51 +0000 Subject: [PATCH] Add an `extendedIndentForExpressionBodies` flag to the `WRONG_INDENTATION` check (#1443) ### What's done: * This commit adds a separate `extendedIndentForExpressionBodies` flag, defaulting to `false`. * The flag controls whether continuation indent is used to indent expression bodies (previously, controlled with `extendedIndentAfterOperators`). * The corresponding flags are: * _IDEA_: `CONTINUATION_INDENT_FOR_EXPRESSION_BODIES` * `.editorconfig`: `ij_kotlin_continuation_indent_for_expression_bodies` --- diktat-analysis.yml | 6 ++ .../src/test/resources/test-rules-config.yml | 1 + .../ruleset/utils/indentation/Checkers.kt | 4 +- .../utils/indentation/IndentationConfig.kt | 40 +++++++ .../main/resources/diktat-analysis-huawei.yml | 6 ++ .../src/main/resources/diktat-analysis.yml | 6 ++ .../chapter3/spaces/IndentationRuleFixTest.kt | 37 +++---- .../spaces/IndentationRuleTestMixin.kt | 2 + .../spaces/IndentationRuleTestResources.kt | 102 +++++++++--------- .../spaces/IndentationRuleWarnTest.kt | 34 +++--- .../diktat/ruleset/smoke/DiktatSmokeTest.kt | 4 + .../gradle-groovy-dsl/diktat-analysis.yml | 6 ++ .../diktat-analysis.yml | 1 + .../gradle-kotlin-dsl/diktat-analysis.yml | 6 ++ examples/maven/diktat-analysis.yml | 6 ++ info/available-rules.md | 2 +- wp/sections/appendix.tex | 2 +- 17 files changed, 178 insertions(+), 87 deletions(-) diff --git a/diktat-analysis.yml b/diktat-analysis.yml index d91b1f999d..3c180a7e72 100644 --- a/diktat-analysis.yml +++ b/diktat-analysis.yml @@ -208,6 +208,12 @@ extendedIndentOfParameters: false # If true: if first parameter in parameter list is on the same line as opening parenthesis, then other parameters can be aligned with it alignedParameters: true + # If true, expression bodies which begin on a separate line are indented + # using a continuation indent. The default is false. + # + # This flag is called CONTINUATION_INDENT_FOR_EXPRESSION_BODIES in IDEA and + # ij_kotlin_continuation_indent_for_expression_bodies in .editorconfig. + extendedIndentForExpressionBodies: false # If true: if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one extendedIndentAfterOperators: false # If true: when dot qualified expression starts on a new line, this line will be indented with two indentations instead of one diff --git a/diktat-common/src/test/resources/test-rules-config.yml b/diktat-common/src/test/resources/test-rules-config.yml index f1436ebc9d..9f1db3d9b1 100644 --- a/diktat-common/src/test/resources/test-rules-config.yml +++ b/diktat-common/src/test/resources/test-rules-config.yml @@ -127,6 +127,7 @@ newlineAtEnd: true extendedIndentOfParameters: false alignedParameters: true + extendedIndentForExpressionBodies: false extendedIndentAfterOperators: false indentationSize: 4 - name: EMPTY_BLOCK_STRUCTURE_ERROR 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 7d8457cdee..54aabeabd7 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 @@ -57,14 +57,14 @@ import org.jetbrains.kotlin.psi.psiUtil.siblings /** * Performs the following check: assignment operator increases indent by one step for the expression after it. - * If [IndentationConfig.extendedIndentAfterOperators] is set to true, indentation is increased by two steps instead. + * If [IndentationConfig.extendedIndentForExpressionBodies] is set to `true`, indentation is increased by two steps instead. */ internal class AssignmentOperatorChecker(configuration: IndentationConfig) : CustomIndentationChecker(configuration) { override fun checkNode(whiteSpace: PsiWhiteSpace, indentError: IndentationError): CheckResult? { 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) + (if (configuration.extendedIndentAfterOperators) 2 else 1) * configuration.indentationSize, true) + ?: indentError.expected) + (if (configuration.extendedIndentForExpressionBodies) 2 else 1) * configuration.indentationSize, true) } return null } 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 c147808e50..3a7223ce22 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 @@ -22,6 +22,46 @@ internal class IndentationConfig(config: Map) : RuleConfiguratio */ val alignedParameters = config["alignedParameters"]?.toBoolean() ?: true + /** + * If `true`, expression bodies which begin on a separate line are indented + * using a _continuation indent_. The flag is **off**: + * + * ```kotlin + * val a: Boolean = + * false + * + * val b: Boolean + * get() = + * false + * + * fun f(): Boolean = + * false + * ``` + * + * The flag is **on**: + * + * ```kotlin + * val a: Boolean = + * false + * + * val b: Boolean + * get() = + * false + * + * fun f(): Boolean = + * false + * ``` + * + * The default is `false`. + * + * This flag is called `CONTINUATION_INDENT_FOR_EXPRESSION_BODIES` in _IDEA_ + * and `ij_kotlin_continuation_indent_for_expression_bodies` in + * `.editorconfig`. + * + * @since 1.2.2 + */ + val extendedIndentForExpressionBodies = config["extendedIndentForExpressionBodies"]?.toBoolean() ?: false + /** * If true, if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one */ diff --git a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml index 59b4fc7959..0ddfd45f0f 100644 --- a/diktat-rules/src/main/resources/diktat-analysis-huawei.yml +++ b/diktat-rules/src/main/resources/diktat-analysis-huawei.yml @@ -210,6 +210,12 @@ extendedIndentOfParameters: false # If true: if first parameter in parameter list is on the same line as opening parenthesis, then other parameters can be aligned with it alignedParameters: true + # If true, expression bodies which begin on a separate line are indented + # using a continuation indent. The default is false. + # + # This flag is called CONTINUATION_INDENT_FOR_EXPRESSION_BODIES in IDEA and + # ij_kotlin_continuation_indent_for_expression_bodies in .editorconfig. + extendedIndentForExpressionBodies: false # If true: if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one extendedIndentAfterOperators: false # If true: when dot qualified expression starts on a new line, this line will be indented with two indentations instead of one diff --git a/diktat-rules/src/main/resources/diktat-analysis.yml b/diktat-rules/src/main/resources/diktat-analysis.yml index c2b8b5b8a2..489d7f55e0 100644 --- a/diktat-rules/src/main/resources/diktat-analysis.yml +++ b/diktat-rules/src/main/resources/diktat-analysis.yml @@ -210,6 +210,12 @@ extendedIndentOfParameters: false # If true: if first parameter in parameter list is on the same line as opening parenthesis, then other parameters can be aligned with it alignedParameters: true + # If true, expression bodies which begin on a separate line are indented + # using a continuation indent. The default is false. + # + # This flag is called CONTINUATION_INDENT_FOR_EXPRESSION_BODIES in IDEA and + # ij_kotlin_continuation_indent_for_expression_bodies in .editorconfig. + extendedIndentForExpressionBodies: false # If true: if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one extendedIndentAfterOperators: false # The indentation size for each file diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleFixTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleFixTest.kt index 9e744752b9..c5a67c9779 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleFixTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleFixTest.kt @@ -40,6 +40,7 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation", "newlineAtEnd" to "true", // expected file should have two newlines at end in order to be read by BufferedReader correctly "extendedIndentOfParameters" to "true", "alignedParameters" to "true", + "extendedIndentForExpressionBodies" to "true", "extendedIndentAfterOperators" to "true", "extendedIndentBeforeDot" to "true", ) @@ -120,29 +121,29 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation", @Nested @TestMethodOrder(DisplayName::class) inner class `Expression body functions` { - @ParameterizedTest(name = "extendedIndentAfterOperators = {0}") + @ParameterizedTest(name = "extendedIndentForExpressionBodies = {0}") @ValueSource(booleans = [false, true]) @Tag(WarningNames.WRONG_INDENTATION) - fun `should remain unchanged if properly indented`(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) { + fun `should remain unchanged if properly indented`(extendedIndentForExpressionBodies: Boolean, @TempDir tempDir: Path) { val defaultConfig = IndentationConfig("newlineAtEnd" to false) - val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators) + val customConfig = defaultConfig.withCustomParameters("extendedIndentForExpressionBodies" to extendedIndentForExpressionBodies) lintMultipleMethods( - expressionBodyFunctions[extendedIndentAfterOperators].assertNotNull(), + expressionBodyFunctions[extendedIndentForExpressionBodies].assertNotNull(), tempDir = tempDir, rulesConfigList = customConfig.asRulesConfigList()) } - @ParameterizedTest(name = "extendedIndentAfterOperators = {0}") + @ParameterizedTest(name = "extendedIndentForExpressionBodies = {0}") @ValueSource(booleans = [false, true]) @Tag(WarningNames.WRONG_INDENTATION) - fun `should be reformatted if mis-indented`(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) { + fun `should be reformatted if mis-indented`(extendedIndentForExpressionBodies: Boolean, @TempDir tempDir: Path) { val defaultConfig = IndentationConfig("newlineAtEnd" to false) - val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators) + val customConfig = defaultConfig.withCustomParameters("extendedIndentForExpressionBodies" to extendedIndentForExpressionBodies) lintMultipleMethods( - actualContent = expressionBodyFunctions[!extendedIndentAfterOperators].assertNotNull(), - expectedContent = expressionBodyFunctions[extendedIndentAfterOperators].assertNotNull(), + actualContent = expressionBodyFunctions[!extendedIndentForExpressionBodies].assertNotNull(), + expectedContent = expressionBodyFunctions[extendedIndentForExpressionBodies].assertNotNull(), tempDir = tempDir, rulesConfigList = customConfig.asRulesConfigList()) } @@ -222,29 +223,29 @@ class IndentationRuleFixTest : FixTestBase("test/paragraph3/indentation", @Nested @TestMethodOrder(DisplayName::class) inner class `Parentheses-surrounded infix expressions` { - @ParameterizedTest(name = "extendedIndentAfterOperators = {0}") + @ParameterizedTest(name = "extendedIndentForExpressionBodies = {0}") @ValueSource(booleans = [false, true]) @Tag(WarningNames.WRONG_INDENTATION) - fun `should be properly indented`(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) { + fun `should be properly indented`(extendedIndentForExpressionBodies: Boolean, @TempDir tempDir: Path) { val defaultConfig = IndentationConfig("newlineAtEnd" to false) - val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators) + val customConfig = defaultConfig.withCustomParameters("extendedIndentForExpressionBodies" to extendedIndentForExpressionBodies) lintMultipleMethods( - parenthesesSurroundedInfixExpressions[extendedIndentAfterOperators].assertNotNull(), + parenthesesSurroundedInfixExpressions[extendedIndentForExpressionBodies].assertNotNull(), tempDir = tempDir, rulesConfigList = customConfig.asRulesConfigList()) } - @ParameterizedTest(name = "extendedIndentAfterOperators = {0}") + @ParameterizedTest(name = "extendedIndentForExpressionBodies = {0}") @ValueSource(booleans = [false, true]) @Tag(WarningNames.WRONG_INDENTATION) - fun `should be reformatted if mis-indented`(extendedIndentAfterOperators: Boolean, @TempDir tempDir: Path) { + fun `should be reformatted if mis-indented`(extendedIndentForExpressionBodies: Boolean, @TempDir tempDir: Path) { val defaultConfig = IndentationConfig("newlineAtEnd" to false) - val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators) + val customConfig = defaultConfig.withCustomParameters("extendedIndentForExpressionBodies" to extendedIndentForExpressionBodies) lintMultipleMethods( - actualContent = parenthesesSurroundedInfixExpressions[!extendedIndentAfterOperators].assertNotNull(), - expectedContent = parenthesesSurroundedInfixExpressions[extendedIndentAfterOperators].assertNotNull(), + actualContent = parenthesesSurroundedInfixExpressions[!extendedIndentForExpressionBodies].assertNotNull(), + expectedContent = parenthesesSurroundedInfixExpressions[extendedIndentForExpressionBodies].assertNotNull(), tempDir = tempDir, rulesConfigList = customConfig.asRulesConfigList()) } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleTestMixin.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleTestMixin.kt index 77a9065ced..0b5759aab5 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleTestMixin.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleTestMixin.kt @@ -36,6 +36,7 @@ internal object IndentationRuleTestMixin { "indentationSize" to "$indentationSize", "newlineAtEnd" to "$newlineAtEnd", "extendedIndentOfParameters" to "$extendedIndentOfParameters", + "extendedIndentForExpressionBodies" to "$extendedIndentForExpressionBodies", "extendedIndentAfterOperators" to "$extendedIndentAfterOperators", "extendedIndentBeforeDot" to "$extendedIndentBeforeDot", ).apply { @@ -68,6 +69,7 @@ internal object IndentationRuleTestMixin { fun extendedIndent(enabled: Boolean): Array> = arrayOf( "extendedIndentOfParameters" to enabled, + "extendedIndentForExpressionBodies" to enabled, "extendedIndentAfterOperators" to enabled, "extendedIndentBeforeDot" to enabled) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleTestResources.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleTestResources.kt index 277b96917f..4dc1bc14e2 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleTestResources.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationRuleTestResources.kt @@ -93,14 +93,16 @@ internal object IndentationRuleTestResources { """.trimMargin(), """ - |fun f() = x + (y + - | g(x) - |) + |fun f() = + | x + (y + + | g(x) + | ) """.trimMargin(), """ - |fun f() = (1 + - | 2) + |fun f() = + | (1 + + | 2) """.trimMargin(), ) @@ -160,11 +162,11 @@ internal object IndentationRuleTestResources { | } else { | @Suppress("DEPRECATION") | systemUiVisibility = (View.SYSTEM_UI_FLAG_LOW_PROFILE or - | View.SYSTEM_UI_FLAG_FULLSCREEN or - | View.SYSTEM_UI_FLAG_LAYOUT_STABLE or - | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or - | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or - | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) + | View.SYSTEM_UI_FLAG_FULLSCREEN or + | View.SYSTEM_UI_FLAG_LAYOUT_STABLE or + | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY or + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION or + | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) | } | } """.trimMargin(), @@ -186,14 +188,16 @@ internal object IndentationRuleTestResources { """.trimMargin(), """ - |fun f() = x + (y + - | g(x) - |) + |fun f() = + | x + (y + + | g(x) + | ) """.trimMargin(), """ - |fun f() = (1 + - | 2) + |fun f() = + | (1 + + | 2) """.trimMargin(), ) @@ -703,16 +707,16 @@ internal object IndentationRuleTestResources { """ |val value2 = - | 1 to - | 2 to - | 3 + | 1 to + | 2 to + | 3 """.trimMargin(), """ |val value3 = - | (1 to - | 2 to - | 3) + | (1 to + | 2 to + | 3) """.trimMargin(), """ @@ -736,9 +740,9 @@ internal object IndentationRuleTestResources { |fun identity(t: T): T = t | |val value6 = - | identity(1 to - | 2 to - | 3) + | identity(1 to + | 2 to + | 3) """.trimMargin(), """ @@ -752,10 +756,10 @@ internal object IndentationRuleTestResources { | * 3. on each infix function call ([to]). | */ |val value7 = - | identity( - | 1 to - | 2 to - | 3) + | identity( + | 1 to + | 2 to + | 3) """.trimMargin(), """ @@ -779,37 +783,37 @@ internal object IndentationRuleTestResources { |fun identity(t: T): T = t | |val value10 = - | identity(identity(1 to - | 2 to - | 3)) + | identity(identity(1 to + | 2 to + | 3)) """.trimMargin(), """ |fun identity(t: T): T = t | |val value11 = - | identity(identity( - | 1 to - | 2 to - | 3)) + | identity(identity( + | 1 to + | 2 to + | 3)) """.trimMargin(), """ |// Same as above, but using a custom getter instead of an explicit initializer. |val value12 | get() = - | 1 to - | 2 to - | 3 + | 1 to + | 2 to + | 3 """.trimMargin(), """ |// Same as above, but using a custom getter instead of an explicit initializer. |val value13 | get() = - | (1 to - | 2 to - | 3) + | (1 to + | 2 to + | 3) """.trimMargin(), """ @@ -818,9 +822,9 @@ internal object IndentationRuleTestResources { |// Same as above, but using a custom getter instead of an explicit initializer. |val value14 | get() = - | identity(1 to - | 2 to - | 3) + | identity(1 to + | 2 to + | 3) """.trimMargin(), """ @@ -829,9 +833,9 @@ internal object IndentationRuleTestResources { |// Same as above, but using a custom getter instead of an explicit initializer. |val value15 | get() = - | identity(identity(1 to - | 2 to - | 3)) + | identity(identity(1 to + | 2 to + | 3)) """.trimMargin(), ) @@ -846,7 +850,7 @@ internal object IndentationRuleTestResources { /** * Parenthesized expressions, single indent - * (`extendedIndentAfterOperators` is **off**). + * (`extendedIndentForExpressionBodies` is **off**). * * When adding new code fragments to this list, be sure to also add their * counterparts (preserving order) to @@ -909,7 +913,7 @@ internal object IndentationRuleTestResources { /** * Parenthesized expressions, continuation indent - * (`extendedIndentAfterOperators` is **on**). + * (`extendedIndentForExpressionBodies` is **on**). * * When adding new code fragments to this list, be sure to also add their * counterparts (preserving order) to 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 fdda961e50..16bf3a5321 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 @@ -43,6 +43,7 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { mapOf( "extendedIndentOfParameters" to "true", "alignedParameters" to "true", + "extendedIndentForExpressionBodies" to "true", "extendedIndentAfterOperators" to "true", "extendedIndentBeforeDot" to "false", "indentationSize" to "4" @@ -54,6 +55,7 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { mapOf( "extendedIndentOfParameters" to "false", "alignedParameters" to "false", + "extendedIndentForExpressionBodies" to "false", "extendedIndentAfterOperators" to "false", "extendedIndentBeforeDot" to "false", "indentationSize" to "4" @@ -820,29 +822,29 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { @Nested @TestMethodOrder(DisplayName::class) inner class `Expression body functions` { - @ParameterizedTest(name = "extendedIndentAfterOperators = {0}") + @ParameterizedTest(name = "extendedIndentForExpressionBodies = {0}") @ValueSource(booleans = [false, true]) @Tag(WarningNames.WRONG_INDENTATION) - fun `should be properly indented`(extendedIndentAfterOperators: Boolean) { + fun `should be properly indented`(extendedIndentForExpressionBodies: Boolean) { val defaultConfig = IndentationConfig("newlineAtEnd" to false) - val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators) + val customConfig = defaultConfig.withCustomParameters("extendedIndentForExpressionBodies" to extendedIndentForExpressionBodies) lintMultipleMethods( - expressionBodyFunctions[extendedIndentAfterOperators].assertNotNull(), + expressionBodyFunctions[extendedIndentForExpressionBodies].assertNotNull(), lintErrors = emptyArray(), customConfig.asRulesConfigList() ) } - @ParameterizedTest(name = "extendedIndentAfterOperators = {0}") + @ParameterizedTest(name = "extendedIndentForExpressionBodies = {0}") @ValueSource(booleans = [false, true]) @Tag(WarningNames.WRONG_INDENTATION) - fun `should be reported if mis-indented`(extendedIndentAfterOperators: Boolean) { + fun `should be reported if mis-indented`(extendedIndentForExpressionBodies: Boolean) { val defaultConfig = IndentationConfig("newlineAtEnd" to false) - val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators) + val customConfig = defaultConfig.withCustomParameters("extendedIndentForExpressionBodies" to extendedIndentForExpressionBodies) assertSoftly { softly -> - expressionBodyFunctions[!extendedIndentAfterOperators].assertNotNull().forEach { code -> + expressionBodyFunctions[!extendedIndentForExpressionBodies].assertNotNull().forEach { code -> softly.assertThat(lintResult(code, customConfig.asRulesConfigList())) .describedAs("lint result for ${code.describe()}") .isNotEmpty @@ -904,29 +906,29 @@ class IndentationRuleWarnTest : LintTestBase(::IndentationRule) { @Nested @TestMethodOrder(DisplayName::class) inner class `Parentheses-surrounded infix expressions` { - @ParameterizedTest(name = "extendedIndentAfterOperators = {0}") + @ParameterizedTest(name = "extendedIndentForExpressionBodies = {0}") @ValueSource(booleans = [false, true]) @Tag(WarningNames.WRONG_INDENTATION) - fun `should be properly indented`(extendedIndentAfterOperators: Boolean) { + fun `should be properly indented`(extendedIndentForExpressionBodies: Boolean) { val defaultConfig = IndentationConfig("newlineAtEnd" to false) - val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators) + val customConfig = defaultConfig.withCustomParameters("extendedIndentForExpressionBodies" to extendedIndentForExpressionBodies) lintMultipleMethods( - parenthesesSurroundedInfixExpressions[extendedIndentAfterOperators].assertNotNull(), + parenthesesSurroundedInfixExpressions[extendedIndentForExpressionBodies].assertNotNull(), lintErrors = emptyArray(), customConfig.asRulesConfigList() ) } - @ParameterizedTest(name = "extendedIndentAfterOperators = {0}") + @ParameterizedTest(name = "extendedIndentForExpressionBodies = {0}") @ValueSource(booleans = [false, true]) @Tag(WarningNames.WRONG_INDENTATION) - fun `should be reported if mis-indented`(extendedIndentAfterOperators: Boolean) { + fun `should be reported if mis-indented`(extendedIndentForExpressionBodies: Boolean) { val defaultConfig = IndentationConfig("newlineAtEnd" to false) - val customConfig = defaultConfig.withCustomParameters("extendedIndentAfterOperators" to extendedIndentAfterOperators) + val customConfig = defaultConfig.withCustomParameters("extendedIndentForExpressionBodies" to extendedIndentForExpressionBodies) assertSoftly { softly -> - parenthesesSurroundedInfixExpressions[!extendedIndentAfterOperators].assertNotNull().forEach { code -> + parenthesesSurroundedInfixExpressions[!extendedIndentForExpressionBodies].assertNotNull().forEach { code -> softly.assertThat(lintResult(code, customConfig.asRulesConfigList())) .describedAs("lint result for ${code.describe()}") .hasSizeBetween(0, 3).allSatisfy(Consumer { lintError -> diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt index d0578e8a0a..b42dcab3a6 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/smoke/DiktatSmokeTest.kt @@ -125,6 +125,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", rulesToDisable = emptyList(), rulesToOverride = mapOf( WRONG_INDENTATION.name to mapOf( + "extendedIndentForExpressionBodies" to "true", "extendedIndentAfterOperators" to "true", "extendedIndentBeforeDot" to "true", ) @@ -172,6 +173,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", rulesToDisable = emptyList(), rulesToOverride = mapOf( WRONG_INDENTATION.name to mapOf( + "extendedIndentForExpressionBodies" to "true", "extendedIndentAfterOperators" to "true", "extendedIndentBeforeDot" to "false", ) @@ -199,6 +201,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", rulesToDisable = emptyList(), rulesToOverride = mapOf( WRONG_INDENTATION.name to mapOf( + "extendedIndentForExpressionBodies" to "true", "extendedIndentAfterOperators" to "true", "extendedIndentBeforeDot" to "true", ) @@ -222,6 +225,7 @@ class DiktatSmokeTest : FixTestBase("test/smoke/src/main/kotlin", rulesToDisable = emptyList(), rulesToOverride = mapOf( WRONG_INDENTATION.name to mapOf( + "extendedIndentForExpressionBodies" to "true", "extendedIndentAfterOperators" to "true", "extendedIndentBeforeDot" to "false", ) diff --git a/examples/gradle-groovy-dsl/diktat-analysis.yml b/examples/gradle-groovy-dsl/diktat-analysis.yml index c2b8b5b8a2..489d7f55e0 100644 --- a/examples/gradle-groovy-dsl/diktat-analysis.yml +++ b/examples/gradle-groovy-dsl/diktat-analysis.yml @@ -210,6 +210,12 @@ extendedIndentOfParameters: false # If true: if first parameter in parameter list is on the same line as opening parenthesis, then other parameters can be aligned with it alignedParameters: true + # If true, expression bodies which begin on a separate line are indented + # using a continuation indent. The default is false. + # + # This flag is called CONTINUATION_INDENT_FOR_EXPRESSION_BODIES in IDEA and + # ij_kotlin_continuation_indent_for_expression_bodies in .editorconfig. + extendedIndentForExpressionBodies: false # If true: if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one extendedIndentAfterOperators: false # The indentation size for each file diff --git a/examples/gradle-kotlin-dsl-multiproject/diktat-analysis.yml b/examples/gradle-kotlin-dsl-multiproject/diktat-analysis.yml index 0c6d87f3a2..95a018f1d2 100644 --- a/examples/gradle-kotlin-dsl-multiproject/diktat-analysis.yml +++ b/examples/gradle-kotlin-dsl-multiproject/diktat-analysis.yml @@ -130,6 +130,7 @@ newlineAtEnd: true extendedIndentOfParameters: false alignedParameters: true + extendedIndentForExpressionBodies: false extendedIndentAfterOperators: false indentationSize: 4 - name: EMPTY_BLOCK_STRUCTURE_ERROR diff --git a/examples/gradle-kotlin-dsl/diktat-analysis.yml b/examples/gradle-kotlin-dsl/diktat-analysis.yml index c2b8b5b8a2..489d7f55e0 100644 --- a/examples/gradle-kotlin-dsl/diktat-analysis.yml +++ b/examples/gradle-kotlin-dsl/diktat-analysis.yml @@ -210,6 +210,12 @@ extendedIndentOfParameters: false # If true: if first parameter in parameter list is on the same line as opening parenthesis, then other parameters can be aligned with it alignedParameters: true + # If true, expression bodies which begin on a separate line are indented + # using a continuation indent. The default is false. + # + # This flag is called CONTINUATION_INDENT_FOR_EXPRESSION_BODIES in IDEA and + # ij_kotlin_continuation_indent_for_expression_bodies in .editorconfig. + extendedIndentForExpressionBodies: false # If true: if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one extendedIndentAfterOperators: false # The indentation size for each file diff --git a/examples/maven/diktat-analysis.yml b/examples/maven/diktat-analysis.yml index c2b8b5b8a2..489d7f55e0 100644 --- a/examples/maven/diktat-analysis.yml +++ b/examples/maven/diktat-analysis.yml @@ -210,6 +210,12 @@ extendedIndentOfParameters: false # If true: if first parameter in parameter list is on the same line as opening parenthesis, then other parameters can be aligned with it alignedParameters: true + # If true, expression bodies which begin on a separate line are indented + # using a continuation indent. The default is false. + # + # This flag is called CONTINUATION_INDENT_FOR_EXPRESSION_BODIES in IDEA and + # ij_kotlin_continuation_indent_for_expression_bodies in .editorconfig. + extendedIndentForExpressionBodies: false # If true: if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one extendedIndentAfterOperators: false # The indentation size for each file diff --git a/info/available-rules.md b/info/available-rules.md index 6eafa692d3..11923a387d 100644 --- a/info/available-rules.md +++ b/info/available-rules.md @@ -67,7 +67,7 @@ | 3 | 3.1.5 | TOP_LEVEL_ORDER | Check: warns if the top level order is incorrect. | yes | no | - | | 3 | 3.2.1 | NO_BRACES_IN_CONDITIONALS_AND_LOOPS | Check: warns if braces are not used in `if`, `else`, `when`, `for`, `do`, and `while` statements. Exception: single line if statement (ternary operator).
Fix: adds missing braces. | yes | no | - | | 3 | 3.2.2 | BRACES_BLOCK_STRUCTURE_ERROR | Check: warns if non-empty code blocks with braces do not follow the K&R style (1TBS or OTBS style). | yes | openBraceNewline closeBraceNewline | - | -| 3 | 3.3.1 | WRONG_INDENTATION | Check: warns if an indentation is incorrect.
Fix: corrects the indentation.

Basic cases are covered currently. | yes | extendedIndentOfParameters
alignedParameters
extendedIndentAfterOperators
extendedIndentBeforeDot
indentationSize | - | +| 3 | 3.3.1 | WRONG_INDENTATION | Check: warns if an indentation is incorrect.
Fix: corrects the indentation.

Basic cases are covered currently. | yes | extendedIndentOfParameters
alignedParameters
extendedIndentForExpressionBodies
extendedIndentAfterOperators
extendedIndentBeforeDot
indentationSize | - | | 3 | 3.4.1 | EMPTY_BLOCK_STRUCTURE_ERROR | Check: warns if an empty block exists or if its style is incorrect. | yes | allowEmptyBlocks styleEmptyBlockWithNewline | - | | 3 | 3.5.1 | LONG_LINE | Check: warns if the length doesn't exceed the specified length. | no | lineLength | Handle json method in KDoc. | | 3 | 3.6.1 | MORE_THAN_ONE_STATEMENT_PER_LINE | Check: warns if there is more than one statement per line. | yes | no | - | diff --git a/wp/sections/appendix.tex b/wp/sections/appendix.tex index c4becfaec4..42cf658915 100644 --- a/wp/sections/appendix.tex +++ b/wp/sections/appendix.tex @@ -82,7 +82,7 @@ \section*{Available Rules} TOP\underline{ }LEVEL\underline{ }ORDER & \hyperref[sec:3.1.5]{3.1.5} & yes & no \\ NO\underline{ }BRACES\underline{ }IN\underline{ }CONDITIONALS\underline{ }AND\underline{ }LOOPS & \hyperref[sec:3.2.1]{3.2.1} & yes & no \\ BRACES\underline{ }BLOCK\underline{ }STRUCTURE\underline{ }ERROR & \hyperref[sec:3.2.2]{3.2.2} & yes & openBraceNewline closeBraceNewline \\ -WRONG\underline{ }INDENTATION & \hyperref[sec:3.3.1]{3.3.1} & yes & extendedIndentOfParameters alignedParameters extendedIndentAfterOperators extendedIndentBeforeDot indentationSize \\ +WRONG\underline{ }INDENTATION & \hyperref[sec:3.3.1]{3.3.1} & yes & extendedIndentOfParameters alignedParameters extendedIndentForExpressionBodies extendedIndentAfterOperators extendedIndentBeforeDot indentationSize \\ EMPTY\underline{ }BLOCK\underline{ }STRUCTURE\underline{ }ERROR & \hyperref[sec:3.4.1]{3.4.1} & yes & allowEmptyBlocks styleEmptyBlockWithNewline \\ LONG\underline{ }LINE & \hyperref[sec:3.5.1]{3.5.1} & yes & lineLength \\ MORE\underline{ }THAN\underline{ }ONE\underline{ }STATEMENT\underline{ }PER\underline{ }LINE & \hyperref[sec:3.6.1]{3.6.1} & yes & no \\