Skip to content

Commit 0cd4aa1

Browse files
Add support for an extended indent after an opening ( in expressions
### What's done: * The value of the indent is controlled with the `extendedIndentAfterOperators` flag. * Fixes #1448.
1 parent 7f068a8 commit 0cd4aa1

9 files changed

Lines changed: 257 additions & 151 deletions

File tree

diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/files/IndentationRule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ class IndentationRule(configRules: List<RulesConfig>) : DiktatRule(
504504
* indentation if it's immediately followed by a newline.
505505
*/
506506
LPAR -> when {
507-
treeNext.isWhiteSpaceWithNewline() -> SINGLE
507+
treeNext.isWhiteSpaceWithNewline() -> IndentationAmount.valueOf(configuration.extendedIndentAfterOperators)
508508
else -> NONE
509509
}
510510

diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/indentation/IndentationConfig.kt

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,18 @@ internal class IndentationConfig(config: Map<String, String>) : RuleConfiguratio
99
/**
1010
* Is newline at the end of a file needed
1111
*/
12-
val newlineAtEnd = config["newlineAtEnd"]?.toBoolean() ?: true
12+
val newlineAtEnd = config[NEWLINE_AT_END]?.toBoolean() ?: true
1313

1414
/**
1515
* If true, in parameter list when parameters are split by newline they are indented with two indentations instead of one
1616
*/
17-
val extendedIndentOfParameters = config["extendedIndentOfParameters"]?.toBoolean() ?: false
17+
val extendedIndentOfParameters = config[EXTENDED_INDENT_OF_PARAMETERS]?.toBoolean() ?: false
1818

1919
/**
2020
* If true, if first parameter in parameter list is on the same line as opening parenthesis, then other parameters
2121
* can be aligned with it
2222
*/
23-
val alignedParameters = config["alignedParameters"]?.toBoolean() ?: true
23+
val alignedParameters = config[ALIGNED_PARAMETERS]?.toBoolean() ?: true
2424

2525
/**
2626
* If `true`, expression bodies which begin on a separate line are indented
@@ -60,29 +60,63 @@ internal class IndentationConfig(config: Map<String, String>) : RuleConfiguratio
6060
*
6161
* @since 1.2.2
6262
*/
63-
val extendedIndentForExpressionBodies = config["extendedIndentForExpressionBodies"]?.toBoolean() ?: false
63+
val extendedIndentForExpressionBodies = config[EXTENDED_INDENT_FOR_EXPRESSION_BODIES]?.toBoolean() ?: false
6464

6565
/**
6666
* If true, if expression is split by newline after operator like +/-/`*`, then the next line is indented with two indentations instead of one
6767
*/
68-
val extendedIndentAfterOperators = config["extendedIndentAfterOperators"]?.toBoolean() ?: true
68+
val extendedIndentAfterOperators = config[EXTENDED_INDENT_AFTER_OPERATORS]?.toBoolean() ?: true
6969

7070
/**
7171
* If true, when dot qualified expression starts on a new line, this line will be indented with
7272
* two indentations instead of one
7373
*/
74-
val extendedIndentBeforeDot = config["extendedIndentBeforeDot"]?.toBoolean() ?: false
74+
val extendedIndentBeforeDot = config[EXTENDED_INDENT_BEFORE_DOT]?.toBoolean() ?: false
7575

7676
/**
7777
* The indentation size for each file
7878
*/
79-
val indentationSize = config["indentationSize"]?.toInt() ?: DEFAULT_INDENT_SIZE
79+
val indentationSize = config[INDENTATION_SIZE]?.toInt() ?: DEFAULT_INDENTATION_SIZE
80+
81+
override fun equals(other: Any?): Boolean =
82+
other is IndentationConfig && configWithExplicitDefaults == other.configWithExplicitDefaults
83+
84+
override fun hashCode(): Int =
85+
configWithExplicitDefaults.hashCode()
86+
87+
override fun toString(): String =
88+
"${javaClass.simpleName}$configWithExplicitDefaults"
89+
90+
internal companion object {
91+
internal const val ALIGNED_PARAMETERS = "alignedParameters"
8092

81-
private companion object {
8293
/**
8394
* The default indent size (space characters), configurable via
8495
* `indentationSize`.
8596
*/
86-
private const val DEFAULT_INDENT_SIZE = 4
97+
private const val DEFAULT_INDENTATION_SIZE = 4
98+
internal const val EXTENDED_INDENT_AFTER_OPERATORS = "extendedIndentAfterOperators"
99+
internal const val EXTENDED_INDENT_BEFORE_DOT = "extendedIndentBeforeDot"
100+
internal const val EXTENDED_INDENT_FOR_EXPRESSION_BODIES = "extendedIndentForExpressionBodies"
101+
internal const val EXTENDED_INDENT_OF_PARAMETERS = "extendedIndentOfParameters"
102+
internal const val INDENTATION_SIZE = "indentationSize"
103+
internal const val NEWLINE_AT_END = "newlineAtEnd"
104+
105+
@Suppress(
106+
"CUSTOM_GETTERS_SETTERS",
107+
"STRING_TEMPLATE_QUOTES",
108+
)
109+
private val IndentationConfig.configWithExplicitDefaults: Map<String, String>
110+
get() =
111+
mutableMapOf<String, String>().apply {
112+
putAll(config)
113+
putIfAbsent(ALIGNED_PARAMETERS, "$alignedParameters")
114+
putIfAbsent(EXTENDED_INDENT_AFTER_OPERATORS, "$extendedIndentAfterOperators")
115+
putIfAbsent(EXTENDED_INDENT_BEFORE_DOT, "$extendedIndentBeforeDot")
116+
putIfAbsent(EXTENDED_INDENT_FOR_EXPRESSION_BODIES, "$extendedIndentForExpressionBodies")
117+
putIfAbsent(EXTENDED_INDENT_OF_PARAMETERS, "$extendedIndentOfParameters")
118+
putIfAbsent(INDENTATION_SIZE, "$indentationSize")
119+
putIfAbsent(NEWLINE_AT_END, "$newlineAtEnd")
120+
}
87121
}
88122
}

diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/spaces/IndentationConfigAwareTest.kt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationAmount.EXTENDED
55
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationAmount.NONE
66
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationAmount.SINGLE
77
import org.cqfn.diktat.ruleset.rules.chapter3.files.IndentationConfigAware.Factory.withIndentationConfig
8+
import org.cqfn.diktat.ruleset.utils.indentation.IndentationConfig.Companion.INDENTATION_SIZE
89

910
import org.assertj.core.api.Assertions.assertThat
1011
import org.junit.jupiter.api.MethodOrderer.DisplayName
@@ -14,10 +15,10 @@ import org.junit.jupiter.params.provider.ValueSource
1415

1516
@TestMethodOrder(DisplayName::class)
1617
class IndentationConfigAwareTest {
17-
@ParameterizedTest(name = "indentationSize = {0}")
18+
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
1819
@ValueSource(ints = [2, 4, 8])
1920
fun `Int + IndentationAmount`(indentationSize: Int) {
20-
val config = IndentationConfig("indentationSize" to indentationSize)
21+
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)
2122

2223
withIndentationConfig(config) {
2324
assertThat(42 + NONE).isEqualTo(42)
@@ -26,10 +27,10 @@ class IndentationConfigAwareTest {
2627
}
2728
}
2829

29-
@ParameterizedTest(name = "indentationSize = {0}")
30+
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
3031
@ValueSource(ints = [2, 4, 8])
3132
fun `Int - IndentationAmount`(indentationSize: Int) {
32-
val config = IndentationConfig("indentationSize" to indentationSize)
33+
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)
3334

3435
withIndentationConfig(config) {
3536
assertThat(42 - NONE).isEqualTo(42)
@@ -38,10 +39,10 @@ class IndentationConfigAwareTest {
3839
}
3940
}
4041

41-
@ParameterizedTest(name = "indentationSize = {0}")
42+
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
4243
@ValueSource(ints = [2, 4, 8])
4344
fun `IndentationAmount + Int`(indentationSize: Int) {
44-
val config = IndentationConfig("indentationSize" to indentationSize)
45+
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)
4546

4647
withIndentationConfig(config) {
4748
assertThat(NONE + 42).isEqualTo(42 + NONE)
@@ -52,10 +53,10 @@ class IndentationConfigAwareTest {
5253
}
5354
}
5455

55-
@ParameterizedTest(name = "indentationSize = {0}")
56+
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
5657
@ValueSource(ints = [2, 4, 8])
5758
fun `IndentationAmount - Int`(indentationSize: Int) {
58-
val config = IndentationConfig("indentationSize" to indentationSize)
59+
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)
5960

6061
withIndentationConfig(config) {
6162
assertThat(NONE - 42).isEqualTo(-(42 - NONE))
@@ -66,10 +67,10 @@ class IndentationConfigAwareTest {
6667
}
6768
}
6869

69-
@ParameterizedTest(name = "indentationSize = {0}")
70+
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
7071
@ValueSource(ints = [2, 4, 8])
7172
fun `IndentationAmount + IndentationAmount`(indentationSize: Int) {
72-
val config = IndentationConfig("indentationSize" to indentationSize)
73+
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)
7374

7475
withIndentationConfig(config) {
7576
assertThat(NONE + SINGLE).isEqualTo(0 + SINGLE)
@@ -80,10 +81,10 @@ class IndentationConfigAwareTest {
8081
}
8182
}
8283

83-
@ParameterizedTest(name = "indentationSize = {0}")
84+
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
8485
@ValueSource(ints = [2, 4, 8])
8586
fun `IndentationAmount - IndentationAmount`(indentationSize: Int) {
86-
val config = IndentationConfig("indentationSize" to indentationSize)
87+
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)
8788

8889
withIndentationConfig(config) {
8990
assertThat(NONE - SINGLE).isEqualTo(0 - SINGLE)
@@ -95,10 +96,10 @@ class IndentationConfigAwareTest {
9596
}
9697
}
9798

98-
@ParameterizedTest(name = "indentationSize = {0}")
99+
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
99100
@ValueSource(ints = [2, 4, 8])
100101
fun unaryPlus(indentationSize: Int) {
101-
val config = IndentationConfig("indentationSize" to indentationSize)
102+
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)
102103

103104
withIndentationConfig(config) {
104105
assertThat(+NONE).isEqualTo(0)
@@ -109,10 +110,10 @@ class IndentationConfigAwareTest {
109110
}
110111
}
111112

112-
@ParameterizedTest(name = "indentationSize = {0}")
113+
@ParameterizedTest(name = "$INDENTATION_SIZE = {0}")
113114
@ValueSource(ints = [2, 4, 8])
114115
fun unaryMinus(indentationSize: Int) {
115-
val config = IndentationConfig("indentationSize" to indentationSize)
116+
val config = IndentationConfig(INDENTATION_SIZE to indentationSize)
116117

117118
withIndentationConfig(config) {
118119
assertThat(-NONE).isEqualTo(0)

0 commit comments

Comments
 (0)